Changeset 767 for cpp/frams/genetics


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

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

Location:
cpp/frams/genetics/f4
Files:
3 edited

Legend:

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

    r760 r767  
    466466                                float w = gcur->f1;
    467467                                f4_Cell *tneu = NULL;
    468                                 if (t > 0) // sensors
     468                                if (t < 0) // wrong sensor
     469                                {
     470                                        string buf = "wrong sensor in link '";
     471                                        buf.append(gcur->s1);
     472                                        buf.append("'");
     473                                        logMessage("f4_Cell", "onestep", LOG_ERROR, buf.c_str());
     474                                        org->setRepairRemove(gcur->pos, gcur);
     475                                        return 1;
     476                                }
     477                                else if (t > 0) // sensors
    469478                                {
    470479                                        char *temp = (char*)gcur->s1.c_str();
     
    476485                                                buf.append(gcur->s1);
    477486                                                buf.append("'");
    478                                                 logMessage("f4_Cell", "onestep", LOG_WARN, buf.c_str()); //TODO ask
     487                                                logMessage("f4_Cell", "onestep", LOG_ERROR, buf.c_str());
    479488                                                org->setRepairRemove(gcur->pos, gcur);
    480489                                                return 1;
     
    12271236void f4_node::sprint(SString& out)
    12281237{
    1229         string buf2 = "";
     1238        char buf2[20];
    12301239        // special case: repetition code
    12311240        if (name == "#")
     
    12341243                if (i1 != 1)
    12351244                {
    1236                         sprintf((char*)buf2.c_str(), "%d", i1);
    1237                         out += buf2.c_str();
     1245                        sprintf(buf2, "%d", i1);
     1246                        out += buf2;
    12381247                }
    12391248        }
     
    12501259                        else
    12511260                        {
    1252                                 sprintf((char*)buf2.c_str(), "%ld", l1);
    1253                                 out += buf2.c_str();
    1254                         }
    1255                         sprintf((char*)buf2.c_str(), ":%g]", f1);
    1256                         out += buf2.c_str();
     1261                                sprintf(buf2, "%ld", l1);
     1262                                out += buf2;
     1263                        }
     1264                        sprintf(buf2, ":%g]", f1);
     1265                        out += buf2;
    12571266                }
    12581267                else if (name == ":")
    12591268                {
    1260                         sprintf((char*)buf2.c_str(), ":%c%c:", l1 ? '+' : '-', (char)i1);
    1261                         out += buf2.c_str();
     1269                        sprintf(buf2, ":%c%c:", l1 ? '+' : '-', (char)i1);
     1270                        out += buf2;
    12621271                }
    12631272                else if (name == "@" || name == "|")
     
    12651274                        if (parent->name == "N")
    12661275                        {
    1267                                 buf2 = name;
    1268                                 out += buf2.c_str();
     1276                                out += name.c_str();
    12691277                        }
    12701278                        else
    12711279                        {
    12721280                                out += "N:";
    1273                                 buf2 = name;
    1274                                 out += buf2.c_str();
     1281                                out += name.c_str();
    12751282                        }
    12761283                }
     
    12831290                                out += "N:";
    12841291                        }
    1285                         buf2 = name;
    1286                         out += buf2.c_str();
     1292                        out += name.c_str();
    12871293                }
    12881294        }
     
    14021408                {
    14031409                        // repetition marker, 1 by default
    1404                         if (sscanf(genot + gpos, "#%d", &i) != 1) i = 1;
     1410                        ExtValue val;
     1411                        const char * end = val.parseNumber(genot + gpos + 1, ExtPType::TInt);
     1412                        if (end == NULL) i = 1;
     1413                        else i = val.getInt();
    14051414                        // find out genotype start for continuation
    14061415                        j = scanrec(genot + gpos + 1, strlen(genot + gpos + 1), '>');
    14071416                        // skip number
    14081417                        oldpos = gpos;
    1409                         gpos++;
    1410                         while ((genot[gpos] >= '0') && (genot[gpos] <= '9')) gpos++;
     1418                        gpos += end - (genot + gpos);
     1419                        //gpos++;
     1420                        //while ((genot[gpos] >= '0') && (genot[gpos] <= '9')) gpos++;node1 = new f4_node("#", par, oldpos);
    14111421                        node1 = new f4_node("#", par, oldpos);
    14121422                        node1->i1 = i;
     
    15271537                                end = parseConnectionWithNeuron(genot + gpos, neutype, w);
    15281538                                if (end == NULL) t = -1;
    1529                                 t = 1;
     1539                                else t = 1;
    15301540                        }
    15311541                        else
  • 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
  • cpp/frams/genetics/f4/oper_f4.cpp

    r760 r767  
    2020#define F4_MODIFIERS_VISUAL "" //not supported in f4
    2121#define F4_MODIFIERS_RARE "EeWwAaSs" //expdef would need to handle these properly/specifically to ensure reasonable behavior, and hardly any expdef does. Modifying initial energy of a creature as a result of its genes (Ee) is in general not a good idea. Weight (Ww) works only in water, and in water sinking/going up should usually be caused by real "intentional" activity of a creature, not by its inherited weight. For assimilation (Aa), there is a dedicated parameter in CreaturesGroup. Stamina (Ss) is no longer needed as destructive collisions are not supported, and even if they were, some expdef would need to impose reasonable restrictions on the value of this parameter (e.g. similar to normalizeBiol4()) so there is some cost associated with it, and the specific consequences of destructions should be defined as needed.
    22 #define F4_MODIFIERS "LlRrCcQqFfMmIi" F4_MODIFIERS_RARE F4_MODIFIERS_VISUAL
     22#define F4_MODIFIERS ",LlRrCcQqFfMmIi" F4_MODIFIERS_RARE F4_MODIFIERS_VISUAL
    2323const char *Geno_f4::all_modifiers = F4_MODIFIERS;
     24
     25// codes that can be changed (apart from being added/deleted)
     26#define MUT_CHAN_CODES "<[#"
     27#define REP_MAXCOUNT 19
    2428
    2529#define FIELDSTRUCT Geno_f4
     
    134138        // ! the genotype is g->child (not g) !
    135139
    136         // codes that can be changed (apart being added/deleted)
    137 #define MUT_CHAN_CODES "<[#"
    138 #define ADD_SIMPLE_CODES ",XlLcCrRaAiIsSmMfFwWeEN@|"
    139 #define REP_MAXCOUNT 19
    140 
    141140        // do the mutation
    142141        // pick a random node
     
    176175                                else
    177176                                {
    178                                         f4_node *n4 = new f4_node(rndclass->getName().c_str(), n2, n2->pos); //TODO move this above
     177                                        f4_node *n4 = new f4_node(rndclass->getName().c_str(), n2, n2->pos);
    179178                                        if (rndclass->getPreferredInputs() != 0)
    180179                                        {
     
    280279                        // choose a simple node from ADD_SIMPLE_CODES
    281280                        n1->parent->removeChild(n1);
    282                         f4_node *n2 = new f4_node(ADD_SIMPLE_CODES[randomN(strlen(ADD_SIMPLE_CODES))], n1->parent, n1->parent->pos);
     281                        //f4_node *n2 = new f4_node(ADD_SIMPLE_CODES[randomN(strlen(ADD_SIMPLE_CODES))], n1->parent, n1->parent->pos);
     282                        int modifierid = GenoOperators::getRandomChar(all_modifiers, excluded_modifiers.c_str());
     283                        f4_node *n2 = new f4_node(all_modifiers[modifierid], n1->parent, n1->parent->pos);
    283284                        n2->addChild(n1);
    284285                        n1->parent = n2;
     
    717718}
    718719
    719 
    720 
    721720uint32_t Geno_f4::style(const char *g, int pos)
    722721{
     
    727726#define STYL4CAT_DIGIT "0123456789."
    728727#define STYL4CAT_REST "XN<># "
    729         if (!strchr(STYL4CAT_MODIFIC STYL4CAT_NEUMOD STYL4CAT_DIGIT STYL4CAT_REST, ch))
     728
     729        if (!isalpha(ch) && !isdigit(ch) &&  !strchr("<>-+[]:,.@|#*/=!\t",ch)) {
    730730                return GENSTYLE_CS(0, GENSTYLE_INVALID);
     731        }
    731732        uint32_t style = GENSTYLE_CS(0, GENSTYLE_STRIKEOUT); //default, should be changed below
    732733        if (strchr("X ", ch))              style = GENSTYLE_CS(0, GENSTYLE_NONE);
     
    737738        if (strchr(STYL4CAT_MODIFIC, ch))   style = GENSTYLE_RGBS(100, 100, 100, GENSTYLE_NONE);
    738739        if (strchr(STYL4CAT_NEUMOD, ch))    style = GENSTYLE_RGBS(0, 150, 0, GENSTYLE_NONE);
     740        if (isalpha(ch)) {
     741                int p = pos;
     742                while (p > 0) {
     743                        p--;
     744                        if (!isalpha(g[p])) {
     745                                if (isupper(g[p+1]) && (g[p] == ':' || g[p] == '[')) { // name of neuron class
     746                                        style = GENSTYLE_RGBS(150,0,150,GENSTYLE_ITALIC);
     747                                }
     748                                else { // property
     749                                        style = GENSTYLE_RGBS(255,140,0,GENSTYLE_BOLD);
     750                                }
     751                        }
     752                }
     753        }
    739754        return style;
    740755}
Note: See TracChangeset for help on using the changeset viewer.