Changeset 955 for cpp/frams/genetics
- Timestamp:
- 06/25/20 00:34:29 (4 years ago)
- Location:
- cpp/frams/genetics
- Files:
-
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
cpp/frams/genetics/f4/f4_general.cpp
r896 r955 1304 1304 unsigned int len; 1305 1305 // build in a SString, with initial size 1306 SString out (strlen(buf) + 2000);1307 out = "";1306 SString out; 1307 out.reserve(int(strlen(buf)) + 2000); 1308 1308 1309 1309 sprint(out); -
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 } -
cpp/frams/genetics/genman.h
r779 r955 52 52 string HTMLize(const char *g); //returns colored genotype in HTML. 53 53 string HTMLizeShort(const char *g); //returns colored genotype (abbreviated if needed) in HTML. 54 Geno getSimplest(c harformat); ///<returns pointer to the simplest genotype of \e format or empty Geno()55 const char *getOpName(c harformat); ///<returns pointer to the active operator set for \e format54 Geno getSimplest(const SString& format); ///<returns pointer to the simplest genotype of \e format or empty Geno() 55 const char *getOpName(const SString& format); ///<returns pointer to the active operator set for \e format 56 56 const vector<GenoOperators*>& GetOperators() const { return oper_fx_list; } ///<returns the list of available genetic operators 57 57 private: 58 58 vector<GenoOperators*> oper_fx_list; 59 59 void saveLink(const string parent1, const string parent2, const string child, const float chg); 60 GenoOperators* getOper_f(c harformat);60 GenoOperators* getOper_f(const SString& format); 61 61 string HTMLize(const char *g, bool shorten); 62 int findOperFormatIndex(const SString& format); 62 63 public: 63 64 vector<GenoLink> GenoLinkList; … … 68 69 int valid_m, valid_xo, validated_m, validated_xo, invalid_m, invalid_xo, failed_m, failed_xo; 69 70 double mutchg, xochg; 70 SListTempl< char> operformats; //the list of supported_format, in the same order as in seloperpar71 SListTempl<SString*> operformats; //the list of supported_format, in the same order as in seloperpar (raw SString* taken from oper_fx_list, not owned by operformats) 71 72 int* seloper; //fields for seloperpar 72 73 Param localpar, localstats; -
cpp/frams/genetics/geno.cpp
r841 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 … … 24 24 } 25 25 26 void Geno::init(const SString& genstring, char genformat, const SString& genname, const SString& comment) 26 const SString Geno::INVALID_FORMAT = "invalid"; 27 const SString Geno::UNKNOWN_FORMAT = ""; 28 29 void Geno::init(const SString& genstring, const SString& genformat, const SString& genname, const SString& comment) 27 30 { 28 31 refcount = 1; … … 35 38 } 36 39 37 void Geno::setGenesAndFormat(const SString& genstring, char genformat) 40 static SString trimAndValidateFormat(const SString& input) //the new requirement for genotype format name: no whitespace 41 { 42 SString format = trim(input); 43 if (format.len() == 0 || strContainsOneOf(format.c_str(), " \r\n\t")) 44 return Geno::INVALID_FORMAT; 45 return format; 46 } 47 48 void Geno::setGenesAndFormat(const SString& genstring, const SString& in_genformat) 38 49 { 39 50 mapinshift = 0; 40 51 mapoutshift = 0; 41 52 SString gencopy(genstring); 42 if (genformat == -1) 53 SString genformat = in_genformat; 54 if (genformat == UNKNOWN_FORMAT) 43 55 { // unknown format 44 genformat = '1';56 genformat = "1"; 45 57 if (genstring.charAt(0) == '/') 46 58 { … … 49 61 { 50 62 case '/': 51 genformat = genstring.charAt(2);52 63 if ((end = genstring.indexOf('\n')) >= 0) 53 64 { 65 genformat = trimAndValidateFormat(genstring.substr(2, end - 2)); 54 66 mapinshift = end + 1; 55 67 gencopy = genstring.substr(end + 1); 56 68 if ((end > 0) && (genstring[end - 1] == '\r')) end--; 57 69 error_end = end; 58 if (end != 3) genformat = INVALID_FORMAT;59 70 } 60 71 else 61 72 { 62 if (genstring.len() != 3) genformat = INVALID_FORMAT;63 gencopy = 0;73 genformat = trimAndValidateFormat(genstring.substr(2)); 74 gencopy = ""; 64 75 mapinshift = genstring.len(); 65 76 } 66 77 break; 67 78 case '*': 68 genformat = genstring.charAt(2);69 79 if ((end = genstring.indexOf("*/")) >= 0) 70 80 { 81 genformat = trimAndValidateFormat(genstring.substr(2, end - 2)); 71 82 error_end = end + 2; 72 if (end != 3) genformat = INVALID_FORMAT;73 83 gencopy = genstring.substr(end + 2); 74 84 mapinshift = end + 2; … … 76 86 else 77 87 { 78 if (genstring.len() != 5) genformat = INVALID_FORMAT;79 gencopy = 0;88 genformat = trimAndValidateFormat(genstring.substr(2)); 89 gencopy = ""; 80 90 mapinshift = genstring.len(); 81 91 } 82 92 break; 83 93 } 84 if (!isalnum(genformat)) genformat = INVALID_FORMAT;85 94 if (genformat == INVALID_FORMAT) 86 95 { 87 96 SString cut; 88 if (error_end <0) error_end = genstring.len();97 if (error_end < 0) error_end = genstring.len(); 89 98 static const int MAX_ERROR = 20; 90 if (error_end >MAX_ERROR)99 if (error_end > MAX_ERROR) 91 100 cut = genstring.substr(0, MAX_ERROR) + "..."; 92 101 else … … 113 122 } 114 123 124 Geno::Geno(const char *genstring, const char* genformat, const char *genname, const char *comment) 125 { 126 init(SString(genstring), SString(genformat), SString(genname), SString(comment)); 127 } 128 115 129 Geno::Geno(const char *genstring, char genformat, const char *genname, const char *comment) 116 130 { 117 init(SString(genstring), genformat, SString(genname), SString(comment)); 118 } 119 120 Geno::Geno(const SString& genstring, char genformat, const SString& genname, const SString& comment) 131 SString genformat_string; 132 if (genformat > 0) 133 genformat_string = SString(&genformat, 1); 134 init(genstring, genformat_string, genname, comment); 135 } 136 137 Geno::Geno(const SString& genstring, const SString& genformat, const SString& genname, const SString& comment) 121 138 { 122 139 init(genstring, genformat, genname, comment); … … 148 165 Geno::Geno(const SString& src) 149 166 { 150 init(src, -1, SString::empty(), SString::empty());167 init(src, UNKNOWN_FORMAT, SString::empty(), SString::empty()); 151 168 } 152 169 … … 161 178 { 162 179 freeF0(); 163 init(g, -1, SString::empty(), SString::empty());180 init(g, UNKNOWN_FORMAT, SString::empty(), SString::empty()); 164 181 } 165 182 … … 177 194 { 178 195 SString out; 179 if (format != '1')196 if (format != "1") 180 197 { 181 198 if (multiline) … … 183 200 else 184 201 out += "/*"; 185 if (format == 0) 186 out += "invalid"; 187 else 188 out += format; 202 out += format; 189 203 if (multiline) 190 204 out += "\n"; … … 213 227 SString Geno::getGenes(void) const { return gen; } 214 228 SString Geno::getName(void) const { return name; } 215 charGeno::getFormat(void) const { return format; }229 SString Geno::getFormat(void) const { return format; } 216 230 SString Geno::getComment(void) const { return txt; } 217 231 218 232 int ModelGenoValidator::testGenoValidity(Geno& g) 219 233 { 220 if (g.getFormat() == '0')234 if (g.getFormat() == "0") 221 235 { 222 236 Model mod(g); … … 226 240 { 227 241 bool converter_missing; 228 Geno f0geno = g.getConverted( '0', NULL, false, &converter_missing);242 Geno f0geno = g.getConverted("0", NULL, false, &converter_missing); 229 243 if (converter_missing) 230 244 return -1;//no result … … 243 257 #ifdef WARN_VALIDATION_INCONSISTENCY 244 258 vector<int> results; 245 int first_result =-1;259 int first_result = -1; 246 260 FOREACH(GenoValidator*, v, (*vals)) 247 261 { 248 int r =v->testGenoValidity(*this);249 if (first_result <0) first_result=r;262 int r = v->testGenoValidity(*this); 263 if (first_result < 0) first_result = r; 250 264 results.push_back(r); 251 265 } 252 int N =vals->size();253 for (int i=1;i<N;i++)254 if (results[i] !=results[0])266 int N = vals->size(); 267 for (int i = 1; i < N; i++) 268 if (results[i] != results[0]) 255 269 { 256 SString txt="Inconsistent validation results";257 for(int i=0;i<N;i++)258 txt+=SString::sprintf(" %d",results[i]);259 txt+=" for genotype '";260 txt+=getGene();261 txt+="'";262 logPrintf("Geno","validate",LOG_WARN,txt.c_str());263 break;270 SString txt = "Inconsistent validation results"; 271 for (int i = 0; i < N; i++) 272 txt += SString::sprintf(" %d", results[i]); 273 txt += " for genotype '"; 274 txt += getGene(); 275 txt += "'"; 276 logPrintf("Geno", "validate", LOG_WARN, txt.c_str()); 277 break; 264 278 } 265 isvalid =first_result;266 if (isvalid >=0)279 isvalid = first_result; 280 if (isvalid >= 0) 267 281 return; 268 282 #else … … 273 287 } 274 288 isvalid = 0; 275 logPrintf("Geno", "validate", LOG_WARN, "Wrong configuration? No genotype validators defined for genetic format 'f% c'.", format);289 logPrintf("Geno", "validate", LOG_WARN, "Wrong configuration? No genotype validators defined for genetic format 'f%s'.", format.c_str()); 276 290 } 277 291 … … 295 309 } 296 310 297 Geno Geno::getConverted( charotherformat, MultiMap *m, bool using_checkpoints, bool *converter_missing)311 Geno Geno::getConverted(SString otherformat, MultiMap *m, bool using_checkpoints, bool *converter_missing) 298 312 { 299 313 if (otherformat == getFormat()) { if (converter_missing) *converter_missing = false; return *this; } … … 302 316 if (converters) 303 317 { 304 if ((otherformat == '0') && (!m) && (!using_checkpoints))318 if ((otherformat == "0") && (!m) && (!using_checkpoints)) 305 319 { 306 320 if (!f0gen) … … 317 331 #endif 318 332 if (converter_missing) *converter_missing = true; 319 return (otherformat == getFormat()) ? *this : Geno( 0, 0, 0, "GenConvManager not available");333 return (otherformat == getFormat()) ? *this : Geno("", "", "", "GenConvManager not available"); 320 334 } 321 335 -
cpp/frams/genetics/geno.h
r732 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 … … 34 34 SString gen; 35 35 SString name; 36 charformat;36 SString format; 37 37 SString txt; 38 38 int isvalid; ///< <0 -> unknown >=0 -> value for "isValid" … … 44 44 int multiline; 45 45 46 void init(const SString& genstring, c hargenformat, const SString& genname, const SString& comment);46 void init(const SString& genstring, const SString& genformat, const SString& genname, const SString& comment); 47 47 void validate(void); 48 48 … … 55 55 56 56 public: 57 static const char INVALID_FORMAT = '!'; 57 static const SString INVALID_FORMAT; 58 static const SString UNKNOWN_FORMAT; 58 59 typedef SListTempl<GenoValidator*> Validators; 59 60 … … 62 63 /// @param genformat genotype format 63 64 /// @param comment information about genotype (for genetic operators and "history") 64 Geno(const char *genstring = 0, char genformat = -1, const char *genname = 0, const char *comment = 0); 65 Geno(const char *genstring = 0, const char* genformat = 0, const char *genname = 0, const char *comment = 0); 66 67 Geno(const char *genstring, char genformat, const char *genname = 0, const char *comment = 0); //old style Geno, accepts char genoformat 65 68 66 69 /// create a genotype object from primitives … … 69 72 /// @param name genotype name, new name will generated if needed 70 73 /// @param comment information about genotype (for genetic operators and "history") 71 Geno(const SString& genstring, c hargenformat, const SString& genname, const SString& comment);74 Geno(const SString& genstring, const SString& genformat, const SString& genname, const SString& comment); 72 75 73 76 /// create object from full string, containing optional format and comment information … … 89 92 void setString(const SString& genewithcomments); 90 93 91 /** @param genformat= -1-> detect genotype format from genstring comment (like the constructor does), else specify the valid format in genformat and pure genes in genstring. */92 void setGenesAndFormat(const SString& genstring, c har genformat = -1);94 /** @param genformat="" -> detect genotype format from genstring comment (like the constructor does), else specify the valid format in genformat and pure genes in genstring. */ 95 void setGenesAndFormat(const SString& genstring, const SString& genformat = UNKNOWN_FORMAT); 93 96 /** g must be pure genes, without format. For the standard behavior use setGenesAndFormat() */ 94 97 void setGenesAssumingSameFormat(const SString& g); … … 97 100 SString getName(void) const; 98 101 void setName(const SString&); 99 chargetFormat(void) const;102 SString getFormat(void) const; 100 103 101 104 SString getComment(void) const; … … 107 110 /// make converted version of the genotype. 108 111 /// @param converter_missing optional output parameter (ignored when NULL). Receives true if the conversion fails because of the lack of appropriate converter(s) (the returned Geno is always invalid in this case). Receives false if the genotype was converted by a converter or a converter chain (the returned Geno can be valid or invalid, depending on the converter's decision). 109 Geno getConverted( charotherformat, MultiMap *m = 0, bool using_checkpoints = false, bool *converter_missing = NULL);112 Geno getConverted(SString otherformat, MultiMap *m = 0, bool using_checkpoints = false, bool *converter_missing = NULL); 110 113 111 114 /// @return -1 = before first char in the string -
cpp/frams/genetics/genoconv.cpp
r841 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 … … 57 57 pe->flags = 0; 58 58 std::string descr = "f"; 59 descr += gk->in_format ;59 descr += gk->in_format.c_str(); 60 60 descr += " --> f"; 61 descr += gk->out_format ;61 descr += gk->out_format.c_str(); 62 62 descr += " : "; 63 63 descr += gk->name; … … 101 101 } 102 102 103 GenoConverter *GenoConvManager::findConverters(SListTempl<GenoConverter*>* result, c har in, charout, int enabled, char* name)103 GenoConverter *GenoConvManager::findConverters(SListTempl<GenoConverter*>* result, const SString& in, const SString& out, int enabled, char* name) 104 104 { 105 105 GenoConverter *gk, *retval = 0; … … 107 107 for (; gk = (GenoConverter*)converters(i); i++) 108 108 { 109 if ((in != -1) && (in != gk->in_format)) continue;110 if ((out != -1) && (out != gk->out_format)) continue;109 if ((in != Geno::UNKNOWN_FORMAT) && (in != gk->in_format)) continue; 110 if ((out != Geno::UNKNOWN_FORMAT) && (out != gk->out_format)) continue; 111 111 if ((enabled != -1) && (enabled != gk->enabled)) continue; 112 112 if ((name) && (strcmp(name, gk->name))) continue; … … 123 123 /// (can be NULL if you don't need this information) 124 124 125 char *GenoConvManager::getPath(char in, char out, char*path, int maxlen, int *mapavailable)125 GenoConverter **GenoConvManager::getPath(const SString& in, const SString& out, GenoConverter **path, int maxlen, int *mapavailable) 126 126 { 127 127 if (!maxlen) return 0; … … 132 132 if ((gk->enabled) && (gk->in_format == in)) 133 133 { 134 *path = (char)i;134 *path = gk; 135 135 if (gk->out_format == out) 136 136 { … … 142 142 { 143 143 int mapavail; 144 char*ret = getPath(gk->out_format, out, path + 1, maxlen - 1, &mapavail);144 GenoConverter **ret = getPath(gk->out_format, out, path + 1, maxlen - 1, &mapavail); 145 145 if (ret) 146 146 { … … 155 155 } 156 156 157 char *GenoConvManager::getFormatPath(char in, char out, char *path, int maxlen, int *mapavailable) 158 { 159 char *ret = getPath(in, out, path, maxlen, mapavailable); 160 if (ret) 161 { 162 for (char*t = path; t <= ret; t++) 163 *t = ((GenoConverter*)converters(*t))->out_format; 164 } 165 return ret; 166 } 167 168 Geno GenoConvManager::convert(Geno &in, char format, MultiMap *map, bool using_checkpoints, bool *converter_missing) 157 Geno GenoConvManager::convert(Geno &in, SString format, MultiMap *map, bool using_checkpoints, bool *converter_missing) 169 158 { 170 159 if (in.getFormat() == format) { if (converter_missing) *converter_missing = false; return in; } 171 charpath[10];160 GenoConverter *path[10]; 172 161 int dep; 173 char*ret;174 if (in.isInvalid()) { if (converter_missing) *converter_missing = false; return Geno("", 0, "", "invalid genotype cannot be converted"); }162 GenoConverter **ret; 163 if (in.isInvalid()) { if (converter_missing) *converter_missing = false; return Geno("", Geno::INVALID_FORMAT, "", "invalid genotype cannot be converted"); } 175 164 int mapavail; 176 165 for (dep = 1; dep < (int)sizeof(path); dep++) //iterative deepening 177 166 if (ret = getPath(in.getFormat(), format, path, dep, &mapavail)) break; 178 if (!ret) { if (converter_missing) *converter_missing = true; return Geno("", 0, "", "converter not found"); }167 if (!ret) { if (converter_missing) *converter_missing = true; return Geno("", Geno::INVALID_FORMAT, "", "converter not found"); } 179 168 if (converter_missing) *converter_missing = false; 180 169 if (!map) mapavail = 0; 181 char*t = path;170 GenoConverter **t = path; 182 171 SString tmp; 183 172 tmp = in.getGenes(); … … 186 175 for (; t <= ret; t++) 187 176 { 188 GenoConverter *gk = (GenoConverter*)converters(*t);177 GenoConverter *gk = *t; 189 178 tmp = gk->convert(tmp, mapavail ? &tmpmap : 0, using_checkpoints); 190 179 if (!tmp.len()) 191 180 { 192 string t = ssprintf("f% c->f%c conversion failed (%s)", gk->in_format, gk->out_format, gk->name);193 return Geno( 0, 0, 0, t.c_str());181 string t = ssprintf("f%s->f%s conversion failed (%s)", gk->in_format.c_str(), gk->out_format.c_str(), gk->name); 182 return Geno("", Geno::INVALID_FORMAT, "", t.c_str()); 194 183 } 195 184 if (mapavail) -
cpp/frams/genetics/genoconv.h
r783 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 … … 43 43 public: 44 44 const char *name; //< converter name (short) 45 char in_format, //< input format, eg. '1'46 out_format; //< output format, eg. '0'45 SString in_format, //< input format, eg. "1" 46 out_format; //< output format, eg. "0" 47 47 paInt enabled; //< don't touch this! (used by configuration module) 48 48 paInt mapsupport; //< set to 1 if your converter supports genotype mapping … … 56 56 virtual ~GenoConverter() {} 57 57 /// Don't forget to set public fields in your constructor 58 GenoConverter() :name(""), in_format( -1), out_format('0'), enabled(1), mapsupport(0) {}58 GenoConverter() :name(""), in_format(Geno::UNKNOWN_FORMAT), out_format("0"), enabled(1), mapsupport(0) {} 59 59 }; 60 60 … … 77 77 /// make a genotype in other format. genotype will be invalid 78 78 /// if GenoConvManager cannot convert it. 79 Geno convert(Geno &in, charformat, MultiMap *map = 0, bool using_checkpoints = false, bool *converter_missing = NULL);79 Geno convert(Geno &in, SString format, MultiMap *map = 0, bool using_checkpoints = false, bool *converter_missing = NULL); 80 80 /// register GenoConverter, the added object will be automatically deleted when GenoConvManager is destructed (call removeConverter() if this is not desirable) 81 81 void addConverter(GenoConverter *conv); … … 83 83 void removeConverter(GenoConverter *conv); 84 84 85 char *getPath(char in, char out, char *path, int maxlen, int *mapavailable = 0); 86 char *getFormatPath(char in, char out, char *path, int maxlen, int *mapavailable = 0); 85 GenoConverter **getPath(const SString& in, const SString& out, GenoConverter **path, int maxlen, int *mapavailable = 0); 87 86 /// returns the list of converters meeting the specified criteria 88 87 /// pass result=0 if you only need one result (by return value) 89 88 /// default criteria values mean "don't care", pass anything else to narrow your search 90 GenoConverter *findConverters(SListTempl<GenoConverter*>* result = 0, c har in = -1, char out = -1, int enabled = -1, char* name = 0);89 GenoConverter *findConverters(SListTempl<GenoConverter*>* result = 0, const SString& in = Geno::UNKNOWN_FORMAT, const SString& out = Geno::UNKNOWN_FORMAT, int enabled = -1, char* name = 0); 91 90 }; 92 91 -
cpp/frams/genetics/genooperators.cpp
r935 r955 76 76 } 77 77 78 int GenoOperators::selectRandom Property(Neuro *n)78 int GenoOperators::selectRandomNeuProperty(Neuro *n) 79 79 { 80 80 int neuext = n->extraProperties().getPropCount(), -
cpp/frams/genetics/genooperators.h
r935 r955 81 81 public: 82 82 Param par; 83 char supported_format; ///<genotype format which is supported by this class ('6' for GenoOper_f6, 'F' for GenoOper_fF, etc.). Must be initialized in constructor84 string name; ///< name of thisset of genetic operators83 SString supported_format; ///<genotype format which is supported by this class ("6" for GenoOper_f6, "Latent" for GenoOper_fLatent, etc.). Must be initialized in constructor. 84 string name; ///<short human-friendly name of this genetic representation/set of genetic operators 85 85 const char **mutation_method_names; ///<array of names for mutation methods. If initialized (by new const char*[]), must have entries for each method index returned by mutate(geno,chg,METHOD). If initialized, it is automatically freed by this destructor. 86 GenoOperators() : par(empty_paramtab) { supported_format = 'x'; name = "Default"; mutation_method_names = NULL; setDefaults(); }86 GenoOperators() : par(empty_paramtab) { supported_format = "x"; name = "Default"; mutation_method_names = NULL; setDefaults(); } 87 87 88 88 /**Used to perform initializations of Param parameters that are not handled by the Param itself … … 182 182 static int roulette(const double *probtab, const int count); ///<returns random index according to probabilities in the \e probtab table or -1 if all probs are zero. \e count is the number of elements in \e probtab. 183 183 static bool getMinMaxDef(ParamInterface *p, int propindex, double &mn, double &mx, double &def); ///<perhaps a more useful (higher-level) way to obtain min/max/def info for integer and double properties. Returns true if min/max/def was really available (otherwise it is just invented). 184 static int selectRandom Property(Neuro* n); ///<selects random property (either 0-based extraproperty of Neuro or 100-based property of its NeuroClass). -1 if Neuro has no properties.184 static int selectRandomNeuProperty(Neuro* n); ///<selects random property (either 0-based extraproperty of Neuro or 100-based property of its NeuroClass). -1 if Neuro has no properties. 185 185 static double mutateNeuProperty(double current, Neuro *n, int propindex); ///<returns value \e current mutated for the property \e propindex of NeuroClass \e nc or for extraproperty (\e propindex - 100) of Neuro. Neuro is used as read-only. Give \e propindex == -1 to mutate connection weight (\e nc is then ignored). 186 186 static bool mutatePropertyNaive(ParamInterface &p, int propindex); ///<creep-mutate selected property. Returns true when success. mutateProperty() should be used instead of this function.
Note: See TracChangeset
for help on using the changeset viewer.