source: cpp/f4/conv_f4.cpp @ 100

Last change on this file since 100 was 4, checked in by Maciej Komosinski, 16 years ago

added f4, a genetic representation that describes development of an organism

File size: 8.1 KB
Line 
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 */
24
25#include "conv_f4.h"
26#include "framsg.h"
27#include "geno_fx.h" //for GENOPER_OK constant
28
29#ifdef DMALLOC
30#include <dmalloc.h>
31#endif
32
33
34GenoConv_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
44SString 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;
56}
57
58
59GenoConv_F41_TestOnly::GenoConv_F41_TestOnly()
60{
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)";
66}
67
68
69SString GenoConv_F41_TestOnly::convert(SString &in, MultiMap * map)
70{
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;
79}
80
81
82f4_Model::f4_Model() : Model()
83{
84  cells = NULL;
85}
86
87f4_Model::~f4_Model()
88{
89  if (cells) delete cells;
90}
91
92int f4_Model::buildFromF4(SString &geno)
93{
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;
142}
143
144
145f4_Cell * f4_Model::getStick(f4_Cell * C)
146{
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;
157}
158
159
160/// updated by Macko to follow new GDK standards (no more neuroitems)
161int f4_Model::buildModelRec(f4_Cell * C)
162{
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;
296}
297
298
299void f4_Model::toF1Geno(SString &out)
300{
301  cells->toF1Geno(out);
302}
303
304
Note: See TracBrowser for help on using the repository browser.