source: tags/ms_r16q3/MULTI_PROBE/MP_probe.cxx

Last change on this file was 11571, checked in by westram, 10 years ago
  • silence some clang warnings
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.4 KB
Line 
1// ============================================================= //
2//                                                               //
3//   File      : MP_probe.cxx                                    //
4//   Purpose   :                                                 //
5//                                                               //
6//   Institute of Microbiology (Technical University Munich)     //
7//   http://www.arb-home.de/                                     //
8//                                                               //
9// ============================================================= //
10
11#include "MP_probe.hxx"
12#include "MP_externs.hxx"
13#include "MultiProbe.hxx"
14
15#include <aw_window.hxx>
16#include <aw_msg.hxx>
17#include <aw_select.hxx>
18#include <arb_progress.h>
19
20#include <ctime>
21
22void ProbeValuation::evolution() {
23    long n = 0;
24    for (int i=0; i<size_sonden_array; i++) {            // Mismatche (=duplikate) aufsummieren, um Groesse von Pool zu bestimmen.
25        n += mismatch_array[i]+1;
26    }
27
28    long moeglichkeiten = k_aus_n(mp_gl_awars.no_of_probes, n);
29    double avg_fit = 0.0;
30    if (moeglichkeiten <= MAXINITPOPULATION) {
31        act_generation->calcFitness(false, avg_fit);
32    }
33    else {
34        // assumption: genetic algorithm needs about 1/3 of attempts (compared with brute force)
35        long max_generation = moeglichkeiten/(3*MAXPOPULATION)-1;
36        if (max_generation<1) max_generation = 1;
37
38        arb_progress progress(max_generation);
39        MP_aborted(0, 0.0, 0.0, 0.0, progress);
40
41        do { // genetic algorithm loop
42            bool aborted = act_generation->calcFitness(true, avg_fit);
43            if (aborted) break;
44
45            avg_fit = act_generation->get_avg_fit();
46#if defined(DEBUG)
47            printf("Generation %i: avg_fit=%f\n", act_generation->get_generation(), avg_fit);
48#endif
49
50            if (avg_fit == 0) {
51                aw_message("Please choose better Probes!");
52                break;
53            }
54            child_generation = act_generation->create_next_generation();
55            delete act_generation; act_generation = NULL;
56
57            child_generation->check_for_results();
58
59            act_generation = child_generation;
60            progress.inc();
61        }
62        while (act_generation->get_generation() <= max_generation);
63        progress.done();
64    }
65    if (act_generation) act_generation->check_for_results();
66}
67
68void ProbeValuation::insert_in_result_list(probe_combi_statistic *pcs)      // pcs darf nur eingetragen werden, wenn es nicht schon vorhanden ist
69{
70    char *new_list_string;
71    char *misms;
72    char *probe_string;
73    char  temp_misms[3];
74    int   probe_len = 0;
75    char  buf[25];
76    char  ecoli_pos[40], temp_ecol[10];
77    int   buf_len, i;
78
79    result_struct *rs = new result_struct;
80    result_struct *elem;
81
82    memset(rs, 0, sizeof(result_struct));
83    memset(ecoli_pos, 0, 40);
84
85    for (i=0; i<mp_gl_awars.no_of_probes; i++)
86        probe_len += strlen(sondenarray[pcs->get_probe_combi(i)->probe_index]) + 1; // 1 fuer space bzw. 0-Zeichen
87
88    misms = new char[2*mp_gl_awars.no_of_probes+1];
89    probe_string = new char[probe_len+1];
90
91    probe_string[0] = misms[0] = 0;
92    for (i=0; i<mp_gl_awars.no_of_probes; i++)
93    {
94        if (i>0)
95        {
96            strcat(misms, " ");
97            strcat(probe_string, " ");
98        }
99
100
101        sprintf(temp_misms, "%1d", pcs->get_probe_combi(i)->allowed_mismatches);
102        strcat(misms, temp_misms);
103        sprintf(temp_ecol, "%6d ", pcs->get_probe_combi(i)->e_coli_pos);
104        strcat(ecoli_pos, temp_ecol);
105        strcat(probe_string, sondenarray[pcs->get_probe_combi(i)->probe_index]);
106    }
107
108    ecoli_pos[strlen(ecoli_pos)-1] = 0;
109
110    new_list_string = new char[21+
111                               probe_len+
112                               2*mp_gl_awars.no_of_probes+2+
113                               2*strlen(SEPARATOR) + strlen(ecoli_pos)+
114                               1];  // 1 fuer 0-Zeichen
115
116    sprintf(buf, "%f", pcs->get_fitness());
117    buf_len = strlen(buf);
118    for (i=0; i<20-buf_len; i++)
119        strcat(buf, " ");
120
121    sprintf(new_list_string, "%20s%s%s%s%s%s%s", buf, SEPARATOR, misms, SEPARATOR, ecoli_pos, SEPARATOR, probe_string);
122    delete [] misms;
123    delete [] probe_string;
124
125    rs->ps = pcs->duplicate();
126    rs->view_string = new_list_string;
127    elem = computation_result_list->get_first();
128    if (! elem)
129        computation_result_list->insert_as_first(rs);
130    else
131    {
132        while (elem)                    // Liste ist sortiert von groesster Fitness bis kleinster Fitness
133        {
134            if (strcmp(elem->view_string, new_list_string) == 0)
135            {
136                delete [] new_list_string;
137                delete rs->ps;
138                delete rs;
139                return;
140            }
141
142            if (pcs->get_fitness() > elem->ps->get_fitness())
143            {
144                computation_result_list->insert_before_current(rs);
145                break;
146            }
147
148            elem = computation_result_list->get_next();
149        }
150
151        if (!elem)
152            computation_result_list->insert_as_last(rs);
153    }
154
155
156
157    result_probes_list->clear();
158
159    elem = computation_result_list->get_first();
160    while (elem)
161    {
162        result_probes_list->insert(elem->view_string, elem->view_string);
163        elem = computation_result_list->get_next();
164    }
165
166    result_probes_list->insert_default("", "");
167    result_probes_list->update();
168}
169
170void ProbeValuation::init_valuation()
171{
172    int    i, j, k, counter = 0;
173    probe *temp_probe;
174    char  *ptr;
175
176    if (new_pt_server)
177    {
178        new_pt_server = false;
179
180        if (mp_main->get_stc())
181            delete mp_main->get_stc();
182
183        mp_main->set_stc(new ST_Container(MAXSONDENHASHSIZE));
184    }
185
186    if (pt_server_different)
187    {
188        mp_main->set_stc(NULL);
189        new_pt_server = true;
190        return;
191    }
192
193    AW_selection_list_iterator selentry(selected_list); 
194
195    if (max_init_pop_combis < MAXINITPOPULATION) {
196        for (i=0; i<size_sonden_array; i++)         // generierung eines pools, in dem jede Sonde nur einmal pro Mismatch
197        {                           // vorkommt, damit alle moeglichen Kombinationen deterministisch
198            const char *ptr2    = selentry.get_value();
199            ++selentry;
200
201            for (j=0; j<=mismatch_array[i]; j++)        // generiert werden koennen.
202            {
203                temp_probe = new probe;
204                temp_probe->probe_index = i;
205                temp_probe->allowed_mismatches = j;
206                temp_probe->e_coli_pos = atoi(ptr = MP_get_comment(3, ptr2));
207                free(ptr);
208
209                probe_pool[counter++] = temp_probe;
210            }
211        }
212        pool_length = counter;
213    }
214    else {
215        for (i=0; i<size_sonden_array; i++)
216        {                               // Generierung eines Pools, in dem die Wahrscheinlichkeiten fuer die Erfassung
217            const char *ptr2 = selentry.get_value();
218            ++selentry;
219
220            for (j=0; j<=mismatch_array[i]; j++)        // der Sonden schon eingearbeitet sind. DIe WS werden vom Benutzer fuer jedE
221            {                           // einzelne Sonde bestimmt
222                for (k=0; k < bewertungarray[i]; k++)
223                {
224                    temp_probe = new probe;
225                    temp_probe->probe_index = i;
226                    temp_probe->allowed_mismatches = j;
227                    temp_probe->e_coli_pos = atoi(ptr = MP_get_comment(3, ptr2));
228                    free(ptr);
229
230                    probe_pool[counter++] = temp_probe;
231                }
232            }
233        }
234    }
235
236    act_generation->init_valuation();
237    evolution();
238
239    mp_main->get_mp_window()->get_result_window()->activate();
240}
241
242
243
244ProbeValuation::ProbeValuation(char **sonden_array, int no_of_sonden, int *bewertung, int *mismatch) {
245    memset((void*)this, 0, sizeof(ProbeValuation)); // @@@ potentially dangerous (overwrites vtable pointer!)
246
247    sondenarray     = sonden_array;
248    bewertungarray  = bewertung;
249    size_sonden_array   = no_of_sonden;
250    mismatch_array  = mismatch;
251
252    computation_result_list = new List<result_struct>;
253
254    for (int i=0; i<size_sonden_array; i++)             // Mismatche (=duplikate) aufsummieren, um Groesse von Pool zu bestimmen.
255    {
256        max_init_pop_combis += mismatch[i]+1;
257        pool_length += (mismatch_array[i]+1) * bewertungarray[i];
258    }
259
260    max_init_pop_combis = k_aus_n(mp_gl_awars.no_of_probes, max_init_pop_combis);
261
262    if (max_init_pop_combis > MAXINITPOPULATION)        // Ausgangspopulationsgroesse ist limitiert
263        max_init_pop_combis = MAXINITPOPULATION;
264
265    probe_pool = new probe*[pool_length];
266    memset(probe_pool, 0, pool_length * sizeof(probe*));    // Struktur mit 0 initialisieren.
267
268}
269
270
271ProbeValuation::~ProbeValuation()
272{
273    int i;
274    result_struct *elem;
275
276    for (i=0; i<size_sonden_array; i++) free(sondenarray[i]);
277    for (i=0; i<pool_length; i++) delete probe_pool[i];
278
279    elem = computation_result_list->get_first();
280    while (elem) {
281        computation_result_list->remove_first();
282        delete [] elem->view_string;
283        delete elem;
284        elem = computation_result_list->get_first();
285    }
286
287    delete computation_result_list;
288
289    if (act_generation == child_generation) delete act_generation;
290    else {
291        delete act_generation;
292        delete child_generation;
293    }
294
295    delete [] sondenarray;
296    delete [] bewertungarray;
297    delete [] mismatch_array;
298    delete [] probe_pool;
299}
300
Note: See TracBrowser for help on using the repository browser.