Changeset 955 for cpp/frams/genetics/genman.cpp
- Timestamp:
- 06/25/20 00:34:29 (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
cpp/frams/genetics/genman.cpp
r896 r955 1 1 // This file is a part of Framsticks SDK. http://www.framsticks.com/ 2 // Copyright (C) 1999-20 18Maciej Komosinski and Szymon Ulatowski.2 // Copyright (C) 1999-2020 Maciej Komosinski and Szymon Ulatowski. 3 3 // See LICENSE.txt for details. 4 4 … … 68 68 #ifdef USE_GENMAN_fL 69 69 #include "fL/fL_oper.h" 70 #endif 71 #ifdef USE_GENMAN_fS 72 #include "fS/fS_oper.h" 70 73 #endif 71 74 … … 177 180 oper_fx_list.push_back(new Geno_fL); 178 181 #endif 182 #ifdef USE_GENMAN_fS 183 oper_fx_list.push_back(new GenoOper_fS); 184 #endif 179 185 180 186 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 … … 182 188 for (unsigned int i = 0; i < oper_fx_list.size(); i++) 183 189 { 184 if ( operformats.find(oper_fx_list[i]->supported_format) != -1) continue;190 if (findOperFormatIndex(oper_fx_list[i]->supported_format) != -1) continue; 185 191 string type = string("~") + oper_fx_list[i]->name; 186 192 int dup = 0; … … 188 194 if (oper_fx_list[i]->supported_format == oper_fx_list[j]->supported_format) 189 195 { 190 type += "~";191 type += oper_fx_list[j]->name;192 dup++;196 type += "~"; 197 type += oper_fx_list[j]->name; 198 dup++; 193 199 } 194 200 type = ssprintf("d 0 %d ", dup) + type; 195 string id = ssprintf("genoper_f% c", oper_fx_list[i]->supported_format);196 string name = ssprintf("Operators for f% c", oper_fx_list[i]->supported_format);201 string id = ssprintf("genoper_f%s", oper_fx_list[i]->supported_format.c_str()); 202 string name = ssprintf("Operators for f%s", oper_fx_list[i]->supported_format.c_str()); 197 203 seloper[selopercount] = 0; 198 operformats += oper_fx_list[i]->supported_format;204 operformats += &oper_fx_list[i]->supported_format; 199 205 //printf("%x %s %s %s\n",&seloper[selopercount],(const char*)id,(const char*)type,(const char*)name); 200 seloperpar.addProperty(&seloper[selopercount++], id.c_str(), type.c_str(), name.c_str(), "", PARAM_READONLY *(dup == 0));206 seloperpar.addProperty(&seloper[selopercount++], id.c_str(), type.c_str(), name.c_str(), "", PARAM_READONLY * (dup == 0)); 201 207 } 202 208 … … 214 220 for (unsigned int i = 0; i < oper_fx_list.size(); i++) delete oper_fx_list[i]; 215 221 delete[] seloper; 222 } 223 224 int GenMan::findOperFormatIndex(const SString& format) 225 { 226 for (int i = 0; i < operformats.size(); i++) 227 if (*operformats(i) == format) 228 return i; 229 return -1; 216 230 } 217 231 … … 265 279 Geno GenMan::validate(const Geno& geny) 266 280 { 267 charformat = geny.getFormat();281 SString format = geny.getFormat(); 268 282 GenoOperators *gf = getOper_f(format); 269 283 if (gf == NULL) 270 return Geno( SString::empty(), -1, SString::empty(), SString::sprintf("GENOPER_NOOPER: Validate(): don't know how to handle genetic format %c", format));284 return Geno("", Geno::INVALID_FORMAT, "", SString::sprintf("GENOPER_NOOPER: Validate(): don't know how to handle genetic format %s", format.c_str())); 271 285 char *g2 = strdup(geny.getGenes().c_str()); //copy for validation 272 286 int res = gf->validate(g2, geny.getName().c_str()); … … 276 290 return Geno(sg2, format, geny.getName(), geny.getComment()); 277 291 else 278 return Geno( SString::empty(), -1, SString::empty(), SString::sprintf("GENOPER_NOOPER: validate() for format %c returned invalid value", format));292 return Geno("", Geno::INVALID_FORMAT, "", SString::sprintf("GENOPER_NOOPER: validate() for format %s returned invalid value", format.c_str())); 279 293 } 280 294 … … 283 297 float chg; //how many changes 284 298 int method; //mutation method 285 charformat = g.getFormat();299 SString format = g.getFormat(); 286 300 GenoOperators *gf = getOper_f(format); 287 301 if (gf == NULL) 288 return Geno( SString::empty(), -1, SString::empty(), SString::sprintf("GENOPER_NOOPER: Mutate(): don't know how to handle genetic format %c", format));302 return Geno("", Geno::INVALID_FORMAT, "", SString::sprintf("GENOPER_NOOPER: Mutate(): don't know how to handle genetic format %s", format.c_str())); 289 303 Geno gv = g; 290 304 bool canvalidate = true; 291 305 if (testValidity(gv, canvalidate) > 0 && canvalidate == false) 292 return Geno("", -1, "", "GENOPER_OPFAIL: Mutate(): cannot validate invalid source genotype");306 return Geno("", Geno::INVALID_FORMAT, "", "GENOPER_OPFAIL: Mutate(): cannot validate invalid source genotype"); 293 307 bool ok = false; 294 308 int pcount = count; … … 307 321 if (res > 0 && canvalidate == false) invalid_m++; else 308 322 { 309 validated_m++; ok = true;323 validated_m++; ok = true; 310 324 } 311 325 if (ok) gv = G; … … 316 330 if (!ok && (count - pcount > GENMAN_REPEAT_FAILED)) 317 331 { 318 logPrintf("GenMan", "Mutate", 2, "Tried " GENMAN_REPEAT_FAILED_STR "x and failed: %s", g.getGenes().c_str());332 logPrintf("GenMan", "Mutate", LOG_WARN, "Tried " GENMAN_REPEAT_FAILED_STR "x and failed: %s", g.getGenes().c_str()); 319 333 return Geno("", -1, "", "GENOPER_OPFAIL: Mutate() tried " GENMAN_REPEAT_FAILED_STR "x and failed"); 320 334 } … … 332 346 Geno GenMan::crossOver(const Geno& g1, const Geno& g2) 333 347 { 334 charformat = g1.getFormat();335 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()));348 SString format = g1.getFormat(); 349 if (format != g2.getFormat()) return Geno("", Geno::INVALID_FORMAT, "", SString::sprintf("GENOPER_NOOPER: CrossOver(): does not work for parents with differing genetic formats (%s and %s)", format.c_str(), g2.getFormat().c_str())); 336 350 GenoOperators *gf = getOper_f(format); 337 351 if (gf == NULL) 338 return Geno( SString::empty(), -1, SString::empty(), SString::sprintf("GENOPER_NOOPER: CrossOver(): no operators found for genetic format %c", format));352 return Geno("", Geno::INVALID_FORMAT, "", SString::sprintf("GENOPER_NOOPER: CrossOver(): no operators found for genetic format %s", format.c_str())); 339 353 Geno g1v = g1, g2v = g2; 340 354 … … 343 357 bool canvalidate = true; 344 358 if (testValidity(g1v, canvalidate) > 0 && canvalidate == false) 345 return Geno("", -1, "", "GENOPER_OPFAIL: CrossOver(): cannot validate invalid source genotype #1");359 return Geno("", Geno::INVALID_FORMAT, "", "GENOPER_OPFAIL: CrossOver(): cannot validate invalid source genotype #1"); 346 360 canvalidate = true; 347 361 if (testValidity(g2v, canvalidate) > 0 && canvalidate == false) 348 return Geno("", -1, "", "GENOPER_OPFAIL: CrossOver(): cannot validate invalid source genotype #2");362 return Geno("", Geno::INVALID_FORMAT, "", "GENOPER_OPFAIL: CrossOver(): cannot validate invalid source genotype #2"); 349 363 } 350 364 … … 373 387 if (res > 0 && canvalidate == false) invalid_xo++; else 374 388 { 375 validated_xo++; ok = true;389 validated_xo++; ok = true; 376 390 } 377 391 if (ok) g1v = G; … … 383 397 if (!ok && (count - pcount > GENMAN_REPEAT_FAILED)) 384 398 { 385 logPrintf("GenMan", "CrossOver", 2, "Tried " GENMAN_REPEAT_FAILED_STR "x and failed: %s and %s", g1.getGenes().c_str(), g2.getGenes().c_str());386 return Geno("", -1, "", "GENOPER_OPFAIL: CrossOver() tried " GENMAN_REPEAT_FAILED_STR "x and failed");399 logPrintf("GenMan", "CrossOver", LOG_WARN, "Tried " GENMAN_REPEAT_FAILED_STR "x and failed: %s and %s", g1.getGenes().c_str(), g2.getGenes().c_str()); 400 return Geno("", Geno::INVALID_FORMAT, "", "GENOPER_OPFAIL: CrossOver() tried " GENMAN_REPEAT_FAILED_STR "x and failed"); 387 401 } 388 402 } … … 398 412 float GenMan::similarity(const Geno& g1, const Geno& g2) 399 413 { 400 charformat = g1.getFormat();414 SString format = g1.getFormat(); 401 415 if (format != g2.getFormat()) return GENOPER_NOOPER; 402 416 GenoOperators *gf = getOper_f(format); … … 406 420 uint32_t GenMan::getStyle(const char *g, const Geno *G, int pos) 407 421 { 408 charformat = G->getFormat();422 SString format = G->getFormat(); 409 423 if (format == Geno::INVALID_FORMAT) 410 424 return GENSTYLE_RGBS(64, 64, 64, 0); // gray & "valid" (unknown format so we don't know what is valid and what is not) … … 423 437 void GenMan::getFullStyle(const char *g, const Geno *G, uint32_t *styletab) 424 438 { 425 charformat = G->getFormat();439 SString format = G->getFormat(); 426 440 if (format == Geno::INVALID_FORMAT) 427 441 { … … 438 452 else if (!gf) styletab[pos] = GENSTYLE_CS(0, 0); //black & valid 439 453 else styletab[pos] = gf->style(geny.c_str(), posmapped); 440 //logPrintf("GenMan", "getFullStyle", 0, "%d char='%c' (%d) format=0x%08x", pos, g[pos], g[pos], styletab[pos]);454 //logPrintf("GenMan", "getFullStyle", LOG_INFO, "%d char='%c' (%d) format=0x%08x", pos, g[pos], g[pos], styletab[pos]); 441 455 } 442 456 } … … 473 487 color = GENGETCOLOR(styletab[i]); 474 488 if ((i != 0 && (color != prevcolor))) html += "</font>"; 475 if ((style &GENSTYLE_INVALID) != (prevstyle&GENSTYLE_INVALID))476 { 477 html += "<"; if (!(style &GENSTYLE_INVALID)) html += "/"; html += "u>";478 } 479 if ((style &GENSTYLE_BOLD) != (prevstyle&GENSTYLE_BOLD))480 { 481 html += "<"; if (!(style &GENSTYLE_BOLD)) html += "/"; html += "b>";482 } 483 if ((style &GENSTYLE_ITALIC) != (prevstyle&GENSTYLE_ITALIC))484 { 485 html += "<"; if (!(style &GENSTYLE_ITALIC)) html += "/"; html += "i>";489 if ((style & GENSTYLE_INVALID) != (prevstyle & GENSTYLE_INVALID)) 490 { 491 html += "<"; if (!(style & GENSTYLE_INVALID)) html += "/"; html += "u>"; 492 } 493 if ((style & GENSTYLE_BOLD) != (prevstyle & GENSTYLE_BOLD)) 494 { 495 html += "<"; if (!(style & GENSTYLE_BOLD)) html += "/"; html += "b>"; 496 } 497 if ((style & GENSTYLE_ITALIC) != (prevstyle & GENSTYLE_ITALIC)) 498 { 499 html += "<"; if (!(style & GENSTYLE_ITALIC)) html += "/"; html += "i>"; 486 500 } 487 501 if ((i == 0 || (color != prevcolor))) … … 509 523 } 510 524 511 Geno GenMan::getSimplest(c harformat)525 Geno GenMan::getSimplest(const SString& format) 512 526 { 513 527 GenoOperators *gf = getOper_f(format); 514 528 if (!gf) return Geno(); 515 string info = "The simplest genotype of format f"; info += format ;529 string info = "The simplest genotype of format f"; info += format.c_str(); 516 530 info += " for operators '"; info += gf->name; info += "'."; 517 531 return Geno(gf->getSimplest(), format, "Root", info.c_str()); … … 520 534 void GenMan::p_getsimplest(ExtValue *args, ExtValue *ret) 521 535 { 522 intformat = GenoObj::formatFromExtValue(args[0]);536 SString format = GenoObj::formatFromExtValue(args[0]); 523 537 if (!getOper_f(format)) 524 538 ret->setEmpty(); … … 527 541 } 528 542 529 const char *GenMan::getOpName(c harformat)543 const char *GenMan::getOpName(const SString& format) 530 544 { 531 545 GenoOperators *gf = getOper_f(format); … … 533 547 } 534 548 535 GenoOperators* GenMan::getOper_f(c harformat)536 { 537 int ind = operformats.find(format);549 GenoOperators* GenMan::getOper_f(const SString& format) 550 { 551 int ind = findOperFormatIndex(format); 538 552 if (ind == -1) return NULL; 539 553 int which_oper_of_format = seloper[ind]; … … 602 616 } 603 617 // if (oper_fx_list[i]->similarity("","")!=GENOPER_NOOPER) l+=" similarity"; 604 logPrintf("GenMan", "Report", 0, "format f%c(%s):%s",605 oper_fx_list[i]->supported_format , oper_fx_list[i]->name.c_str(), l.c_str());618 logPrintf("GenMan", "Report", LOG_INFO, "format f%s (%s):%s", 619 oper_fx_list[i]->supported_format.c_str(), oper_fx_list[i]->name.c_str(), l.c_str()); 606 620 } 607 621 }
Note: See TracChangeset
for help on using the changeset viewer.