| 1 | #ifndef MP_PROBE_HXX |
|---|
| 2 | #define MP_PROBE_HXX |
|---|
| 3 | |
|---|
| 4 | #ifndef SOTL_HXX |
|---|
| 5 | #include "SoTl.hxx" |
|---|
| 6 | #endif |
|---|
| 7 | |
|---|
| 8 | #define MAXLIFEFORCOMBI 2 // Eine Sondenkombination lebt maximal MAXLIFEFORCOMBI Generationen |
|---|
| 9 | |
|---|
| 10 | class Generation; |
|---|
| 11 | class GenerationDuplicates; |
|---|
| 12 | class probe_statistic; |
|---|
| 13 | class probe_combi_statistic; |
|---|
| 14 | |
|---|
| 15 | struct probe { |
|---|
| 16 | int probe_index; |
|---|
| 17 | int allowed_mismatches; |
|---|
| 18 | int e_coli_pos; |
|---|
| 19 | }; |
|---|
| 20 | |
|---|
| 21 | struct result_struct { |
|---|
| 22 | probe_combi_statistic *ps; |
|---|
| 23 | char *view_string; |
|---|
| 24 | }; |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | class probe_tabs : virtual Noncopyable { |
|---|
| 29 | int *group_tab; |
|---|
| 30 | int *non_group_tab; |
|---|
| 31 | int length_of_group_tabs; |
|---|
| 32 | public: |
|---|
| 33 | int get_non_group_tab(int j) { return non_group_tab[j]; }; |
|---|
| 34 | int get_group_tab(int j) { return group_tab[j]; }; |
|---|
| 35 | int get_len_group_tabs() { return length_of_group_tabs; }; |
|---|
| 36 | |
|---|
| 37 | probe_tabs *duplicate(); |
|---|
| 38 | void print(); |
|---|
| 39 | probe_tabs(int *new_group_field = NULp, int *new_non_group_field = NULp, int len_group = 0); |
|---|
| 40 | ~probe_tabs(); |
|---|
| 41 | }; |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | class probe_combi_statistic : virtual Noncopyable { |
|---|
| 46 | // die Sondenkombis werden in dieser Klasse gespeichert |
|---|
| 47 | probe **probe_combi; |
|---|
| 48 | probe_tabs *probe_tab; |
|---|
| 49 | |
|---|
| 50 | double expected_children; |
|---|
| 51 | double fitness; |
|---|
| 52 | int life_counter; // Eine Sondenkombination hat nur eine Lebenslaenge von MAXLIFEFORCOMBI |
|---|
| 53 | |
|---|
| 54 | private: |
|---|
| 55 | void quicksort(long left, |
|---|
| 56 | long right); // Randomized Quicksort |
|---|
| 57 | |
|---|
| 58 | int get_dupl_pos(); // gibt Index einer Stelle zurueck, die doppelt vorkommt; field muss sortiert sein !!! |
|---|
| 59 | int modificated_hamming_dist(int one, int two); // pseudo hamming distanz einer Sondenkombi |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | public: |
|---|
| 63 | void set_probe_combi(int j, probe *f) { probe_combi[j] = f; } |
|---|
| 64 | probe *get_probe_combi(int j) { return probe_combi[j]; } |
|---|
| 65 | |
|---|
| 66 | double get_fitness() { return fitness; } |
|---|
| 67 | double get_expected_children() { return expected_children; } |
|---|
| 68 | int sub_expected_children(double val); |
|---|
| 69 | void init_stats(); |
|---|
| 70 | void scale(double a, double b) { fitness = a * fitness + b; } |
|---|
| 71 | |
|---|
| 72 | bool ok_for_next_gen(int &len_roul_wheel); |
|---|
| 73 | |
|---|
| 74 | void sub_life_counter() { life_counter --; } |
|---|
| 75 | void init_life_counter(); |
|---|
| 76 | bool is_dead() { return life_counter <= 0; } |
|---|
| 77 | |
|---|
| 78 | void sigma_truncation(double average_fit, double dev); // dient zur Skalierung der Fitness; um zu dominante Kombis zu vermeiden |
|---|
| 79 | double calc_fitness(int len_of_field); // fitness-berechnung einer Sondenkombi |
|---|
| 80 | double calc_expected_children(double average_fitness); |
|---|
| 81 | |
|---|
| 82 | void mutate_Probe(); // mutiert zufaellig die Sondenkombination nr_of_probe. |
|---|
| 83 | void crossover_Probes(probe_combi_statistic *pcombi2); // realisiert den Crossover zwischen Probe1 und Probe2 |
|---|
| 84 | void swap(probe **a, probe **b); |
|---|
| 85 | |
|---|
| 86 | void sort(long feld_laenge); // es wird ein randomized quicksort verwendet |
|---|
| 87 | probe_combi_statistic *duplicate(); // dupliziert dieses Objekt (z.B. fuer naechste Generation) |
|---|
| 88 | probe_combi_statistic *check_duplicates(GenerationDuplicates *dup_tree = NULp); |
|---|
| 89 | // rueckgabewert ist NULp, wenn das Feld duplikate enthaelt bzw. |
|---|
| 90 | // es wird sortiertes field zurueckgegeben. Wenn NULp zurueckkommt, dann |
|---|
| 91 | // wurde field jedoch noch nicht deleted |
|---|
| 92 | int calc_index_system3(int *field); |
|---|
| 93 | void print(); |
|---|
| 94 | void print(probe *p); |
|---|
| 95 | void print(probe **arr, int length); |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | probe_combi_statistic(probe **pc = NULp, probe_tabs *ps = NULp, double exp = 0, double fit = 0, int lifec = MAXLIFEFORCOMBI); |
|---|
| 99 | ~probe_combi_statistic(); |
|---|
| 100 | }; |
|---|
| 101 | |
|---|
| 102 | class Generation : virtual Noncopyable { |
|---|
| 103 | probe_combi_statistic **probe_combi_stat_array; // Liste von Sondenkombinationen, auf denen der genetische Algorithmus aus- |
|---|
| 104 | // gefuehrt wird. |
|---|
| 105 | int probe_combi_array_length; |
|---|
| 106 | GenerationDuplicates *dup_tree; |
|---|
| 107 | double average_fitness; |
|---|
| 108 | double min_fit; |
|---|
| 109 | double max_fit; |
|---|
| 110 | double deviation; // Abweichung |
|---|
| 111 | |
|---|
| 112 | int len_roulette_wheel; // wichtig zur Erstellung der naechsten Generation |
|---|
| 113 | int generation_counter; |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | // ***intern variables |
|---|
| 117 | int last_elem; |
|---|
| 118 | |
|---|
| 119 | private: |
|---|
| 120 | void prescale(double *a, double *b); // berechnet Koeffizienten fuer lineare Skalierung |
|---|
| 121 | probe_combi_statistic *choose_combi_for_next_generation(); |
|---|
| 122 | |
|---|
| 123 | public: |
|---|
| 124 | double get_avg_fit() { return average_fitness; }; |
|---|
| 125 | int get_generation() { return generation_counter; }; |
|---|
| 126 | GenerationDuplicates *get_dup_tree() { return dup_tree; }; |
|---|
| 127 | void set_length() { probe_combi_array_length = last_elem; }; // nur verwenden, wenn man weiss was man tut !!!! |
|---|
| 128 | void check_for_results(); // traegt eventuelle. resultate in Ergebnisfenster ein |
|---|
| 129 | |
|---|
| 130 | bool insert(probe_combi_statistic *pcs); // false wenn Generation schon MAXPOPULATION Eintraege hat |
|---|
| 131 | |
|---|
| 132 | void init_valuation(); |
|---|
| 133 | void gen_determ_combis(int beg, // wo faengt der Alg. an |
|---|
| 134 | int len, // wieviele probes muessen noch drangehaengt werden |
|---|
| 135 | int &pos_counter, // zaehler fuer probe_combi_stat_array |
|---|
| 136 | probe_combi_statistic *p); // bisher zusammengestellte probe |
|---|
| 137 | |
|---|
| 138 | bool calcFitness(bool use_genetic_algo, double old_avg_fit); // fitness-berechnung aller Sondenkombis im Feld; und average_fitness und deviation |
|---|
| 139 | |
|---|
| 140 | void init_roulette_wheel(); |
|---|
| 141 | Generation *create_next_generation(); // die Kindergeneration wird zurueckgegeben |
|---|
| 142 | |
|---|
| 143 | probe_combi_statistic *single_in_generation(probe_combi_statistic *field); // Nach der Funktion ist sichergestellt, dass dieses field in der |
|---|
| 144 | // Generation nur einmal vorkommt. Achtung: wenn Population sehr klein |
|---|
| 145 | // => Endlosschleifengefahr ( nicht in dieser Funktion, sondern u.U. in der |
|---|
| 146 | // aufrufenden |
|---|
| 147 | |
|---|
| 148 | void print(); |
|---|
| 149 | |
|---|
| 150 | Generation(int len, int gen_nr); |
|---|
| 151 | ~Generation(); |
|---|
| 152 | }; |
|---|
| 153 | |
|---|
| 154 | class ProbeValuation : virtual Noncopyable { |
|---|
| 155 | char **sondenarray; |
|---|
| 156 | int *bewertungarray; |
|---|
| 157 | int *mismatch_array; |
|---|
| 158 | int size_sonden_array; |
|---|
| 159 | |
|---|
| 160 | probe **probe_pool; // Generierung eines Pools, in dem die Wahrscheinlichkeiten fuer die Erfassung |
|---|
| 161 | // der Sonden schon eingearbeitet sind. DIe WS werden vom Benutzer fuer jede einzelne Sonde bestimmt. |
|---|
| 162 | |
|---|
| 163 | int pool_length; |
|---|
| 164 | int max_init_pop_combis; |
|---|
| 165 | |
|---|
| 166 | Generation *act_generation; |
|---|
| 167 | Generation *child_generation; // @@@ can be removed |
|---|
| 168 | List<result_struct> *computation_result_list; |
|---|
| 169 | |
|---|
| 170 | public: |
|---|
| 171 | void set_act_gen(Generation *g) { act_generation = g; }; |
|---|
| 172 | int get_max_init_for_gen() { return max_init_pop_combis; }; |
|---|
| 173 | int get_pool_length() { return pool_length; }; |
|---|
| 174 | probe **get_probe_pool() { return probe_pool; }; |
|---|
| 175 | int get_size_sondenarray() { return size_sonden_array; }; |
|---|
| 176 | char **get_sondenarray() { return sondenarray; }; |
|---|
| 177 | |
|---|
| 178 | void insert_in_result_list(probe_combi_statistic *pcs); |
|---|
| 179 | |
|---|
| 180 | void init_valuation(); // Zufaellige Auswahl einer Grundmenge von Sondenkombinationen |
|---|
| 181 | void evolution(); // Evolution |
|---|
| 182 | |
|---|
| 183 | ProbeValuation(char **sonden_array, int no_of_sonden, int *bewertung, int *single_mismatch); |
|---|
| 184 | ~ProbeValuation(); |
|---|
| 185 | }; |
|---|
| 186 | |
|---|
| 187 | class GenerationDuplicates : virtual Noncopyable { |
|---|
| 188 | // Fuer eine Generation muss ueberprueft werden, ob es doppelte Sondenkombinationen gibt. (aha) |
|---|
| 189 | int intern_size; // enthaelt size aus dem Konstruktor (size entspricht muss der groesse von sondenarray entsprechen |
|---|
| 190 | GenerationDuplicates **next; // die laenge dieses arrays entspricht der laenge des sondenarrays in ProbeValuation |
|---|
| 191 | int *next_mism; // zu jedem next eintrag merkt man sich wieviele Mismatche schon aufgetreten sind |
|---|
| 192 | // laenge von next_mism ist die maximale anzahl der Mismatche |
|---|
| 193 | |
|---|
| 194 | public: |
|---|
| 195 | bool insert(probe_combi_statistic *sondenkombi, bool &result, int depth = 0); // fuegt sondenkombination ein, wenn es Sie in dieser Struktur noch nicht gibt(=> true). |
|---|
| 196 | // Wenn es Sie schon gibt, dann false. depth ist nur fuer interne Zwecke. |
|---|
| 197 | |
|---|
| 198 | GenerationDuplicates(int size); |
|---|
| 199 | ~GenerationDuplicates(); // loescht rekursiv nach unten alles. |
|---|
| 200 | }; |
|---|
| 201 | |
|---|
| 202 | #else |
|---|
| 203 | #error MP_probe.hxx included twice |
|---|
| 204 | #endif |
|---|
| 205 | |
|---|