source: cpp/frams/genetics/genman.cpp @ 197

Last change on this file since 197 was 197, checked in by Maciej Komosinski, 11 years ago

GDK used by developers since 1999, distributed on the web since 2002

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