Changeset 973 for cpp/frams/genetics/fL
- Timestamp:
- 07/03/20 00:37:13 (4 years ago)
- Location:
- cpp/frams/genetics/fL
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
cpp/frams/genetics/fL/fL_general.cpp
r857 r973 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 … … 76 76 { 77 77 // if position exceeds length then return false 78 if (pos >= src.len ()) return false;78 if (pos >= src.length()) return false; 79 79 int opencount = -1; 80 80 int i = pos; 81 for (; i < src.len (); i++)81 for (; i < src.length(); i++) 82 82 { 83 83 // token cannot contain branching parenthesis … … 169 169 SString temp; 170 170 temp = token.substr(tokpos); 171 temp = temp.substr(0, temp.len () - 1);171 temp = temp.substr(0, temp.length() - 1); 172 172 173 173 // if word has parameters … … 199 199 // if string is empty, then evaluate this with 0 200 200 // if sequence could not be evaluated, then return error 201 if (seq.len () > 0)201 if (seq.length() > 0) 202 202 { 203 203 eval = new MathEvaluation(numparams); … … 217 217 } 218 218 } 219 else if (word->npar == 0 && temp.len () > 0)219 else if (word->npar == 0 && temp.length() > 0) 220 220 { 221 221 SString message = "Too many parameters for word: "; … … 351 351 { 352 352 // if word already exist, then return error 353 if (this->name.len () == 0)353 if (this->name.length() == 0) 354 354 { 355 355 logMessage("fL_Word", "processDefinition", LOG_ERROR, "Axiom name is empty"); … … 456 456 switch (type) 457 457 { 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 458 case fLElementType::TERM: 459 { 460 tab = fL_word_paramtab; 461 obj = new fL_Word(); 462 break; 463 } 464 case fLElementType::INFO: 465 { 466 tab = fL_builder_paramtab; 467 obj = this; 468 break; 469 } 470 case fLElementType::RULE: 471 { 472 tab = fL_rule_paramtab; 473 obj = new fL_Rule(begin, end); 474 break; 475 } 476 default: 477 tab = NULL; 478 obj = NULL; 479 break; 480 480 } 481 481 Param par(tab); … … 573 573 while (genotype.getNextToken(pos, line, '\n')) 574 574 { 575 if (line.len () > 0)575 if (line.length() > 0) 576 576 { 577 577 // words can be defined in the beginning of genotype … … 851 851 { 852 852 SString t = par.getString(q); 853 if (t.len () > 0)853 if (t.length() > 0) 854 854 { 855 855 fL_Word *attrword = NULL; … … 957 957 } 958 958 double partprop = (ppar.getDoubleById(fL_part_names[i]) * alterationcount + 959 959 currval) / (alterationcount + 1.0); 960 960 ppar.setDoubleById(fL_part_names[i], partprop); 961 961 } … … 1097 1097 delete newpart; 1098 1098 logMessage("fL_Builder", "developModel", LOG_ERROR, 1099 1099 "Error parsing word parameter"); 1100 1100 return 1; 1101 1101 } … … 1124 1124 { 1125 1125 logMessage("fL_Builder", "developModel", LOG_ERROR, 1126 1126 "Error parsing word parameter"); 1127 1127 delete newjoint; 1128 1128 return 1; … … 1162 1162 else if (word->name == "C") 1163 1163 { 1164 connsbuffer.push_back({ w, currstate.currneuron});1164 connsbuffer.push_back({ w, currstate.currneuron }); 1165 1165 } 1166 1166 else if (word->name.startsWith("rot")) … … 1182 1182 if (word->name == "rotX") 1183 1183 { 1184 rotmatrix.rotate(Pt3D(rot, 0,0));1184 rotmatrix.rotate(Pt3D(rot, 0, 0)); 1185 1185 } 1186 1186 else if (word->name == "rotY") 1187 1187 { 1188 rotmatrix.rotate(Pt3D(0, rot,0));1188 rotmatrix.rotate(Pt3D(0, rot, 0)); 1189 1189 } 1190 1190 else if (word->name == "rotZ") 1191 1191 { 1192 rotmatrix.rotate(Pt3D(0, 0,rot));1192 rotmatrix.rotate(Pt3D(0, 0, rot)); 1193 1193 } 1194 1194 currstate.direction = rotmatrix.transform(currstate.direction); … … 1212 1212 { 1213 1213 if (connsbuffer[i].second == NULL || 1214 1214 (connsbuffer[i].second->getClass()->getPreferredInputs() != -1 && 1215 1215 connsbuffer[i].second->getInputCount() >= 1216 1216 connsbuffer[i].second->getClass()->getPreferredInputs())) … … 1225 1225 SString attr = par.getStringById(FL_PE_CONN_ATTR); 1226 1226 fL_Word *attractor = NULL; 1227 if (attr.len () > 0)1227 if (attr.length() > 0) 1228 1228 { 1229 1229 createWord(attr, attractor, 0, (*connsbuffer[i].first)->begin, (*connsbuffer[i].first)->end); … … 1236 1236 { 1237 1237 logMessage("fL_Builder", "developModel", LOG_ERROR, 1238 1238 "Error parsing word parameter"); 1239 1239 delete attractor; 1240 1240 return 1; … … 1245 1245 connsbuffer[i].second->addInput(neu, weight); 1246 1246 if (using_mapping) neu->addMapping( 1247 1248 1247 IRange((*connsbuffer[i].first)->begin, 1248 (*connsbuffer[i].first)->end)); 1249 1249 } 1250 1250 else … … 1252 1252 connsbuffer[i].second->addInput(connsbuffer[i].second, weight); 1253 1253 if (using_mapping) neu->addMapping( 1254 1255 1254 IRange((*connsbuffer[i].first)->begin, 1255 (*connsbuffer[i].first)->end)); 1256 1256 } 1257 1257 delete attractor; … … 1286 1286 m->open(using_checkpoints); 1287 1287 bool wordsexceeded = false; 1288 for (; t <= time; t += timestamp)1288 for (; t <= time; t += timestamp) 1289 1289 { 1290 1290 alterTimedProperties(t); // always alter timed properties in the beginning … … 1293 1293 { 1294 1294 iterate(t); 1295 curriter +=1.0;1295 curriter += 1.0; 1296 1296 } 1297 1297 if (using_checkpoints) … … 1362 1362 { 1363 1363 int count = genotype.size(); 1364 for (fL_Rule *rul : rules)1364 for (fL_Rule *rul : rules) 1365 1365 { 1366 1366 count += rul->objsucc.size(); … … 1373 1373 { 1374 1374 for (std::vector<fL_Rule *>::iterator it = rules.begin(); 1375 1375 it != rules.end(); it++) 1376 1376 { 1377 1377 std::vector<fL_Rule *>::iterator it2 = it; … … 1393 1393 { 1394 1394 if ((*it)->condeval->getStringifiedRPN() == 1395 1395 (*it2)->condeval->getStringifiedRPN()) 1396 1396 { 1397 1397 todelete = true; -
cpp/frams/genetics/fL/fL_oper.cpp
r935 r973 285 285 switch (method) 286 286 { 287 case FL_CHG_ITER: 288 { 289 if (rndUint(2) == 0) 290 { 291 creature->time = creature->time + iterchangestep <= ExtValue::getDouble(FL_MAXITER) ? 292 creature->time + iterchangestep : creature->time - iterchangestep; 293 } 294 else 295 { 296 creature->time = creature->time - iterchangestep >= 0 ? 297 creature->time - iterchangestep : creature->time + iterchangestep; 298 } 287 case FL_CHG_ITER: 288 { 289 if (rndUint(2) == 0) 290 { 291 creature->time = creature->time + iterchangestep <= ExtValue::getDouble(FL_MAXITER) ? 292 creature->time + iterchangestep : creature->time - iterchangestep; 293 } 294 else 295 { 296 creature->time = creature->time - iterchangestep >= 0 ? 297 creature->time - iterchangestep : creature->time + iterchangestep; 298 } 299 break; 300 } 301 case FL_CHG_COND: 302 { 303 if (creature->rules.size() > 0) 304 { 305 int ruleid = rndUint(creature->rules.size()); 306 if (!creature->rules[ruleid]->condeval) 307 { 308 creature->rules[ruleid]->condeval = new MathEvaluation(creature->rules[ruleid]->objpred->npar); 309 } 310 creature->rules[ruleid]->condeval->mutateConditional(); 299 311 break; 300 312 } 301 case FL_CHG_COND: 302 { 303 if (creature->rules.size() > 0) 304 { 305 int ruleid = rndUint(creature->rules.size()); 306 if (!creature->rules[ruleid]->condeval) 307 { 308 creature->rules[ruleid]->condeval = new MathEvaluation(creature->rules[ruleid]->objpred->npar); 309 } 310 creature->rules[ruleid]->condeval->mutateConditional(); 311 break; 312 } 313 // if there are no rules - create one 314 } 315 [[fallthrough]]; 316 case FL_ADD_RULE: 317 { 318 std::unordered_map<std::string, fL_Word *>::iterator pred = creature->words.begin(); 319 std::vector<fL_Word *> wordswithnorules; 320 for (; pred != creature->words.end(); pred++) 321 { 322 if (!pred->second->builtin) 323 { 324 bool norules = true; 325 for (fL_Rule * r : creature->rules) 326 { 327 if (pred->second->name == r->objpred->name && 328 pred->second->npar == r->objpred->npar) 329 { 330 norules = false; 331 break; 332 } 333 } 334 if (norules) 335 { 336 wordswithnorules.push_back(pred->second); 337 } 338 } 339 } 340 if (wordswithnorules.size() > 0) 341 { 342 int predid = rndUint(wordswithnorules.size()); 343 fL_Rule *newrule = new fL_Rule(0,0); 344 fL_Word *pred = new fL_Word(); 345 *pred = *wordswithnorules[predid]; 346 newrule->objpred = pred; 347 fL_Word *initdef = randomWordDefinition(creature, roulette(addtypes, FL_ADD_COUNT - 1)); // -1 to avoid branching 348 addWord(&newrule->objsucc, initdef, newrule->objsucc.begin()); 349 creature->rules.push_back(newrule); 350 break; 351 } 352 else if (creature->rules.size() > 0) 353 { 354 int ruleid = rndUint(creature->rules.size()); 355 fL_Rule *newrule = new fL_Rule(0, 0); 356 fL_Word *pred = new fL_Word(); 357 *pred = *creature->rules[ruleid]->objpred; 358 newrule->objpred = pred; 359 if (creature->rules[ruleid]->condeval) 360 { 361 std::string formula = ""; 362 creature->rules[ruleid]->condeval->RPNToInfix(formula); 363 if (formula.find("1.0-(") != 0) 364 { 365 std::string res = "1.0-("; 366 res += formula; 367 res += ")"; 368 newrule->condeval = new MathEvaluation(pred->npar); 369 newrule->condeval->convertString(res); 370 } 371 else 372 { 373 newrule->condeval = new MathEvaluation(pred->npar); 374 newrule->condeval->mutateConditional(); 375 } 313 // if there are no rules - create one 314 } 315 [[fallthrough]]; 316 case FL_ADD_RULE: 317 { 318 std::unordered_map<std::string, fL_Word *>::iterator pred = creature->words.begin(); 319 std::vector<fL_Word *> wordswithnorules; 320 for (; pred != creature->words.end(); pred++) 321 { 322 if (!pred->second->builtin) 323 { 324 bool norules = true; 325 for (fL_Rule * r : creature->rules) 326 { 327 if (pred->second->name == r->objpred->name && 328 pred->second->npar == r->objpred->npar) 329 { 330 norules = false; 331 break; 332 } 333 } 334 if (norules) 335 { 336 wordswithnorules.push_back(pred->second); 337 } 338 } 339 } 340 if (wordswithnorules.size() > 0) 341 { 342 int predid = rndUint(wordswithnorules.size()); 343 fL_Rule *newrule = new fL_Rule(0, 0); 344 fL_Word *pred = new fL_Word(); 345 *pred = *wordswithnorules[predid]; 346 newrule->objpred = pred; 347 fL_Word *initdef = randomWordDefinition(creature, roulette(addtypes, FL_ADD_COUNT - 1)); // -1 to avoid branching 348 addWord(&newrule->objsucc, initdef, newrule->objsucc.begin()); 349 creature->rules.push_back(newrule); 350 break; 351 } 352 else if (creature->rules.size() > 0) 353 { 354 int ruleid = rndUint(creature->rules.size()); 355 fL_Rule *newrule = new fL_Rule(0, 0); 356 fL_Word *pred = new fL_Word(); 357 *pred = *creature->rules[ruleid]->objpred; 358 newrule->objpred = pred; 359 if (creature->rules[ruleid]->condeval) 360 { 361 std::string formula = ""; 362 creature->rules[ruleid]->condeval->RPNToInfix(formula); 363 if (formula.find("1.0-(") != 0) 364 { 365 std::string res = "1.0-("; 366 res += formula; 367 res += ")"; 368 newrule->condeval = new MathEvaluation(pred->npar); 369 newrule->condeval->convertString(res); 376 370 } 377 371 else … … 380 374 newrule->condeval->mutateConditional(); 381 375 } 382 fL_Word *worddef = randomWordDefinition(creature, roulette(addtypes, FL_ADD_COUNT - 1)); 383 addWord(&newrule->objsucc, worddef, newrule->objsucc.begin()); 384 creature->rules.push_back(newrule); 385 break; 386 } 387 // if there are no words, from which rules can be formed, then add one 388 } 389 [[fallthrough]]; 390 case FL_ADD_WDEF: 391 { 392 if (creature->countDefinedWords() <= maxdefinedwords) 393 { 394 int npar = rndUint(ExtValue::getInt(FL_MAXPARAMS, false)); 395 for (int i = 0; i < maxdefinedwords; i++) 396 { 397 std::string name = "w"; 398 name += std::to_string(i); 399 if (creature->words.find(name) == creature->words.end()) 400 { 401 fL_Word *word = new fL_Word(false, 0, 0); 402 word->npar = npar; 403 word->name = name.c_str(); 404 word->processDefinition(creature); 405 break; 406 } 407 } 408 break; 409 } 410 //no break at the end of case - if there is too many words, then 411 // deletion should be performed 412 } 413 [[fallthrough]]; 414 case FL_DEL_WORD: 415 { 376 } 377 else 378 { 379 newrule->condeval = new MathEvaluation(pred->npar); 380 newrule->condeval->mutateConditional(); 381 } 382 fL_Word *worddef = randomWordDefinition(creature, roulette(addtypes, FL_ADD_COUNT - 1)); 383 addWord(&newrule->objsucc, worddef, newrule->objsucc.begin()); 384 creature->rules.push_back(newrule); 385 break; 386 } 387 // if there are no words, from which rules can be formed, then add one 388 } 389 [[fallthrough]]; 390 case FL_ADD_WDEF: 391 { 392 if (creature->countDefinedWords() <= maxdefinedwords) 393 { 394 int npar = rndUint(ExtValue::getInt(FL_MAXPARAMS, false)); 395 for (int i = 0; i < maxdefinedwords; i++) 396 { 397 std::string name = "w"; 398 name += std::to_string(i); 399 if (creature->words.find(name) == creature->words.end()) 400 { 401 fL_Word *word = new fL_Word(false, 0, 0); 402 word->npar = npar; 403 word->name = name.c_str(); 404 word->processDefinition(creature); 405 break; 406 } 407 } 408 break; 409 } 410 //no break at the end of case - if there is too many words, then 411 // deletion should be performed 412 } 413 [[fallthrough]]; 414 case FL_DEL_WORD: 415 { 416 int numpars = 0; 417 int ruleid = 0; 418 std::list<fL_Word *> *list = selectRandomSequence(creature, numpars, ruleid); 419 if (ruleid == -1 && creature->countSticksInSequence(list) == 1) 420 { 421 if (list->size() > 1) 422 { 423 int rndid = rndUint(list->size() - 1); 424 int j = 0; 425 std::list<fL_Word *>::iterator it = list->begin(); 426 if ((*it)->name == "S") 427 { 428 it++; 429 } 430 while (it != list->end() && j < rndid && ((*it)->name == "S")) 431 { 432 if ((*it)->name != "S") 433 { 434 j++; 435 } 436 it++; 437 } 438 if (it != list->end()) 439 { 440 if ((*it)->type == fLElementType::BRANCH) 441 { 442 deleteBranch(list, it); 443 } 444 else 445 { 446 delete (*it); 447 list->erase(it); 448 } 449 break; 450 } 451 // else add word 452 } 453 // else add word 454 } 455 else 456 { 457 int rndid = rndUint(list->size()); 458 std::list<fL_Word *>::iterator it = list->begin(); 459 std::advance(it, rndid); 460 if ((*it)->type == fLElementType::BRANCH) 461 { 462 deleteBranch(list, it); 463 } 464 else 465 { 466 delete (*it); 467 list->erase(it); 468 } 469 if (ruleid > -1 && creature->rules[ruleid]->objsucc.size() == 0) 470 { 471 delete creature->rules[ruleid]; 472 creature->rules.erase(creature->rules.begin() + ruleid); 473 } 474 break; 475 } 476 // if no words available, then add word 477 } 478 [[fallthrough]]; 479 case FL_ADD_WORD: 480 { 481 int numpars = 0; 482 int tmp = 0; 483 std::list<fL_Word *> *list = selectRandomSequence(creature, numpars, tmp); 484 int rndid = rndUint(list->size()); 485 std::list<fL_Word *>::iterator it = list->begin(); 486 std::advance(it, rndid); 487 int meth = roulette(addtypes, FL_ADD_COUNT); 488 if (tmp == -1) 489 { // if sequence is axiom and it does not have non-builtin words 490 bool hasdefined = false; 491 for (std::list<fL_Word *>::iterator elem = list->begin(); elem != list->end(); elem++) 492 { 493 if (!(*elem)->builtin) 494 { 495 hasdefined = true; 496 break; 497 } 498 } 499 if (!hasdefined) 500 { 501 meth = FL_ADD_OTHER; 502 } 503 504 } 505 if (meth != FL_ADD_BRANCH) 506 { 507 fL_Word *worddef = randomWordDefinition(creature, meth); 508 addWord(list, worddef, it); 509 } 510 else 511 { 512 fL_Branch *start = new fL_Branch(fL_Branch::BranchType::OPEN, 0, 0); 513 list->insert(it, start); 514 int rottype = rndUint(2); 515 switch (rottype) 516 { 517 case 0: 518 addWord(list, creature->words["rotY"], it); 519 case 1: 520 addWord(list, creature->words["rotZ"], it); 521 } 522 addWord(list, creature->words["S"], it); 523 fL_Branch *end = new fL_Branch(fL_Branch::BranchType::CLOSE, 0, 0); 524 list->insert(it, end); 525 } 526 break; 527 } 528 case FL_CHG_WORD: 529 { 530 int numpars = 0; 531 int tmp = 0; 532 std::list<fL_Word *> *list = selectRandomSequence(creature, numpars, tmp); 533 int rndid = rndUint(list->size()); 534 std::list<fL_Word *>::iterator selectedword = list->begin(); 535 std::advance(selectedword, rndid); 536 if ((*selectedword)->type == fLElementType::BRANCH) 537 { 538 break; 539 } 540 int chgtype = roulette(chgoperations, FL_CHG_COUNT); 541 if (creature->countSticksInSequence(list) == 1 && tmp == -1) // if sequence is axiom 542 { 543 fL_Word *worddef = randomWordDefinition(creature, roulette(addtypes, FL_ADD_COUNT - 1)); 544 416 545 int numpars = 0; 417 int ruleid = 0;418 std::list<fL_Word *> *list = selectRandomSequence(creature, numpars, ruleid);419 if (ruleid == -1 && creature->countSticksInSequence(list) == 1)420 {421 if (list->size() > 1)422 {423 int rndid = rndUint(list->size() - 1);424 int j = 0;425 std::list<fL_Word *>::iterator it = list->begin();426 if ((*it)->name == "S")427 {428 it++;429 }430 while (it != list->end() && j < rndid && ((*it)->name == "S"))431 {432 if ((*it)->name != "S")433 {434 j++;435 }436 it++;437 }438 if (it != list->end())439 {440 if ((*it)->type == fLElementType::BRANCH)441 {442 deleteBranch(list, it);443 }444 else445 {446 delete (*it);447 list->erase(it);448 }449 break;450 }451 // else add word452 }453 // else add word454 }455 else456 {457 int rndid = rndUint(list->size());458 std::list<fL_Word *>::iterator it = list->begin();459 std::advance(it, rndid);460 if ((*it)->type == fLElementType::BRANCH)461 {462 deleteBranch(list, it);463 }464 else465 {466 delete (*it);467 list->erase(it);468 }469 if (ruleid > -1 && creature->rules[ruleid]->objsucc.size() == 0)470 {471 delete creature->rules[ruleid];472 creature->rules.erase(creature->rules.begin() + ruleid);473 }474 break;475 }476 // if no words available, then add word477 }478 [[fallthrough]];479 case FL_ADD_WORD:480 {481 int numpars = 0;482 int tmp = 0;483 546 std::list<fL_Word *> *list = selectRandomSequence(creature, numpars, tmp); 484 547 int rndid = rndUint(list->size()); 485 548 std::list<fL_Word *>::iterator it = list->begin(); 486 549 std::advance(it, rndid); 487 int meth = roulette(addtypes, FL_ADD_COUNT); 488 if (tmp == -1) 489 { // if sequence is axiom and it does not have non-builtin words 490 bool hasdefined = false; 491 for (std::list<fL_Word *>::iterator elem = list->begin(); elem != list->end(); elem++) 492 { 493 if (!(*elem)->builtin) 494 { 495 hasdefined = true; 496 break; 497 } 498 } 499 if (!hasdefined) 500 { 501 meth = FL_ADD_OTHER; 502 } 503 504 } 505 if (meth != FL_ADD_BRANCH) 506 { 507 fL_Word *worddef = randomWordDefinition(creature, meth); 508 addWord(list, worddef, it); 550 551 addWord(list, worddef, it); 552 553 break; 554 } 555 else if (chgtype == FL_CHG_WORD_NAME) 556 { 557 if ((*selectedword)->builtin) 558 { 559 delete (*selectedword); 560 selectedword = list->erase(selectedword); 561 fL_Word *worddef = randomWordDefinition(creature, roulette(addtypes, FL_ADD_COUNT - 1)); 562 addWord(list, worddef, selectedword); 509 563 } 510 564 else 511 565 { 512 fL_Branch *start = new fL_Branch(fL_Branch::BranchType::OPEN, 0, 0); 513 list->insert(it, start); 514 int rottype = rndUint(2); 515 switch (rottype) 516 { 517 case 0: 518 addWord(list, creature->words["rotY"], it); 519 case 1: 520 addWord(list, creature->words["rotZ"], it); 521 } 522 addWord(list, creature->words["S"], it); 523 fL_Branch *end = new fL_Branch(fL_Branch::BranchType::CLOSE, 0, 0); 524 list->insert(it, end); 525 } 526 break; 527 } 528 case FL_CHG_WORD: 529 { 530 int numpars = 0; 531 int tmp = 0; 532 std::list<fL_Word *> *list = selectRandomSequence(creature, numpars, tmp); 533 int rndid = rndUint(list->size()); 534 std::list<fL_Word *>::iterator selectedword = list->begin(); 535 std::advance(selectedword, rndid); 536 if ((*selectedword)->type == fLElementType::BRANCH) 537 { 538 break; 539 } 540 int chgtype = roulette(chgoperations, FL_CHG_COUNT); 541 if (creature->countSticksInSequence(list) == 1 && tmp == -1) // if sequence is axiom 542 { 543 fL_Word *worddef = randomWordDefinition(creature, roulette(addtypes, FL_ADD_COUNT - 1)); 544 545 int numpars = 0; 546 std::list<fL_Word *> *list = selectRandomSequence(creature, numpars, tmp); 547 int rndid = rndUint(list->size()); 548 std::list<fL_Word *>::iterator it = list->begin(); 549 std::advance(it, rndid); 550 551 addWord(list, worddef, it); 552 553 break; 554 } 555 else if (chgtype == FL_CHG_WORD_NAME) 556 { 557 if ((*selectedword)->builtin) 566 std::vector<fL_Word *> available; 567 for (std::unordered_map<std::string, fL_Word *>::iterator wit = creature->words.begin(); 568 wit != creature->words.end(); wit++) 569 { 570 if ((*selectedword)->npar == wit->second->npar && 571 (*selectedword)->name != wit->second->name && 572 !wit->second->builtin) 573 { 574 available.push_back(wit->second); 575 } 576 } 577 if (available.size() > 0) 578 { 579 int newnameid = rndUint(available.size()); 580 (*selectedword)->name = available[newnameid]->name; 581 } 582 else 558 583 { 559 584 delete (*selectedword); … … 562 587 addWord(list, worddef, selectedword); 563 588 } 564 else 565 { 566 std::vector<fL_Word *> available; 567 for (std::unordered_map<std::string, fL_Word *>::iterator wit = creature->words.begin(); 568 wit != creature->words.end(); wit++) 569 { 570 if ((*selectedword)->npar == wit->second->npar && 571 (*selectedword)->name != wit->second->name && 572 !wit->second->builtin) 589 } 590 } 591 else 592 { 593 if ((*selectedword)->npar > 0) 594 { 595 int randeval = rndUint((*selectedword)->npar); 596 Param par((*selectedword)->tab, (*selectedword)->data); 597 if ((*selectedword)->builtin && (*selectedword)->name == "N" 598 && strcmp(par.id(randeval), FL_PE_NEURO_DET) == 0) 599 { 600 SString res = par.getStringById(FL_PE_NEURO_DET); 601 Geno_fH::mutateNeuronProperties(res); 602 par.setStringById(FL_PE_NEURO_DET, res); 603 } 604 else if ((*selectedword)->builtin && 605 (*selectedword)->name == "C" && 606 strcmp(par.id(randeval), FL_PE_CONN_ATTR) == 0) 607 { 608 SString strattractor = par.getStringById(FL_PE_CONN_ATTR); 609 if (strattractor.length() > 0) 610 { 611 fL_Word *w = NULL; 612 creature->createWord(strattractor, w, numpars, 0, 0); 613 // mutate attractor parameter 614 if (w->npar > 0) 573 615 { 574 available.push_back(wit->second); 616 int rndattr = rndUint(w->npar); 617 if (!w->parevals[rndattr]) 618 { 619 w->parevals[rndattr] = new MathEvaluation(numpars); 620 } 621 w->parevals[rndattr]->mutate(false, false); 575 622 } 576 } 577 if (available.size() > 0) 578 { 579 int newnameid = rndUint(available.size()); 580 (*selectedword)->name = available[newnameid]->name; 623 strattractor = w->stringify(true); 624 par.setStringById(FL_PE_CONN_ATTR, strattractor); 625 delete w; 581 626 } 582 627 else 583 628 { 584 delete (*selectedword); 585 selectedword = list->erase(selectedword); 586 fL_Word *worddef = randomWordDefinition(creature, roulette(addtypes, FL_ADD_COUNT - 1)); 587 addWord(list, worddef, selectedword); 588 } 589 } 590 } 591 else 592 { 593 if ((*selectedword)->npar > 0) 594 { 595 int randeval = rndUint((*selectedword)->npar); 596 Param par((*selectedword)->tab, (*selectedword)->data); 597 if ((*selectedword)->builtin && (*selectedword)->name == "N" 598 && strcmp(par.id(randeval), FL_PE_NEURO_DET) == 0) 599 { 600 SString res = par.getStringById(FL_PE_NEURO_DET); 601 Geno_fH::mutateNeuronProperties(res); 602 par.setStringById(FL_PE_NEURO_DET, res); 603 } 604 else if ((*selectedword)->builtin && 605 (*selectedword)->name == "C" && 606 strcmp(par.id(randeval), FL_PE_CONN_ATTR) == 0) 607 { 608 SString strattractor = par.getStringById(FL_PE_CONN_ATTR); 609 if (strattractor.len() > 0) 629 if (creature->builtincount < (int)creature->words.size()) 610 630 { 611 fL_Word *w = NULL; 612 creature->createWord(strattractor, w, numpars, 0, 0); 613 // mutate attractor parameter 631 fL_Word *wdef = randomWordDefinition(creature, FL_ADD_OTHER); 632 fL_Word *w = new fL_Word(); 633 *w = *wdef; 634 w->data = ParamObject::makeObject(w->tab); 635 Param apar(w->tab); 636 apar.select(w->data); 637 apar.setDefault(); 614 638 if (w->npar > 0) 615 639 { 616 640 int rndattr = rndUint(w->npar); 617 if (!w->parevals[rndattr])641 for (int i = 0; i < w->npar; i++) 618 642 { 619 w->parevals[rndattr] = new MathEvaluation(numpars); 643 if (i == rndattr) 644 { 645 MathEvaluation *ev = new MathEvaluation(numpars); 646 ev->mutate(false, false); 647 w->parevals.push_back(ev); 648 } 649 else 650 { 651 w->parevals.push_back(NULL); 652 } 620 653 } 621 w->parevals[rndattr]->mutate(false, false); 654 622 655 } 623 strattractor = w->stringify( true);656 strattractor = w->stringify(false); 624 657 par.setStringById(FL_PE_CONN_ATTR, strattractor); 625 658 delete w; 626 659 } 627 else 628 { 629 if (creature->builtincount < (int)creature->words.size()) 630 { 631 fL_Word *wdef = randomWordDefinition(creature, FL_ADD_OTHER); 632 fL_Word *w = new fL_Word(); 633 *w = *wdef; 634 w->data = ParamObject::makeObject(w->tab); 635 Param apar(w->tab); 636 apar.select(w->data); 637 apar.setDefault(); 638 if (w->npar > 0) 639 { 640 int rndattr = rndUint(w->npar); 641 for (int i = 0; i < w->npar; i++) 642 { 643 if (i == rndattr) 644 { 645 MathEvaluation *ev = new MathEvaluation(numpars); 646 ev->mutate(false, false); 647 w->parevals.push_back(ev); 648 } 649 else 650 { 651 w->parevals.push_back(NULL); 652 } 653 } 654 655 } 656 strattractor = w->stringify(false); 657 par.setStringById(FL_PE_CONN_ATTR, strattractor); 658 delete w; 659 } 660 } 661 } 662 else 663 { 664 if (!(*selectedword)->parevals[randeval]) 665 { 666 (*selectedword)->parevals[randeval] = new MathEvaluation(numpars); 667 } 668 (*selectedword)->parevals[randeval]->mutate(false, iterchangestep != 1.0); 669 } 670 } 671 } 672 break; 673 } 660 } 661 } 662 else 663 { 664 if (!(*selectedword)->parevals[randeval]) 665 { 666 (*selectedword)->parevals[randeval] = new MathEvaluation(numpars); 667 } 668 (*selectedword)->parevals[randeval]->mutate(false, iterchangestep != 1.0); 669 } 670 } 671 } 672 break; 673 } 674 674 } 675 675 … … 702 702 } 703 703 else if (to->words.find(fromword->name.c_str()) != to->words.end() && 704 704 to->words[fromword->name.c_str()]->npar == fromword->npar) // if there is already same word with same number of parameters 705 705 { 706 706 fL_Word *newword = new fL_Word(); … … 710 710 } 711 711 for (std::unordered_map<std::string, fL_Word *>::iterator it = to->words.begin(); 712 712 it != to->words.end(); it++) 713 713 { // find word with same number of parameters 714 714 if (fromword->npar == it->second->npar && map.find(fromword->name.c_str()) == map.end() && !it->second->builtin) … … 774 774 std::string form; 775 775 if (w->builtin && w->name == "N" 776 776 && strcmp(par.id(i), FL_PE_NEURO_DET) == 0) 777 777 { 778 778 SString res = origpar.getStringById(FL_PE_NEURO_DET); … … 781 781 } 782 782 else if (w->builtin && w->name == "C" 783 783 && strcmp(par.id(i), FL_PE_CONN_ATTR) == 0) 784 784 { 785 785 SString strattractor = origpar.getStringById(FL_PE_CONN_ATTR); 786 if (strattractor.len () > 0)786 if (strattractor.length() > 0) 787 787 { 788 788 fL_Word *tmp = NULL;
Note: See TracChangeset
for help on using the changeset viewer.