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

Last change on this file since 646 was 534, checked in by Maciej Komosinski, 8 years ago

Renamed: get/setGene -> get/setGenes, setGeneOnly -> setGenesAssumingSameFormat

  • Property svn:eol-style set to native
File size: 21.4 KB
RevLine 
[286]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.
[138]4
5#include "genman.h"
6#include <frams/vm/classes/genoobj.h>
7#include GEN_CONFIG_FILE //configuration of active genetic operators
[375]8#include "common/log.h"
[138]9#include "common/nonstd_math.h"
[200]10#include "common/stl-util.h"
[391]11#include <common/loggers/loggers.h>
[138]12
13
[139]14#ifdef USE_GENMAN_f0
[138]15#include "f0/oper_f0.h"
16#endif
[139]17#ifdef USE_GENMAN_f0FUZZY
[138]18#include "f0/oper_f0Fuzzy.h"
19#endif
[139]20#ifdef USE_GENMAN_f1
[138]21#include "f1/oper_f1.h"
22#endif
[139]23#ifdef USE_GENMAN_f2
[138]24#include "f2/oper_f2.h"
25#endif
[139]26#ifdef USE_GENMAN_f2
[138]27#include "f3/oper_f3.h"
28#endif
[139]29#ifdef USE_GENMAN_f4
[138]30#include "f4/oper_f4.h"
31#endif
[139]32#ifdef USE_GENMAN_f5
[138]33#include "f5/oper_f5.h"
34#endif
[139]35#ifdef USE_GENMAN_f6
[138]36#include "f6/oper_f6.h"
37#endif
[139]38#ifdef USE_GENMAN_f7
[138]39#include "f7/oper_f7.h"
40#endif
[139]41#ifdef USE_GENMAN_f8
[138]42#include "f8/oper_f8.h"
43#endif
[139]44#ifdef USE_GENMAN_f9
[138]45#include "f9/oper_f9.h"
46#endif
[139]47#ifdef USE_GENMAN_fF
48#include "fF/oper_fF.h"
49#endif
[194]50#ifdef USE_GENMAN_fT
51#include "fT/oper_fTest.h"
52#endif
[138]53
54using 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
65static 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", },
[240]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.", },
[138]78        { 0, },
79};
80
81static 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
101GenMan::GenMan() : localpar(GMparam_tab, this), localstats(GMstats_tab, this),
102seloperpar("GenOperators", "Genetics: Active operators"),
[319]103neuronsparam("Genetics: Neurons to add", "neuronsAdd", "neuadd_"),
104par("GenMan", "Manages various genetic operations, using appropriate operators for the argument genotype format.")
[138]105{
106        history = 0;
107        hilite = 1;
108        clearStats();
109
[139]110#ifdef USE_GENMAN_f0
[145]111        oper_fx_list.push_back(new Geno_f0);
[138]112#endif
[139]113#ifdef USE_GENMAN_f0FUZZY
[145]114        oper_fx_list.push_back(new Geno_f0Fuzzy);
[138]115#endif
[139]116#ifdef USE_GENMAN_f1
[145]117        oper_fx_list.push_back(new Geno_f1);
[138]118#endif
[139]119#ifdef USE_GENMAN_f2
[145]120        oper_fx_list.push_back(new Geno_f2);
[138]121#endif
[139]122#ifdef USE_GENMAN_f3
[145]123        oper_fx_list.push_back(new Geno_f3);
[138]124#endif
[139]125#ifdef USE_GENMAN_f4
[145]126        oper_fx_list.push_back(new Geno_f4);
[138]127#endif
[139]128#ifdef USE_GENMAN_f5
[145]129        oper_fx_list.push_back(new Geno_f5);
[138]130#endif
[139]131#ifdef USE_GENMAN_f6
[145]132        oper_fx_list.push_back(new Geno_f6);
[138]133#endif
[139]134#ifdef USE_GENMAN_f7
[145]135        oper_fx_list.push_back(new Geno_f7);
[138]136#endif
[139]137#ifdef USE_GENMAN_f8
[145]138        oper_fx_list.push_back(new Geno_f8);
[138]139#endif
[139]140#ifdef USE_GENMAN_f9
[145]141        oper_fx_list.push_back(new GenoOper_f9);
[138]142#endif
[139]143#ifdef USE_GENMAN_fF
[145]144        oper_fx_list.push_back(new GenoOper_fF);
[139]145#endif
[194]146#ifdef USE_GENMAN_fT
147        oper_fx_list.push_back(new GenoOper_fTest);
148#endif
[138]149
[145]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
[138]151        int selopercount = 0;
[145]152        for (unsigned int i = 0; i < oper_fx_list.size(); i++)
[138]153        {
[145]154                if (operformats.find(oper_fx_list[i]->supported_format) != -1) continue;
[200]155                string type = string("~") + oper_fx_list[i]->name;
[138]156                int dup = 0;
[145]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)
[138]159                        {
[200]160                                type += "~";
161                                type += oper_fx_list[j]->name;
162                                dup++;
[138]163                        }
[200]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);
[138]167                seloper[selopercount] = 0;
[145]168                operformats += oper_fx_list[i]->supported_format;
[138]169                //printf("%x %s %s %s\n",&seloper[selopercount],(const char*)id,(const char*)type,(const char*)name);
[200]170                seloperpar.addProperty(&seloper[selopercount++], id.c_str(), type.c_str(), name.c_str(), "", PARAM_READONLY*(dup == 0));
[138]171        }
172
173        par += &localpar;
174        par += &seloperpar;
175        par += &neuronsparam;
[145]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;
[138]178}
179
180GenMan::~GenMan()
181{
[145]182        for (unsigned int i = 0; i < oper_fx_list.size(); i++) delete oper_fx_list[i];
[138]183        delete[] seloper;
184}
185
186void GenMan::setDefaults()
187{
[145]188        for (unsigned int i = 0; i < oper_fx_list.size(); i++)
[138]189        {
[145]190                oper_fx_list[i]->par.setDefault();
191                oper_fx_list[i]->setDefaults();
[138]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
198int GenMan::testValidity(Geno &g, bool &canvalidate)
199{
[534]200        SString ggs=g.getGenes();
[348]201        const char *gg = ggs.c_str();
[145]202        GenoOperators *gf = getOper_f(g.getFormat());
[138]203        int check1;
204        if (!gf) { canvalidate = false; return GENOPER_NOOPER; }
[513]205        else check1 = gf->checkValidity(gg,g.getName().c_str());
[138]206        if (!canvalidate) return check1; //just checking
207        if (check1 == GENOPER_OK) { canvalidate = false; return check1; }
208        char *g2 = strdup(gg);
[513]209        if (gf->validate(g2,g.getName().c_str()) == GENOPER_NOOPER) { free(g2); canvalidate = false; return check1; }
[138]210        if (check1 == GENOPER_NOOPER) //disaster: cannot check because there is no check operator
211        {
[534]212                g.setGenesAssumingSameFormat(g2); free(g2); canvalidate = false; return GENOPER_NOOPER;
[138]213        }
[513]214        int check2 = gf->checkValidity(g2, "validated");
[534]215        if (check2 == GENOPER_OK) g.setGenesAssumingSameFormat(g2);
[138]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
[532]233Geno GenMan::validate(const Geno& geny)
[138]234{
[168]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));
[534]239        char *g2 = strdup(geny.getGenes().c_str()); //copy for validation
[513]240        int res = gf->validate(g2, geny.getName().c_str());
[168]241        SString sg2 = g2;
[138]242        free(g2);
[168]243        if (res == GENOPER_OK)
244                return Geno(sg2, format, geny.getName(), geny.getComment());
[138]245        else
[168]246                return Geno(SString::empty(), -1, SString::empty(), SString::sprintf("GENOPER_NOOPER: validate() for format %c returned invalid value", format));
[138]247}
248
[532]249Geno GenMan::mutate(const Geno& g)
[138]250{
251        float chg; //how many changes
252        int method; //mutation method
[168]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;
[138]263        while (!ok)
264        {
[534]265                char *gn = strdup(gv.getGenes().c_str()); //copy for mutation
[168]266                chg = 0;
267                if (gf->mutate(gn, chg, method) == GENOPER_OK)
[138]268                {
[375]269                        LoggerToMemory eh(LoggerBase::Enable | LoggerToMemory::StoreFirstMessage); //mute testValidity()
[168]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++;
[138]282                free(gn);
283                count++;
[168]284                if (!ok && (count - pcount > 100))
[138]285                {
[534]286                        logPrintf("GenMan", "Mutate", 2, "Tried 100x and failed: %s", g.getGenes().c_str());
[168]287                        return Geno("", -1, "", "GENOPER_OPFAIL: Mutate() tried 100x and failed");
[138]288                }
289        }
[168]290        mutchg += chg;
[534]291        if (history) saveLink(g.getGenes().c_str(), "", gv.getGenes().c_str(), chg);
[138]292        SString mutinfo;
[348]293        if (extmutinfo == 0) mutinfo = SString::sprintf("%.2f%% mutation of '%s'", 100 * chg, g.getName().c_str()); else
294                if (extmutinfo == 1) mutinfo = SString::sprintf("%.2f%% mutation(%d) of '%s'", 100 * chg, method, g.getName().c_str()); else
295                        mutinfo = SString::sprintf("%.2f%% mutation(%s) of '%s'", 100 * chg, gf->mutation_method_names ? gf->mutation_method_names[method] : "unspecified method name", g.getName().c_str());
[138]296        gv.setComment(mutinfo);
297        return gv;
298}
299
[532]300Geno GenMan::crossOver(const Geno& g1, const Geno& g2)
[138]301{
302        char format = g1.getFormat();
[257]303        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()));
[145]304        GenoOperators *gf = getOper_f(format);
[138]305        if (gf == NULL)
[257]306                return Geno(SString::empty(), -1, SString::empty(), SString::sprintf("GENOPER_NOOPER: CrossOver(): no operators found for genetic format %c", format));
[138]307        Geno g1v = g1, g2v = g2;
308
309        {
[375]310                LoggerToMemory eh(LoggerBase::Enable | LoggerToMemory::StoreFirstMessage); //mute testValidity()
[138]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;
[534]326                char *g1n = strdup(g1.getGenes().c_str()); //copy for crossover
327                char *g2n = strdup(g2.getGenes().c_str()); //copy for crossover
[138]328                chg1 = chg2 = 0;
329                if (gf->crossOver(g1n, g2n, chg1, chg2) == GENOPER_OK)
330                {
331                        char *gn;
[168]332                        if (g1n[0] && g2n[0]) if (randomN(2) == 0) g1n[0] = 0; else g2n[0] = 0; //both provided? we want only one
[138]333                        if (g1n[0]) { gn = g1n; chg = chg1; }
334                        else { gn = g2n; chg = chg2; }
[375]335                        LoggerToMemory eh(LoggerBase::Enable | LoggerToMemory::StoreFirstMessage); //mute testValidity()
[138]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                {
[534]353                        logPrintf("GenMan", "CrossOver", 2, "Tried 100x and failed: %s and %s", g1.getGenes().c_str(), g2.getGenes().c_str());
[138]354                        return Geno("", -1, "", "GENOPER_OPFAIL: CrossOver() tried 100x and failed");
355                }
356        }
357        // result in g1v
358        xochg += chg;
[534]359        if (history) saveLink(g1.getGenes().c_str(), g2.getGenes().c_str(), g1v.getGenes().c_str(), chg);
[138]360        SString xoinfo = SString::sprintf("Crossing over of '%s' (%.2f%%) and '%s' (%.2f%%)",
[348]361                g1.getName().c_str(), 100 * chg, g2.getName().c_str(), 100 * (1 - chg));
[138]362        g1v.setComment(xoinfo);
363        return g1v;
364}
365
[532]366float GenMan::similarity(const Geno& g1, const Geno& g2)
[138]367{
368        char format = g1.getFormat();
369        if (format != g2.getFormat()) return GENOPER_NOOPER;
[145]370        GenoOperators *gf = getOper_f(format);
[534]371        if (!gf) return GENOPER_NOOPER; else return gf->similarity(g1.getGenes().c_str(), g2.getGenes().c_str());
[138]372}
373
[532]374uint32_t GenMan::getStyle(const char *g, const Geno *G, int pos)
[138]375{
[532]376        char format = G->getFormat();
377        if (format == Geno::INVALID_FORMAT)
378                return GENSTYLE_RGBS(64, 64, 64, 0); // gray & "valid" (unknown format so we don't know what is valid and what is not)
379        if ((pos = G->mapStringToGen(pos)) == -1) return GENSTYLE_COMMENT;
380        GenoOperators *gf = getOper_f(format);
[138]381        if (!gf) return GENSTYLE_CS(0, 0); //black & valid
[534]382        else return gf->style(G->getGenes().c_str(), pos);
[138]383}
384
[532]385uint32_t GenMan::getStyle(const char *g, int pos)
[138]386{
387        Geno G(g);
[532]388        return getStyle(g, &G, pos);
389}
390
391void GenMan::getFullStyle(const char *g, const Geno *G, uint32_t *styletab)
392{
393        char format = G->getFormat();
394        if (format == Geno::INVALID_FORMAT)
395        {
396                for (unsigned int pos = 0; pos < strlen(g); pos++)
397                        styletab[pos] = GENSTYLE_RGBS(64, 64, 64, 0); // gray & "valid" (unknown format so we don't know what is valid and what is not)
398                return;
399        }
400        GenoOperators *gf = getOper_f(format);
[534]401        SString geny=G->getGenes();
[138]402        for (unsigned int pos = 0; pos < strlen(g); pos++)
403        {
[532]404                int posmapped = G->mapStringToGen(pos);
[138]405                if (posmapped == -1) styletab[pos] = GENSTYLE_COMMENT;
406                else if (!gf) styletab[pos] = GENSTYLE_CS(0, 0); //black & valid
[348]407                else styletab[pos] = gf->style(geny.c_str(), posmapped);
[532]408                //logPrintf("GenMan", "getFullStyle", 0, "%d  char='%c' (%d)  format=0x%08x", pos, g[pos], g[pos], styletab[pos]);
[138]409        }
410}
411
[532]412void GenMan::getFullStyle(const char *g, uint32_t *styletab)
413{
414        Geno G(g);
415        getFullStyle(g, &G, styletab);
416}
417
[467]418string GenMan::HTMLize(const char *g) { return HTMLize(g, false); }
[138]419
[467]420string GenMan::HTMLizeShort(const char *g) { return HTMLize(g, true); }
[138]421
[467]422string GenMan::HTMLize(const char *g, bool shorten)
[138]423{
424        char buf[50];
425        int len = strlen(g);
426        int chars = 0, lines = 0;
427        bool shortened = false;
[247]428        uint32_t *styletab = new uint32_t[len];
[532]429        getFullStyle(g, styletab);
[467]430        string html = "\n<div style=\"background:white;padding:0.2em;font-family:arial,helvetica,sans-serif;font-size:90%\">";
[247]431        uint32_t prevstyle, prevcolor, style = 0, color = 0;
[138]432        for (int i = 0; i<len; i++)
433        {
434                if (shorten && ((lines == 0 && chars>160) || (lines > 5 || chars > 300))) { shortened = true; break; }
435                if (g[i] == '\r') continue;
436                if (g[i] == '\n') { html += "<br>\n"; lines++; continue; }
437                chars++;
438                prevstyle = style;
439                prevcolor = color;
440                style = GENGETSTYLE(styletab[i]);
441                color = GENGETCOLOR(styletab[i]);
442                if ((i != 0 && (color != prevcolor))) html += "</font>";
443                if ((style&GENSTYLE_INVALID) != (prevstyle&GENSTYLE_INVALID))
444                {
445                        html += "<"; if (!(style&GENSTYLE_INVALID)) html += "/"; html += "u>";
446                }
447                if ((style&GENSTYLE_BOLD) != (prevstyle&GENSTYLE_BOLD))
448                {
449                        html += "<"; if (!(style&GENSTYLE_BOLD)) html += "/"; html += "b>";
450                }
451                if ((style&GENSTYLE_ITALIC) != (prevstyle&GENSTYLE_ITALIC))
452                {
453                        html += "<"; if (!(style&GENSTYLE_ITALIC)) html += "/"; html += "i>";
454                }
455                if ((i == 0 || (color != prevcolor)))
456                {
457                        sprintf(buf, "<font color=#%02x%02x%02x>", GENGET_R(color), GENGET_G(color), GENGET_B(color)); html += buf;
458                }
459                if (g[i] == '<') html += "&lt;"; else if (g[i] == '>') html += "&gt;"; else html += g[i];
460                if ((i % 3) == 0 && g[i] == ' ') html += "\n"; //for readability, insert some newlines into html...
461        }
462        delete[] styletab;
463        html += "</u></b></i></font>";
464        if (shortened) html += " [etc...]";
465        html += "</div>\n";
466        return html;
467}
468
469void GenMan::p_htmlize(ExtValue *args, ExtValue *ret)
470{
[467]471        ret->setString(HTMLize(args->getString().c_str()).c_str());
[138]472}
473
474void GenMan::p_htmlizeshort(ExtValue *args, ExtValue *ret)
475{
[467]476        ret->setString(HTMLizeShort(args->getString().c_str()).c_str());
[138]477}
478
[532]479Geno GenMan::getSimplest(char format)
[138]480{
[145]481        GenoOperators *gf = getOper_f(format);
[138]482        if (!gf) return Geno();
[200]483        string info = "The simplest genotype of format f"; info += format;
[138]484        info += " for operators '"; info += gf->name; info += "'.";
[200]485        return Geno(gf->getSimplest(), format, "Root", info.c_str());
[138]486}
487
488void GenMan::p_getsimplest(ExtValue *args, ExtValue *ret)
489{
[516]490        int format = GenoObj::formatFromExtValue(args[0]);
[145]491        if (!getOper_f(format))
[138]492                ret->setEmpty();
493        else
[532]494                *ret = GenoObj::makeDynamicObjectAndDecRef(new Geno(getSimplest(format)));
[138]495}
496
[532]497const char *GenMan::getOpName(char format)
[138]498{
[145]499        GenoOperators *gf = getOper_f(format);
[200]500        if (!gf) return "n/a"; else return gf->name.c_str();
[138]501}
502
[145]503GenoOperators* GenMan::getOper_f(char format)
[138]504{
505        int ind = operformats.find(format);
506        if (ind == -1) return NULL;
[168]507        int which_oper_of_format = seloper[ind];
[145]508        for (unsigned int i = 0; i < oper_fx_list.size(); i++)
509                if (oper_fx_list[i]->supported_format == format)
[168]510                        if (which_oper_of_format == 0) return oper_fx_list[i]; else which_oper_of_format--;
[138]511        return NULL; //should never happen
512}
513
[275]514void GenMan::saveLink(const string parent1, const string parent2, const string child, const float chg)
[138]515{
516        GenoLink l;
517        l.count = count;
[275]518        l.parent1 = parent1;
519        l.parent2 = parent2;
520        l.child = child;
[138]521        l.chg = chg;
522        l.fit = 0; //temporarily. Will be set when the genotype dies
[375]523        //logPrintf("GenMan","saveLink",0,"#%d: [%d] '%s' + '%s' -> '%s'",GenoLinkList.size(),count,parent1.c_str(),parent2.c_str(),child.c_str());
[138]524        GenoLinkList.push_back(l);
525}
526
[247]527void GenMan::onDelGen(void *obj, intptr_t n)
[138]528{
529        //old code needs update:
530        //   ((SpeciesList*)obj)->przyDodaniu(i);
531        /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
532           GenMan *gm=(GenMan*)obj;
533           Genotype *gt=(Genotype*)(*listaGen)(n); //there is no more "listaGen"
534           string g=(const char*)gt->genotype.getGene();
535           float fit=gt->getFinalFitness();
536           for(int i=0;i<gm->GenoLinkList.size();i++) //find genotype
537           if (gm->GenoLinkList[i].g1==g) {gm->GenoLinkList[i].fit=fit; break;}
538           */
539}
540
541void GenMan::clearStats()
542{
543        count = 0;
544        valid_m = valid_xo = validated_m = validated_xo = invalid_m = invalid_xo = failed_m = failed_xo = 0;
545        mutchg = xochg = 0;
546        GenoLinkList.clear();
547}
548
549void GenMan::p_clearStats(ExtValue *args, ExtValue *ret) { clearStats(); }
550
551void GenMan::p_report(ExtValue *args, ExtValue *ret)
552{                      //should be updated to handle multiple operators for a single format
553        char *g, *g2;
[145]554        float f1, f2;
555        int m;
[375]556        logMessage("GenMan", "Report", 0, "The following genetic operators are available:");
[145]557        for (unsigned int i = 0; i < oper_fx_list.size(); i++)
[138]558        {
[200]559                string l;
[513]560                if (oper_fx_list[i]->checkValidity("","") != GENOPER_NOOPER) l += " checkValidity";
[145]561                if (oper_fx_list[i]->getSimplest())
[138]562                {
[145]563                        g = strdup(oper_fx_list[i]->getSimplest());
[138]564                        g2 = strdup(g);
[513]565                        if (oper_fx_list[i]->validate(g,"") != GENOPER_NOOPER) l += " validate";
[145]566                        if (oper_fx_list[i]->mutate(g, f1, m) != GENOPER_NOOPER) l += " mutate";
567                        if (oper_fx_list[i]->crossOver(g, g2, f1, f2) != GENOPER_NOOPER) l += " crossover";
[138]568                        l += " getSimplest";
569                        free(g); free(g2);
570                }
[145]571                //      if (oper_fx_list[i]->similarity("","")!=GENOPER_NOOPER) l+=" similarity";
[375]572                logPrintf("GenMan", "Report", 0, "format f%c (%s):%s",
[200]573                        oper_fx_list[i]->supported_format, oper_fx_list[i]->name.c_str(), l.c_str());
[138]574        }
575}
576
577void GenMan::p_validate(ExtValue *args, ExtValue *ret)
578{
579        Geno *g = GenoObj::fromObject(args[0]);
580        if (g == NULL)
581                ret->setEmpty();
582        else
[532]583                *ret = GenoObj::makeDynamicObjectAndDecRef(new Geno(validate(*g)));
[138]584}
585
586void GenMan::p_mutate(ExtValue *args, ExtValue *ret)
587{
588        Geno *g = GenoObj::fromObject(args[0]);
589        if (g == NULL)
590                ret->setEmpty();
591        else
[532]592                *ret = GenoObj::makeDynamicObjectAndDecRef(new Geno(mutate(*g)));
[138]593}
594
595void GenMan::p_crossover(ExtValue *args, ExtValue *ret)
596{
597        Geno *g1 = GenoObj::fromObject(args[1]);
598        Geno *g2 = GenoObj::fromObject(args[0]);
599        if (g1 == NULL || g2 == NULL)
600                ret->setEmpty();
601        else
[532]602                *ret = GenoObj::makeDynamicObjectAndDecRef(new Geno(crossOver(*g1, *g2)));
[138]603}
[532]604
Note: See TracBrowser for help on using the repository browser.