Changeset 767 for cpp/frams/genetics/f4/f4_general.h
- Timestamp:
- 03/29/18 22:52:36 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
cpp/frams/genetics/f4/f4_general.h
r760 r767 97 97 inline void dec() { count--; }; 98 98 f4_node *node; ///<pointer to the repetition code 99 charcount; ///<repetition counter99 int count; ///<repetition counter 100 100 }; 101 101 … … 140 140 * @param newP genotype properties of a given cell 141 141 */ 142 f4_Cell(int nname, 143 f4_Cell * ndad, int nangle, GeneProps newP); 142 f4_Cell(int nname, f4_Cell *ndad, int nangle, GeneProps newP); 144 143 /** 145 144 * Creates a new f4_Cell object. … … 152 151 * @param newP genotype properties of a given cell 153 152 */ 154 f4_Cell(f4_Cells *nO, int nname, f4_node *ngeno, f4_node *ngcur, 155 f4_Cell *ndad, int nangle, GeneProps newP); 153 f4_Cell(f4_Cells *nO, int nname, f4_node *ngeno, f4_node *ngcur, f4_Cell *ndad, int nangle, GeneProps newP); 156 154 157 155 ~f4_Cell(); 158 156 159 157 /** 160 * Performs one step of cell development. This method requirespointer to161 * f4_Cells object in org attribute. If the current node in genotype tree162 * represents branching, thenthe cell divides into two cells, unless the163 * cell was already differentiated into stick cell. Otherwisethe current164 * differentiation or modification is performed on cell. If current node is165 * creating a connection between two neuron nodes ,and the input node is not166 * yet developed, the n the simulation of current cell halts andwaits until167 * the input node will be developed. The onestep method is ran on eachcell168 * at least once and if one cell requires another to develop, thenonestep158 * Performs a single step of cell development. This method requires a pointer to 159 * the f4_Cells object in org attribute. If the current node in genotype tree 160 * is the branching character '<', the cell divides into two cells, unless the 161 * cell was already differentiated into the stick cell. Otherwise, the current 162 * differentiation or modification is performed on the cell. If current node is 163 * creating a connection between two neuron nodes and the input node is not 164 * yet developed, the simulation of the development of the current cell waits until 165 * the input node is created. The onestep method is deployed for every cell 166 * at least once. If one cell requires another one to develop, onestep 169 167 * should be deployed again on this cell. This method, unlike genotype tree 170 168 * creation, checks semantics. This means that this function will fail if: 171 * - the stick cell will have divide node,172 * - the undifferentiated cell will have '>' node (end of cell development),173 * - the stack of repetition marker '#' will be overflowed,169 * - the cell differentiated as a stick will have branching node '<', 170 * - the undifferentiated cell will have termination node '>' (end of cell development without differentiation), 171 * - the stack of repetition marker '#' will exceed maximum allowed value of repetition, 174 172 * - the stick modifiers, like rotation, will be applied on neuron cell, 175 173 * - the differentiated cell will be differentiated again, 176 * - the neuron class inside cell connection definitionis not a sensor,177 * - the connection between neurons c ouldnot be established,174 * - the neuron class inside cell connection (i.e. N[G:5]) is not a sensor, 175 * - the connection between neurons cannot be established, 178 176 * - the neuron class is not valid. 179 177 * 180 178 * @return 0 if development was successful, 1 if there was an error in genotype tree 181 179 */ 182 int onestep(); // execute one simulation step (till a division)183 184 /** 185 * Add link between this neuron cell and a given neuron cell. Ifnfrom object186 * is not given, than neuron type in nt holds thesensor type.180 int onestep(); 181 182 /** 183 * Adds a link between this neuron cell and a given neuron cell in nfrom. If the nfrom object 184 * is not given, neuron type in nt should be a sensor type. 187 185 * @param nfrom input neuron cell, or NULL if not given 188 186 * @param nw weight of connection … … 190 188 * @return 0 if link is established, -1 otherwise 191 189 */ 192 int addlink(f4_Cell * 190 int addlink(f4_Cell *nfrom, double nw, string nt); 193 191 194 192 /** … … 197 195 void adjustRec(); 198 196 199 int name; ///<name of cell (number)197 int name; ///<name of cell (number) 200 198 int type; ///<type 201 f4_Cell * dadlink;///<pointer to cell parent202 f4_Cells *org; ///<uplink to organism203 204 f4_node * genot;///<genotype tree205 f4_node * gcur;///<current genotype execution pointer206 int active; ///<whether development is still active199 f4_Cell *dadlink; ///<pointer to cell parent 200 f4_Cells *org; ///<uplink to organism 201 202 f4_node *genot; ///<genotype tree 203 f4_node *gcur; ///<current genotype execution pointer 204 int active; ///<determines whether development is still active 207 205 repeat_stack repeat; ///<stack holding repetition nodes and counters 208 int recProcessedFlag;///<used during recursive traverse206 int recProcessedFlag; ///<used during recursive traverse 209 207 MultiRange genoRange; ///<remember the genotype codes affecting this cell so far 210 208 211 GeneProps P;///<properties209 GeneProps P; ///<properties 212 210 int anglepos; ///<number of position within dad's children (,) 213 211 int childcount; ///<number of children … … 218 216 219 217 double mz; ///<freedom in z 220 int p2_refno; ///< number oflast end part object, used in f0221 int joint_refno; ///< number of the joint object, used in f0222 int neuro_refno; ///< number of the neuro object, used in f0218 int p2_refno; ///<the number of the last end part object, used in f0 219 int joint_refno; ///<the number of the joint object, used in f0 220 int neuro_refno; ///<the number of the neuro object, used in f0 223 221 224 222 int ctrl; ///<neuron type … … 227 225 double force; ///<force of neuron 228 226 double sigmo; ///<sigmoid of neuron 229 f4_CellLink *links[MAXINPUTS]; ///<array of neuron links227 f4_CellLink *links[MAXINPUTS]; ///<array of neuron links 230 228 int nolink; ///<number of links 231 NeuroClass * neuclass = NULL;///<pointer to neuron class229 NeuroClass *neuclass; ///<pointer to neuron class 232 230 }; 233 231 … … 239 237 public: 240 238 /** 241 * Constructor for f4_CellLink class. Parameter nfrom can representinput242 * neuron cell or be NULL, if connection has neuron cell definition inside.243 * The inside definition must be hold in nt parameter and a neuron class244 * must represent sensor.239 * Constructor for f4_CellLink class. Parameter nfrom represents input 240 * neuron cell or NULL if connection has defined sensor type inside, like "[G:5]". 241 * The name of sensor class defined inside neuron connection is stored in the nt 242 * parameter. 245 243 * @param nfrom pointer to input neuron cell or NULL 246 244 * @param nw weight of connection 247 245 * @param nt name of neuron class or empty string 248 246 */ 249 f4_CellLink(f4_Cell * 250 251 f4_Cell * 252 string t; ///<empty if fromcell is given, NeuroClass name otherwise253 double w;///<weight of connection247 f4_CellLink(f4_Cell *nfrom, double nw, string nt); 248 249 f4_Cell *from; ///<pointer to input neuron cell 250 string t; ///<empty if 'from' cell is given, NeuroClass name otherwise 251 double w; ///<weight of connection 254 252 }; 255 253 … … 257 255 // a collection of cells, like Organism, for developmental encoding 258 256 /** 259 * Class representing a collection of cells. It is equivalent oforganism.257 * A class representing a collection of cells. It is equivalent to an organism. 260 258 */ 261 259 class f4_Cells … … 268 266 * @param nrepair 0 if nothing to repair 269 267 */ 270 f4_Cells(f4_node * 268 f4_Cells(f4_node *genome, int nrepair); 271 269 272 270 /** … … 283 281 284 282 /** 285 * Adds new cell to organism.283 * Adds a new cell to organism. 286 284 * @param newcell cell to be added 287 285 */ 288 void addCell(f4_Cell * newcell); 289 290 /** 291 * Creates approximate genotype in f1 encoding and stores it in a given 292 * parameter. 293 * @param out the string in which approximate f1 genotype will be stored 286 void addCell(f4_Cell *newcell); 287 288 /** 289 * Creates an approximate genotype in the f1 encoding and stores it in a given parameter. 290 * @param out the string in which the approximate f1 genotype will be stored 294 291 */ 295 292 void toF1Geno(SString &out); 296 293 297 294 /** 298 * Performs single step of organism development. It runs each active cell299 * in organism.300 * @return 0 if all cells are developed, or1 otherwise295 * Performs a single step of organism development. It runs each active cell 296 * in the organism. 297 * @return 0 if all cells are developed, 1 otherwise 301 298 */ 302 299 int onestep(); 303 300 304 301 /** 305 * Performs full development of organism and returns error code if something302 * Performs the full development of organism and returns error code if something 306 303 * went wrong. 307 304 * @return 0 if organism developed successfully, error code if something went wrong … … 310 307 311 308 /** 312 * Returns error code of last simulation.309 * Returns error code of the last simulation. 313 310 * @return error code 314 311 */ … … 316 313 317 314 /** 318 * Returns position of error in genotype.319 * @return position of error315 * Returns position of an error in genotype. 316 * @return position of an error 320 317 */ 321 318 int geterrorpos() { return errorpos; }; 322 319 323 320 /** 324 * Sets error code GENOPER_OPFAIL for simulation on a given position.325 * @param nerrpos position of error321 * Sets error code GENOPER_OPFAIL for a simulation on a given position. 322 * @param nerrpos position of an error 326 323 */ 327 324 void setError(int nerrpos); … … 329 326 /** 330 327 * Sets the element of genotype to be repaired by removal. 331 * @param nerrpos position of error in genotype332 * @param rem the f4_node to be removed from genotype tree in order to repair333 */ 334 void setRepairRemove(int nerrpos, f4_node * 335 336 /** 337 * Sets repairing of genotype by inserting new node tocurrent genotype.338 * @param nerrpos position of error in genotype339 * @param parent the parent of new element328 * @param nerrpos position of an error in genotype 329 * @param rem the f4_node to be removed from the genotype tree in order to repair 330 */ 331 void setRepairRemove(int nerrpos, f4_node *rem); 332 333 /** 334 * Sets repairing of a genotype by inserting a new node to the current genotype. 335 * @param nerrpos position of an error in genotype 336 * @param parent the parent of a new element 340 337 * @param insert the element to be inserted 341 338 * @return 0 if repair can be performed, -1 otherwise (the repair flag wasn't set in constructor) 342 339 */ 343 int setRepairInsert(int nerrpos, f4_node * parent, f4_node *insert);344 345 /** 346 * Repairs genotype according to setRepairRemove or setRepairInsert method.347 * @param geno pointer to genotype tree340 int setRepairInsert(int nerrpos, f4_node *parent, f4_node *insert); 341 342 /** 343 * Repairs the genotype according to setRepairRemove or setRepairInsert methods. 344 * @param geno pointer to the genotype tree 348 345 * @param whichchild 1 if first child, 2 otherwise 349 346 */ 350 void repairGeno(f4_node * 347 void repairGeno(f4_node *geno, int whichchild); 351 348 352 349 // the cells 353 f4_Cell * C[MAX4CELLS]; ///<Array of all cells oforganism354 int nc; ///<Number of cells in organism350 f4_Cell *C[MAX4CELLS]; ///<Array of all cells of an organism 351 int nc; ///<Number of cells in an organism 355 352 356 353 private: … … 359 356 int error; 360 357 int errorpos; 361 f4_node * 362 f4_node * 363 f4_node * 358 f4_node *repair_remove; 359 f4_node *repair_parent; 360 f4_node *repair_insert; 364 361 void toF1GenoRec(int curc, SString &out); 365 f4_Cell * 366 f4_node * 362 f4_Cell *tmpcel; // needed by toF1Geno 363 f4_node *f4rootnode; // used by constructor 367 364 }; 368 365 369 366 370 367 /** 371 * Class to organize a f4 genotype in a tree structure.368 * A class to organize a f4 genotype in a tree structure. 372 369 */ 373 370 class f4_node … … 375 372 public: 376 373 string name; ///<one-letter 'name', multiple characters for classes 377 f4_node *parent; ///<parent link ,or NULL378 f4_node *child; ///<child ,or NULL379 f4_node *child2; ///<second child ,or NULL380 int pos; ///<original position instring381 int 382 int 383 double 374 f4_node *parent; ///<parent link or NULL 375 f4_node *child; ///<child or NULL 376 f4_node *child2; ///<second child or NULL 377 int pos; ///<original position in the string 378 int i1; ///<internal int parameter1 379 int l1; ///<internal long parameter1 380 double f1; ///<internal double parameter1 384 381 string s1; ///<internal string parameter1 385 382 386 /**387 * Default constructor.388 */389 383 f4_node(); 390 384 … … 392 386 * Multiple-character name constructor. 393 387 * @param nname string from genotype representing node 394 * @param nparent pointer to parent of node395 * @param npos position of node substring in genotype string396 */ 397 f4_node(string nname, f4_node * 398 399 /** 400 * One-character name constructor.388 * @param nparent pointer to parent of the node 389 * @param npos position of node substring in the genotype string 390 */ 391 f4_node(string nname, f4_node *nparent, int npos); 392 393 /** 394 * Single-character name constructor. 401 395 * @param nname character from genotype representing node 402 * @param nparent pointer to parent of node 403 * @param npos position of node character in genotype string 404 */ 405 f4_node(char nname, f4_node * nparent, int npos); 406 407 /** 408 * Desctructor of object. 409 */ 396 * @param nparent pointer to parent of the node 397 * @param npos position of node character in the genotype string 398 */ 399 f4_node(char nname, f4_node *nparent, int npos); 400 410 401 ~f4_node(); 411 402 412 403 /** 413 * Method for adding child to anode.414 * @param nchi child to be added tonode415 * @return 0 if child could be added, -1 otherwise416 */ 417 int addChild(f4_node *nchi);418 419 /** 420 * Method for removing child fromnode.421 * @param nchi child to be removed fromnode404 * Adds the child to the node. 405 * @param nchi the child to be added to the node 406 * @return 0 if the child could be added, -1 otherwise 407 */ 408 int addChild(f4_node *nchi); 409 410 /** 411 * Removes the child from the node. 412 * @param nchi the child to be removed from the node 422 413 * @return 0 if child could be removed, -1 otherwise 423 414 */ 424 int removeChild(f4_node *nchi);425 426 /** 427 * Returns number of children.415 int removeChild(f4_node *nchi); 416 417 /** 418 * Returns the number of children. 428 419 * @return 0, 1 or 2 429 420 */ 430 int 431 432 /** 433 * Returns number of nodes coming from this node in a recursive way.434 * @return number of nodes from this node435 */ 436 int 421 int childCount(); 422 423 /** 424 * Returns the number of nodes coming from this node in a recursive way. 425 * @return the number of nodes from this node 426 */ 427 int count(); 437 428 438 429 /** 439 430 * Returns the nth subnode (0-) 440 * @param n index of child to be found441 * @return pointer to nth subnode or NULL if not found431 * @param n index of the child to be found 432 * @return pointer to the nth subnode or NULL if not found 442 433 */ 443 434 f4_node * ordNode(int n); … … 450 441 451 442 /** 452 * Returns a random subnode with given size.443 * Returns a random subnode with a given size. 453 444 * @param min minimum size 454 445 * @param max maximum size 455 * @return a random subnode with given size or NULL446 * @return a random subnode with a given size or NULL 456 447 */ 457 448 f4_node * randomNodeWithSize(int min, int max); 458 449 459 450 /** 460 * Prints recursively t ree from a given node.461 * @param buf variable storingprinting result462 */ 463 void sprintAdj(char *& 464 465 /** 466 * Recursively copies genotype tree from this node.451 * Prints recursively the tree from a given node. 452 * @param buf variable to store printing result 453 */ 454 void sprintAdj(char *&buf); 455 456 /** 457 * Recursively copies the genotype tree from this node. 467 458 * @return pointer to a tree copy 468 459 */ … … 474 465 void destroy(); 475 466 private: 476 void sprint(SString & 467 void sprint(SString &out); // print recursively 477 468 }; 478 469 … … 480 471 481 472 /** 482 * Main function to perform conversion of f4 geno totree structure. Prepares473 * The main function for converting a string of f4 encoding to a tree structure. Prepares 483 474 * f4_node root of tree and runs f4_processrec function for it. 484 * @param geno string representingf4 genotype485 * @return pointer to f4_node object representingf4 tree root486 */ 487 f4_node * f4_processtree(const char * 488 489 /** 490 * Scans genotype string from a given position. This recursive method creates491 * tree of f4_node objects. The method extracteach potentially functional element492 * of genotype string to separate f4_nodes. Whenbranching character '<' occurs,493 * then f4_processrec is ran forlatest f4_node element. This method does not494 * analyse genotype semantically, it checks only ifsyntax is proper. The only495 * semantic aspect is neuron class name extraction, in whichthe GenoOperators496 * class is used to parse possible neuron class in genotype.497 * @param genot the string holding all genotype475 * @param geno the string representing an f4 genotype 476 * @return a pointer to the f4_node object representing the f4 tree root 477 */ 478 f4_node * f4_processtree(const char *geno); 479 480 /** 481 * Scans a genotype string starting from a given position. This recursive method creates 482 * a tree of f4_node objects. This method extracts each potentially functional element 483 * of a genotype string to a separate f4_nodes. When the branching character '<' occurs, 484 * f4_processrec is deployed for the latest f4_node element. This method does not 485 * analyse the genotype semantically, it only checks if the syntax is proper. The only 486 * semantic aspect is neuron class name extraction, where the GenoOperators 487 * class is used to parse the potential neuron class name. 488 * @param genot the string holding all the genotype 498 489 * @param pos0 the current position of processing in string 499 * @param current parent of analysed branch ofgenotype500 * @return 0 if processing was successful or position of error ingenotype501 */ 502 int f4_processrec(const char * genot, unsigned pos0, f4_node *parent);503 504 /** 505 * Function parses notation of neuron connection - it takes beginning ofconnection506 * definition, extracts relative position of input neurons and weight ofconnection.507 * After successful parsing it returns pointer to first character afterconnection508 * definition, or NULL if connection definition was not valid -lack of [, :, ]509 * characters or wrongvalue of relfrom or weight.510 * @param fragm the beginning of connection definition, it should be '[' character511 * @param relfrom the reference to int variable, in which relative position ofinput neuron will be stored512 * @param weight the reference to double variable, in which weight ofconnection will be stored513 * @return the pointer to first character in string after connection definition514 */ 515 const char * parseConnection(const char * fragm, int&relfrom, double &weight);516 517 /** 518 * Function parses notation of neuron connection with neuron definition - it519 * t akes beginning ofconnection definition, extracts the name of neuron class520 * that will be the input for current neuron and weight ofconnection.521 * After successful parsing it returns pointer to first character afterconnection522 * definition, or NULL if connection definition was not valid -lack of [, :, ]523 * characters, wrong value of weight orinvalid neuron class name.524 * @param fragm the beginning of connection definition, it should be '[' character525 * @param neutype the reference to string representing input neuron class name. The name of class is validated with GenoOperators::parseNeuroClass526 * @param weight the reference to double variable, in which weight ofconnection will be stored527 * @return the pointer to first character in string afterconnection definition528 */ 529 const char * parseConnectionWithNeuron(const char * fragm, string&neutype, double &weight);490 * @param current parent of the analysed branch of the genotype 491 * @return 0 if processing was successful, otherwise returns the position of an error in the genotype 492 */ 493 int f4_processrec(const char *genot, unsigned pos0, f4_node *parent); 494 495 /** 496 * Parses notation of the neuron connection - takes the beginning of the connection 497 * definition, extracts the relative position of input neurons and the weight of the connection. 498 * After successful parsing, returns the pointer to the first character after the connection 499 * definition, or NULL if the connection definition was not valid due to the lack of [, :, ] 500 * characters or an invalid value of relfrom or weight. 501 * @param fragm the beginning of connection definition, should be the '[' character 502 * @param relfrom the reference to an int variable in which the relative position of the input neuron will be stored 503 * @param weight the reference to a double variable in which the weight of the connection will be stored 504 * @return the pointer to the first character in string after connection definition 505 */ 506 const char * parseConnection(const char *fragm, int &relfrom, double &weight); 507 508 /** 509 * Parses the notation of the neuron connection with neuron definition - takes 510 * the beginning of the connection definition, extracts the name of neuron class 511 * that will be the input for the current neuron and the weight of the connection. 512 * After successful parsing, returns a pointer to the first character after the connection 513 * definition, or NULL if the connection definition was not valid due to the lack of [, :, ] 514 * characters, an invalid value of the weight or an invalid neuron class name. 515 * @param fragm the beginning of the connection definition, should be the '[' character 516 * @param neutype the reference to a string representing the input neuron class name. The name of the class is validated with GenoOperators::parseNeuroClass() 517 * @param weight the reference to a double variable in which the weight of the connection will be stored 518 * @return the pointer to the first character in string after the connection definition 519 */ 520 const char * parseConnectionWithNeuron(const char *fragm, string &neutype, double &weight); 530 521 531 522 #endif
Note: See TracChangeset
for help on using the changeset viewer.