Changeset 1249 for cpp


Ignore:
Timestamp:
05/21/23 23:16:51 (10 months ago)
Author:
Maciej Komosinski
Message:

Multiple issues fixed when calculating bending range for the '|' bending muscle (earlier it was always 1.0 despite the code that supposedly calculated this)

Location:
cpp/frams/genetics
Files:
5 edited

Legend:

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

    r1240 r1249  
    124124        // reset recursive traverse flags
    125125        for (int i = 0; i < cells->cell_count; i++)
    126                 cells->C[i]->recProcessedFlag = 0;
     126                cells->C[i]->recurProcessedFlag = false;
    127127
    128128        open(using_checkpoints); // begin model build
     
    131131        for (int i = 0; i < cells->cell_count; i++)
    132132        {
    133                 int res = buildModelRec(cells->C[i]);
     133                int res = buildModelRecur(cells->C[i]);
    134134                if (res)
    135135                {
     
    166166
    167167
    168 int f4_Model::buildModelRec(f4_Cell *C)
    169 {
    170         int partidx;
    171         int j, res;
    172         MultiRange range;
    173 
    174         if (C->recProcessedFlag)
     168int f4_Model::buildModelRecur(f4_Cell *C)
     169{
     170        if (C->recurProcessedFlag)
    175171                // already processed
    176172                return 0;
    177173
    178174        // mark it processed
    179         C->recProcessedFlag = 1;
     175        C->recurProcessedFlag = true;
    180176
    181177        // make sure parent is a stick
    182         if (NULL != C->dadlink)
     178        if (C->dadlink != NULL)
    183179                if (C->dadlink->type != CELL_STICK)
    184180                {
     
    187183
    188184        // make sure its parent is processed first
    189         if (NULL != C->dadlink)
    190         {
    191                 res = buildModelRec(C->dadlink);
     185        if (C->dadlink != NULL)
     186        {
     187                int res = buildModelRecur(C->dadlink);
    192188                if (res) return res;
    193189        }
    194190
    195191        char tmpLine[100];
    196 
    197         range = C->genoRange;
     192        MultiRange range = C->genoRange;
     193
    198194        if (C->type == CELL_STICK)
    199195        {
     
    208204                                //C->firstend.x, C->firstend.y, C->firstend.z
    209205                        );
    210                         partidx = addFromString(PartType, tmpLine, &range);
    211                         if (partidx < 0) return -1;
     206                        jj_p1_refno = addFromString(PartType, tmpLine, &range);
     207                        if (jj_p1_refno < 0) return -1;
    212208                        this->checkpoint();
    213                         jj_p1_refno = partidx;
    214209                }
    215210                else {
     
    225220                        /*"vol=" 1.0/C->P.mass,*/ C->P.friction, C->P.ingestion, C->P.assimilation
    226221                );
    227                 partidx = addFromString(PartType, tmpLine, &range);
    228                 if (partidx < 0) return -2;
    229                 C->p2_refno = partidx;
     222                C->p2_refno = addFromString(PartType, tmpLine, &range);
     223                if (C->p2_refno < 0) return -2;
    230224
    231225                // new joint object
     
    245239                        C->P.stamina
    246240                );
    247                 partidx = addFromString(JointType, tmpLine, &range);
    248                 if (partidx < 0) return -13;
     241                C->joint_refno = addFromString(JointType, tmpLine, &range);
     242                if (C->joint_refno < 0) return -13;
    249243                this->checkpoint();
    250                 C->joint_refno = partidx;
    251244        }
    252245
     
    254247        {
    255248                const char* nclass = C->neuclass->name.c_str();
    256                 int partno, jointno;
    257249                if (C->neuclass->getPreferredLocation() == 0)
    258250                {
    259                         if (strcmp(nclass, "N") == 0)
    260                         {
    261                                 partno = C->dadlink->p2_refno;
    262                                 if ((partno < 0) || (partno >= getPartCount())) return -21;
    263                                 else sprintf(tmpLine, "p=%d,d=\"N:in=%g,fo=%g,si=%g\"", partno, C->inertia, C->force, C->sigmo);
    264                         }
     251                        if (strcmp(nclass, "N") == 0) //special case just to specify the only neuron properties supported by f4, i.e., the properties for neuron class 'N'
     252                                sprintf(tmpLine, "d=\"N:in=%g,fo=%g,si=%g\"", C->inertia, C->force, C->sigmo);
    265253                        else
    266                         {
    267254                                sprintf(tmpLine, "d=\"%s\"", nclass);
    268                         }
    269                         partidx = addFromString(NeuronType, tmpLine, &range);
    270                         if (partidx < 0) return -22;
     255
     256                        C->neuro_refno = addFromString(NeuronType, tmpLine, &range);
     257                        if (C->neuro_refno < 0) return -22;
    271258                        this->checkpoint();
    272                         C->neuro_refno = partidx;
    273259                }
    274260                else if (C->neuclass->getPreferredLocation() == 1) // attached to Part or have no required attachment - also part
    275261                {
    276                         partno = C->dadlink->p2_refno;
     262                        int partno = C->dadlink->p2_refno;
    277263                        if ((partno < 0) || (partno >= getPartCount())) return -21;
    278264
    279                         if (strcmp(nclass, "N") == 0)
    280                                 sprintf(tmpLine, "p=%d,d=\"N:in=%g,fo=%g,si=%g\"", partno, C->inertia, C->force, C->sigmo);
    281                         else
    282                                 sprintf(tmpLine, "p=%d,d=\"%s\"", partno, nclass);
    283 
    284                         partidx = addFromString(NeuronType, tmpLine, &range);
    285                         if (partidx < 0) return -22;
     265                        sprintf(tmpLine, "p=%d,d=\"%s\"", partno, nclass);
     266
     267                        C->neuro_refno = addFromString(NeuronType, tmpLine, &range);
     268                        if (C->neuro_refno < 0) return -22;
    286269                        this->checkpoint();
    287                         C->neuro_refno = partidx;
    288270                }
    289271                else // attached to Joint, assume there are only three possibilities of getPreferredLocation()
    290272                {
    291                         jointno = C->dadlink->joint_refno;
     273                        int jointno = C->dadlink->joint_refno;
    292274
    293275                        if (strcmp(nclass, "@") == 0)
    294276                                sprintf(tmpLine, "j=%d,d=\"@:p=%g\"", jointno, C->P.muscle_power);
    295277                        else if (strcmp(nclass, "|") == 0)
    296                                 sprintf(tmpLine, "j=%d,d=\"|:p=%g,r=%g\"", jointno, C->P.muscle_power, C->mz);
     278                        {
     279                                sprintf(tmpLine, "j=%d,d=\"|:p=%g,r=%g\"", jointno, C->P.muscle_power, C->dadlink->P.muscle_bend_range); //Macko 2023-05 change: we take muscle_bend_range from dadlink, not from C, because we also assign this neuron to C->dadlink->joint_refno. Without this, for example in /*4*/<<X><<<<X>N:|>X>X>X>X the muscle is attached to the junction with 3 sticks, but gets range=33% as in a four-stick junction. f1 correctly sets range=0.5 for the analogous phenotype: X(X[|],X(X,X,X))
     280                        }
    297281                        else
    298282                                sprintf(tmpLine, "j=%d,d=\"%s\"", jointno, nclass);
    299283
    300                         partidx = addFromString(NeuronType, tmpLine, &range);
    301                         if (partidx < 0) return -32;
     284                        C->neuro_refno = addFromString(NeuronType, tmpLine, &range);
     285                        if (C->neuro_refno < 0) return -32;
    302286                        this->checkpoint();
    303287                }
    304                 C->neuro_refno = partidx;
    305                 int n_refno = C->neuro_refno;
    306 
    307                 for (j = 0; j < C->conns_count; j++)
     288                for (int j = 0; j < C->conns_count; j++)
    308289                {
    309290                        if (C->conns[j]->from != NULL)
    310                                 buildModelRec(C->conns[j]->from);
     291                                buildModelRecur(C->conns[j]->from);
    311292
    312293                        tmpLine[0] = 0;
     
    320301                        if (from >= 0)
    321302                        {
    322                                 sprintf(tmpLine, "%d,%d,%g", n_refno, from, C->conns[j]->weight);
     303                                sprintf(tmpLine, "%d,%d,%g", C->neuro_refno, from, C->conns[j]->weight);
    323304                                if (addFromString(NeuronConnectionType, tmpLine, &range) < 0) return -35;
    324305                                this->checkpoint();
  • cpp/frams/genetics/f4/f4_conv.h

    r1238 r1249  
    8383private:
    8484        f4_Cells *cells;
    85         int        buildModelRec(f4_Cell *ndad);
     85        int        buildModelRecur(f4_Cell *ndad);
    8686        /**
    8787         * Get a cell which is a stick, by traversing dadlinks.
  • cpp/frams/genetics/f4/f4_general.cpp

    r1241 r1249  
    1212#include <frams/model/model.h> // for min and max attributes
    1313#include <common/nonstd_math.h>
     14#include <algorithm> // std::min, std::max
    1415
    1516#ifdef DMALLOC
     
    4647        anglepos = nangle;
    4748        commacount = 0;
    48         childcount = 0;
     49        stickchildcount = 0;
    4950        P = newP;
    5051        rolling = 0;
     
    6566                        //firstend = ndad->lastend;
    6667                        //OM = ndad->OM;
    67                         ndad->childcount++;
     68                        ndad->stickchildcount++;
    6869                }
    6970                if (ndad->type == CELL_NEURON)
     
    7677        // adjust lastend
    7778        //lastend = firstend + ((Orient)OM * (Pt3D(1,0,0) * P.len));
    78         mz = 1;
     79        P.muscle_bend_range = 1;
    7980}
    8081
     
    9697        anglepos = nangle;
    9798        commacount = 0;
    98         childcount = 0;
     99        stickchildcount = 0;
    99100        P = newP;
    100101        rolling = 0;
     
    115116                        //firstend = ndad->lastend;
    116117                        //OM = ndad->OM;
    117                         ndad->childcount++;
     118                        ndad->stickchildcount++;
    118119                }
    119120                if (ndad->type == CELL_NEURON)
     
    126127        // adjust lastend
    127128        //lastend = firstend + ((Orient)OM * (Pt3D(1,0,0) * P.len));
    128         mz = 1;
     129        P.muscle_bend_range = 1;
    129130}
    130131
     
    548549
    549550
    550 void f4_Cell::adjustRec()
    551 {
    552         //f4_OrientMat rot;
    553         int i;
    554 
    555         if (recProcessedFlag)
     551void f4_Cell::adjustRecur()
     552{
     553        if (recurProcessedFlag)
    556554                // already processed
    557555                return;
    558556
    559557        // mark it processed
    560         recProcessedFlag = 1;
     558        recurProcessedFlag = true;
    561559
    562560        // make sure its parent is processed first
    563561        if (dadlink != NULL)
    564                 dadlink->adjustRec();
     562                dadlink->adjustRecur();
    565563
    566564        // count children
    567         childcount = 0;
    568         for (i = 0; i < org->cell_count; i++)
     565        stickchildcount = 0;
     566        for (int i = 0; i < org->cell_count; i++)
    569567        {
    570568                if (org->C[i]->dadlink == this)
    571569                        if (org->C[i]->type == CELL_STICK)
    572                                 childcount++;
    573         }
     570                                stickchildcount++;
     571        }
     572
     573        if (dadlink == NULL)
     574                P.muscle_bend_range = 1.0;
     575        else
     576                P.muscle_bend_range = 1.0 / std::max(1, dadlink->stickchildcount); //bend range in f1: 0, 1 (line XX[|]) -> 100%, 2 (Y-shape X(X[|],X)) -> 50%, 3 (cross X(X[|],X,X)) -> 33%
     577        //MacKo 2023-05: but shouldn't this formula ^^ also take commacount into consideration, like in f1?
    574578
    575579        if (type == CELL_STICK)
     
    580584                        // rotation due to rolling
    581585                        xrot = rolling;
    582                         mz = 1;
    583586                }
    584587                else
     
    589592                        Padj.propagateAlong(false);
    590593
    591                         //rot = Orient_1;
     594                        //f4_OrientMat rot = Orient_1;
    592595
    593596                        // rotation due to rolling
     
    612615                        // rotation in world coordinates
    613616                        //OM =  ((f4_OrientMat)dadlink->OM) * OM;
    614                         mz = dadlink->mz / dadlink->childcount;
    615617                }
    616618                //Pt3D lastoffset = (Orient)OM * (Pt3D(1,0,0)*P.len);
     
    737739        // reset recursive traverse flags
    738740        for (int i = 0; i < cell_count; i++)
    739                 C[i]->recProcessedFlag = 0;
     741                C[i]->recurProcessedFlag = false;
    740742        // process every cell
    741743        for (int i = 0; i < cell_count; i++)
    742                 C[i]->adjustRec();
     744                C[i]->adjustRecur();
    743745
    744746        //DB( printf("Cell simulation done, %d cells. \n", nc); )
    745747
    746748        if (PRINT_CELLS_DEVELOPMENT) print_cells("Final");
     749        if (PRINT_CELLS_DEVELOPMENT)
     750                for (int i = 0; i < cell_count; i++)
     751                        printf("%d,%d,dad=%d\tstick_children=%d\tcommas=%d\t|:range=%g\n", i, C[i]->nr, C[i]->dadlink ? C[i]->dadlink->nr : -1, C[i]->stickchildcount, C[i]->commacount, C[i]->P.muscle_bend_range);
    747752
    748753        return errorcode;
  • cpp/frams/genetics/f4/f4_general.h

    r1239 r1249  
    181181         * Adjusts properties of stick objects.
    182182         */
    183         void  adjustRec();
    184 
    185         int        nr;                 ///<number of cell (seems to be used only in old f1 converter for neuron connections)
     183        void  adjustRecur();
     184
     185        int        nr;                 ///<number of cell (seems to be used only in the approximate f1 converter for neuron connections)
    186186        int        type;               ///<type
    187187        f4_Cell *dadlink;              ///<pointer to cell parent
     
    192192        f4_Node *old_gcur;             ///<used externally by f4_Cells::oneStep() to track changes of gcur, i.e., to detect progress in cell development
    193193        repeat_stack repeat;           ///<stack holding repetition nodes and counters
    194         int recProcessedFlag;          ///<used during recursive traverse
     194        bool recurProcessedFlag;       ///<used during recursive traverse
    195195        MultiRange genoRange;          ///<remember the genotype codes affecting this cell so far
    196196
    197197        GeneProps    P;                ///<properties
    198198        int          anglepos;         ///<number of position within dad's children (,)
    199         int          childcount;       ///<number of children
     199        int          stickchildcount;  ///<number of children (sticks only)
    200200        int          commacount;       ///<number of postitions at lastend (>=childcount)
    201201        double       rolling;          ///<rolling angle ('R') (around x)
     
    203203        double       zrot;             ///<horizontal rotation angle due to branching (around z)
    204204
    205         double       mz;               ///<freedom in z
    206205        int          p2_refno;         ///<the number of the last end part object, used in f0
    207206        int          joint_refno;      ///<the number of the joint object, used in f0
    208207        int          neuro_refno;      ///<the number of the neuro object, used in f0
    209208
    210         double       inertia;          ///<inertia of neuron
    211         double       force;            ///<force of neuron
    212         double       sigmo;            ///<sigmoid of neuron
     209        double       inertia;          ///<inertia of neuron N
     210        double       force;            ///<force of neuron N
     211        double       sigmo;            ///<sigmoid of neuron N
    213212        f4_CellConn *conns[F4_MAX_CELL_INPUTS]; ///<array of neuron connections
    214213        int          conns_count;      ///<number of connections
  • cpp/frams/genetics/geneprops.h

    r1247 r1249  
    9898
    9999/**
    100  * Contains physical, biological and other properties of
    101  * stick, except for rotation. The constructor initializes properties of sticks with
    102  * default values. In order to change a property of a stick, the executeModifier() method
     100 * Contains physical, biological and other properties of a Part and a Joint (as handled
     101 * by the f1 and f4 encodings), except for rotation. The constructor initializes properties with
     102 * default values. In order to change a property, the executeModifier() method
    103103 * should be called. Modification of length, curvedness and twist properties
    104104 * usually affects further sticks, so new sticks should have properties of
Note: See TracChangeset for help on using the changeset viewer.