Ignore:
Timestamp:
03/29/18 22:52:36 (6 years ago)
Author:
Maciej Komosinski
Message:

More strict parsing of the repetition counter; bug fixes; improved docs [refs #62]

File:
1 edited

Legend:

Unmodified
Added
Removed
  • cpp/frams/genetics/f4/f4_general.h

    r760 r767  
    9797                inline void dec() { count--; };
    9898                f4_node    *node; ///<pointer to the repetition code
    99                 char       count; ///<repetition counter
     99                int       count; ///<repetition counter
    100100        };
    101101
     
    140140         * @param newP genotype properties of a given cell
    141141         */
    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);
    144143        /**
    145144         * Creates a new f4_Cell object.
     
    152151         * @param newP genotype properties of a given cell
    153152         */
    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);
    156154
    157155        ~f4_Cell();
    158156
    159157        /**
    160          * Performs one step of cell development. This method requires pointer to
    161          * f4_Cells object in org attribute. If the current node in genotype tree
    162          * represents branching, then the cell divides into two cells, unless the
    163          * cell was already differentiated into stick cell. Otherwise the current
    164          * differentiation or modification is performed on cell. If current node is
    165          * creating a connection between two neuron nodes, and the input node is not
    166          * yet developed, then the simulation of current cell halts and waits until
    167          * the input node will be developed. The onestep method is ran on each cell
    168          * at least once and if one cell requires another to develop, then onestep
     158         * 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
    169167         * should be deployed again on this cell. This method, unlike genotype tree
    170168         * 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,
    174172         *  - the stick modifiers, like rotation, will be applied on neuron cell,
    175173         *  - the differentiated cell will be differentiated again,
    176          *  - the neuron class inside cell connection definition is not a sensor,
    177          *  - the connection between neurons could not 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,
    178176         *  - the neuron class is not valid.
    179177         *
    180178         * @return 0 if development was successful, 1 if there was an error in genotype tree
    181179         */
    182         int onestep();  // execute one simulation step (till a division)
    183 
    184         /**
    185          * Add link between this neuron cell and a given neuron cell. If nfrom object
    186          * is not given, than neuron type in nt holds the sensor 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.
    187185         * @param nfrom input neuron cell, or NULL if not given
    188186         * @param nw weight of connection
     
    190188         * @return 0 if link is established, -1 otherwise
    191189         */
    192         int   addlink(f4_Cell * nfrom, double nw, string nt);
     190        int   addlink(f4_Cell *nfrom, double nw, string nt);
    193191
    194192        /**
     
    197195        void  adjustRec();
    198196
    199         int        name;               ///<name of cell(number)
     197        int        name;               ///<name of cell (number)
    200198        int        type;               ///<type
    201         f4_Cell *  dadlink;            ///<pointer to cell parent
    202         f4_Cells * org;                ///<uplink to organism
    203 
    204         f4_node *  genot;                  ///<genotype tree
    205         f4_node *  gcur;               ///<current genotype execution pointer
    206         int        active;             ///<whether development is still active
     199        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
    207205        repeat_stack repeat;           ///<stack holding repetition nodes and counters
    208         int        recProcessedFlag;   ///<used during recursive traverse
     206        int recProcessedFlag;          ///<used during recursive traverse
    209207        MultiRange genoRange;          ///<remember the genotype codes affecting this cell so far
    210208
    211         GeneProps     P;               ///<properties
     209        GeneProps    P;                ///<properties
    212210        int          anglepos;         ///<number of position within dad's children (,)
    213211        int          childcount;       ///<number of children
     
    218216
    219217        double       mz;               ///<freedom in z
    220         int          p2_refno;         ///<number of last end part object, used in f0
    221         int          joint_refno;      ///<number of the joint object, used in f0
    222         int          neuro_refno;      ///<number of the neuro object, used in f0
     218        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
    223221
    224222        int          ctrl;             ///<neuron type
     
    227225        double       force;            ///<force of neuron
    228226        double       sigmo;            ///<sigmoid of neuron
    229         f4_CellLink* links[MAXINPUTS]; ///<array of neuron links
     227        f4_CellLink *links[MAXINPUTS]; ///<array of neuron links
    230228        int          nolink;           ///<number of links
    231         NeuroClass * neuclass = NULL;  ///<pointer to neuron class
     229        NeuroClass *neuclass;          ///<pointer to neuron class
    232230};
    233231
     
    239237public:
    240238        /**
    241          * Constructor for f4_CellLink class. Parameter nfrom can represent input
    242          * 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 class
    244          * 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.
    245243         * @param nfrom pointer to input neuron cell or NULL
    246244         * @param nw weight of connection
    247245         * @param nt name of neuron class or empty string
    248246         */
    249         f4_CellLink(f4_Cell * nfrom, double nw, string nt);
    250 
    251         f4_Cell *    from; ///<pointer to input neuron cell
    252         string  t;    ///<empty if from cell is given, NeuroClass name otherwise
    253         double       w;    ///<weight of connection
     247        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
    254252};
    255253
     
    257255// a collection of cells, like Organism, for developmental encoding
    258256/**
    259  * Class representing a collection of cells. It is equivalent of organism.
     257 * A class representing a collection of cells. It is equivalent to an organism.
    260258 */
    261259class f4_Cells
     
    268266         * @param nrepair 0 if nothing to repair
    269267         */
    270         f4_Cells(f4_node * genome, int nrepair);
     268        f4_Cells(f4_node *genome, int nrepair);
    271269
    272270        /**
     
    283281
    284282        /**
    285          * Adds new cell to organism.
     283         * Adds a new cell to organism.
    286284         * @param newcell cell to be added
    287285         */
    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
    294291         */
    295292        void toF1Geno(SString &out);
    296293
    297294        /**
    298          * Performs single step of organism development. It runs each active cell
    299          * in organism.
    300          * @return 0 if all cells are developed, or 1 otherwise
     295         * 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
    301298         */
    302299        int  onestep();
    303300
    304301        /**
    305          * Performs full development of organism and returns error code if something
     302         * Performs the full development of organism and returns error code if something
    306303         * went wrong.
    307304         * @return 0 if organism developed successfully, error code if something went wrong
     
    310307
    311308        /**
    312          * Returns error code of last simulation.
     309         * Returns error code of the last simulation.
    313310         * @return error code
    314311         */
     
    316313
    317314        /**
    318          * Returns position of error in genotype.
    319          * @return position of error
     315         * Returns position of an error in genotype.
     316         * @return position of an error
    320317         */
    321318        int  geterrorpos() { return errorpos; };
    322319
    323320        /**
    324          * Sets error code GENOPER_OPFAIL for simulation on a given position.
    325          * @param nerrpos position of error
     321         * Sets error code GENOPER_OPFAIL for a simulation on a given position.
     322         * @param nerrpos position of an error
    326323         */
    327324        void setError(int nerrpos);
     
    329326        /**
    330327         * Sets the element of genotype to be repaired by removal.
    331          * @param nerrpos position of error in genotype
    332          * @param rem the f4_node to be removed from genotype tree in order to repair
    333          */
    334         void setRepairRemove(int nerrpos, f4_node * rem);
    335 
    336         /**
    337          * Sets repairing of genotype by inserting new node to current genotype.
    338          * @param nerrpos position of error in genotype
    339          * @param parent the parent of new element
     328         * @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
    340337         * @param insert the element to be inserted
    341338         * @return 0 if repair can be performed, -1 otherwise (the repair flag wasn't set in constructor)
    342339         */
    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 tree
     340        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
    348345         * @param whichchild 1 if first child, 2 otherwise
    349346         */
    350         void repairGeno(f4_node * geno, int whichchild);
     347        void repairGeno(f4_node *geno, int whichchild);
    351348
    352349        // the cells
    353         f4_Cell * C[MAX4CELLS]; ///<Array of all cells of organism
    354         int       nc;           ///<Number of cells in organism
     350        f4_Cell *C[MAX4CELLS];  ///<Array of all cells of an organism
     351        int       nc;           ///<Number of cells in an organism
    355352
    356353private:
     
    359356        int error;
    360357        int errorpos;
    361         f4_node * repair_remove;
    362         f4_node * repair_parent;
    363         f4_node * repair_insert;
     358        f4_node *repair_remove;
     359        f4_node *repair_parent;
     360        f4_node *repair_insert;
    364361        void toF1GenoRec(int curc, SString &out);
    365         f4_Cell * tmpcel;               // needed by toF1Geno
    366         f4_node * f4rootnode;          // used by constructor
     362        f4_Cell *tmpcel;                // needed by toF1Geno
     363        f4_node *f4rootnode;          // used by constructor
    367364};
    368365
    369366
    370367/**
    371  * Class to organize a f4 genotype in a tree structure.
     368 * A class to organize a f4 genotype in a tree structure.
    372369 */
    373370class f4_node
     
    375372public:
    376373        string name; ///<one-letter 'name', multiple characters for classes
    377         f4_node *parent; ///<parent link, or NULL
    378         f4_node *child; ///<child, or NULL
    379         f4_node *child2; ///<second child, or NULL
    380         int         pos; ///<original position in string
    381         int         i1; ///<internal int  parameter1
    382         int         l1; ///<internal long parameter1
    383         double      f1; ///<internal double parameter1
     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
    384381        string s1; ///<internal string parameter1
    385382
    386         /**
    387          * Default constructor.
    388          */
    389383        f4_node();
    390384
     
    392386         * Multiple-character name constructor.
    393387         * @param nname string from genotype representing node
    394          * @param nparent pointer to parent of node
    395          * @param npos position of node substring in genotype string
    396          */
    397         f4_node(string nname, f4_node * nparent, int npos);
    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.
    401395         * @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
    410401        ~f4_node();
    411402
    412403        /**
    413          * Method for adding child to a node.
    414          * @param nchi child to be added to node
    415          * @return 0 if child could be added, -1 otherwise
    416          */
    417         int       addChild(f4_node * nchi);
    418 
    419         /**
    420          * Method for removing child from node.
    421          * @param nchi child to be removed from node
     404         * 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
    422413         * @return 0 if child could be removed, -1 otherwise
    423414         */
    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.
    428419         * @return 0, 1 or 2
    429420         */
    430         int       childCount();
    431 
    432         /**
    433          * Returns number of nodes coming from this node in a recursive way.
    434          * @return number of nodes from this node
    435          */
    436         int       count();
     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();
    437428
    438429        /**
    439430         * Returns the nth subnode (0-)
    440          * @param n index of child to be found
    441          * @return pointer to nth subnode or NULL if not found
     431         * @param n index of the child to be found
     432         * @return pointer to the nth subnode or NULL if not found
    442433         */
    443434        f4_node * ordNode(int n);
     
    450441
    451442        /**
    452          * Returns a random subnode with given size.
     443         * Returns a random subnode with a given size.
    453444         * @param min minimum size
    454445         * @param max maximum size
    455          * @return a random subnode with given size or NULL
     446         * @return a random subnode with a given size or NULL
    456447         */
    457448        f4_node * randomNodeWithSize(int min, int max);
    458449
    459450        /**
    460          * Prints recursively tree from a given node.
    461          * @param buf variable storing printing result
    462          */
    463         void      sprintAdj(char *& buf);
    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.
    467458         * @return pointer to a tree copy
    468459         */
     
    474465        void      destroy();
    475466private:
    476         void     sprint(SString & out); // print recursively
     467        void     sprint(SString &out);  // print recursively
    477468};
    478469
     
    480471
    481472/**
    482  * Main function to perform conversion of f4 geno to tree structure. Prepares
     473 * The main function for converting a string of f4 encoding to a tree structure. Prepares
    483474 * f4_node root of tree and runs f4_processrec function for it.
    484  * @param geno string representing f4 genotype
    485  * @return pointer to f4_node object representing f4 tree root
    486  */
    487 f4_node * f4_processtree(const char * geno);
    488 
    489 /**
    490  * Scans genotype string from a given position. This recursive method creates
    491  * tree of f4_node objects. The method extract each potentially functional element
    492  * of genotype string to separate f4_nodes. When branching character '<' occurs,
    493  * then f4_processrec is ran for latest f4_node element. This method does not
    494  * analyse genotype semantically, it checks only if syntax is proper. The only
    495  * semantic aspect is neuron class name extraction, in which the GenoOperators
    496  * class is used to parse possible neuron class in genotype.
    497  * @param genot the string holding all genotype
     475 * @param geno the string representing an f4 genotype
     476 * @return a pointer to the f4_node object representing the f4 tree root
     477 */
     478f4_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
    498489 * @param pos0 the current position of processing in string
    499  * @param current parent of analysed branch of genotype
    500  * @return 0 if processing was successful or position of error in genotype
    501  */
    502 int f4_processrec(const char * genot, unsigned pos0, f4_node * parent);
    503 
    504 /**
    505  * Function parses notation of neuron connection - it takes beginning of connection
    506  * definition, extracts relative position of input neurons and weight of connection.
    507  * After successful parsing it returns pointer to first character after connection
    508  * definition, or NULL if connection definition was not valid - lack of [, :, ]
    509  * characters or wrong value of relfrom or weight.
    510  * @param fragm the beginning of connection definition, it should be '[' character
    511  * @param relfrom the reference to int variable, in which relative position of input neuron will be stored
    512  * @param weight the reference to double variable, in which weight of connection will be stored
    513  * @return the pointer to first character in string after connection definition
    514  */
    515 const char * parseConnection(const char * fragm, int& relfrom, double &weight);
    516 
    517 /**
    518  * Function parses notation of neuron connection with neuron definition - it
    519  * takes beginning of connection definition, extracts the name of neuron class
    520  * that will be the input for current neuron and weight of connection.
    521  * After successful parsing it returns pointer to first character after connection
    522  * definition, or NULL if connection definition was not valid - lack of [, :, ]
    523  * characters, wrong value of weight or invalid neuron class name.
    524  * @param fragm the beginning of connection definition, it should be '[' character
    525  * @param neutype the reference to string representing input neuron class name. The name of class is validated with GenoOperators::parseNeuroClass
    526  * @param weight the reference to double variable, in which weight of connection will be stored
    527  * @return the pointer to first character in string after connection definition
    528  */
    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 */
     493int 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 */
     506const 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 */
     520const char * parseConnectionWithNeuron(const char *fragm, string &neutype, double &weight);
    530521
    531522#endif
Note: See TracChangeset for help on using the changeset viewer.