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