source: branches/alilink/MULTI_PROBE/MP_probe.cxx

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