Changeset 774
- Timestamp:
- 04/11/18 01:16:08 (7 years ago)
- Location:
- cpp/frams/genetics/f4
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
cpp/frams/genetics/f4/conv_f4.cpp
r760 r774 25 25 26 26 27 SString GenoConv_f40::convert(SString &in, MultiMap * 27 SString GenoConv_f40::convert(SString &in, MultiMap *map, bool using_checkpoints) 28 28 { 29 29 int res; 30 f4_Model * 30 f4_Model *model = new f4_Model(); 31 31 res = model->buildFromF4(in, using_checkpoints); 32 32 if (GENOPER_OK != res) return SString(); // oops … … 50 50 51 51 52 SString GenoConv_F41_TestOnly::convert(SString &in, MultiMap * 52 SString GenoConv_F41_TestOnly::convert(SString &in, MultiMap *map, bool using_checkpoints) 53 53 { 54 54 int res; 55 f4_Model * 55 f4_Model *model = new f4_Model(); 56 56 res = model->buildFromF4(in, using_checkpoints); 57 57 if (GENOPER_OK != res) return SString(); // oops … … 126 126 127 127 128 f4_Cell * f4_Model::getStick(f4_Cell *C)128 f4_Cell* f4_Model::getStick(f4_Cell *C) 129 129 { 130 130 if (T_STICK4 == C->type) return C; … … 142 142 143 143 /// updated by Macko to follow new SDK standards (no more neuroitems) 144 int f4_Model::buildModelRec(f4_Cell * 144 int f4_Model::buildModelRec(f4_Cell *C) 145 145 { 146 146 int partidx; … … 192 192 // adjust mass/vol of first endpoint 193 193 jj_p1_refno = C->dadlink->p2_refno; 194 Part * 194 Part *p1 = getPart(jj_p1_refno); 195 195 p1->mass += 1.0; 196 196 // p1->volume += 1.0/C->P.mass; … … 230 230 if (C->type == T_NEURON4) ///<this case was updated by MacKo 231 231 { 232 const char 232 const char* nclass = C->neuclass->name.c_str(); 233 233 int partno, jointno; 234 234 if (C->neuclass->getPreferredLocation() == 0) … … 298 298 if (C->links[j]->from == NULL) 299 299 { 300 const char 301 char 302 NeuroClass * 300 const char* nclass = C->links[j]->t.c_str(); 301 char* temp = (char*)C->links[j]->t.c_str(); 302 NeuroClass *sensortest = GenoOperators::parseNeuroClass(temp); 303 303 //backward compatibility for G*TS 304 304 if (C->links[j]->t == "*" || C->links[j]->t == "S" || C->links[j]->t == "T") -
cpp/frams/genetics/f4/conv_f4.h
r760 r774 32 32 * @return generated f0 genotype 33 33 */ 34 SString convert(SString &in, MultiMap * 34 SString convert(SString &in, MultiMap *map, bool using_checkpoints); 35 35 }; 36 36 … … 57 57 * @return generated approximate f1 genotype 58 58 */ 59 SString convert(SString &in, MultiMap * 59 SString convert(SString &in, MultiMap *map, bool using_checkpoints); 60 60 }; 61 61 … … 82 82 void toF1Geno(SString &out); // output to f1 format, approximation 83 83 private: 84 f4_Cells * 85 int buildModelRec(f4_Cell * 84 f4_Cells *cells; 85 int buildModelRec(f4_Cell *ndad); 86 86 /** 87 87 * Get a cell which is a stick, by traversing dadlinks. 88 88 */ 89 f4_Cell * getStick(f4_Cell *C);89 f4_Cell* getStick(f4_Cell *C); 90 90 int error; 91 91 int errorpos; -
cpp/frams/genetics/f4/f4_general.cpp
r770 r774 17 17 #endif 18 18 19 void rolling_dec(double * 19 void rolling_dec(double *v) 20 20 { 21 21 *v -= 0.7853; // 0.7853981 45 degrees 22 22 } 23 23 24 void rolling_inc(double * 24 void rolling_inc(double *v) 25 25 { 26 26 *v += 0.7853; // 0.7853981 45 degrees 27 27 } 28 28 29 int scanrec(const char 29 int scanrec(const char* s, unsigned int slen, char stopchar) 30 30 { 31 31 unsigned int i = 0; … … 63 63 64 64 f4_Cell::f4_Cell(int nname, 65 f4_Cell * 65 f4_Cell *ndad, int nangle, GeneProps newP) 66 66 { 67 67 name = nname; … … 84 84 //OM = Orient_1; 85 85 ctrl = 0; 86 state = 0;87 86 inertia = 0.8; 88 87 force = 0.04; … … 102 101 if (T_NEURON4 == ndad->type) 103 102 { 104 state = ndad->state;105 103 inertia = ndad->inertia; 106 104 force = ndad->force; … … 114 112 115 113 116 f4_Cell::f4_Cell(f4_Cells * nO, int nname, f4_node * ngeno, f4_node * ngcur, f4_Cell *ndad, int nangle, GeneProps newP)114 f4_Cell::f4_Cell(f4_Cells *nO, int nname, f4_node *ngeno, f4_node *ngcur, f4_Cell *ndad, int nangle, GeneProps newP) 117 115 { 118 116 name = nname; … … 138 136 //OM = Orient_1; 139 137 ctrl = 0; 140 state = 0;141 138 inertia = 0.8; 142 139 force = 0.04; … … 156 153 if (T_NEURON4 == ndad->type) 157 154 { 158 state = ndad->state;159 155 inertia = ndad->inertia; 160 156 force = ndad->force; … … 228 224 GeneProps newP = P; 229 225 newP.propagateAlong(false); 230 f4_Cell * 226 f4_Cell *tmp = new f4_Cell(org, org->nc, genot, gcur->child2, this, commacount, newP); 231 227 tmp->repeat = repeat; 232 228 repeat.clear(); … … 246 242 tmp->neuclass = neuclass; 247 243 // duplicate links 248 f4_CellLink * 244 f4_CellLink *ll; 249 245 for (int i = 0; i < nolink; i++) 250 246 { … … 640 636 641 637 642 int f4_Cell::addlink(f4_Cell * 638 int f4_Cell::addlink(f4_Cell *nfrom, double nw, string nt) 643 639 { 644 640 // if incoming neuron does not produce output, return error … … 725 721 726 722 727 f4_CellLink::f4_CellLink(f4_Cell * 723 f4_CellLink::f4_CellLink(f4_Cell *nfrom, double nw, string nt) 728 724 { 729 725 from = nfrom; … … 734 730 735 731 736 f4_Cells::f4_Cells(f4_node * 732 f4_Cells::f4_Cells(f4_node *genome, int nrepair) 737 733 { 738 734 // create ancestor cell … … 862 858 863 859 864 void f4_Cells::addCell(f4_Cell * 860 void f4_Cells::addCell(f4_Cell *newcell) 865 861 { 866 862 if (nc >= MAX4CELLS - 1) … … 880 876 } 881 877 882 void f4_Cells::setRepairRemove(int nerrpos, f4_node * 878 void f4_Cells::setRepairRemove(int nerrpos, f4_node *rem) 883 879 { 884 880 if (!repair) … … 896 892 } 897 893 898 int f4_Cells::setRepairInsert(int nerrpos, f4_node * parent, f4_node *insert)894 int f4_Cells::setRepairInsert(int nerrpos, f4_node *parent, f4_node *insert) 899 895 { 900 896 if (!repair) … … 915 911 } 916 912 917 void f4_Cells::repairGeno(f4_node * 913 void f4_Cells::repairGeno(f4_node *geno, int whichchild) 918 914 { 919 915 // assemble repaired geno, if the case … … 921 917 if ((NULL == repair_remove) && (NULL == repair_insert)) return; 922 918 // traverse genotype tree, remove / insert node 923 f4_node * 919 f4_node *g2; 924 920 if (1 == whichchild) g2 = geno->child; 925 921 else g2 = geno->child2; … … 928 924 if (g2 == repair_remove) 929 925 { 930 f4_node * 926 f4_node *oldgeno; 931 927 geno->removeChild(g2); 932 928 if (g2->child) … … 973 969 { 974 970 int i, j, ccount; 975 f4_Cell * 976 f4_Cell * 971 f4_Cell *thisti; 972 f4_Cell *thneu; 977 973 char buf[200]; 978 974 … … 1117 1113 } 1118 1114 1119 f4_node::f4_node(char nname, f4_node * 1115 f4_node::f4_node(char nname, f4_node *nparent, int npos) 1120 1116 { 1121 1117 name = nname; … … 1138 1134 } 1139 1135 1140 int f4_node::addChild(f4_node * 1136 int f4_node::addChild(f4_node *nchi) 1141 1137 { 1142 1138 if (NULL == child) … … 1153 1149 } 1154 1150 1155 int f4_node::removeChild(f4_node * 1151 int f4_node::removeChild(f4_node *nchi) 1156 1152 { 1157 1153 if (nchi == child2) … … 1190 1186 } 1191 1187 1192 f4_node 1188 f4_node* f4_node::ordNode(int n) 1193 1189 { 1194 1190 int n1; … … 1210 1206 } 1211 1207 1212 f4_node 1208 f4_node* f4_node::randomNode() 1213 1209 { 1214 1210 int n = count(); … … 1217 1213 } 1218 1214 1219 f4_node 1215 f4_node* f4_node::randomNodeWithSize(int mn, int mx) 1220 1216 { 1221 1217 // try random nodes, and accept if size in range 1222 1218 // limit to maxlim tries 1223 1219 int i, n, maxlim; 1224 f4_node * 1220 f4_node *nod = NULL; 1225 1221 maxlim = count(); 1226 1222 for (i = 0; i < maxlim; i++) … … 1285 1281 { 1286 1282 char *temp = (char*)name.c_str(); 1287 NeuroClass * 1283 NeuroClass *nc = GenoOperators::parseNeuroClass(temp); 1288 1284 if (nc != NULL) 1289 1285 { … … 1326 1322 } 1327 1323 1328 f4_node 1329 { 1330 f4_node * 1324 f4_node* f4_node::duplicate() 1325 { 1326 f4_node *copy; 1331 1327 copy = new f4_node(*this); 1332 1328 copy->parent = NULL; // set later … … 1356 1352 // scan genotype string and build tree 1357 1353 // return >1 for error (errorpos) 1358 int f4_processrec(const char * genot, unsigned pos0, f4_node *parent)1354 int f4_processrec(const char* genot, unsigned pos0, f4_node *parent) 1359 1355 { 1360 1356 int i, j, res, t; … … 1363 1359 double w; 1364 1360 unsigned gpos, oldpos; 1365 f4_node * 1361 f4_node *node1, *par; 1366 1362 unsigned beginindex; 1367 1363 string neutype = ""; … … 1409 1405 // repetition marker, 1 by default 1410 1406 ExtValue val; 1411 const char 1407 const char* end = val.parseNumber(genot + gpos + 1, ExtPType::TInt); 1412 1408 if (end == NULL) i = 1; 1413 1409 else i = val.getInt(); … … 1499 1495 gpos += 2; //skipping "N:" 1500 1496 beginindex = gpos; 1501 char 1497 char* end = (char*)genot + beginindex; 1502 1498 GenoOperators::parseNeuroClass(end); 1503 1499 gpos += end - genot - beginindex; … … 1602 1598 if (*parser != '[') return NULL; 1603 1599 parser++; 1604 char 1600 char* p = (char*)parser; 1605 1601 if (GenoOperators::parseNeuroClass(p) == NULL) return NULL; 1606 1602 neutype = string(parser, (const char *)p); … … 1617 1613 } 1618 1614 1619 f4_node * f4_processtree(const char* geno)1620 { 1621 f4_node * 1615 f4_node* f4_processtree(const char* geno) 1616 { 1617 f4_node *root; 1622 1618 int res; 1623 1619 root = new f4_node(); … … 1628 1624 if (root->child) 1629 1625 { 1630 char 1626 char* buf = (char*)malloc(300); 1631 1627 DB(printf("(%d) ", root->child->count());) 1632 1628 buf[0] = 0; -
cpp/frams/genetics/f4/f4_general.h
r767 r774 21 21 * @param v pointer to the decremented value 22 22 */ 23 void rolling_dec(double * 23 void rolling_dec(double *v); 24 24 25 25 /** … … 27 27 * @param v pointer to the incremented value 28 28 */ 29 void rolling_inc(double * 29 void rolling_inc(double *v); 30 30 31 31 class f4_node; // later … … 51 51 * @return 1 if end of string was reached, or position of found character in sequence 52 52 */ 53 int scanrec(const char 53 int scanrec(const char* s, unsigned int slen, char stopchar); 54 54 55 55 … … 221 221 222 222 int ctrl; ///<neuron type 223 double state; ///<state of neuron224 223 double inertia; ///<inertia of neuron 225 224 double force; ///<force of neuron … … 253 252 254 253 255 // a collection of cells, like Organism, for developmental encoding256 254 /** 257 255 * A class representing a collection of cells. It is equivalent to an organism. … … 295 293 * Performs a single step of organism development. It runs each active cell 296 294 * in the organism. 297 * @return 0 if all cells are developed, 1 otherwise295 * @return 0 if all cells are developed, or 1 otherwise 298 296 */ 299 297 int onestep(); … … 336 334 * @param parent the parent of a new element 337 335 * @param insert the element to be inserted 338 * @return 0 if repair can be performed, -1 otherwise (the repair flag wasn't set in constructor)336 * @return 0 if repair can be performed, or -1 otherwise because the repair flag wasn't set in the constructor 339 337 */ 340 338 int setRepairInsert(int nerrpos, f4_node *parent, f4_node *insert); … … 432 430 * @return pointer to the nth subnode or NULL if not found 433 431 */ 434 f4_node 432 f4_node* ordNode(int n); 435 433 436 434 /** … … 438 436 * @return random subnode 439 437 */ 440 f4_node 438 f4_node* randomNode(); 441 439 442 440 /** … … 446 444 * @return a random subnode with a given size or NULL 447 445 */ 448 f4_node 446 f4_node* randomNodeWithSize(int min, int max); 449 447 450 448 /** … … 458 456 * @return pointer to a tree copy 459 457 */ 460 f4_node 458 f4_node* duplicate(); 461 459 462 460 /** … … 468 466 }; 469 467 470 // convert f4 geno string to tree structure (internal)471 472 468 /** 473 469 * The main function for converting a string of f4 encoding to a tree structure. Prepares … … 476 472 * @return a pointer to the f4_node object representing the f4 tree root 477 473 */ 478 f4_node 474 f4_node* f4_processtree(const char *geno); 479 475 480 476 /** … … 488 484 * @param genot the string holding all the genotype 489 485 * @param pos0 the current position of processing in string 490 * @param current parent of the analysed branch of the genotype486 * @param parent current parent of the analysed branch of the genotype 491 487 * @return 0 if processing was successful, otherwise returns the position of an error in the genotype 492 488 */ … … 504 500 * @return the pointer to the first character in string after connection definition 505 501 */ 506 const char * 502 const char *parseConnection(const char *fragm, int &relfrom, double &weight); 507 503 508 504 /** … … 518 514 * @return the pointer to the first character in string after the connection definition 519 515 */ 520 const char * 516 const char *parseConnectionWithNeuron(const char *fragm, string &neutype, double &weight); 521 517 522 518 #endif -
cpp/frams/genetics/f4/oper_f4.cpp
r773 r774 67 67 } 68 68 69 int Geno_f4::ValidateRec(f4_node * 69 int Geno_f4::ValidateRec(f4_node *geno, int retrycount) const 70 70 { 71 71 // ! the genotype is geno->child (not geno) ! … … 113 113 114 114 115 int Geno_f4::checkValidity(const char 115 int Geno_f4::checkValidity(const char* geno, const char *genoname) 116 116 { 117 117 f4_node root; … … 164 164 { 165 165 // make neuron 166 NeuroClass * 166 NeuroClass *rndclass = GenoOperators::getRandomNeuroClass(); 167 167 if (rndclass == NULL) 168 168 { … … 177 177 for (int i = 0; i < g->count(); i++) 178 178 { 179 f4_node * 180 char 181 NeuroClass * 179 f4_node *gcur = g->ordNode(i); 180 char* temp = (char*)gcur->name.c_str(); 181 NeuroClass *neuclass = GenoOperators::parseNeuroClass(temp); 182 182 if (neuclass != NULL) 183 183 { … … 215 215 { 216 216 // add link 217 f4_node * 218 char 219 NeuroClass * 217 f4_node *par = n1->parent; 218 char* temp = (char*)par->name.c_str(); 219 NeuroClass *neuclass = GenoOperators::parseNeuroClass(temp); 220 220 if (neuclass != NULL) 221 221 { … … 456 456 457 457 // change a [ node 458 void Geno_f4::linkNodeChangeRandom(f4_node * 458 void Geno_f4::linkNodeChangeRandom(f4_node *nn, int neuid, std::vector<NeuroClass*> neulist) const //rewritten by M.K. - should work as before (not tested) 459 459 { 460 460 double probs[3] = { 0.1, 0.3, 0.6 }; … … 495 495 496 496 // make a random : node 497 void Geno_f4::nparNodeMakeRandom(f4_node * 497 void Geno_f4::nparNodeMakeRandom(f4_node *nn) const 498 498 { 499 499 int sign = (int)(2.0f * rnd01); … … 505 505 506 506 // change a repeat # node 507 void Geno_f4::repeatNodeChangeRandom(f4_node * 507 void Geno_f4::repeatNodeChangeRandom(f4_node *nn) const 508 508 { 509 509 int count; … … 526 526 // ! the genotype is g->child (not g) ! 527 527 int i, res; 528 f4_node * 528 f4_node *gcopy = NULL; 529 529 // try this max 20 times: 530 530 // copy, mutate, then validate … … 600 600 601 601 // convert to tree 602 f4_node * 602 f4_node *root; 603 603 root = new f4_node(); 604 604 res = f4_processrec(g, 0, root); … … 654 654 655 655 656 int Geno_f4::CrossOverOne(f4_node * g1, f4_node *g2, float chg) const656 int Geno_f4::CrossOverOne(f4_node *g1, f4_node *g2, float chg) const 657 657 { 658 658 // ! the genotypes are g1->child and g2->child (not g1 g2) ! … … 660 660 int smin, smax; 661 661 float size; 662 f4_node * 662 f4_node *n1, *n2, *n1p, *n2p; 663 663 664 664 // determine desired size -
cpp/frams/genetics/f4/oper_f4.h
r760 r774 63 63 * @return GENOOPER_OK if genotype is valid, GENOPER_REPAIR if genotype can be repaired, GENOPER_OPFAIL if genotype can't be repaired 64 64 */ 65 int ValidateRec(f4_node * 65 int ValidateRec(f4_node *geno, int retrycount) const; 66 66 67 67 /** … … 95 95 * @param neulist list of genotype neuron classes 96 96 */ 97 void linkNodeMakeRandom(f4_node * 97 void linkNodeMakeRandom(f4_node *nn, int neuid, std::vector<NeuroClass*> neulist) const; 98 98 99 99 /** … … 104 104 * @param neulist list of genotype neuron classes 105 105 */ 106 void linkNodeChangeRandom(f4_node * 106 void linkNodeChangeRandom(f4_node *nn, int neuid, std::vector<NeuroClass*> neulist) const; 107 107 108 108 /** … … 124 124 * @return GENOPER_OK if performed successful mutation, GENOPER_FAIL otherwise 125 125 */ 126 int MutateOneValid(f4_node * 126 int MutateOneValid(f4_node *&g, int &method) const; 127 127 128 128 /**
Note: See TracChangeset
for help on using the changeset viewer.