Changeset 1227 for cpp/frams/genetics/f4/f4_general.h
- Timestamp:
- 04/27/23 04:04:06 (2 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
cpp/frams/genetics/f4/f4_general.h
r829 r1227 1 1 // This file is a part of Framsticks SDK. http://www.framsticks.com/ 2 // Copyright (C) 1999-20 15Maciej Komosinski and Szymon Ulatowski.2 // Copyright (C) 1999-2023 Maciej Komosinski and Szymon Ulatowski. 3 3 // See LICENSE.txt for details. 4 4 … … 29 29 void rolling_inc(double *v); 30 30 31 class f4_ node; // later31 class f4_Node; // later 32 32 class f4_Cell; // later 33 33 class f4_Cells; // later … … 36 36 /** @name Types of f4_Cell's */ 37 37 //@{ 38 #define T_UNDIFF440 ///<undifferentiated cell39 #define T_STICK441 ///<differentiated to stick, cannot divide40 #define T_NEURON442 ///<differentiated to neuron, can divide38 #define CELL_UNDIFF 40 ///<undifferentiated cell 39 #define CELL_STICK 41 ///<differentiated to stick, cannot divide 40 #define CELL_NEURON 42 ///<differentiated to neuron, can divide 41 41 //@} 42 42 … … 54 54 55 55 56 class f4_Cell Link;56 class f4_CellConn; 57 57 58 58 /** @name Constraints of f4 genotype structures */ 59 59 //@{ 60 #define MAXINPUTS 100 ///<maximum number of neuron inputs61 #define MAX4CELLS 100 ///<maximum number of f4 organism cells60 #define F4_MAX_CELL_INPUTS 10 ///<maximum number of neuron inputs in a developing organism 61 #define F4_MAX_CELLS 100 ///<maximum number of f4 organism cells 62 62 //@} 63 63 64 64 /** 65 * Abstract cell type - the representation of single component in thedevelopmental65 * Abstract cell type - the representation of a single component in the developmental 66 66 * encoding. In the beginning, each f4_Cell is undifferentiated. During the process 67 67 * of development it can divide or differentiate into a stick or a neuron. If it … … 86 86 /** 87 87 * A constructor that takes the pointer to the repetition node and the count of repetitions. 88 * @param a pointer to f4_ node for repetition character88 * @param a pointer to f4_Node for repetition character 89 89 * @param b the number of repetitions 90 90 */ 91 repeat_ptr(f4_ node *a, int b) : node(a), count(b) { };91 repeat_ptr(f4_Node *a, int b) : node(a), count(b) { }; 92 92 93 93 inline void makeNull() { node = NULL; count = -1; }; … … 96 96 97 97 inline void dec() { count--; }; 98 f4_ node *node; ///<pointer to the repetition code98 f4_Node *node; ///<pointer to the repetition code 99 99 int count; ///<repetition counter 100 100 }; … … 130 130 static const int stackSize = 4; ///<max 4 nested levels 131 131 repeat_ptr ptr[stackSize]; ///<array holding pointers to repeat_ptr 132 shortint top; ///<index of the top of the stack132 int top; ///<index of the top of the stack 133 133 }; 134 134 135 135 /** 136 136 * Creates a new f4_Cell object. 137 * @param nn ame name of a cell, can be T_UNDIFF4, T_STICK4 or T_NEURON4137 * @param nnr number of the cell 138 138 * @param ndad pointer to the parent of the created cell 139 139 * @param nangle the amount of commas affecting branch angles 140 140 * @param newP genotype properties of a given cell 141 141 */ 142 f4_Cell(int nn ame, f4_Cell *ndad, int nangle, GeneProps newP);142 f4_Cell(int nnr, f4_Cell *ndad, int nangle, GeneProps newP); 143 143 /** 144 144 * Creates a new f4_Cell object. 145 145 * @param nO pointer to an organism containing the cell 146 * @param nn ame name of the cell, can be T_UNDIFF4, T_STICK4 or T_NEURON4146 * @param nnr number of the cell 147 147 * @param ngeno pointer to the root of the genotype tree 148 * @param ngcur pointer to the f4_ node representing the current cell in the genotype tree148 * @param ngcur pointer to the f4_Node representing the current cell in the genotype tree 149 149 * @param ndad pointer to the parent of the created cell 150 150 * @param nangle the number of commas affecting branch angles 151 151 * @param newP genotype properties of a given cell 152 152 */ 153 f4_Cell(f4_Cells *nO, int nn ame, f4_node *ngeno, f4_node *ngcur, f4_Cell *ndad, int nangle, GeneProps newP);153 f4_Cell(f4_Cells *nO, int nnr, f4_Node *ngeno, f4_Node *ngcur, f4_Cell *ndad, int nangle, GeneProps newP); 154 154 155 155 ~f4_Cell(); … … 172 172 * - the stick modifiers, like rotation, will be applied on neuron cell, 173 173 * - the differentiated cell will be differentiated again, 174 * - the neuron class inside cell connection (i.e. N[G:5]) is not a sensor,175 174 * - the connection between neurons cannot be established, 176 175 * - the neuron class is not valid. … … 178 177 * @return 0 if development was successful, 1 if there was an error in genotype tree 179 178 */ 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. 185 * @param nfrom input neuron cell, or NULL if not given 186 * @param nw weight of connection 187 * @param nt empty string or name of sensor class 188 * @return 0 if link is established, -1 otherwise 189 */ 190 int addlink(f4_Cell *nfrom, double nw, string nt); 179 int oneStep(); 180 181 /** 182 * Adds a connection between this neuron cell and a given neuron cell in nfrom. 183 * @param nfrom input neuron cell 184 * @param nweight weight of connection 185 * @return 0 if connection is established, -1 otherwise 186 */ 187 int addConnection(f4_Cell *nfrom, double nweight); 191 188 192 189 /** … … 195 192 void adjustRec(); 196 193 197 int n ame; ///<name of cell (number)194 int nr; ///<number of cell (seems to be used only in old f1 converter for neuron connections) 198 195 int type; ///<type 199 196 f4_Cell *dadlink; ///<pointer to cell parent 200 197 f4_Cells *org; ///<uplink to organism 201 198 202 f4_ node *genot; ///<genotype tree203 f4_ node *gcur; ///<current genotype execution pointer204 int active; ///<determines whether development is still active199 f4_Node *genot; ///<genotype tree 200 f4_Node *gcur; ///<current genotype execution pointer 201 bool active; ///<determines whether development is still active; even if false, the cell may "yield" - may be halted (but still having its onStep() called) due to neural connections waiting for other cells to potentially develop neurons 205 202 repeat_stack repeat; ///<stack holding repetition nodes and counters 206 203 int recProcessedFlag; ///<used during recursive traverse … … 220 217 int neuro_refno; ///<the number of the neuro object, used in f0 221 218 222 int ctrl; ///<neuron type223 219 double inertia; ///<inertia of neuron 224 220 double force; ///<force of neuron 225 221 double sigmo; ///<sigmoid of neuron 226 f4_Cell Link *links[MAXINPUTS]; ///<array of neuron links227 int nolink; ///<number of links222 f4_CellConn *conns[F4_MAX_CELL_INPUTS]; ///<array of neuron connections 223 int conns_count; ///<number of connections 228 224 NeuroClass *neuclass; ///<pointer to neuron class 229 225 }; 230 226 231 227 /** 232 * Class representing linkbetween neuron cells.233 */ 234 class f4_Cell Link228 * Class representing a connection between neuron cells. 229 */ 230 class f4_CellConn 235 231 { 236 232 public: 237 233 /** 238 234 * Constructor for f4_CellLink class. Parameter nfrom represents input 239 * neuron cell or NULL if connection has defined sensor type inside, like "[G:5]". 240 * The name of sensor class defined inside neuron connection is stored in the nt 241 * parameter. 242 * @param nfrom pointer to input neuron cell or NULL 243 * @param nw weight of connection 244 * @param nt name of neuron class or empty string 245 */ 246 f4_CellLink(f4_Cell *nfrom, double nw, string nt); 247 248 f4_Cell *from; ///<pointer to input neuron cell 249 string t; ///<empty if 'from' cell is given, NeuroClass name otherwise 250 double w; ///<weight of connection 235 * neuron cell. 236 * @param nfrom pointer to input neuron cell 237 * @param nweight weight of connection 238 */ 239 f4_CellConn(f4_Cell *nfrom, double nweight); 240 241 f4_Cell *from; ///<pointer to input neuron cell 242 double weight; ///<weight of connection 251 243 }; 252 244 … … 264 256 * @param nrepair 0 if nothing to repair 265 257 */ 266 f4_Cells(f4_ node *genome, int nrepair);258 f4_Cells(f4_Node *genome, int nrepair); 267 259 268 260 /** … … 291 283 292 284 /** 293 * Performs a single step of organism development. It runs each active cell 294 * in the organism. 295 * @return 0 if all cells are developed, or 1 otherwise 296 */ 297 int onestep(); 285 * Performs a single step of organism development. It runs each active cell in the organism. 286 * @return false if all cells are developed or there is an error, true otherwise 287 */ 288 bool oneStep(); 298 289 299 290 /** … … 302 293 * @return 0 if organism developed successfully, error code if something went wrong 303 294 */ 304 int simulate(); 295 int simulate(); 296 297 /** 298 * Prints the current state of the organism (for debugging purposes). 299 * @param description printout header 300 */ 301 void print_cells(const char* description); 305 302 306 303 /** … … 308 305 * @return error code 309 306 */ 310 int geterror() { return error; };307 int getErrorCode() { return errorcode; }; 311 308 312 309 /** … … 314 311 * @return position of an error 315 312 */ 316 int geterrorpos() { return errorpos; };313 int getErrorPos() { return errorpos; }; 317 314 318 315 /** … … 325 322 * Sets the element of genotype to be repaired by removal. 326 323 * @param nerrpos position of an error in genotype 327 * @param rem the f4_ node to be removed from the genotype tree in order to repair328 */ 329 void setRepairRemove(int nerrpos, f4_ node *rem);324 * @param rem the f4_Node to be removed from the genotype tree in order to repair 325 */ 326 void setRepairRemove(int nerrpos, f4_Node *rem); 330 327 331 328 /** … … 336 333 * @return 0 if repair can be performed, or -1 otherwise because the repair flag wasn't set in the constructor 337 334 */ 338 int setRepairInsert(int nerrpos, f4_node *parent, f4_node *insert);335 int setRepairInsert(int nerrpos, f4_Node *parent, f4_Node *insert); 339 336 340 337 /** … … 343 340 * @param whichchild 1 if first child, 2 otherwise 344 341 */ 345 void repairGeno(f4_ node *geno, int whichchild);342 void repairGeno(f4_Node *geno, int whichchild); 346 343 347 344 // the cells 348 f4_Cell *C[ MAX4CELLS]; ///<Array of all cells of an organism349 int nc;///<Number of cells in an organism345 f4_Cell *C[F4_MAX_CELLS]; ///<Array of all cells of an organism 346 int cell_count; ///<Number of cells in an organism 350 347 351 348 private: 352 349 // for error reporting / genotype fixing 353 350 int repair; 354 int error ;351 int errorcode; 355 352 int errorpos; 356 f4_ node *repair_remove;357 f4_ node *repair_parent;358 f4_ node *repair_insert;353 f4_Node *repair_remove; 354 f4_Node *repair_parent; 355 f4_Node *repair_insert; 359 356 void toF1GenoRec(int curc, SString &out); 360 357 f4_Cell *tmpcel; // needed by toF1Geno 361 f4_ node *f4rootnode; // used by constructor358 f4_Node *f4rootnode; // used by constructor 362 359 }; 363 360 … … 366 363 * A class to organize a f4 genotype in a tree structure. 367 364 */ 368 class f4_ node365 class f4_Node 369 366 { 370 367 public: 371 string name; ///<one-letter 'name', multiple characters for classes372 f4_ node *parent; ///<parent link or NULL373 f4_ node *child; ///<child or NULL374 f4_ node *child2; ///<second child or NULL368 string name; ///<one-letter gene code or multiple characters for neuron classes (then neuclass != NULL) 369 f4_Node *parent; ///<parent link or NULL 370 f4_Node *child; ///<child or NULL 371 f4_Node *child2; ///<second child or NULL 375 372 int pos; ///<original position in the string 376 int i1; ///<internal int parameter1 377 int l1; ///<internal long parameter1 (now also int, since long is not well specified and it is in our scenarios equivalent to int) 378 double f1; ///<internal double parameter1 379 string s1; ///<internal string parameter1 380 381 f4_node(); 373 374 int reps; ///<repetition counter for the '#' gene 375 char prop_symbol; ///<old-style properties (force,intertia,sigmoid) of the N neuron: !=/ 376 bool prop_increase; ///<false=decrease neuron property (force,intertia,sigmoid), true=increase it 377 int conn_from; ///<relative number of the neuron this neuron get an input from 378 double conn_weight; ///<neuron connection weight 379 NeuroClass *neuclass; ///< NULL or not if "name" is a neuroclass name with a proper genotype context ("N:neuroclassname"). New in 2023-04 - to fix fatal flaw with fundamental assumptions: it was impossible to distinguish between single-character neuron names such as S, D, G and single-character modifiers. They were all stored in the "name" field. Before 2018 this was never a problem because the only supported neuroclasses had distinctive symbols such as @|*GTS, and the set of supported modifiers was small and different from neuroclass letters (no G,D,S clash). 380 381 f4_Node(); 382 382 383 383 /** … … 387 387 * @param npos position of node substring in the genotype string 388 388 */ 389 f4_ node(string nname, f4_node *nparent, int npos);389 f4_Node(string nname, f4_Node *nparent, int npos); 390 390 391 391 /** … … 395 395 * @param npos position of node character in the genotype string 396 396 */ 397 f4_node(char nname, f4_node *nparent, int npos); 398 399 ~f4_node(); 397 f4_Node(char nname, f4_Node *nparent, int npos); 398 399 ~f4_Node(); 400 401 /** 402 * Recursively print subtree (for debugging). 403 * @param root starting node 404 * @param indent initial indentation 405 */ 406 static void print_tree(const f4_Node *root, int indent); 400 407 401 408 /** … … 404 411 * @return 0 if the child could be added, -1 otherwise 405 412 */ 406 int addChild(f4_ node *nchi);413 int addChild(f4_Node *nchi); 407 414 408 415 /** … … 411 418 * @return 0 if child could be removed, -1 otherwise 412 419 */ 413 int removeChild(f4_ node *nchi);420 int removeChild(f4_Node *nchi); 414 421 415 422 /** … … 423 430 * @return the number of nodes from this node 424 431 */ 425 int count() ;432 int count() const; 426 433 427 434 /** … … 430 437 * @return pointer to the nth subnode or NULL if not found 431 438 */ 432 f4_ node* ordNode(int n);439 f4_Node* ordNode(int n); 433 440 434 441 /** … … 436 443 * @return random subnode 437 444 */ 438 f4_ node* randomNode();445 f4_Node* randomNode(); 439 446 440 447 /** … … 444 451 * @return a random subnode with a given size or NULL 445 452 */ 446 f4_ node* randomNodeWithSize(int min, int max);453 f4_Node* randomNodeWithSize(int min, int max); 447 454 448 455 /** … … 456 463 * @return pointer to a tree copy 457 464 */ 458 f4_ node* duplicate();465 f4_Node* duplicate(); 459 466 460 467 /** … … 468 475 /** 469 476 * The main function for converting a string of f4 encoding to a tree structure. Prepares 470 * f4_ node root of tree and runs f4_processrec function for it.477 * f4_Node root of tree and runs f4_processrec function for it. 471 478 * @param geno the string representing an f4 genotype 472 * @return a pointer to the f4_ node object representing the f4 tree root473 */ 474 f4_ node* f4_processtree(const char *geno);479 * @return a pointer to the f4_Node object representing the f4 tree root 480 */ 481 f4_Node* f4_processtree(const char *geno); 475 482 476 483 /** 477 484 * Scans a genotype string starting from a given position. This recursive method creates 478 * a tree of f4_ node objects. This method extracts each potentially functional element479 * of a genotype string to a separate f4_ nodes. When the branching character '<' occurs,480 * f4_processrec is deployed for the latest f4_ node element. This method does not485 * a tree of f4_Node objects. This method extracts each potentially functional element 486 * of a genotype string to a separate f4_Nodes. When the branching character '<' occurs, 487 * f4_processrec is deployed for the latest f4_Node element. This method does not 481 488 * analyse the genotype semantically, it only checks if the syntax is proper. The only 482 489 * semantic aspect is neuron class name extraction, where the GenoOperators … … 487 494 * @return 0 if processing was successful, otherwise returns the position of an error in the genotype 488 495 */ 489 int f4_processrec(const char *genot, unsigned pos0, f4_ node *parent);496 int f4_processrec(const char *genot, unsigned pos0, f4_Node *parent); 490 497 491 498 /** … … 502 509 const char *parseConnection(const char *fragm, int &relfrom, double &weight); 503 510 504 /**505 * Parses the notation of the neuron connection with neuron definition - takes506 * the beginning of the connection definition, extracts the name of neuron class507 * that will be the input for the current neuron and the weight of the connection.508 * After successful parsing, returns a pointer to the first character after the connection509 * definition, or NULL if the connection definition was not valid due to the lack of [, :, ]510 * characters, an invalid value of the weight or an invalid neuron class name.511 * @param fragm the beginning of the connection definition, should be the '[' character512 * @param neutype the reference to a string representing the input neuron class name. The name of the class is validated with GenoOperators::parseNeuroClass()513 * @param weight the reference to a double variable in which the weight of the connection will be stored514 * @return the pointer to the first character in string after the connection definition515 */516 const char *parseConnectionWithNeuron(const char *fragm, string &neutype, double &weight);517 518 511 #endif
Note: See TracChangeset
for help on using the changeset viewer.