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