1 | // This file is a part of Framsticks SDK. http://www.framsticks.com/ |
---|
2 | // Copyright (C) 1999-2015 Maciej Komosinski and Szymon Ulatowski. |
---|
3 | // See LICENSE.txt for details. |
---|
4 | |
---|
5 | #include "genman.h" |
---|
6 | #include <frams/vm/classes/genoobj.h> |
---|
7 | #include GEN_CONFIG_FILE //configuration of active genetic operators |
---|
8 | #include "common/framsg.h" |
---|
9 | #include "common/nonstd_math.h" |
---|
10 | #include "common/stl-util.h" |
---|
11 | #include <frams/errmgr/errmanager.h> |
---|
12 | |
---|
13 | |
---|
14 | #ifdef USE_GENMAN_f0 |
---|
15 | #include "f0/oper_f0.h" |
---|
16 | #endif |
---|
17 | #ifdef USE_GENMAN_f0FUZZY |
---|
18 | #include "f0/oper_f0Fuzzy.h" |
---|
19 | #endif |
---|
20 | #ifdef USE_GENMAN_f1 |
---|
21 | #include "f1/oper_f1.h" |
---|
22 | #endif |
---|
23 | #ifdef USE_GENMAN_f2 |
---|
24 | #include "f2/oper_f2.h" |
---|
25 | #endif |
---|
26 | #ifdef USE_GENMAN_f2 |
---|
27 | #include "f3/oper_f3.h" |
---|
28 | #endif |
---|
29 | #ifdef USE_GENMAN_f4 |
---|
30 | #include "f4/oper_f4.h" |
---|
31 | #endif |
---|
32 | #ifdef USE_GENMAN_f5 |
---|
33 | #include "f5/oper_f5.h" |
---|
34 | #endif |
---|
35 | #ifdef USE_GENMAN_f6 |
---|
36 | #include "f6/oper_f6.h" |
---|
37 | #endif |
---|
38 | #ifdef USE_GENMAN_f7 |
---|
39 | #include "f7/oper_f7.h" |
---|
40 | #endif |
---|
41 | #ifdef USE_GENMAN_f8 |
---|
42 | #include "f8/oper_f8.h" |
---|
43 | #endif |
---|
44 | #ifdef USE_GENMAN_f9 |
---|
45 | #include "f9/oper_f9.h" |
---|
46 | #endif |
---|
47 | #ifdef USE_GENMAN_fF |
---|
48 | #include "fF/oper_fF.h" |
---|
49 | #endif |
---|
50 | #ifdef USE_GENMAN_fT |
---|
51 | #include "fT/oper_fTest.h" |
---|
52 | #endif |
---|
53 | |
---|
54 | using namespace std; //string, vector |
---|
55 | |
---|
56 | //old code needs update: |
---|
57 | //#include "gengroups.h" |
---|
58 | //extern GenGroup *listaGen; |
---|
59 | // GENGROUP(0)->l_del.add(sim->GM.onDelGen,&sim->GM); //before delete |
---|
60 | // GENGROUP(0)->l_del.remove(sim->GM.onDelGen,&sim->GM); //before delete |
---|
61 | |
---|
62 | |
---|
63 | #define FIELDSTRUCT GenMan |
---|
64 | |
---|
65 | static ParamEntry GMparam_tab[] = |
---|
66 | { |
---|
67 | { "Genetics", 1, 10, "GenMan", }, |
---|
68 | { "gen_hist", 0, PARAM_DONTSAVE, "Remember history of genetic operations", "d 0 1 0", FIELD(history), "Required for phylogenetic analysis", }, |
---|
69 | { "gen_hilite", 0, 0, "Use syntax highlighting", "d 0 1 1", FIELD(hilite), "Use colors for genes?\n(slows down viewing/editing of huge genotypes)", }, |
---|
70 | { "gen_extmutinfo", 0, 0, "Extended mutation info", "d 0 2 0 ~Off~Method ID~Method description", FIELD(extmutinfo), "If active, information about employed mutation method will be stored in the 'info' field of each mutated genotype.", }, |
---|
71 | { "operReport", 0, PARAM_DONTSAVE, "Operators report", "p()", PROCEDURE(p_report), "Show available genetic operators", }, |
---|
72 | { "toHTML", 0, PARAM_DONTSAVE, "HTMLize a genotype", "p s(s)", PROCEDURE(p_htmlize), "returns genotype expressed as colored HTML", }, |
---|
73 | { "toHTMLshort", 0, PARAM_DONTSAVE, "HTMLize a genotype, shorten if needed", "p s(s)", PROCEDURE(p_htmlizeshort), "returns genotype (abbreviated if needed) expressed as colored HTML", }, |
---|
74 | { "validate", 0, PARAM_DONTSAVE | PARAM_USERHIDDEN, "Validate", "p oGeno(oGeno)", PROCEDURE(p_validate), "returns validated (if possible) Geno object from supplied Geno", }, |
---|
75 | { "mutate", 0, PARAM_DONTSAVE | PARAM_USERHIDDEN, "Mutate", "p oGeno(oGeno)", PROCEDURE(p_mutate), "returns mutated Geno object from supplied Geno", }, |
---|
76 | { "crossOver", 0, PARAM_DONTSAVE | PARAM_USERHIDDEN, "Crossover", "p oGeno(oGeno,oGeno)", PROCEDURE(p_crossover), "returns crossed over genotype", }, |
---|
77 | { "getSimplest", 0, PARAM_DONTSAVE | PARAM_USERHIDDEN, "Get simplest genotype", "p oGeno(d format)", PROCEDURE(p_getsimplest), "returns the simplest genotype for a given encoding (format). 0 means f0, 4 means f4, etc.", }, |
---|
78 | { 0, }, |
---|
79 | }; |
---|
80 | |
---|
81 | static ParamEntry GMstats_tab[] = |
---|
82 | { |
---|
83 | { "Genetics", 1, 12, "GenManStats", "Statistics for genetic operations." }, |
---|
84 | { "gen_count", 0, PARAM_READONLY, "Number of genetic operations so far", "d", FIELD(count), "", }, |
---|
85 | { "gen_mvalid", 0, PARAM_READONLY, "Mutations valid", "d", FIELD(valid_m), "", }, |
---|
86 | { "gen_mvalidated", 0, PARAM_READONLY, "Mutations validated", "d", FIELD(validated_m), "", }, |
---|
87 | { "gen_minvalid", 0, PARAM_READONLY, "Mutations invalid", "d", FIELD(invalid_m), "couldn't be repaired", }, |
---|
88 | { "gen_mfailed", 0, PARAM_READONLY, "Mutations failed", "d", FIELD(failed_m), "couldn't be performed", }, |
---|
89 | { "gen_xovalid", 0, PARAM_READONLY, "Crossovers valid", "d", FIELD(valid_xo), "", }, |
---|
90 | { "gen_xovalidated", 0, PARAM_READONLY, "Crossovers validated", "d", FIELD(validated_xo), "", }, |
---|
91 | { "gen_xoinvalid", 0, PARAM_READONLY, "Crossovers invalid", "d", FIELD(invalid_xo), "couldn't be repaired", }, |
---|
92 | { "gen_xofailed", 0, PARAM_READONLY, "Crossovers failed", "d", FIELD(failed_xo), "couldn't be performed", }, |
---|
93 | { "gen_mutimpr", 0, PARAM_READONLY, "Mutations total effect", "f", FIELD(mutchg), "total cumulative mutation change", }, |
---|
94 | { "gen_xoimpr", 0, PARAM_READONLY, "Crossovers total effect", "f", FIELD(xochg), "total cumulative crossover change", }, |
---|
95 | { "clrstats", 0, PARAM_DONTSAVE, "Clear stats and history", "p()", PROCEDURE(p_clearStats), "", }, |
---|
96 | { 0, }, |
---|
97 | }; |
---|
98 | |
---|
99 | #undef FIELDSTRUCT |
---|
100 | |
---|
101 | GenMan::GenMan() : localpar(GMparam_tab, this), localstats(GMstats_tab, this), |
---|
102 | seloperpar("GenOperators", "Genetics: Active operators"), |
---|
103 | neuronsparam("Genetics: Neurons to add", "neuronsAdd", "neuadd_"), |
---|
104 | par("GenMan", "Manages various genetic operations, using appropriate operators for the argument genotype format.") |
---|
105 | { |
---|
106 | history = 0; |
---|
107 | hilite = 1; |
---|
108 | clearStats(); |
---|
109 | |
---|
110 | #ifdef USE_GENMAN_f0 |
---|
111 | oper_fx_list.push_back(new Geno_f0); |
---|
112 | #endif |
---|
113 | #ifdef USE_GENMAN_f0FUZZY |
---|
114 | oper_fx_list.push_back(new Geno_f0Fuzzy); |
---|
115 | #endif |
---|
116 | #ifdef USE_GENMAN_f1 |
---|
117 | oper_fx_list.push_back(new Geno_f1); |
---|
118 | #endif |
---|
119 | #ifdef USE_GENMAN_f2 |
---|
120 | oper_fx_list.push_back(new Geno_f2); |
---|
121 | #endif |
---|
122 | #ifdef USE_GENMAN_f3 |
---|
123 | oper_fx_list.push_back(new Geno_f3); |
---|
124 | #endif |
---|
125 | #ifdef USE_GENMAN_f4 |
---|
126 | oper_fx_list.push_back(new Geno_f4); |
---|
127 | #endif |
---|
128 | #ifdef USE_GENMAN_f5 |
---|
129 | oper_fx_list.push_back(new Geno_f5); |
---|
130 | #endif |
---|
131 | #ifdef USE_GENMAN_f6 |
---|
132 | oper_fx_list.push_back(new Geno_f6); |
---|
133 | #endif |
---|
134 | #ifdef USE_GENMAN_f7 |
---|
135 | oper_fx_list.push_back(new Geno_f7); |
---|
136 | #endif |
---|
137 | #ifdef USE_GENMAN_f8 |
---|
138 | oper_fx_list.push_back(new Geno_f8); |
---|
139 | #endif |
---|
140 | #ifdef USE_GENMAN_f9 |
---|
141 | oper_fx_list.push_back(new GenoOper_f9); |
---|
142 | #endif |
---|
143 | #ifdef USE_GENMAN_fF |
---|
144 | oper_fx_list.push_back(new GenoOper_fF); |
---|
145 | #endif |
---|
146 | #ifdef USE_GENMAN_fT |
---|
147 | oper_fx_list.push_back(new GenoOper_fTest); |
---|
148 | #endif |
---|
149 | |
---|
150 | seloper = new int[oper_fx_list.size()]; //may result in a little overhead if some of the operators on the oper_fx_list concern the same genetic format |
---|
151 | int selopercount = 0; |
---|
152 | for (unsigned int i = 0; i < oper_fx_list.size(); i++) |
---|
153 | { |
---|
154 | if (operformats.find(oper_fx_list[i]->supported_format) != -1) continue; |
---|
155 | string type = string("~") + oper_fx_list[i]->name; |
---|
156 | int dup = 0; |
---|
157 | for (unsigned int j = i + 1; j < oper_fx_list.size(); j++) |
---|
158 | if (oper_fx_list[i]->supported_format == oper_fx_list[j]->supported_format) |
---|
159 | { |
---|
160 | type += "~"; |
---|
161 | type += oper_fx_list[j]->name; |
---|
162 | dup++; |
---|
163 | } |
---|
164 | type = ssprintf("d 0 %d ", dup) + type; |
---|
165 | string id = ssprintf("genoper_f%c", oper_fx_list[i]->supported_format); |
---|
166 | string name = ssprintf("Operators for f%c", oper_fx_list[i]->supported_format); |
---|
167 | seloper[selopercount] = 0; |
---|
168 | operformats += oper_fx_list[i]->supported_format; |
---|
169 | //printf("%x %s %s %s\n",&seloper[selopercount],(const char*)id,(const char*)type,(const char*)name); |
---|
170 | seloperpar.addProperty(&seloper[selopercount++], id.c_str(), type.c_str(), name.c_str(), "", PARAM_READONLY*(dup == 0)); |
---|
171 | } |
---|
172 | |
---|
173 | par += &localpar; |
---|
174 | par += &seloperpar; |
---|
175 | par += &neuronsparam; |
---|
176 | for (unsigned int i = 0; i < oper_fx_list.size(); i++) |
---|
177 | if (oper_fx_list[i]->par.getParamTab()) par += &oper_fx_list[i]->par; |
---|
178 | } |
---|
179 | |
---|
180 | GenMan::~GenMan() |
---|
181 | { |
---|
182 | for (unsigned int i = 0; i < oper_fx_list.size(); i++) delete oper_fx_list[i]; |
---|
183 | delete[] seloper; |
---|
184 | } |
---|
185 | |
---|
186 | void GenMan::setDefaults() |
---|
187 | { |
---|
188 | for (unsigned int i = 0; i < oper_fx_list.size(); i++) |
---|
189 | { |
---|
190 | oper_fx_list[i]->par.setDefault(); |
---|
191 | oper_fx_list[i]->setDefaults(); |
---|
192 | } |
---|
193 | localpar.setDefault(); |
---|
194 | //...and we do not reset others that are linked to 'par', |
---|
195 | //because there quite a few of them, and not every of them defines defaults for each of its parameters. |
---|
196 | } |
---|
197 | |
---|
198 | int GenMan::testValidity(Geno &g, bool &canvalidate) |
---|
199 | { |
---|
200 | const char *gg = g.getGene(); |
---|
201 | GenoOperators *gf = getOper_f(g.getFormat()); |
---|
202 | int check1; |
---|
203 | if (!gf) { canvalidate = false; return GENOPER_NOOPER; } |
---|
204 | else check1 = gf->checkValidity(gg); |
---|
205 | if (!canvalidate) return check1; //just checking |
---|
206 | if (check1 == GENOPER_OK) { canvalidate = false; return check1; } |
---|
207 | char *g2 = strdup(gg); |
---|
208 | if (gf->validate(g2) == GENOPER_NOOPER) { free(g2); canvalidate = false; return check1; } |
---|
209 | if (check1 == GENOPER_NOOPER) //disaster: cannot check because there is no check operator |
---|
210 | { |
---|
211 | g.setGene(g2); free(g2); canvalidate = false; return GENOPER_NOOPER; |
---|
212 | } |
---|
213 | int check2 = gf->checkValidity(g2); |
---|
214 | if (check2 == GENOPER_OK) g.setGene(g2); |
---|
215 | free(g2); |
---|
216 | if (check2 == GENOPER_OK) return check1; |
---|
217 | canvalidate = false; |
---|
218 | return check1; //could not validate. |
---|
219 | } |
---|
220 | |
---|
221 | int GenMan::testGenoValidity(Geno& g) |
---|
222 | { |
---|
223 | bool fix = false; |
---|
224 | switch (testValidity(g, fix)) |
---|
225 | { |
---|
226 | case GENOPER_OK: return 1; |
---|
227 | case GENOPER_NOOPER: return -1; |
---|
228 | default: return 0; |
---|
229 | } |
---|
230 | } |
---|
231 | |
---|
232 | Geno GenMan::Validate(const Geno& geny) |
---|
233 | { |
---|
234 | char format = geny.getFormat(); |
---|
235 | GenoOperators *gf = getOper_f(format); |
---|
236 | if (gf == NULL) |
---|
237 | return Geno(SString::empty(), -1, SString::empty(), SString::sprintf("GENOPER_NOOPER: Validate(): don't know how to handle genetic format %c", format)); |
---|
238 | char *g2 = strdup(geny.getGene()); //copy for validation |
---|
239 | int res = gf->validate(g2); |
---|
240 | SString sg2 = g2; |
---|
241 | free(g2); |
---|
242 | if (res == GENOPER_OK) |
---|
243 | return Geno(sg2, format, geny.getName(), geny.getComment()); |
---|
244 | else |
---|
245 | return Geno(SString::empty(), -1, SString::empty(), SString::sprintf("GENOPER_NOOPER: validate() for format %c returned invalid value", format)); |
---|
246 | } |
---|
247 | |
---|
248 | Geno GenMan::Mutate(const Geno& g) |
---|
249 | { |
---|
250 | float chg; //how many changes |
---|
251 | int method; //mutation method |
---|
252 | char format = g.getFormat(); |
---|
253 | GenoOperators *gf = getOper_f(format); |
---|
254 | if (gf == NULL) |
---|
255 | return Geno(SString::empty(), -1, SString::empty(), SString::sprintf("GENOPER_NOOPER: Mutate(): don't know how to handle genetic format %c", format)); |
---|
256 | Geno gv = g; |
---|
257 | bool canvalidate = true; |
---|
258 | if (testValidity(gv, canvalidate) > 0 && canvalidate == false) |
---|
259 | return Geno("", -1, "", "GENOPER_OPFAIL: Mutate(): cannot validate invalid source genotype"); |
---|
260 | bool ok = false; |
---|
261 | int pcount = count; |
---|
262 | while (!ok) |
---|
263 | { |
---|
264 | char *gn = strdup(gv.getGene()); //copy for mutation |
---|
265 | chg = 0; |
---|
266 | if (gf->mutate(gn, chg, method) == GENOPER_OK) |
---|
267 | { |
---|
268 | ErrorHandler eh(ErrorHandler::StoreFirstMessage); //mute testValidity() |
---|
269 | Geno G(gn, gv.getFormat(), "", ""); |
---|
270 | canvalidate = true; |
---|
271 | int res = testValidity(G, canvalidate); |
---|
272 | if (res == GENOPER_OK && canvalidate == false) { valid_m++; ok = true; } |
---|
273 | else |
---|
274 | if (res > 0 && canvalidate == false) invalid_m++; else |
---|
275 | { |
---|
276 | validated_m++; ok = true; |
---|
277 | } |
---|
278 | if (ok) gv = G; |
---|
279 | } |
---|
280 | else failed_m++; |
---|
281 | free(gn); |
---|
282 | count++; |
---|
283 | if (!ok && (count - pcount > 100)) |
---|
284 | { |
---|
285 | FMprintf("GenMan", "Mutate", 2, "Tried 100x and failed: %s", (const char*)g.getGene()); |
---|
286 | return Geno("", -1, "", "GENOPER_OPFAIL: Mutate() tried 100x and failed"); |
---|
287 | } |
---|
288 | } |
---|
289 | mutchg += chg; |
---|
290 | if (history) saveLink((const char*)g.getGene(), "", (const char*)gv.getGene(), chg); |
---|
291 | SString mutinfo; |
---|
292 | if (extmutinfo == 0) mutinfo = SString::sprintf("%.2f%% mutation of '%s'", 100 * chg, (const char*)g.getName()); else |
---|
293 | if (extmutinfo == 1) mutinfo = SString::sprintf("%.2f%% mutation(%d) of '%s'", 100 * chg, method, (const char*)g.getName()); else |
---|
294 | mutinfo = SString::sprintf("%.2f%% mutation(%s) of '%s'", 100 * chg, gf->mutation_method_names ? gf->mutation_method_names[method] : "unspecified method name", (const char*)g.getName()); |
---|
295 | gv.setComment(mutinfo); |
---|
296 | return gv; |
---|
297 | } |
---|
298 | |
---|
299 | Geno GenMan::CrossOver(const Geno& g1, const Geno& g2) |
---|
300 | { |
---|
301 | char format = g1.getFormat(); |
---|
302 | if (format != g2.getFormat()) return Geno(SString::empty(), -1, SString::empty(), SString::sprintf("GENOPER_NOOPER: CrossOver(): does not work for parents with differing genetic formats (%c and %c)", format, g2.getFormat())); |
---|
303 | GenoOperators *gf = getOper_f(format); |
---|
304 | if (gf == NULL) |
---|
305 | return Geno(SString::empty(), -1, SString::empty(), SString::sprintf("GENOPER_NOOPER: CrossOver(): no operators found for genetic format %c", format)); |
---|
306 | Geno g1v = g1, g2v = g2; |
---|
307 | |
---|
308 | { |
---|
309 | ErrorHandler eh(ErrorHandler::StoreFirstMessage); //mute testValidity() |
---|
310 | bool canvalidate = true; |
---|
311 | if (testValidity(g1v, canvalidate) > 0 && canvalidate == false) |
---|
312 | return Geno("", -1, "", "GENOPER_OPFAIL: CrossOver(): cannot validate invalid source genotype #1"); |
---|
313 | canvalidate = true; |
---|
314 | if (testValidity(g2v, canvalidate) > 0 && canvalidate == false) |
---|
315 | return Geno("", -1, "", "GENOPER_OPFAIL: CrossOver(): cannot validate invalid source genotype #2"); |
---|
316 | } |
---|
317 | |
---|
318 | float chg; |
---|
319 | bool ok = false; |
---|
320 | int pcount = count; |
---|
321 | |
---|
322 | while (!ok) |
---|
323 | { |
---|
324 | float chg1, chg2; |
---|
325 | char *g1n = strdup(g1.getGene()); //copy for crossover |
---|
326 | char *g2n = strdup(g2.getGene()); //copy for crossover |
---|
327 | chg1 = chg2 = 0; |
---|
328 | if (gf->crossOver(g1n, g2n, chg1, chg2) == GENOPER_OK) |
---|
329 | { |
---|
330 | char *gn; |
---|
331 | if (g1n[0] && g2n[0]) if (randomN(2) == 0) g1n[0] = 0; else g2n[0] = 0; //both provided? we want only one |
---|
332 | if (g1n[0]) { gn = g1n; chg = chg1; } |
---|
333 | else { gn = g2n; chg = chg2; } |
---|
334 | ErrorHandler eh(ErrorHandler::StoreFirstMessage); //mute testValidity() |
---|
335 | Geno G(gn, g1v.getFormat(), "", ""); |
---|
336 | bool canvalidate = true; |
---|
337 | int res = testValidity(G, canvalidate); |
---|
338 | if (res == GENOPER_OK && canvalidate == false) { valid_xo++; ok = true; } |
---|
339 | else |
---|
340 | if (res > 0 && canvalidate == false) invalid_xo++; else |
---|
341 | { |
---|
342 | validated_xo++; ok = true; |
---|
343 | } |
---|
344 | if (ok) g1v = G; |
---|
345 | } |
---|
346 | else failed_xo++; |
---|
347 | free(g1n); |
---|
348 | free(g2n); |
---|
349 | count++; |
---|
350 | if (!ok && (count - pcount > 100)) |
---|
351 | { |
---|
352 | FMprintf("GenMan", "CrossOver", 2, "Tried 100x and failed: %s and %s", (const char*)g1.getGene(), (const char*)g2.getGene()); |
---|
353 | return Geno("", -1, "", "GENOPER_OPFAIL: CrossOver() tried 100x and failed"); |
---|
354 | } |
---|
355 | } |
---|
356 | // result in g1v |
---|
357 | xochg += chg; |
---|
358 | if (history) saveLink((const char*)g1.getGene(), (const char*)g2.getGene(), (const char*)g1v.getGene(), chg); |
---|
359 | SString xoinfo = SString::sprintf("Crossing over of '%s' (%.2f%%) and '%s' (%.2f%%)", |
---|
360 | (const char*)g1.getName(), 100 * chg, (const char*)g2.getName(), 100 * (1 - chg)); |
---|
361 | g1v.setComment(xoinfo); |
---|
362 | return g1v; |
---|
363 | } |
---|
364 | |
---|
365 | float GenMan::Similarity(const Geno& g1, const Geno& g2) |
---|
366 | { |
---|
367 | char format = g1.getFormat(); |
---|
368 | if (format != g2.getFormat()) return GENOPER_NOOPER; |
---|
369 | GenoOperators *gf = getOper_f(format); |
---|
370 | if (!gf) return GENOPER_NOOPER; else return gf->similarity(g1.getGene(), g2.getGene()); |
---|
371 | } |
---|
372 | |
---|
373 | uint32_t GenMan::Style(const char *g, int pos) |
---|
374 | { |
---|
375 | Geno G(g); |
---|
376 | if ((pos = G.mapStringToGen(pos)) == -1) return GENSTYLE_COMMENT; |
---|
377 | GenoOperators *gf = getOper_f(G.getFormat()); |
---|
378 | if (!gf) return GENSTYLE_CS(0, 0); //black & valid |
---|
379 | else return gf->style(G.getGene(), pos); |
---|
380 | } |
---|
381 | |
---|
382 | void GenMan::GetFullStyle(const char *g, uint32_t *styletab) |
---|
383 | { |
---|
384 | Geno G(g); |
---|
385 | GenoOperators *gf = getOper_f(G.getFormat()); |
---|
386 | SString geny = G.getGene(); |
---|
387 | for (unsigned int pos = 0; pos < strlen(g); pos++) |
---|
388 | { |
---|
389 | int posmapped = G.mapStringToGen(pos); |
---|
390 | if (posmapped == -1) styletab[pos] = GENSTYLE_COMMENT; |
---|
391 | else if (!gf) styletab[pos] = GENSTYLE_CS(0, 0); //black & valid |
---|
392 | else styletab[pos] = gf->style(geny, posmapped); |
---|
393 | } |
---|
394 | } |
---|
395 | |
---|
396 | SString GenMan::HTMLize(const char *g) { return HTMLize(g, false); } |
---|
397 | |
---|
398 | SString GenMan::HTMLizeShort(const char *g) { return HTMLize(g, true); } |
---|
399 | |
---|
400 | SString GenMan::HTMLize(const char *g, bool shorten) |
---|
401 | { |
---|
402 | char buf[50]; |
---|
403 | int len = strlen(g); |
---|
404 | int chars = 0, lines = 0; |
---|
405 | bool shortened = false; |
---|
406 | uint32_t *styletab = new uint32_t[len]; |
---|
407 | GetFullStyle(g, styletab); |
---|
408 | SString html = "\n<div style=\"background:white;padding:0.2em;font-family:arial,helvetica,sans-serif;font-size:90%\">"; |
---|
409 | uint32_t prevstyle, prevcolor, style = 0, color = 0; |
---|
410 | for (int i = 0; i<len; i++) |
---|
411 | { |
---|
412 | if (shorten && ((lines == 0 && chars>160) || (lines > 5 || chars > 300))) { shortened = true; break; } |
---|
413 | if (g[i] == '\r') continue; |
---|
414 | if (g[i] == '\n') { html += "<br>\n"; lines++; continue; } |
---|
415 | chars++; |
---|
416 | prevstyle = style; |
---|
417 | prevcolor = color; |
---|
418 | style = GENGETSTYLE(styletab[i]); |
---|
419 | color = GENGETCOLOR(styletab[i]); |
---|
420 | if ((i != 0 && (color != prevcolor))) html += "</font>"; |
---|
421 | if ((style&GENSTYLE_INVALID) != (prevstyle&GENSTYLE_INVALID)) |
---|
422 | { |
---|
423 | html += "<"; if (!(style&GENSTYLE_INVALID)) html += "/"; html += "u>"; |
---|
424 | } |
---|
425 | if ((style&GENSTYLE_BOLD) != (prevstyle&GENSTYLE_BOLD)) |
---|
426 | { |
---|
427 | html += "<"; if (!(style&GENSTYLE_BOLD)) html += "/"; html += "b>"; |
---|
428 | } |
---|
429 | if ((style&GENSTYLE_ITALIC) != (prevstyle&GENSTYLE_ITALIC)) |
---|
430 | { |
---|
431 | html += "<"; if (!(style&GENSTYLE_ITALIC)) html += "/"; html += "i>"; |
---|
432 | } |
---|
433 | if ((i == 0 || (color != prevcolor))) |
---|
434 | { |
---|
435 | sprintf(buf, "<font color=#%02x%02x%02x>", GENGET_R(color), GENGET_G(color), GENGET_B(color)); html += buf; |
---|
436 | } |
---|
437 | if (g[i] == '<') html += "<"; else if (g[i] == '>') html += ">"; else html += g[i]; |
---|
438 | if ((i % 3) == 0 && g[i] == ' ') html += "\n"; //for readability, insert some newlines into html... |
---|
439 | } |
---|
440 | delete[] styletab; |
---|
441 | html += "</u></b></i></font>"; |
---|
442 | if (shortened) html += " [etc...]"; |
---|
443 | html += "</div>\n"; |
---|
444 | return html; |
---|
445 | } |
---|
446 | |
---|
447 | void GenMan::p_htmlize(ExtValue *args, ExtValue *ret) |
---|
448 | { |
---|
449 | ret->setString(HTMLize(args->getString())); |
---|
450 | } |
---|
451 | |
---|
452 | void GenMan::p_htmlizeshort(ExtValue *args, ExtValue *ret) |
---|
453 | { |
---|
454 | ret->setString(HTMLizeShort(args->getString())); |
---|
455 | } |
---|
456 | |
---|
457 | Geno GenMan::GetSimplest(char format) |
---|
458 | { |
---|
459 | GenoOperators *gf = getOper_f(format); |
---|
460 | if (!gf) return Geno(); |
---|
461 | string info = "The simplest genotype of format f"; info += format; |
---|
462 | info += " for operators '"; info += gf->name; info += "'."; |
---|
463 | return Geno(gf->getSimplest(), format, "Root", info.c_str()); |
---|
464 | } |
---|
465 | |
---|
466 | void GenMan::p_getsimplest(ExtValue *args, ExtValue *ret) |
---|
467 | { |
---|
468 | int format = args[0].getInt() + 48; |
---|
469 | if (!getOper_f(format)) |
---|
470 | ret->setEmpty(); |
---|
471 | else |
---|
472 | *ret = GenoObj::makeDynamicObjectAndDecRef(new Geno(GetSimplest(format))); |
---|
473 | } |
---|
474 | |
---|
475 | const char *GenMan::GetOpName(char format) |
---|
476 | { |
---|
477 | GenoOperators *gf = getOper_f(format); |
---|
478 | if (!gf) return "n/a"; else return gf->name.c_str(); |
---|
479 | } |
---|
480 | |
---|
481 | GenoOperators* GenMan::getOper_f(char format) |
---|
482 | { |
---|
483 | int ind = operformats.find(format); |
---|
484 | if (ind == -1) return NULL; |
---|
485 | int which_oper_of_format = seloper[ind]; |
---|
486 | for (unsigned int i = 0; i < oper_fx_list.size(); i++) |
---|
487 | if (oper_fx_list[i]->supported_format == format) |
---|
488 | if (which_oper_of_format == 0) return oper_fx_list[i]; else which_oper_of_format--; |
---|
489 | return NULL; //should never happen |
---|
490 | } |
---|
491 | |
---|
492 | void GenMan::saveLink(const string parent1, const string parent2, const string child, const float chg) |
---|
493 | { |
---|
494 | GenoLink l; |
---|
495 | l.count = count; |
---|
496 | l.parent1 = parent1; |
---|
497 | l.parent2 = parent2; |
---|
498 | l.child = child; |
---|
499 | l.chg = chg; |
---|
500 | l.fit = 0; //temporarily. Will be set when the genotype dies |
---|
501 | //FMprintf("GenMan","saveLink",0,"#%d: [%d] '%s' + '%s' -> '%s'",GenoLinkList.size(),count,parent1.c_str(),parent2.c_str(),child.c_str()); |
---|
502 | GenoLinkList.push_back(l); |
---|
503 | } |
---|
504 | |
---|
505 | void GenMan::onDelGen(void *obj, intptr_t n) |
---|
506 | { |
---|
507 | //old code needs update: |
---|
508 | // ((SpeciesList*)obj)->przyDodaniu(i); |
---|
509 | /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
---|
510 | GenMan *gm=(GenMan*)obj; |
---|
511 | Genotype *gt=(Genotype*)(*listaGen)(n); //there is no more "listaGen" |
---|
512 | string g=(const char*)gt->genotype.getGene(); |
---|
513 | float fit=gt->getFinalFitness(); |
---|
514 | for(int i=0;i<gm->GenoLinkList.size();i++) //find genotype |
---|
515 | if (gm->GenoLinkList[i].g1==g) {gm->GenoLinkList[i].fit=fit; break;} |
---|
516 | */ |
---|
517 | } |
---|
518 | |
---|
519 | void GenMan::clearStats() |
---|
520 | { |
---|
521 | count = 0; |
---|
522 | valid_m = valid_xo = validated_m = validated_xo = invalid_m = invalid_xo = failed_m = failed_xo = 0; |
---|
523 | mutchg = xochg = 0; |
---|
524 | GenoLinkList.clear(); |
---|
525 | } |
---|
526 | |
---|
527 | void GenMan::p_clearStats(ExtValue *args, ExtValue *ret) { clearStats(); } |
---|
528 | |
---|
529 | void GenMan::p_report(ExtValue *args, ExtValue *ret) |
---|
530 | { //should be updated to handle multiple operators for a single format |
---|
531 | char *g, *g2; |
---|
532 | float f1, f2; |
---|
533 | int m; |
---|
534 | FramMessage("GenMan", "Report", "The following genetic operators are available:", 0); |
---|
535 | for (unsigned int i = 0; i < oper_fx_list.size(); i++) |
---|
536 | { |
---|
537 | string l; |
---|
538 | if (oper_fx_list[i]->checkValidity("") != GENOPER_NOOPER) l += " checkValidity"; |
---|
539 | if (oper_fx_list[i]->getSimplest()) |
---|
540 | { |
---|
541 | g = strdup(oper_fx_list[i]->getSimplest()); |
---|
542 | g2 = strdup(g); |
---|
543 | if (oper_fx_list[i]->validate(g) != GENOPER_NOOPER) l += " validate"; |
---|
544 | if (oper_fx_list[i]->mutate(g, f1, m) != GENOPER_NOOPER) l += " mutate"; |
---|
545 | if (oper_fx_list[i]->crossOver(g, g2, f1, f2) != GENOPER_NOOPER) l += " crossover"; |
---|
546 | l += " getSimplest"; |
---|
547 | free(g); free(g2); |
---|
548 | } |
---|
549 | // if (oper_fx_list[i]->similarity("","")!=GENOPER_NOOPER) l+=" similarity"; |
---|
550 | FMprintf("GenMan", "Report", 0, "format f%c (%s):%s", |
---|
551 | oper_fx_list[i]->supported_format, oper_fx_list[i]->name.c_str(), l.c_str()); |
---|
552 | } |
---|
553 | } |
---|
554 | |
---|
555 | void GenMan::p_validate(ExtValue *args, ExtValue *ret) |
---|
556 | { |
---|
557 | Geno *g = GenoObj::fromObject(args[0]); |
---|
558 | if (g == NULL) |
---|
559 | ret->setEmpty(); |
---|
560 | else |
---|
561 | *ret = GenoObj::makeDynamicObjectAndDecRef(new Geno(Validate(*g))); |
---|
562 | } |
---|
563 | |
---|
564 | void GenMan::p_mutate(ExtValue *args, ExtValue *ret) |
---|
565 | { |
---|
566 | Geno *g = GenoObj::fromObject(args[0]); |
---|
567 | if (g == NULL) |
---|
568 | ret->setEmpty(); |
---|
569 | else |
---|
570 | *ret = GenoObj::makeDynamicObjectAndDecRef(new Geno(Mutate(*g))); |
---|
571 | } |
---|
572 | |
---|
573 | void GenMan::p_crossover(ExtValue *args, ExtValue *ret) |
---|
574 | { |
---|
575 | Geno *g1 = GenoObj::fromObject(args[1]); |
---|
576 | Geno *g2 = GenoObj::fromObject(args[0]); |
---|
577 | if (g1 == NULL || g2 == NULL) |
---|
578 | ret->setEmpty(); |
---|
579 | else |
---|
580 | *ret = GenoObj::makeDynamicObjectAndDecRef(new Geno(CrossOver(*g1, *g2))); |
---|
581 | } |
---|