Ignore:
Timestamp:
03/25/14 03:22:37 (10 years ago)
Author:
Maciej Komosinski
Message:

Updated f4 sources and made f4 available for GDK demo apps

Location:
cpp/frams/genetics/f4
Files:
1 added
1 moved

Legend:

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

    r193 r196  
    1 /**
    2  *  conv_f4.cpp - f4 genotype functions.
    3  *
    4  *  f4genotype - f4 format genotype conversions for Framsticks
    5  *
    6  *  Copyright (C) 1999,2000  Adam Rotaru-Varga (adam_rotaru@yahoo.com)
    7  *  Copyright (C) 2001-2003  Maciej Komosinski
    8  *
    9  *  This library is free software; you can redistribute it and/or
    10  *  modify it under the terms of the GNU Lesser General Public
    11  *  License as published by the Free Software Foundation; either
    12  *  version 2.1 of the License, or (at your option) any later version.
    13  *
    14  *  This library is distributed in the hope that it will be useful,
    15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
    16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    17  *  Lesser General Public License for more details.
    18  *
    19  *  You should have received a copy of the GNU Lesser General Public
    20  *  License along with this library; if not, write to the Free Software
    21  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
    22  *
    23  */
     1// This file is a part of the Framsticks GDK.
     2// Copyright (C) 2002-2014  Maciej Komosinski and Szymon Ulatowski.  See LICENSE.txt for details.
     3// Refer to http://www.framsticks.com/ for further information.
     4
     5// Copyright (C) 1999,2000  Adam Rotaru-Varga (adam_rotaru@yahoo.com), GNU LGPL
     6// Copyright (C) since 2001 Maciej Komosinski
    247
    258#include "conv_f4.h"
    26 #include "framsg.h"
    27 #include "geno_fx.h" //for GENOPER_OK constant
     9#include <common/framsg.h>
     10#include "../oper_fx.h" //for GENOPER_OK constant
    2811
    2912#ifdef DMALLOC
     
    3215
    3316
    34 GenoConv_F40::GenoConv_F40()
    35 {
    36   name = "f4 converter";
    37   in_format  = '4';
    38   out_format = '0';
    39   mapsupport = 1;
    40   info = "Developmental encoding";
    41 }
    42 
    43 
    44 SString GenoConv_F40::convert(SString &in, MultiMap * map)
    45 {
    46   int res;
    47   f4_Model * model = new f4_Model();
    48   res = model->buildFromF4(in);
    49   if (GENOPER_OK != res) return SString();  // oops
    50   if (NULL != map)
    51     // generate to-f0 conversion map
    52     model->getCurrentToF0Map(*map);
    53   SString out = model->getF0Geno().getGene();
    54   delete model;
    55   return out;
     17GenoConv_f40::GenoConv_f40()
     18{
     19        name = "Developmental encoding";
     20        in_format = '4';
     21        out_format = '0';
     22        mapsupport = 1;
     23}
     24
     25
     26SString GenoConv_f40::convert(SString &in, MultiMap * map)
     27{
     28        int res;
     29        f4_Model * model = new f4_Model();
     30        res = model->buildFromF4(in);
     31        if (GENOPER_OK != res) return SString();  // oops
     32        if (NULL != map)
     33                // generate to-f0 conversion map
     34                model->getCurrentToF0Map(*map);
     35        SString out = model->getF0Geno().getGene();
     36        delete model;
     37        return out;
    5638}
    5739
     
    5941GenoConv_F41_TestOnly::GenoConv_F41_TestOnly()
    6042{
    61   name = "test-only approximate f4 -> f0 converter";
    62   in_format = '4';
    63   out_format = '1';
    64   mapsupport = 0;
    65   info = "This is for testing.  Do not use in production! (adam)";
     43        name = "test-only approximate f4 -> f1 converter";
     44        in_format = '4';
     45        out_format = '1';
     46        mapsupport = 0;
     47        info = "This is for testing.  Do not use in production! (adam)";
    6648}
    6749
     
    6951SString GenoConv_F41_TestOnly::convert(SString &in, MultiMap * map)
    7052{
    71   int res;
    72   f4_Model * model = new f4_Model();
    73   res = model->buildFromF4(in);
    74   if (GENOPER_OK != res) return SString();  // oops
    75   SString out;
    76   model->toF1Geno(out);
    77   delete model;
    78   return out;
     53        int res;
     54        f4_Model * model = new f4_Model();
     55        res = model->buildFromF4(in);
     56        if (GENOPER_OK != res) return SString();  // oops
     57        SString out;
     58        model->toF1Geno(out);
     59        delete model;
     60        return out;
    7961}
    8062
     
    8264f4_Model::f4_Model() : Model()
    8365{
    84   cells = NULL;
     66        cells = NULL;
    8567}
    8668
    8769f4_Model::~f4_Model()
    8870{
    89   if (cells) delete cells;
     71        if (cells) delete cells;
    9072}
    9173
    9274int f4_Model::buildFromF4(SString &geno)
    9375{
    94   int i;
    95 
    96   error = GENOPER_OK;
    97   errorpos = -1;
    98 
    99   // build cells, and simulate
    100   if (cells) delete cells;
    101   cells = new f4_Cells(geno, 0);
    102   if (GENOPER_OK != cells->geterror())
    103   {
    104     error = cells->geterror();
    105     errorpos = cells->geterrorpos();
    106     //delete cells;
    107     return error;
    108   }
    109 
    110   cells->simulate();
    111   if (GENOPER_OK != cells->geterror())
    112   {
    113     error = cells->geterror();
    114     errorpos = cells->geterrorpos();
    115     return error;
    116   }
    117 
    118   // reset recursive traverse flags
    119   for(i=0; i<cells->nc; i++)
    120     cells->C[i]->recProcessedFlag = 0;
    121 
    122   open(); // begin model build
    123 
    124   // process every cell
    125   int res;
    126   for(i=0; i<cells->nc; i++)
    127   {
    128     res = buildModelRec(cells->C[i]);
    129     if (res)
    130     {
    131       FramMessage("f4_Model", "buildModelRec", "Error in building Model", 2);
    132       error = res;
    133       break;
    134     }
    135   }
    136 
    137   res = close();
    138   if (0 == res) // invalid
    139     error = -10;
    140 
    141   return error;
     76        int i;
     77
     78        error = GENOPER_OK;
     79        errorpos = -1;
     80
     81        // build cells, and simulate
     82        if (cells) delete cells;
     83        cells = new f4_Cells(geno, 0);
     84        if (GENOPER_OK != cells->geterror())
     85        {
     86                error = cells->geterror();
     87                errorpos = cells->geterrorpos();
     88                //delete cells;
     89                return error;
     90        }
     91
     92        cells->simulate();
     93        if (GENOPER_OK != cells->geterror())
     94        {
     95                error = cells->geterror();
     96                errorpos = cells->geterrorpos();
     97                return error;
     98        }
     99
     100        // reset recursive traverse flags
     101        for (i = 0; i < cells->nc; i++)
     102                cells->C[i]->recProcessedFlag = 0;
     103
     104        open(); // begin model build
     105
     106        // process every cell
     107        int res;
     108        for (i = 0; i < cells->nc; i++)
     109        {
     110                res = buildModelRec(cells->C[i]);
     111                if (res)
     112                {
     113                        FramMessage("f4_Model", "buildModelRec", "Error in building Model", 2);
     114                        error = res;
     115                        break;
     116                }
     117        }
     118
     119        res = close();
     120        if (0 == res) // invalid
     121                error = -10;
     122
     123        return error;
    142124}
    143125
     
    145127f4_Cell * f4_Model::getStick(f4_Cell * C)
    146128{
    147   if (T_STICK4 == C->type) return C;
    148   if (NULL != C->dadlink)
    149     return getStick(C->dadlink);
    150   // we have no more dadlinks, find any stick
    151   for (int i=0; i<cells->nc; i++)
    152     if (cells->C[i]->type == T_STICK4)
    153       return cells->C[i];
    154   // none!
    155   FramMessage("f4_Model", "getStick", "Not a single stick", 2);
    156   return NULL;
     129        if (T_STICK4 == C->type) return C;
     130        if (NULL != C->dadlink)
     131                return getStick(C->dadlink);
     132        // we have no more dadlinks, find any stick
     133        for (int i = 0; i < cells->nc; i++)
     134                if (cells->C[i]->type == T_STICK4)
     135                        return cells->C[i];
     136        // none!
     137        FramMessage("f4_Model", "getStick", "Not a single stick", 2);
     138        return NULL;
    157139}
    158140
     
    161143int f4_Model::buildModelRec(f4_Cell * C)
    162144{
    163   int partidx;
    164   int j, res;
    165   MultiRange range;
    166 
    167   if (C->recProcessedFlag)
    168     // already processed
    169     return 0;
    170 
    171   // mark it processed
    172   C->recProcessedFlag = 1;
    173 
    174   // make sure parent is a stick
    175   if (NULL != C->dadlink)
    176     if (C->dadlink->type != T_STICK4)
    177     {
    178       C->dadlink = getStick(C->dadlink);
    179     }
    180 
    181   // make sure its parent is processed first
    182   if (NULL != C->dadlink)
    183   {
    184     res = buildModelRec(C->dadlink);
    185     if (res) return res;
    186   }
    187 
    188   char tmpLine[100];
    189 
    190   range = C->genoRange;
    191   if (C->type == T_STICK4)
    192   {
    193     int jj_p1_refno;  // save for later
    194     // first end is connected to dad, or new
    195     if (C->dadlink == NULL)
    196     {
    197       // new part object for firstend
    198       // coordinates are left to be computed by Model
    199       sprintf(tmpLine, "p:m=1,fr=%g,ing=%g,as=%g",
    200         /*1.0/C->P.mass,*/ C->P.friction, C->P.ingest, C->P.assim
    201         //C->firstend.x, C->firstend.y, C->firstend.z
    202         );
    203       partidx = singleStepBuild(tmpLine, &range );
    204       if (partidx < 0) return -1;
    205       jj_p1_refno = partidx;
    206     } else {
    207       // adjust mass/vol of first endpoint
    208       jj_p1_refno = C->dadlink->p2_refno;
    209       Part * p1 = getPart(jj_p1_refno);
    210       p1->mass += 1.0;
    211 //      p1->volume += 1.0/C->P.mass;
    212     }
    213     // new part object for lastend
    214     sprintf(tmpLine, "p:m=1,fr=%g,ing=%g,as=%g",
    215         //C->lastend.x, C->lastend.y, C->lastend.z
    216         /*"vol=" 1.0/C->P.mass,*/ C->P.friction, C->P.ingest, C->P.assim
    217       );
    218     partidx = singleStepBuild(tmpLine, &range );
    219     if (partidx < 0) return -2;
    220     C->p2_refno = partidx;
    221 
    222     // new joint object
    223     // check that the part references are valid
    224     int jj_p2_refno = C->p2_refno;
    225     if ((jj_p1_refno < 0) || (jj_p1_refno >= getPartCount())) return -11;
    226     if ((jj_p2_refno < 0) || (jj_p2_refno >= getPartCount())) return -12;
    227     sprintf(tmpLine, "j:p1=%ld,p2=%ld,dx=%g,dy=0,dz=0,rx=%g,ry=0,rz=%g"\
    228       ",stam=%g",
    229       jj_p1_refno, jj_p2_refno,
    230       // relative position -- always (len, 0, 0), along the stick
    231       // this is optional!
    232       C->P.len,
    233       // relative rotation
    234       C->xrot, C->zrot,
    235       //C->P.ruch,   // rotstif
    236       C->P.odpor
    237     );
    238     partidx = singleStepBuild(tmpLine, &range );
    239     if (partidx < 0) return -13;
    240     C->joint_refno = partidx;
    241   }
    242 
    243   if (C->type == T_NEURON4) ///<this case was updated by MacKo
    244   {
    245     // new neuron object
    246     // it is linked to a part (not a joint!)
    247     int p_refno = C->dadlink->p2_refno;
    248     if ((p_refno < 0) || (p_refno >= getPartCount())) return -21;
    249     // joint_refno is currently not used
    250     sprintf(tmpLine, "n:p=%ld,d=\"N:in=%g,fo=%g,si=%g\"",
    251       p_refno,
    252       C->inertia, C->force, C->sigmo);
    253     partidx = singleStepBuild(tmpLine, &range );
    254     if (partidx < 0) return -22;
    255     C->neuro_refno = partidx;
    256     int n_refno = C->neuro_refno;
    257 
    258     if (C->ctrl)
    259     {
    260       if (1 == C->ctrl)
    261         sprintf(tmpLine, "n:j=%d,d=\"@:p=%g\"",   C->dadlink->joint_refno, C->P.ruch);
    262       else
    263         sprintf(tmpLine, "n:j=%d,d=\"|:p=%g,r=%g\"",C->dadlink->joint_refno, C->P.ruch, C->mz);
    264       partidx = singleStepBuild(tmpLine, &range );
    265       if (partidx < 0) return -32;
    266       sprintf(tmpLine, "c:%d,%d",partidx,n_refno);
    267       if (singleStepBuild(tmpLine, &range )<0) return -33;
    268     }
    269 
    270     for (j=0; j<C->nolink; j++)
    271     {
    272       if (NULL != C->links[j]->from)
    273         buildModelRec(C->links[j]->from);
    274 
    275       tmpLine[0]=0;
    276       if (1 == C->links[j]->t) sprintf(tmpLine, "n:p=%d,d=\"*\"",p_refno);
    277       if (2 == C->links[j]->t) sprintf(tmpLine, "n:j=%d,d=\"G\"", C->dadlink->joint_refno);
    278       if (3 == C->links[j]->t) sprintf(tmpLine, "n:p=%d,d=\"T\"",p_refno);
    279       if (4 == C->links[j]->t) sprintf(tmpLine, "n:p=%d,d=\"S\"",p_refno);
    280       int from=-1;
    281       if (tmpLine[0]) //input from receptor
    282       {
    283         from=singleStepBuild(tmpLine, &range );
    284         if (from<0) return -34;
    285       } /*could be 'else'...*/
    286       if (NULL != C->links[j]->from) // input from another neuron
    287          from=C->links[j]->from->neuro_refno;
    288       if (from>=0)
    289       {
    290         sprintf(tmpLine, "c:%d,%d,%g", n_refno,from,C->links[j]->w);
    291         if (singleStepBuild(tmpLine, &range ) < 0) return -35;
    292       }
    293     }
    294   }
    295   return 0;
     145        int partidx;
     146        int j, res;
     147        MultiRange range;
     148
     149        if (C->recProcessedFlag)
     150                // already processed
     151                return 0;
     152
     153        // mark it processed
     154        C->recProcessedFlag = 1;
     155
     156        // make sure parent is a stick
     157        if (NULL != C->dadlink)
     158                if (C->dadlink->type != T_STICK4)
     159                {
     160                        C->dadlink = getStick(C->dadlink);
     161                }
     162
     163        // make sure its parent is processed first
     164        if (NULL != C->dadlink)
     165        {
     166                res = buildModelRec(C->dadlink);
     167                if (res) return res;
     168        }
     169
     170        char tmpLine[100];
     171
     172        range = C->genoRange;
     173        if (C->type == T_STICK4)
     174        {
     175                int jj_p1_refno;  // save for later
     176                // first end is connected to dad, or new
     177                if (C->dadlink == NULL)
     178                {
     179                        // new part object for firstend
     180                        // coordinates are left to be computed by Model
     181                        sprintf(tmpLine, "p:m=1,fr=%g,ing=%g,as=%g",
     182                                /*1.0/C->P.mass,*/ C->P.friction, C->P.ingest, C->P.assim
     183                                //C->firstend.x, C->firstend.y, C->firstend.z
     184                                );
     185                        partidx = singleStepBuild(tmpLine, &range);
     186                        if (partidx < 0) return -1;
     187                        jj_p1_refno = partidx;
     188                }
     189                else {
     190                        // adjust mass/vol of first endpoint
     191                        jj_p1_refno = C->dadlink->p2_refno;
     192                        Part * p1 = getPart(jj_p1_refno);
     193                        p1->mass += 1.0;
     194                        //      p1->volume += 1.0/C->P.mass;
     195                }
     196                // new part object for lastend
     197                sprintf(tmpLine, "p:m=1,fr=%g,ing=%g,as=%g",
     198                        //C->lastend.x, C->lastend.y, C->lastend.z
     199                        /*"vol=" 1.0/C->P.mass,*/ C->P.friction, C->P.ingest, C->P.assim
     200                        );
     201                partidx = singleStepBuild(tmpLine, &range);
     202                if (partidx < 0) return -2;
     203                C->p2_refno = partidx;
     204
     205                // new joint object
     206                // check that the part references are valid
     207                int jj_p2_refno = C->p2_refno;
     208                if ((jj_p1_refno < 0) || (jj_p1_refno >= getPartCount())) return -11;
     209                if ((jj_p2_refno < 0) || (jj_p2_refno >= getPartCount())) return -12;
     210                sprintf(tmpLine, "j:p1=%ld,p2=%ld,dx=%g,dy=0,dz=0,rx=%g,ry=0,rz=%g"\
     211                        ",stam=%g",
     212                        jj_p1_refno, jj_p2_refno,
     213                        // relative position -- always (len, 0, 0), along the stick
     214                        // this is optional!
     215                        C->P.len,
     216                        // relative rotation
     217                        C->xrot, C->zrot,
     218                        //C->P.ruch,   // rotstif
     219                        C->P.odpor
     220                        );
     221                partidx = singleStepBuild(tmpLine, &range);
     222                if (partidx < 0) return -13;
     223                C->joint_refno = partidx;
     224        }
     225
     226        if (C->type == T_NEURON4) ///<this case was updated by MacKo
     227        {
     228                // new neuron object
     229                // it is linked to a part (not a joint!)
     230                int p_refno = C->dadlink->p2_refno;
     231                if ((p_refno < 0) || (p_refno >= getPartCount())) return -21;
     232                // joint_refno is currently not used
     233                sprintf(tmpLine, "n:p=%ld,d=\"N:in=%g,fo=%g,si=%g\"",
     234                        p_refno,
     235                        C->inertia, C->force, C->sigmo);
     236                partidx = singleStepBuild(tmpLine, &range);
     237                if (partidx < 0) return -22;
     238                C->neuro_refno = partidx;
     239                int n_refno = C->neuro_refno;
     240
     241                if (C->ctrl)
     242                {
     243                        if (1 == C->ctrl)
     244                                sprintf(tmpLine, "n:j=%d,d=\"@:p=%g\"", C->dadlink->joint_refno, C->P.ruch);
     245                        else
     246                                sprintf(tmpLine, "n:j=%d,d=\"|:p=%g,r=%g\"", C->dadlink->joint_refno, C->P.ruch, C->mz);
     247                        partidx = singleStepBuild(tmpLine, &range);
     248                        if (partidx < 0) return -32;
     249                        sprintf(tmpLine, "c:%d,%d", partidx, n_refno);
     250                        if (singleStepBuild(tmpLine, &range) < 0) return -33;
     251                }
     252
     253                for (j = 0; j < C->nolink; j++)
     254                {
     255                        if (NULL != C->links[j]->from)
     256                                buildModelRec(C->links[j]->from);
     257
     258                        tmpLine[0] = 0;
     259                        if (1 == C->links[j]->t) sprintf(tmpLine, "n:p=%d,d=\"*\"", p_refno);
     260                        if (2 == C->links[j]->t) sprintf(tmpLine, "n:j=%d,d=\"G\"", C->dadlink->joint_refno);
     261                        if (3 == C->links[j]->t) sprintf(tmpLine, "n:p=%d,d=\"T\"", p_refno);
     262                        if (4 == C->links[j]->t) sprintf(tmpLine, "n:p=%d,d=\"S\"", p_refno);
     263                        int from = -1;
     264                        if (tmpLine[0]) //input from receptor
     265                        {
     266                                from = singleStepBuild(tmpLine, &range);
     267                                if (from < 0) return -34;
     268                        } /*could be 'else'...*/
     269                        if (NULL != C->links[j]->from) // input from another neuron
     270                                from = C->links[j]->from->neuro_refno;
     271                        if (from >= 0)
     272                        {
     273                                sprintf(tmpLine, "c:%d,%d,%g", n_refno, from, C->links[j]->w);
     274                                if (singleStepBuild(tmpLine, &range) < 0) return -35;
     275                        }
     276                }
     277        }
     278        return 0;
    296279}
    297280
     
    299282void f4_Model::toF1Geno(SString &out)
    300283{
    301   cells->toF1Geno(out);
    302 }
    303 
    304 
     284        cells->toF1Geno(out);
     285}
Note: See TracChangeset for help on using the changeset viewer.