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

Last change on this file since 170 was 168, checked in by sz, 10 years ago

reformatting

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