- Timestamp:
- 03/28/18 02:26:29 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
cpp/frams/genetics/genman.cpp
r748 r761 1 1 // This file is a part of Framsticks SDK. http://www.framsticks.com/ 2 // Copyright (C) 1999-201 5Maciej Komosinski and Szymon Ulatowski.2 // Copyright (C) 1999-2018 Maciej Komosinski and Szymon Ulatowski. 3 3 // See LICENSE.txt for details. 4 4 … … 10 10 #include "common/stl-util.h" 11 11 #include <common/loggers/loggers.h> 12 13 14 #define GENMAN_REPEAT_FAILED 10 //how many times GenMan tries to repeat a mutation or crossover when the operator does not return acceptable genotype 15 #define STRINGIFY_1(x) #x 16 #define STRINGIFY(x) STRINGIFY_1(x) //this second-level macro allows the parameter to be a macro itself and to stringify its value, not its name 17 #define GENMAN_REPEAT_FAILED_STR STRINGIFY(GENMAN_REPEAT_FAILED) 12 18 13 19 … … 164 170 if (oper_fx_list[i]->supported_format == oper_fx_list[j]->supported_format) 165 171 { 166 167 168 172 type += "~"; 173 type += oper_fx_list[j]->name; 174 dup++; 169 175 } 170 176 type = ssprintf("d 0 %d ", dup) + type; … … 206 212 int GenMan::testValidity(Geno &g, bool &canvalidate) 207 213 { 208 SString ggs =g.getGenes();214 SString ggs = g.getGenes(); 209 215 const char *gg = ggs.c_str(); 210 216 GenoOperators *gf = getOper_f(g.getFormat()); 211 217 int check1; 212 218 if (!gf) { canvalidate = false; return GENOPER_NOOPER; } 213 else check1 = gf->checkValidity(gg, g.getName().c_str());219 else check1 = gf->checkValidity(gg, g.getName().c_str()); 214 220 if (!canvalidate) return check1; //just checking 215 221 if (check1 == GENOPER_OK) { canvalidate = false; return check1; } 216 222 char *g2 = strdup(gg); 217 if (gf->validate(g2, g.getName().c_str()) == GENOPER_NOOPER) { free(g2); canvalidate = false; return check1; }223 if (gf->validate(g2, g.getName().c_str()) == GENOPER_NOOPER) { free(g2); canvalidate = false; return check1; } 218 224 if (check1 == GENOPER_NOOPER) //disaster: cannot check because there is no check operator 219 225 { … … 283 289 if (res > 0 && canvalidate == false) invalid_m++; else 284 290 { 285 291 validated_m++; ok = true; 286 292 } 287 293 if (ok) gv = G; … … 290 296 free(gn); 291 297 count++; 292 if (!ok && (count - pcount > 100))293 { 294 logPrintf("GenMan", "Mutate", 2, "Tried 100x and failed: %s", g.getGenes().c_str());295 return Geno("", -1, "", "GENOPER_OPFAIL: Mutate() tried 100x and failed");298 if (!ok && (count - pcount > GENMAN_REPEAT_FAILED)) 299 { 300 logPrintf("GenMan", "Mutate", 2, "Tried " GENMAN_REPEAT_FAILED_STR "x and failed: %s", g.getGenes().c_str()); 301 return Geno("", -1, "", "GENOPER_OPFAIL: Mutate() tried " GENMAN_REPEAT_FAILED_STR "x and failed"); 296 302 } 297 303 } … … 349 355 if (res > 0 && canvalidate == false) invalid_xo++; else 350 356 { 351 357 validated_xo++; ok = true; 352 358 } 353 359 if (ok) g1v = G; … … 357 363 free(g2n); 358 364 count++; 359 if (!ok && (count - pcount > 100))360 { 361 logPrintf("GenMan", "CrossOver", 2, "Tried 100x and failed: %s and %s", g1.getGenes().c_str(), g2.getGenes().c_str());362 return Geno("", -1, "", "GENOPER_OPFAIL: CrossOver() tried 100x and failed");365 if (!ok && (count - pcount > GENMAN_REPEAT_FAILED)) 366 { 367 logPrintf("GenMan", "CrossOver", 2, "Tried " GENMAN_REPEAT_FAILED_STR "x and failed: %s and %s", g1.getGenes().c_str(), g2.getGenes().c_str()); 368 return Geno("", -1, "", "GENOPER_OPFAIL: CrossOver() tried " GENMAN_REPEAT_FAILED_STR "x and failed"); 363 369 } 364 370 } … … 407 413 } 408 414 GenoOperators *gf = getOper_f(format); 409 SString geny =G->getGenes();415 SString geny = G->getGenes(); 410 416 for (unsigned int pos = 0; pos < strlen(g); pos++) 411 417 { … … 438 444 string html = "\n<div style=\"background:white;padding:0.2em;font-family:arial,helvetica,sans-serif;font-size:90%\">"; 439 445 uint32_t prevstyle, prevcolor, style = 0, color = 0; 440 for (int i = 0; i <len; i++)441 { 442 if (shorten && ((lines == 0 && chars >160) || (lines > 5 || chars > 300))) { shortened = true; break; }446 for (int i = 0; i < len; i++) 447 { 448 if (shorten && ((lines == 0 && chars > 160) || (lines > 5 || chars > 300))) { shortened = true; break; } 443 449 if (g[i] == '\r') continue; 444 450 if (g[i] == '\n') { html += "<br>\n"; lines++; continue; } … … 566 572 { 567 573 string l; 568 if (oper_fx_list[i]->checkValidity("", "") != GENOPER_NOOPER) l += " checkValidity";574 if (oper_fx_list[i]->checkValidity("", "") != GENOPER_NOOPER) l += " checkValidity"; 569 575 if (oper_fx_list[i]->getSimplest()) 570 576 { 571 577 g = strdup(oper_fx_list[i]->getSimplest()); 572 578 g2 = strdup(g); 573 if (oper_fx_list[i]->validate(g, "") != GENOPER_NOOPER) l += " validate";579 if (oper_fx_list[i]->validate(g, "") != GENOPER_NOOPER) l += " validate"; 574 580 if (oper_fx_list[i]->mutate(g, f1, m) != GENOPER_NOOPER) l += " mutate"; 575 581 if (oper_fx_list[i]->crossOver(g, g2, f1, f2) != GENOPER_NOOPER) l += " crossover";
Note: See TracChangeset
for help on using the changeset viewer.