Changeset 726


Ignore:
Timestamp:
01/19/18 19:37:43 (6 years ago)
Author:
Maciej Komosinski
Message:
  • Changed Model::singleStepBuild() to Model::addFromString() to create model elements; the latter requires explicit indication of element type (P/J/N/C)
  • Removed old compatibility source (#ifdef MODEL_V1_COMPATIBLE) from f1->f0 converter and neuron definitions
Location:
cpp/frams
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • cpp/frams/genetics/f1/conv_f1.cpp

    r719 r726  
    111111        Builder builder(g, map ? 1 : 0);
    112112        builder.model.open();
    113         builder.grow(-1, g, Pt3D_0, stdprops, -1); // uses Model::singleStepBuild to create model elements
     113        builder.grow(-1, g, Pt3D_0, stdprops, -1); // uses Model::addFromString() to create model elements
    114114        if (builder.invalid) return SString();
    115115        builder.addPendingInputs();
     
    323323{
    324324        double len = min(2.0, c.length);
    325         sprintf(tmp, "j:p1=%ld,p2=%ld,dx=%lg,rx=%lg,ry=%lg,rz=%lg,stam=%lg,vr=%g,vg=%g,vb=%g",
     325        sprintf(tmp, "p1=%ld,p2=%ld,dx=%lg,rx=%lg,ry=%lg,rz=%lg,stam=%lg,vr=%g,vg=%g,vb=%g",
    326326                part1, part2, len, angle.x, angle.y, angle.z, c.stamina, c.cred, c.cgreen, c.cblue);
    327327        lastjoint_muscle_power = c.muscle_power;
    328         return model.singleStepBuild(tmp, makeRange(g));
     328        return model.addFromString(Model::JointType, tmp, makeRange(g));
    329329}
    330330
    331331int Builder::growPart(F1Props &c, const char *g)
    332332{
    333         sprintf(tmp, "p:dn=%lg,fr=%lg,ing=%lg,as=%lg,vs=%g,vr=%g,vg=%g,vb=%g",
     333        sprintf(tmp, "dn=%lg,fr=%lg,ing=%lg,as=%lg,vs=%g,vr=%g,vg=%g,vb=%g",
    334334                1.0 / c.weight, c.friction, c.ingestion, c.assimilation, c.visual_size, c.cred, c.cgreen, c.cblue);
    335         return model.singleStepBuild(tmp, makeRange(g));
     335        return model.addFromString(Model::PartType, tmp, makeRange(g));
    336336}
    337337
  • cpp/frams/genetics/f4/conv_f4.cpp

    r675 r726  
    178178                        // new part object for firstend
    179179                        // coordinates are left to be computed by Model
    180                         sprintf(tmpLine, "p:fr=%g,ing=%g,as=%g",
     180                        sprintf(tmpLine, "fr=%g,ing=%g,as=%g",
    181181                                /*1.0/C->P.mass,*/ C->P.friction, C->P.ingestion, C->P.assimilation
    182182                                //C->firstend.x, C->firstend.y, C->firstend.z
    183183                                );
    184                         partidx = singleStepBuild(tmpLine, &range);
     184                        partidx = addFromString(PartType, tmpLine, &range);
    185185                        if (partidx < 0) return -1;
    186186                        jj_p1_refno = partidx;
     
    194194                }
    195195                // new part object for lastend
    196                 sprintf(tmpLine, "p:fr=%g,ing=%g,as=%g",
     196                sprintf(tmpLine, "fr=%g,ing=%g,as=%g",
    197197                        //C->lastend.x, C->lastend.y, C->lastend.z
    198198                        /*"vol=" 1.0/C->P.mass,*/ C->P.friction, C->P.ingestion, C->P.assimilation
    199199                        );
    200                 partidx = singleStepBuild(tmpLine, &range);
     200                partidx = addFromString(PartType, tmpLine, &range);
    201201                if (partidx < 0) return -2;
    202202                C->p2_refno = partidx;
     
    207207                if ((jj_p1_refno < 0) || (jj_p1_refno >= getPartCount())) return -11;
    208208                if ((jj_p2_refno < 0) || (jj_p2_refno >= getPartCount())) return -12;
    209                 sprintf(tmpLine, "j:p1=%ld,p2=%ld,dx=%g,dy=0,dz=0,rx=%g,ry=0,rz=%g"\
     209                sprintf(tmpLine, "p1=%ld,p2=%ld,dx=%g,dy=0,dz=0,rx=%g,ry=0,rz=%g"\
    210210                        ",stam=%g",
    211211                        jj_p1_refno, jj_p2_refno,
     
    218218                        C->P.stamina
    219219                        );
    220                 partidx = singleStepBuild(tmpLine, &range);
     220                partidx = addFromString(JointType, tmpLine, &range);
    221221                if (partidx < 0) return -13;
    222222                C->joint_refno = partidx;
     
    230230                if ((p_refno < 0) || (p_refno >= getPartCount())) return -21;
    231231                // joint_refno is currently not used
    232                 sprintf(tmpLine, "n:p=%ld,d=\"N:in=%g,fo=%g,si=%g\"",
     232                sprintf(tmpLine, "p=%ld,d=\"N:in=%g,fo=%g,si=%g\"",
    233233                        p_refno,
    234234                        C->inertia, C->force, C->sigmo);
    235                 partidx = singleStepBuild(tmpLine, &range);
     235                partidx = addFromString(NeuronType, tmpLine, &range);
    236236                if (partidx < 0) return -22;
    237237                C->neuro_refno = partidx;
     
    241241                {
    242242                        if (1 == C->ctrl)
    243                                 sprintf(tmpLine, "n:j=%d,d=\"@:p=%g\"", C->dadlink->joint_refno, C->P.muscle_power);
     243                                sprintf(tmpLine, "j=%d,d=\"@:p=%g\"", C->dadlink->joint_refno, C->P.muscle_power);
    244244                        else
    245                                 sprintf(tmpLine, "n:j=%d,d=\"|:p=%g,r=%g\"", C->dadlink->joint_refno, C->P.muscle_power, C->mz);
    246                         partidx = singleStepBuild(tmpLine, &range);
     245                                sprintf(tmpLine, "j=%d,d=\"|:p=%g,r=%g\"", C->dadlink->joint_refno, C->P.muscle_power, C->mz);
     246                        partidx = addFromString(NeuronType, tmpLine, &range);
    247247                        if (partidx < 0) return -32;
    248                         sprintf(tmpLine, "c:%d,%d", partidx, n_refno);
    249                         if (singleStepBuild(tmpLine, &range) < 0) return -33;
     248                        sprintf(tmpLine, "%d,%d", partidx, n_refno);
     249                        if (addFromString(NeuronConnectionType, tmpLine, &range) < 0) return -33;
    250250                }
    251251
     
    256256
    257257                        tmpLine[0] = 0;
    258                         if (1 == C->links[j]->t) sprintf(tmpLine, "n:p=%d,d=\"*\"", p_refno);
    259                         if (2 == C->links[j]->t) sprintf(tmpLine, "n:j=%d,d=\"G\"", C->dadlink->joint_refno);
    260                         if (3 == C->links[j]->t) sprintf(tmpLine, "n:p=%d,d=\"T\"", p_refno);
    261                         if (4 == C->links[j]->t) sprintf(tmpLine, "n:p=%d,d=\"S\"", p_refno);
     258                        if (1 == C->links[j]->t) sprintf(tmpLine, "p=%d,d=\"*\"", p_refno);
     259                        if (2 == C->links[j]->t) sprintf(tmpLine, "j=%d,d=\"G\"", C->dadlink->joint_refno);
     260                        if (3 == C->links[j]->t) sprintf(tmpLine, "p=%d,d=\"T\"", p_refno);
     261                        if (4 == C->links[j]->t) sprintf(tmpLine, "p=%d,d=\"S\"", p_refno);
    262262                        int from = -1;
    263263                        if (tmpLine[0]) //input from receptor
    264264                        {
    265                                 from = singleStepBuild(tmpLine, &range);
     265                                from = addFromString(NeuronType, tmpLine, &range);
    266266                                if (from < 0) return -34;
    267267                        } /*could be 'else'...*/
     
    270270                        if (from >= 0)
    271271                        {
    272                                 sprintf(tmpLine, "c:%d,%d,%g", n_refno, from, C->links[j]->w);
    273                                 if (singleStepBuild(tmpLine, &range) < 0) return -35;
     272                                sprintf(tmpLine, "%d,%d,%g", n_refno, from, C->links[j]->w);
     273                                if (addFromString(NeuronConnectionType, tmpLine, &range) < 0) return -35;
    274274                        }
    275275                }
  • cpp/frams/model/model.cpp

    r720 r726  
    2222        startenergy = 1.0;
    2323        checklevel = 1;
    24 #ifdef MODEL_V1_COMPATIBLE
    25         oldneurocount=-1; // == unknown
    26         oldconnections=0;
    27 #endif
    2824        map = 0;
    2925        f0map = 0;
     
    295291}
    296292
     293Model::ItemType Model::itemTypeFromLinePrefix(const char* line)
     294{
     295struct PrefixAndItem { const char* prefix; ItemType type; };
     296static const PrefixAndItem types[]={ {"m:",ModelType},{"p:",PartType},{"j:",JointType},{"n:",NeuronType},{"c:",NeuronConnectionType},{NULL} };
     297for(const PrefixAndItem *t=types;t->prefix!=NULL;t++)
     298        {
     299        const char* in=line;
     300        const char* pattern=t->prefix;
     301        for(;*in==*pattern;in++,pattern++)
     302                if (*pattern==':')
     303                        return t->type;
     304        }
     305return UnknownType;
     306}
     307
    297308void Model::build()
    298309{
     
    322333        for (; f0txt.getNextToken(pos, line, '\n'); lnum++)
    323334        {
     335                const char* line_ptr=line.c_str();
     336                for (; *line_ptr; line_ptr++)
     337                        if (!strchr(" \r\t", *line_ptr)) break;
     338                if (*line_ptr == '#') continue;
     339                if (!*line_ptr) continue;
     340
     341                const char* colon=strchr(line_ptr,':');
     342                ItemType type=UnknownType;
     343                SString excluding_prefix;
     344                if (colon!=NULL)
     345                        {
     346                        colon++;
     347                        type=itemTypeFromLinePrefix(line_ptr);
     348                        for (; *colon; colon++)
     349                                if (!strchr(" \r\t", *colon)) break;
     350                        excluding_prefix=colon;
     351                        }
     352               
    324353                if (autobuildmaps)
    325354                {
     
    328357                }
    329358                mh.reset();
    330                 if (singleStepBuild(line, lnum, autobuildmaps ? (&frommap) : 0) == -1)
     359                if (addFromString(type, excluding_prefix, lnum, autobuildmaps ? (&frommap) : 0) == -1)
    331360                {
    332361                        buildstatus = invalid;
     
    558587}
    559588
    560 int Model::singleStepBuild(const SString &singleline, const MultiRange* srcrange)
    561 {
    562         return singleStepBuild(singleline, 0, srcrange);
    563 }
    564 
    565 int Model::singleStepBuild(const SString &singleline, int line_num, const MultiRange* srcrange)
     589int Model::addFromString(ItemType item_type, const SString &singleline, const MultiRange* srcrange)
     590{
     591        return addFromString(item_type,singleline, 0, srcrange);
     592}
     593
     594int Model::addFromString(ItemType item_type, const SString &singleline, int line_num, const MultiRange* srcrange)
    566595{
    567596        SString error_message;
    568         int result = singleStepBuildNoLog(singleline, error_message, srcrange);
     597        int result = addFromStringNoLog(item_type, singleline, error_message, srcrange);
    569598        if (result < 0)
    570599        {
     
    579608}
    580609
    581 int Model::singleStepBuildNoLog(const SString &line, SString& error_message, const MultiRange* srcrange)
     610int Model::addFromStringNoLog(ItemType item_type, const SString &line, SString& error_message, const MultiRange* srcrange)
    582611{
    583612        error_message = SString::empty();
    584         const char*t = line.c_str();
    585613        ParamInterface::LoadOptions opts;
    586         for (; *t; t++, opts.offset++)
    587                 if (!strchr(" \r\t", *t)) break;
    588         if (*t == '#') return 0;
    589         if (!*t) return 0;
    590         if (!strncmp(t, "p:", 2))
    591         {
     614        switch(item_type)
     615                {
     616                case PartType:
     617                {
    592618                Param partparam(f0_part_paramtab);
    593619                Part *p = new Part();
    594620                partparam.select(p);
    595                 opts.offset += 2;
    596621                partparam.load(ParamInterface::FormatSingleLine, line, &opts);
    597622                if (opts.parse_failed) { delete p; error_message = "Invalid 'p:'"; return -1; }
     
    601626                if (srcrange) p->setMapping(*srcrange);
    602627                return getPartCount() - 1;
    603         }
    604         if (!strncmp(t, "m:", 2))
    605         {
     628                }
     629
     630                case ModelType:
     631                {
    606632                Param modelparam(f0_model_paramtab);
    607633                modelparam.select(this);
    608                 opts.offset += 2;
    609634                modelparam.load(ParamInterface::FormatSingleLine, line, &opts);
    610635                if (opts.parse_failed) { error_message = "Invalid 'm:'"; return -1; }
    611636                return 0;
    612         }
    613         else if (!strncmp(t, "j:", 2))
    614         {
     637                }
     638
     639                case JointType:
     640                {
    615641                Param jointparam(f0_joint_paramtab);
    616642                Joint *j = new Joint();
    617643                jointparam.select(j);
    618                 opts.offset += 2;
    619644                j->owner = this;
    620645                jointparam.load(ParamInterface::FormatSingleLine, line, &opts);
     
    640665                        return -1;
    641666                }
    642         }
    643         else if (!strncmp(t, "n:", 2)) // neuro (or the old neuro object as the special case)
    644         {
     667                }
     668
     669                case NeuronType:
     670                {
    645671                Param neuroparam(f0_neuro_paramtab);
    646672                Neuro *nu = new Neuro();
    647673                neuroparam.select(nu);
    648                 opts.offset += 2;
    649674                neuroparam.load(ParamInterface::FormatSingleLine, line, &opts);
    650675                if (opts.parse_failed) { delete nu; error_message = "Invalid 'n:'"; return -1; }
    651 #ifdef MODEL_V1_COMPATIBLE
    652                 if (nu->neuro_refno>=0) // parent specified...
    653                 {
    654                         if (nu->neuro_refno >= getNeuroCount()) // and it's illegal
    655                         {
    656                                 delete nu;
    657                                 return -1;
    658                         }
    659                         Neuro *parentNU=getNeuro(nu->neuro_refno);
    660                         parentNU->addInput(nu,nu->weight);
    661                         // default class for parented units: n-n link
    662                         //if (nu->moredata.len()==0) nu->moredata="-";
    663                 }
    664                 else
    665 #endif
    666676                {
    667677                        // default class for unparented units: standard neuron
     
    680690                        }
    681691                        */
    682 #ifdef MODEL_V1_COMPATIBLE
    683                 nu->weight=1.0;
    684 #endif
    685692                nu->owner = this;
    686693                // attach to part/joint
     
    703710                if (srcrange) nu->setMapping(*srcrange);
    704711                // todo: check part/joint ref#
    705 #ifdef MODEL_V1_COMPATIBLE
    706                 if (hasOldNeuroLayout())
    707                 {
    708                         int count=old_getNeuroCount();
    709                         neurons.insert(count,nu);
    710                         oldneurocount=count+1;
    711                         return oldneurocount-1;
    712                 }
    713                 else
    714 #endif
    715712                {
    716713                        neurons += nu;
    717714                        return neurons.size() - 1;
    718715                }
    719         }
    720         else if (!strncmp(t, "c:", 2)) // add input
    721         {
     716                }
     717
     718                case NeuronConnectionType:                     
     719                {
    722720                Param ncparam(f0_neuroconn_paramtab);
    723721                NeuroConn c;
    724722                ncparam.select(&c);
    725                 opts.offset += 2;
    726723                ncparam.load(ParamInterface::FormatSingleLine, line, &opts);
    727724                if (opts.parse_failed) { error_message = "Invalid 'c:'"; return -1; }
     
    739736                error_message = SString::sprintf("Invalid reference to Neuro #%d", n1_ok ? c.n2_refno : c.n1_refno);
    740737                return -1;
    741         }
    742 #ifdef MODEL_V1_COMPATIBLE
    743         else if (!strncmp(t,"ni:",3)) // old neuroitem
    744         {
    745                 // we always use old layout for "ni:"
    746                 Param neuroitemparam(f0_neuroitem_paramtab);
    747                 Neuro *nu=new Neuro();
    748                 neuroitemparam.select(nu);
    749                 opts.offset+=3;
    750                 neuroitemparam.loadSingleLine(line,opts);
    751                 if (opts.parse_failed) {delete nu; return -1;}
    752                 // illegal parent?
    753                 if ((nu->neuro_refno<0)||(nu->neuro_refno>=old_getNeuroCount()))
    754                 {
    755                         delete nu;
     738                }
     739
     740                case UnknownType: //handled by addFromString for uniform error handling
    756741                        return -1;
    757742                }
    758                 Neuro *parentN=getNeuro(nu->neuro_refno);
    759                 // copy part/joint refno from parent, if possible
    760                 if ((nu->part_refno<0)&&(parentN->part_refno>=0))
    761                         nu->part_refno=parentN->part_refno;
    762                 if ((nu->joint_refno<0)&&(parentN->joint_refno>=0))
    763                         nu->joint_refno=parentN->joint_refno;
    764                 nu->owner=this;
    765                 // attach to part/joint
    766                 if (nu->part_refno>=0)
    767                         nu->attachToPart(nu->part_refno);
    768                 if (nu->joint_refno>=0)
    769                         nu->attachToJoint(nu->joint_refno);
    770                 if (srcrange)
    771                         nu->setMapping(*srcrange);
    772                 // special case: old muscles
    773                 // PARENT neuron will be set up to be the CHILD of the current one (!)
    774                 if (nu->isOldEffector())
    775                 {
    776                         nu->neuro_refno=parentN->refno;
    777                         neurons+=nu;
    778                         nu->owner=this;
    779                         nu->addInput(parentN,nu->weight); // (!)
    780                         nu->weight=1.0;
    781                         parentN->invalidateOldItems();
    782                         return 0; // !!! -> ...getItemCount()-1;
    783                 }
    784                 parentN->addInput(nu,nu->weight);
    785                 neurons+=nu;
    786                 parentN->invalidateOldItems();
    787                 if (nu->getClassName().len()==0)
    788                 {
    789                         nu->setClassName("-");
    790                         // for compatibility, "ni" can define a n-n connection
    791                         // referring to non-existent neuron (which will be hopefully defined later)
    792                         // internal check will add the proper input to this unit
    793                         // if conn_refno >=0 and input count==0
    794                         oldconnections=1;
    795                         if (srcrange)
    796                                 parentN->addMapping(*srcrange);
    797                 }
    798                 else
    799                         nu->weight=1.0;
    800                 return 0; // !!! -> ...getItemCount()-1;
    801         }
    802 #endif
    803         else return -1;
    804 }
    805 
    806 #ifdef MODEL_V1_COMPATIBLE
    807 int Model::addOldConnectionsInputs()
    808 {
    809         if (!oldconnections) return 1;
    810         Neuro* n;
    811         for (int i=0;i<neurons.size();i++)
    812         {
    813                 n=(Neuro*)neurons(i);
    814                 if (n->conn_refno>=0)
    815                         if (n->isNNConnection())
    816                                 if (n->conn_refno < old_getNeuroCount())
    817                                 { // good reference
    818                         Neuro* parent=n->parent; // nn connection has exactly one parent
    819                         int inp=parent->findInput(n);
    820                         Neuro* target=getNeuro(n->conn_refno);
    821                         parent->setInput(inp,target,n->weight);
    822                         removeNeuro(i,0); // no need to remove references
    823                         i--;
    824                                 }
    825                                 else
    826                                 {
    827                                         logPrintf("Model","internalCheck",LOG_ERROR,
    828                                                 "illegal N-N connection #%d (reference to #%d)%s",
    829                                                 i,n->conn_refno,nameForErrors().c_str());
    830                                         return 0;
    831                                 }
    832         }
    833         oldconnections=0;
    834         return 1;
    835 }
    836 #endif
     743        return -1;
     744}
     745
    837746
    838747/////////////
     
    847756        neurons.insert(newpos, n);
    848757        // conn_refno could be broken -> fix it
    849 #ifdef MODEL_V1_COMPATIBLE
    850         for (int i=0;i<neurons.size();i++)
    851         {
    852                 Neuro *n2=getNeuro(i);
    853                 if (n2->isNNConnection())
    854                         if (n2->conn_refno == oldpos) n2->conn_refno=newpos;
    855                         else
    856                         { if (n2->conn_refno > oldpos) n2->conn_refno--;
    857                 if (n2->conn_refno >= newpos) n2->conn_refno++; }
    858         }
    859         invalidateOldNeuroCount();
    860 #endif
    861 }
    862 
    863 #ifdef MODEL_V1_COMPATIBLE
    864 /** move all old neurons (class "N") to the front of the neurons list.
    865         @return number of old neurons
    866         */
    867 int Model::reorderToOldLayout()
    868 {
    869         Neuro *n;
    870         int neurocount=0;
    871         for (int i=0;i<neurons.size();i++)
    872         {
    873                 n=(Neuro*)neurons(i);
    874                 if (n->isOldNeuron())
    875                 {
    876                         moveNeuro(i,neurocount);
    877                         neurocount++;
    878                         i=neurocount-1;
    879                 }
    880         }
    881         return neurocount;
    882 }
    883 #endif
     758}
     759
    884760////////////
    885761
     
    1058934                }
    1059935        }
    1060 #ifdef MODEL_V1_COMPATIBLE
    1061         if (!addOldConnectionsInputs())
    1062                 return 0;
    1063 #endif
    1064936
    1065937        updateNeuroRefno(); // valid refno is important for n-n connections check (later)
     
    1068940        {
    1069941                n = (Neuro*)neurons(i);
    1070 #ifdef MODEL_V1_COMPATIBLE
    1071                 VALIDMINMAX(n,Neuro,inertia);
    1072                 VALIDMINMAX(n,Neuro,force);
    1073                 VALIDMINMAX(n,Neuro,sigmo);
    1074                 n->conn_refno=-1;
    1075                 n->weight=1.0;
    1076                 n->neuro_refno=-1;
    1077 #endif
    1078942                n->part_refno = (n->part) ? n->part->refno : -1;
    1079943                n->joint_refno = (n->joint) ? n->joint->refno : -1;
     
    12451109}
    12461110
    1247 
    1248 ////////////////////
    1249 
    1250 #ifdef MODEL_V1_COMPATIBLE
    1251 void Model::calcOldNeuroCount()
    1252 {
    1253         if (oldneurocount>=0) return;
    1254         oldneurocount=reorderToOldLayout();
    1255 }
    1256 
    1257 int Model::old_getNeuroCount()
    1258 { calcOldNeuroCount();
    1259 return oldneurocount;}
    1260 
    1261 Neuro* Model::old_getNeuro(int i)
    1262 {calcOldNeuroCount();
    1263 return (i<oldneurocount)? (Neuro*)getNeuro(i) : (Neuro*)0;
    1264 }
    1265 
    1266 int Model::old_findNeuro(Neuro* n)
    1267 {calcOldNeuroCount();
    1268 return findNeuro(n);}
    1269 
    1270 Neuro *Model::old_addNewNeuro()
    1271 {
    1272         int count=old_getNeuroCount();
    1273         Neuro *nu=addNewNeuro();
    1274         nu->setClassName("N");
    1275         moveNeuro(nu->refno,oldneurocount);
    1276         oldneurocount=count+1;
    1277         return (Neuro*)nu;
    1278 }
    1279 #endif
    1280 
    12811111///////////////////////
    12821112
  • cpp/frams/model/model.h

    r660 r726  
    7575        SList parts, joints, neurons;
    7676        char partmappingchanged;
    77 
    78 #ifdef MODEL_V1_COMPATIBLE
    79         /** NeuroCount value.
    80                 compatibility_neurocount = -1 if its value is unknown and the layout is not compatible
    81                 @see reorderToOldLayout()
    82                 */
    83         int oldneurocount;
    84         char oldconnections;
    85 
    86         /** calculate oldNeuroCount */
    87         void calcOldNeuroCount();
    88         /** some new calls can invalidate old compatibility data */
    89         void invalidateOldNeuroCount() {oldneurocount=-1;}
    90         /**
    91            for compatibility with old NeuroClass the layout of Neurons
    92            is changed when old 'Neuro' accessing methods are in use.
    93            Neurons:
    94            0 .. compatibility_neurocount-1                : old Neurons (class "N")
    95            compatibility_neurocount .. neurons.size()-1   : other units
    96            */
    97         int reorderToOldLayout();
    98 
    99         /** check if compatibility should be preserved */
    100         int hasOldNeuroLayout() {return oldneurocount>=0;}
    101 
    102         /** add inputs to the old "-" units.
    103                 @return 1=ok, 0=invalid input detected  */
    104         int addOldConnectionsInputs();
    105 #endif
    10677
    10778        void internalCopy(const Model &mod);
     
    284255        void clear();
    285256
     257        enum ItemType { UnknownType,ModelType,PartType,JointType,NeuronType,NeuronConnectionType };
     258        static ItemType itemTypeFromLinePrefix(const char* line);
    286259        /** Execute single line of <B>f0</B> genotype.
    287260                Return value is non-negative reference number of the created item,
     
    291264                @param srcrange source genotype range which will be mapped to this element
    292265                */
    293         int singleStepBuild(const SString &singleline, int line_num, const MultiRange* srcrange = NULL);
     266        int addFromString(ItemType item_type,const SString &singleline, int line_num, const MultiRange* srcrange = NULL);
    294267        /** Execute single line of <B>f0</B> genotype - compatiblity variant */
    295         int singleStepBuild(const SString &singleline, const MultiRange* srcrange = NULL);
     268        int addFromString(ItemType item_type,const SString &singleline, const MultiRange* srcrange = NULL);
    296269        /** Execute single line of <B>f0</B> genotype - low level variant, used by Model::build(), error messages returned as string instead of calling logger */
    297         int singleStepBuildNoLog(const SString &singleline, SString& error_message, const MultiRange* srcrange = 0);
     270        int addFromStringNoLog(ItemType item_type,const SString &singleline, SString& error_message, const MultiRange* srcrange = 0);
    298271
    299272        /// separate build stages (for future use)
     
    346319        /// you can access neurons 0 .. getNeuroCount()-1.
    347320        Neuro *getNeuro(int i) const;
    348 
    349 #ifdef MODEL_V1_COMPATIBLE
    350         /* compatibility calls for accessing old Neuro objects */
    351 
    352         /// @deprecated Neuro class will be removed soon
    353         /// @see compat
    354         int old_getNeuroCount();
    355         /// you can access neurons 0 .. getNeuroCount()-1.
    356         /// @deprecated Neuro class will be removed soon
    357         Neuro *old_getNeuro(int i);
    358         /// @see addNewNeuro, addNeuro
    359         /// @deprecated Neuro class will be removed soon
    360         Neuro *old_addNewNeuro();
    361         /// @return neuro index or -1 if not found
    362         /// @deprecated Neuro class will be removed soon
    363         int old_findNeuro(Neuro* n);
    364 #endif
    365321
    366322        /** create new Part and add it to the model. @see addPart()  */
  • cpp/frams/model/modelparts.cpp

    r714 r726  
    193193Neuro::Neuro(double _state, double _inertia, double _force, double _sigmo)
    194194        :PartBase(getDefaultStyle()), state(_state)
    195 #ifdef MODEL_V1_COMPATIBLE
    196         ,inertia(_inertia),force(_force),sigmo(_sigmo)
    197 #endif
    198 {
    199 #ifdef MODEL_V1_COMPATIBLE
    200         olditems=0;
    201 #endif
     195{
    202196        flags = 0;
    203197        myclass = 0;
     
    217211        parent = 0; part = 0; joint = 0;
    218212        parentcount = 0;
    219 #ifdef MODEL_V1_COMPATIBLE
    220         olditems=0;
    221 #endif
    222213        flags = 0;
    223214        part_refno = -1; joint_refno = -1;
     
    227218Neuro::~Neuro()
    228219{
    229 #ifdef MODEL_V1_COMPATIBLE
    230         if (olditems) delete olditems;
    231 #endif
    232220        int i;
    233221        for (i = 0; i < inputs.size(); i++)
     
    281269{
    282270        refno = src.refno;
    283 #ifdef MODEL_V1_COMPATIBLE
    284         neuro_refno=-1;
    285         conn_refno=-1;
    286         force=src.force;
    287         sigmo=src.sigmo;
    288         inertia=src.inertia;
    289         weight=src.weight;
    290         olditems=0;
    291 #endif
    292271        state = src.state;
    293272        part_refno = -1;
     
    567546#endif
    568547}
    569 
    570 /////// old items
    571 #ifdef MODEL_V1_COMPATIBLE
    572 void OldItems::buildlist()
    573 { // guaranteed to work only for old NN layouts
    574         // (neurons,neuro connections, old receptors and effectors)
    575         if (listok) return;
    576         // inputs can contain both neuroitem connections (details="") and direct neuron references (details="N")
    577         // in OldItems we create neuroitems freom direct references
    578         for(int i=0;i<neuro.getInputCount();i++)
    579         {
    580                 float w;
    581                 Neuro *n=neuro.getInput(i,w);
    582                 if (n->isOldNeuron())
    583                 {
    584                         Neuro *ni=new Neuro();
    585                         ni->setClassName("-");
    586                         ni->weight=w;
    587                         ni->neuro_refno=neuro.refno;
    588                         ni->conn_refno=n->refno;
    589                         items+=ni;
    590                         syntitems+=ni;
    591                 }
    592                 else
    593                 {
    594                         items+=n;
    595                         n->weight=w;
    596                 }
    597         }
    598         SList outputs;
    599         neuro.findOutputs(outputs);
    600         FOREACH(Neuro*,n,outputs)
    601         {
    602                 if (n->isNNConnection() || n->isOldNeuron())
    603                         outputs-=n;
    604         }
    605         items+=outputs;
    606         listok=1;
    607 }
    608 
    609 void OldItems::freelist()
    610 {
    611         FOREACH(Neuro*,n,syntitems)
    612                 delete n;
    613         syntitems.clear();
    614         items.clear();
    615         listok=0;
    616 }
    617 
    618 int OldItems::getItemCount()
    619 {
    620         buildlist();
    621         return items.size();
    622 }
    623 
    624 NeuroItem *OldItems::getNeuroItem(int i)
    625 {
    626         buildlist();
    627         return (NeuroItem*)items(i);
    628 }
    629 
    630 NeuroItem *OldItems::addNewNeuroItem()
    631 {
    632         Neuro *nu=neuro.getModel().addNewNeuro();
    633         nu->setClassName("-");
    634         if (listok) items+=nu;
    635         neuro.addInput(nu);
    636         return (NeuroItem*)nu;
    637 }
    638 
    639 int OldItems::findNeuroItem(NeuroItem *ni)
    640 {
    641         buildlist();
    642         return items.find((void*)ni);
    643 }
    644 #endif
    645548
    646549///////////////////////////////////////
     
    801704}
    802705
    803 
    804706/////////////////////////////////////////////////////////////////
    805 /////////////////////////////////////////////////////////////////
    806707
    807708#include F0_CLASSES_FILE
    808 
    809 #ifdef MODEL_V1_COMPATIBLE
    810 
    811 #define FIELDSTRUCT Neuro
    812 ParamEntry f0_old_neuro_tab[]=
    813 {
    814         {"Connections",2,6,"n",},
    815         {"Other properties",},
    816         {"p",0,0,"part ref#","d",FIELD(part_refno),},
    817         {"j",0,0,"joint ref#","d",FIELD(joint_refno),},
    818         {"s",1,0,"state","f",FIELD(state),},
    819         {"in",1,0,"Inertia","f",FIELD(inertia),},
    820         {"fo",1,0,"Force","f",FIELD(force),},
    821         {"si",1,0,"Sigmoid","f",FIELD(sigmo),},
    822         {0,0,0,},
    823 };
    824 #undef FIELDSTRUCT
    825 
    826 #define FIELDSTRUCT NeuroItem
    827 ParamEntry f0_neuroitem_paramtab[]=
    828 {
    829         {"Connections",3,12,"ni",},
    830         {"Geometry",},
    831         {"Other",},
    832         {"n",0,0,"neuron ref#","d",FIELD(neuro_refno),},
    833         {"c",2,0,"connection ref#","d",FIELD(conn_refno),},
    834         {"w",2,0,"connection weight","f",FIELD(weight),},
    835         {"p",0,0,"part ref#","d",FIELD(part_refno),},
    836         {"j",0,0,"joint ref#","d",FIELD(joint_refno),},
    837         {"px",1,0,"position.x","f",FIELD(pos.x),},
    838         {"py",1,0,"position.y","f",FIELD(pos.y),},
    839         {"pz",1,0,"position.z","f",FIELD(pos.z),},
    840         {"rx",1,0,"rotation.x","f",FIELD(rot.x),},
    841         {"ry",1,0,"rotation.y","f",FIELD(rot.y),},
    842         {"rz",1,0,"rotation.z","f",FIELD(rot.z),},
    843         {"d",2,0,"item details","s",GETSET(details),},
    844         {0,0,0,},
    845 };
    846 #undef FIELDSTRUCT
    847 #endif
    848709
    849710////////////////////////////////////////
  • cpp/frams/model/modelparts.h

    r714 r726  
    1919
    2020#include <stdio.h>
    21 
    22 //#define MODEL_V1_COMPATIBLE
    2321
    2422class Model;
     
    289287class Neuro;
    290288
    291 #ifdef MODEL_V1_COMPATIBLE
    292 class NeuroItem;
    293 
    294 /** for compatibility with old Neuro/NeuroItem  */
    295 class OldItems
    296 {
    297         Neuro &neuro;
    298         SList syntitems; ///< to be deleted
    299         SList items;
    300         int listok;
    301 public:
    302         OldItems(Neuro &n):neuro(n),listok(0) {}
    303         ~OldItems() {freelist();}
    304         void buildlist();
    305         void freelist();
    306 
    307         int getItemCount();
    308         NeuroItem *getNeuroItem(int i);
    309         NeuroItem *addNewNeuroItem();
    310         int findNeuroItem(NeuroItem *n);
    311 };
    312 #endif
    313 
    314289/** Single processing unit in Framsticks NN.  */
    315290class Neuro : public PartBase
     
    482457        static int getClassIndex(const NeuroClass*nc);
    483458
    484 #ifdef MODEL_V1_COMPATIBLE
    485         friend class OldItems;
    486         paInt neuro_refno; ///< parent ref# (called neuro_refno for compatibility with old Neuro class), @see moredata
    487         paInt conn_refno; ///< the other neuron ref# in N-N connections, can be used by some other items
    488         double weight; ///< weight of the N-N connection and (all?) receptors
    489         double inertia,force,sigmo; //!!!
    490 
    491         /** @deprecated provided only for compatibility with old Neuro/NeuroItem classes.
    492                 use getInputCount() instead. @sa getInputCount() */
    493         int getItemCount() {return oldItems().getItemCount();}
    494 
    495         /** @deprecated provided only for compatibility with old Neuro/NeuroItem classes.
    496                 use getInput() instead. @sa getInput() */
    497         NeuroItem* getNeuroItem(int i) {return oldItems().getNeuroItem(i);}
    498 #endif
    499 
    500 protected:
    501 #ifdef MODEL_V1_COMPATIBLE
    502         /** old Neuro compatibility */
    503         OldItems* olditems;
    504         OldItems& oldItems() {if (!olditems) olditems=new OldItems(*this); return *olditems;}
    505         void invalidateOldItems() {if (olditems) olditems->freelist();}
    506 #endif
    507 
    508 public:
    509 
    510459        // not really private, but you should not access those directly
    511460        double state;
     
    522471};
    523472
    524 #ifdef MODEL_V1_COMPATIBLE
    525 class NeuroItem;
    526 
    527 /// for compatibility with old NeuroItem class.
    528 class NeuroItem: public Neuro
    529 {
    530 public:
    531         NeuroItem() {}
    532 };
    533 #endif
    534 
    535473class NeuroExt : public Neuro
    536474{
  • cpp/frams/neuro/impl/neuroimpl-simple.cpp

    r286 r726  
    1212int NI_StdNeuron::lateinit()
    1313{
    14 #ifdef MODEL_V1_COMPATIBLE
    15 if (inertia<0) inertia=neuro->inertia;
    16 if (force<0) force=neuro->force;
    17 if (sigmo>1e9) sigmo=neuro->sigmo;
    18 #endif
    1914istate=newstate+neuro->state; // neuro->state -> random initialization
    2015calcOutput();
  • cpp/frams/neuro/impl/neuroimpl-simple.h

    r286 r726  
    1919  public:
    2020double inertia,force,sigmo;
    21 NI_StdNeuron():velocity(0)
    22 #ifdef MODEL_V1_COMPATIBLE
    23         ,inertia(-1),force(-1),sigmo(1e10)// illegal values, will be adjusted in lateinit()
    24 #else
    25         ,inertia(0),force(0),sigmo(0)
    26 #endif
     21NI_StdNeuron():velocity(0),inertia(0),force(0),sigmo(0)
    2722        {paramentries=NI_StdNeuron_tab;}
    2823NeuroImpl* makeNew(){return new NI_StdNeuron();} // for NeuroFactory
Note: See TracChangeset for help on using the changeset viewer.