[138] | 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 | |
---|
[139] | 13 | #ifdef USE_GENMAN_f0 |
---|
[138] | 14 | #include "f0/oper_f0.h" |
---|
| 15 | #endif |
---|
[139] | 16 | #ifdef USE_GENMAN_f0FUZZY |
---|
[138] | 17 | #include "f0/oper_f0Fuzzy.h" |
---|
| 18 | #endif |
---|
[139] | 19 | #ifdef USE_GENMAN_f1 |
---|
[138] | 20 | #include "f1/oper_f1.h" |
---|
| 21 | #endif |
---|
[139] | 22 | #ifdef USE_GENMAN_f2 |
---|
[138] | 23 | #include "f2/oper_f2.h" |
---|
| 24 | #endif |
---|
[139] | 25 | #ifdef USE_GENMAN_f2 |
---|
[138] | 26 | #include "f3/oper_f3.h" |
---|
| 27 | #endif |
---|
[139] | 28 | #ifdef USE_GENMAN_f4 |
---|
[138] | 29 | #include "f4/oper_f4.h" |
---|
| 30 | #endif |
---|
[139] | 31 | #ifdef USE_GENMAN_f5 |
---|
[138] | 32 | #include "f5/oper_f5.h" |
---|
| 33 | #endif |
---|
[139] | 34 | #ifdef USE_GENMAN_f6 |
---|
[138] | 35 | #include "f6/oper_f6.h" |
---|
| 36 | #endif |
---|
[139] | 37 | #ifdef USE_GENMAN_f7 |
---|
[138] | 38 | #include "f7/oper_f7.h" |
---|
| 39 | #endif |
---|
[139] | 40 | #ifdef USE_GENMAN_f8 |
---|
[138] | 41 | #include "f8/oper_f8.h" |
---|
| 42 | #endif |
---|
[139] | 43 | #ifdef USE_GENMAN_f9 |
---|
[138] | 44 | #include "f9/oper_f9.h" |
---|
| 45 | #endif |
---|
[139] | 46 | #ifdef USE_GENMAN_fF |
---|
| 47 | #include "fF/oper_fF.h" |
---|
| 48 | #endif |
---|
[138] | 49 | |
---|
| 50 | using 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 | |
---|
| 61 | static 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 | |
---|
| 77 | static 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 | |
---|
| 97 | GenMan::GenMan() : localpar(GMparam_tab, this), localstats(GMstats_tab, this), |
---|
| 98 | seloperpar("GenOperators", "Genetics: Active operators"), |
---|
| 99 | par("GenMan", "Manages various genetic operations, using appropriate operators for the argument genotype format."), |
---|
| 100 | neuronsparam("Genetics: Neurons to add", "neuronsAdd", "neuadd_") |
---|
| 101 | { |
---|
| 102 | history = 0; |
---|
| 103 | hilite = 1; |
---|
| 104 | clearStats(); |
---|
| 105 | |
---|
[139] | 106 | #ifdef USE_GENMAN_f0 |
---|
[145] | 107 | oper_fx_list.push_back(new Geno_f0); |
---|
[138] | 108 | #endif |
---|
[139] | 109 | #ifdef USE_GENMAN_f0FUZZY |
---|
[145] | 110 | oper_fx_list.push_back(new Geno_f0Fuzzy); |
---|
[138] | 111 | #endif |
---|
[139] | 112 | #ifdef USE_GENMAN_f1 |
---|
[145] | 113 | oper_fx_list.push_back(new Geno_f1); |
---|
[138] | 114 | #endif |
---|
[139] | 115 | #ifdef USE_GENMAN_f2 |
---|
[145] | 116 | oper_fx_list.push_back(new Geno_f2); |
---|
[138] | 117 | #endif |
---|
[139] | 118 | #ifdef USE_GENMAN_f3 |
---|
[145] | 119 | oper_fx_list.push_back(new Geno_f3); |
---|
[138] | 120 | #endif |
---|
[139] | 121 | #ifdef USE_GENMAN_f4 |
---|
[145] | 122 | oper_fx_list.push_back(new Geno_f4); |
---|
[138] | 123 | #endif |
---|
[139] | 124 | #ifdef USE_GENMAN_f5 |
---|
[145] | 125 | oper_fx_list.push_back(new Geno_f5); |
---|
[138] | 126 | #endif |
---|
[139] | 127 | #ifdef USE_GENMAN_f6 |
---|
[145] | 128 | oper_fx_list.push_back(new Geno_f6); |
---|
[138] | 129 | #endif |
---|
[139] | 130 | #ifdef USE_GENMAN_f7 |
---|
[145] | 131 | oper_fx_list.push_back(new Geno_f7); |
---|
[138] | 132 | #endif |
---|
[139] | 133 | #ifdef USE_GENMAN_f8 |
---|
[145] | 134 | oper_fx_list.push_back(new Geno_f8); |
---|
[138] | 135 | #endif |
---|
[139] | 136 | #ifdef USE_GENMAN_f9 |
---|
[145] | 137 | oper_fx_list.push_back(new GenoOper_f9); |
---|
[138] | 138 | #endif |
---|
[139] | 139 | #ifdef USE_GENMAN_fF |
---|
[145] | 140 | oper_fx_list.push_back(new GenoOper_fF); |
---|
[139] | 141 | #endif |
---|
[138] | 142 | |
---|
[145] | 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 |
---|
[138] | 144 | int selopercount = 0; |
---|
[145] | 145 | for (unsigned int i = 0; i < oper_fx_list.size(); i++) |
---|
[138] | 146 | { |
---|
[145] | 147 | if (operformats.find(oper_fx_list[i]->supported_format) != -1) continue; |
---|
[138] | 148 | char tmp[10]; |
---|
| 149 | SString id, name, type = "~"; |
---|
[145] | 150 | type += oper_fx_list[i]->name; |
---|
[138] | 151 | int dup = 0; |
---|
[145] | 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) |
---|
[138] | 154 | { |
---|
[145] | 155 | type += "~"; type += oper_fx_list[j]->name; dup++; |
---|
[138] | 156 | } |
---|
| 157 | sprintf(tmp, "d 0 %d ", dup); |
---|
| 158 | type = SString(tmp) + type; |
---|
[145] | 159 | sprintf(tmp, "%c", oper_fx_list[i]->supported_format); |
---|
[138] | 160 | id = "genoper_f"; id += tmp; |
---|
| 161 | name = "Operators for f"; name += tmp; |
---|
| 162 | seloper[selopercount] = 0; |
---|
[145] | 163 | operformats += oper_fx_list[i]->supported_format; |
---|
[138] | 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; |
---|
[145] | 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; |
---|
[138] | 173 | } |
---|
| 174 | |
---|
| 175 | GenMan::~GenMan() |
---|
| 176 | { |
---|
[145] | 177 | for (unsigned int i = 0; i < oper_fx_list.size(); i++) delete oper_fx_list[i]; |
---|
[138] | 178 | delete[] seloper; |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | void GenMan::setDefaults() |
---|
| 182 | { |
---|
[145] | 183 | for (unsigned int i = 0; i < oper_fx_list.size(); i++) |
---|
[138] | 184 | { |
---|
[145] | 185 | oper_fx_list[i]->par.setDefault(); |
---|
| 186 | oper_fx_list[i]->setDefaults(); |
---|
[138] | 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 | |
---|
| 193 | int GenMan::testValidity(Geno &g, bool &canvalidate) |
---|
| 194 | { |
---|
| 195 | const char *gg = g.getGene(); |
---|
[145] | 196 | GenoOperators *gf = getOper_f(g.getFormat()); |
---|
[138] | 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 | |
---|
| 216 | int 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 | |
---|
| 227 | Geno GenMan::Validate(const Geno& geny) |
---|
| 228 | { |
---|
| 229 | char format=geny.getFormat(); |
---|
[145] | 230 | GenoOperators *gf=getOper_f(format); |
---|
[138] | 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 | |
---|
| 243 | Geno GenMan::Mutate(const Geno& g) |
---|
| 244 | { |
---|
| 245 | float chg; //how many changes |
---|
| 246 | int method; //mutation method |
---|
| 247 | char format=g.getFormat(); |
---|
[145] | 248 | GenoOperators *gf=getOper_f(format); |
---|
[138] | 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;} else |
---|
| 268 | if (res>0 && canvalidate==false) invalid_m++; else |
---|
| 269 | {validated_m++; ok=true;} |
---|
| 270 | if (ok) gv=G; |
---|
| 271 | } else failed_m++; |
---|
| 272 | free(gn); |
---|
| 273 | count++; |
---|
| 274 | if (!ok && (count-pcount>100)) |
---|
| 275 | { |
---|
| 276 | FMprintf("GenMan","Mutate",2,"Tried 100x and failed: %s",(const char*)g.getGene()); |
---|
| 277 | return Geno("",-1,"","GENOPER_OPFAIL: Mutate() tried 100x and failed"); |
---|
| 278 | } |
---|
| 279 | } |
---|
| 280 | mutchg+=chg; |
---|
| 281 | if (history) saveLink((const char*)g.getGene(),(const char*)gv.getGene(),chg); |
---|
| 282 | SString mutinfo; |
---|
| 283 | if (extmutinfo == 0) mutinfo = SString::sprintf("%.2f%% mutation of '%s'",100*chg,(const char*)g.getName()); else |
---|
| 284 | if (extmutinfo == 1) mutinfo = SString::sprintf("%.2f%% mutation(%d) of '%s'", 100 * chg, method, (const char*)g.getName()); else |
---|
| 285 | 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()); |
---|
| 286 | gv.setComment(mutinfo); |
---|
| 287 | return gv; |
---|
| 288 | } |
---|
| 289 | |
---|
| 290 | Geno GenMan::CrossOver(const Geno& g1, const Geno& g2) |
---|
| 291 | { |
---|
| 292 | char format = g1.getFormat(); |
---|
| 293 | 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())); |
---|
[145] | 294 | GenoOperators *gf = getOper_f(format); |
---|
[138] | 295 | if (gf == NULL) |
---|
| 296 | return Geno(SString::empty(), -1, SString::empty(), SString::sprintf("GENOPER_NOOPER: CrossOver(): don't know how to handle genetic format %c", format)); |
---|
| 297 | Geno g1v = g1, g2v = g2; |
---|
| 298 | |
---|
| 299 | { |
---|
| 300 | ErrorHandler eh(ErrorHandler::StoreFirstMessage); //mute testValidity() |
---|
| 301 | bool canvalidate = true; |
---|
| 302 | if (testValidity(g1v, canvalidate) > 0 && canvalidate == false) |
---|
| 303 | return Geno("", -1, "", "GENOPER_OPFAIL: CrossOver(): cannot validate invalid source genotype #1"); |
---|
| 304 | canvalidate = true; |
---|
| 305 | if (testValidity(g2v, canvalidate) > 0 && canvalidate == false) |
---|
| 306 | return Geno("", -1, "", "GENOPER_OPFAIL: CrossOver(): cannot validate invalid source genotype #2"); |
---|
| 307 | } |
---|
| 308 | |
---|
| 309 | float chg; |
---|
| 310 | bool ok = false; |
---|
| 311 | int pcount = count; |
---|
| 312 | |
---|
| 313 | while (!ok) |
---|
| 314 | { |
---|
| 315 | float chg1, chg2; |
---|
| 316 | char *g1n = strdup(g1.getGene()); //copy for crossover |
---|
| 317 | char *g2n = strdup(g2.getGene()); //copy for crossover |
---|
| 318 | chg1 = chg2 = 0; |
---|
| 319 | if (gf->crossOver(g1n, g2n, chg1, chg2) == GENOPER_OK) |
---|
| 320 | { |
---|
| 321 | char *gn; |
---|
| 322 | if (g1n[0] && g2n[0]) if (randomN(2) == 0) g1n[0] = 0; else g2n[0] = 0; //we want only one |
---|
| 323 | if (g1n[0]) { gn = g1n; chg = chg1; } |
---|
| 324 | else { gn = g2n; chg = chg2; } |
---|
| 325 | ErrorHandler eh(ErrorHandler::StoreFirstMessage); //mute testValidity() |
---|
| 326 | Geno G(gn, g1v.getFormat(), "", ""); |
---|
| 327 | bool canvalidate = true; |
---|
| 328 | int res = testValidity(G, canvalidate); |
---|
| 329 | if (res == GENOPER_OK && canvalidate == false) { valid_xo++; ok = true; } |
---|
| 330 | else |
---|
| 331 | if (res > 0 && canvalidate == false) invalid_xo++; else |
---|
| 332 | { |
---|
| 333 | validated_xo++; ok = true; |
---|
| 334 | } |
---|
| 335 | if (ok) g1v = G; |
---|
| 336 | } |
---|
| 337 | else failed_xo++; |
---|
| 338 | free(g1n); |
---|
| 339 | free(g2n); |
---|
| 340 | count++; |
---|
| 341 | if (!ok && (count - pcount > 100)) |
---|
| 342 | { |
---|
| 343 | FMprintf("GenMan", "CrossOver", 2, "Tried 100x and failed: %s and %s", (const char*)g1.getGene(), (const char*)g2.getGene()); |
---|
| 344 | return Geno("", -1, "", "GENOPER_OPFAIL: CrossOver() tried 100x and failed"); |
---|
| 345 | } |
---|
| 346 | } |
---|
| 347 | // result in g1v |
---|
| 348 | xochg += chg; |
---|
| 349 | if (history) saveLink((const char*)g1.getGene(), (const char*)g1v.getGene(), chg); |
---|
| 350 | SString xoinfo = SString::sprintf("Crossing over of '%s' (%.2f%%) and '%s' (%.2f%%)", |
---|
| 351 | (const char*)g1.getName(), 100 * chg, (const char*)g2.getName(), 100 * (1 - chg)); |
---|
| 352 | g1v.setComment(xoinfo); |
---|
| 353 | return g1v; |
---|
| 354 | } |
---|
| 355 | |
---|
| 356 | float GenMan::Similarity(const Geno& g1, const Geno& g2) |
---|
| 357 | { |
---|
| 358 | char format = g1.getFormat(); |
---|
| 359 | if (format != g2.getFormat()) return GENOPER_NOOPER; |
---|
[145] | 360 | GenoOperators *gf = getOper_f(format); |
---|
[138] | 361 | if (!gf) return GENOPER_NOOPER; else return gf->similarity(g1.getGene(), g2.getGene()); |
---|
| 362 | } |
---|
| 363 | |
---|
| 364 | unsigned long GenMan::Style(const char *g, int pos) |
---|
| 365 | { |
---|
| 366 | Geno G(g); |
---|
| 367 | if ((pos = G.mapStringToGen(pos)) == -1) return GENSTYLE_COMMENT; |
---|
[145] | 368 | GenoOperators *gf = getOper_f(G.getFormat()); |
---|
[138] | 369 | if (!gf) return GENSTYLE_CS(0, 0); //black & valid |
---|
| 370 | else return gf->style(G.getGene(), pos); |
---|
| 371 | } |
---|
| 372 | |
---|
| 373 | void GenMan::GetFullStyle(const char *g, unsigned long *styletab) |
---|
| 374 | { |
---|
| 375 | Geno G(g); |
---|
[145] | 376 | GenoOperators *gf = getOper_f(G.getFormat()); |
---|
[138] | 377 | SString geny = G.getGene(); |
---|
| 378 | for (unsigned int pos = 0; pos < strlen(g); pos++) |
---|
| 379 | { |
---|
| 380 | int posmapped = G.mapStringToGen(pos); |
---|
| 381 | if (posmapped == -1) styletab[pos] = GENSTYLE_COMMENT; |
---|
| 382 | else if (!gf) styletab[pos] = GENSTYLE_CS(0, 0); //black & valid |
---|
| 383 | else styletab[pos] = gf->style(geny, posmapped); |
---|
| 384 | } |
---|
| 385 | } |
---|
| 386 | |
---|
| 387 | SString GenMan::HTMLize(const char *g) { return HTMLize(g, false); } |
---|
| 388 | |
---|
| 389 | SString GenMan::HTMLizeShort(const char *g) { return HTMLize(g, true); } |
---|
| 390 | |
---|
| 391 | SString GenMan::HTMLize(const char *g, bool shorten) |
---|
| 392 | { |
---|
| 393 | char buf[50]; |
---|
| 394 | int len = strlen(g); |
---|
| 395 | int chars = 0, lines = 0; |
---|
| 396 | bool shortened = false; |
---|
| 397 | unsigned long *styletab = new unsigned long[len]; |
---|
| 398 | GetFullStyle(g, styletab); |
---|
| 399 | SString html = "\n<div style=\"background:white;padding:0.2em;font-family:arial,helvetica,sans-serif;font-size:90%\">"; |
---|
| 400 | unsigned long prevstyle, prevcolor, style = 0, color = 0; |
---|
| 401 | for (int i = 0; i<len; i++) |
---|
| 402 | { |
---|
| 403 | if (shorten && ((lines == 0 && chars>160) || (lines > 5 || chars > 300))) { shortened = true; break; } |
---|
| 404 | if (g[i] == '\r') continue; |
---|
| 405 | if (g[i] == '\n') { html += "<br>\n"; lines++; continue; } |
---|
| 406 | chars++; |
---|
| 407 | prevstyle = style; |
---|
| 408 | prevcolor = color; |
---|
| 409 | style = GENGETSTYLE(styletab[i]); |
---|
| 410 | color = GENGETCOLOR(styletab[i]); |
---|
| 411 | if ((i != 0 && (color != prevcolor))) html += "</font>"; |
---|
| 412 | if ((style&GENSTYLE_INVALID) != (prevstyle&GENSTYLE_INVALID)) |
---|
| 413 | { |
---|
| 414 | html += "<"; if (!(style&GENSTYLE_INVALID)) html += "/"; html += "u>"; |
---|
| 415 | } |
---|
| 416 | if ((style&GENSTYLE_BOLD) != (prevstyle&GENSTYLE_BOLD)) |
---|
| 417 | { |
---|
| 418 | html += "<"; if (!(style&GENSTYLE_BOLD)) html += "/"; html += "b>"; |
---|
| 419 | } |
---|
| 420 | if ((style&GENSTYLE_ITALIC) != (prevstyle&GENSTYLE_ITALIC)) |
---|
| 421 | { |
---|
| 422 | html += "<"; if (!(style&GENSTYLE_ITALIC)) html += "/"; html += "i>"; |
---|
| 423 | } |
---|
| 424 | if ((i == 0 || (color != prevcolor))) |
---|
| 425 | { |
---|
| 426 | sprintf(buf, "<font color=#%02x%02x%02x>", GENGET_R(color), GENGET_G(color), GENGET_B(color)); html += buf; |
---|
| 427 | } |
---|
| 428 | if (g[i] == '<') html += "<"; else if (g[i] == '>') html += ">"; else html += g[i]; |
---|
| 429 | if ((i % 3) == 0 && g[i] == ' ') html += "\n"; //for readability, insert some newlines into html... |
---|
| 430 | } |
---|
| 431 | delete[] styletab; |
---|
| 432 | html += "</u></b></i></font>"; |
---|
| 433 | if (shortened) html += " [etc...]"; |
---|
| 434 | html += "</div>\n"; |
---|
| 435 | return html; |
---|
| 436 | } |
---|
| 437 | |
---|
| 438 | void GenMan::p_htmlize(ExtValue *args, ExtValue *ret) |
---|
| 439 | { |
---|
| 440 | ret->setString(HTMLize(args->getString())); |
---|
| 441 | } |
---|
| 442 | |
---|
| 443 | void GenMan::p_htmlizeshort(ExtValue *args, ExtValue *ret) |
---|
| 444 | { |
---|
| 445 | ret->setString(HTMLizeShort(args->getString())); |
---|
| 446 | } |
---|
| 447 | |
---|
| 448 | Geno GenMan::GetSimplest(char format) |
---|
| 449 | { |
---|
[145] | 450 | GenoOperators *gf = getOper_f(format); |
---|
[138] | 451 | if (!gf) return Geno(); |
---|
| 452 | SString info = "The simplest genotype of format f"; info += format; |
---|
| 453 | info += " for operators '"; info += gf->name; info += "'."; |
---|
| 454 | return Geno(gf->getSimplest(), format, "Root", (const char*)info); |
---|
| 455 | } |
---|
| 456 | |
---|
| 457 | void GenMan::p_getsimplest(ExtValue *args, ExtValue *ret) |
---|
| 458 | { |
---|
| 459 | int format = args[0].getInt() + 48; |
---|
[145] | 460 | if (!getOper_f(format)) |
---|
[138] | 461 | ret->setEmpty(); |
---|
| 462 | else |
---|
| 463 | *ret = GenoObj::makeDynamicObjectAndDecRef(new Geno(GetSimplest(format))); |
---|
| 464 | } |
---|
| 465 | |
---|
| 466 | const char *GenMan::GetOpName(char format) |
---|
| 467 | { |
---|
[145] | 468 | GenoOperators *gf = getOper_f(format); |
---|
[138] | 469 | if (!gf) return "n/a"; else return gf->name; |
---|
| 470 | } |
---|
| 471 | |
---|
[145] | 472 | GenoOperators* GenMan::getOper_f(char format) |
---|
[138] | 473 | { |
---|
| 474 | int ind = operformats.find(format); |
---|
| 475 | if (ind == -1) return NULL; |
---|
| 476 | int ktoryopformatu = seloper[ind]; |
---|
[145] | 477 | for (unsigned int i = 0; i < oper_fx_list.size(); i++) |
---|
| 478 | if (oper_fx_list[i]->supported_format == format) |
---|
| 479 | if (ktoryopformatu == 0) return oper_fx_list[i]; else ktoryopformatu--; |
---|
[138] | 480 | return NULL; //should never happen |
---|
| 481 | } |
---|
| 482 | |
---|
| 483 | void GenMan::saveLink(string prz, string pot, float& chg) |
---|
| 484 | { |
---|
| 485 | GenoLink l; |
---|
| 486 | l.count = count; |
---|
| 487 | l.g1 = prz; |
---|
| 488 | l.g2 = pot; |
---|
| 489 | l.chg = chg; |
---|
| 490 | l.fit = 0; //temporarily. Will be set when the genotype dies |
---|
| 491 | GenoLinkList.push_back(l); |
---|
| 492 | } |
---|
| 493 | |
---|
| 494 | void GenMan::onDelGen(void *obj, long n) |
---|
| 495 | { |
---|
| 496 | //old code needs update: |
---|
| 497 | // ((SpeciesList*)obj)->przyDodaniu(i); |
---|
| 498 | /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
---|
| 499 | GenMan *gm=(GenMan*)obj; |
---|
| 500 | Genotype *gt=(Genotype*)(*listaGen)(n); //there is no more "listaGen" |
---|
| 501 | string g=(const char*)gt->genotype.getGene(); |
---|
| 502 | float fit=gt->getFinalFitness(); |
---|
| 503 | for(int i=0;i<gm->GenoLinkList.size();i++) //find genotype |
---|
| 504 | if (gm->GenoLinkList[i].g1==g) {gm->GenoLinkList[i].fit=fit; break;} |
---|
| 505 | */ |
---|
| 506 | } |
---|
| 507 | |
---|
| 508 | void GenMan::clearStats() |
---|
| 509 | { |
---|
| 510 | count = 0; |
---|
| 511 | valid_m = valid_xo = validated_m = validated_xo = invalid_m = invalid_xo = failed_m = failed_xo = 0; |
---|
| 512 | mutchg = xochg = 0; |
---|
| 513 | GenoLinkList.clear(); |
---|
| 514 | } |
---|
| 515 | |
---|
| 516 | void GenMan::p_clearStats(ExtValue *args, ExtValue *ret) { clearStats(); } |
---|
| 517 | |
---|
| 518 | void GenMan::p_report(ExtValue *args, ExtValue *ret) |
---|
| 519 | { //should be updated to handle multiple operators for a single format |
---|
| 520 | char *g, *g2; |
---|
[145] | 521 | float f1, f2; |
---|
| 522 | int m; |
---|
[138] | 523 | FramMessage("GenMan", "Report", "The following genetic operators are available:", 0); |
---|
[145] | 524 | for (unsigned int i = 0; i < oper_fx_list.size(); i++) |
---|
[138] | 525 | { |
---|
| 526 | SString l; |
---|
[145] | 527 | if (oper_fx_list[i]->checkValidity("") != GENOPER_NOOPER) l += " checkValidity"; |
---|
| 528 | if (oper_fx_list[i]->getSimplest()) |
---|
[138] | 529 | { |
---|
[145] | 530 | g = strdup(oper_fx_list[i]->getSimplest()); |
---|
[138] | 531 | g2 = strdup(g); |
---|
[145] | 532 | if (oper_fx_list[i]->validate(g) != GENOPER_NOOPER) l += " validate"; |
---|
| 533 | if (oper_fx_list[i]->mutate(g, f1, m) != GENOPER_NOOPER) l += " mutate"; |
---|
| 534 | if (oper_fx_list[i]->crossOver(g, g2, f1, f2) != GENOPER_NOOPER) l += " crossover"; |
---|
[138] | 535 | l += " getSimplest"; |
---|
| 536 | free(g); free(g2); |
---|
| 537 | } |
---|
[145] | 538 | // if (oper_fx_list[i]->similarity("","")!=GENOPER_NOOPER) l+=" similarity"; |
---|
[138] | 539 | FMprintf("GenMan", "Report", 0, "format f%c (%s):%s", |
---|
[145] | 540 | oper_fx_list[i]->supported_format, (const char*)oper_fx_list[i]->name, (const char*)l); |
---|
[138] | 541 | } |
---|
| 542 | } |
---|
| 543 | |
---|
| 544 | void GenMan::p_validate(ExtValue *args, ExtValue *ret) |
---|
| 545 | { |
---|
| 546 | Geno *g = GenoObj::fromObject(args[0]); |
---|
| 547 | if (g == NULL) |
---|
| 548 | ret->setEmpty(); |
---|
| 549 | else |
---|
| 550 | *ret = GenoObj::makeDynamicObjectAndDecRef(new Geno(Validate(*g))); |
---|
| 551 | } |
---|
| 552 | |
---|
| 553 | void GenMan::p_mutate(ExtValue *args, ExtValue *ret) |
---|
| 554 | { |
---|
| 555 | Geno *g = GenoObj::fromObject(args[0]); |
---|
| 556 | if (g == NULL) |
---|
| 557 | ret->setEmpty(); |
---|
| 558 | else |
---|
| 559 | *ret = GenoObj::makeDynamicObjectAndDecRef(new Geno(Mutate(*g))); |
---|
| 560 | } |
---|
| 561 | |
---|
| 562 | void GenMan::p_crossover(ExtValue *args, ExtValue *ret) |
---|
| 563 | { |
---|
| 564 | Geno *g1 = GenoObj::fromObject(args[1]); |
---|
| 565 | Geno *g2 = GenoObj::fromObject(args[0]); |
---|
| 566 | if (g1 == NULL || g2 == NULL) |
---|
| 567 | ret->setEmpty(); |
---|
| 568 | else |
---|
| 569 | *ret = GenoObj::makeDynamicObjectAndDecRef(new Geno(CrossOver(*g1, *g2))); |
---|
| 570 | } |
---|