source: cpp/frams/genetics/f4/conv_f4.cpp @ 774

Last change on this file since 774 was 774, checked in by Maciej Komosinski, 6 years ago

Removed unused "state" field (issue number 19), improved docs (issues number 20,21,23), code formatting (issue number 24) [closes #62]

  • Property svn:eol-style set to native
File size: 8.8 KB
Line 
1// This file is a part of Framsticks SDK.  http://www.framsticks.com/
2// Copyright (C) 1999-2017  Maciej Komosinski and Szymon Ulatowski.
3// See LICENSE.txt for details.
4
5// Copyright (C) 1999,2000  Adam Rotaru-Varga (adam_rotaru@yahoo.com), GNU LGPL
6// Copyright (C) since 2001 Maciej Komosinski
7// 2018, Grzegorz Latosinski, added support for new API for neuron types and their properties
8
9#include "conv_f4.h"
10#include <common/log.h>
11#include "../oper_fx.h" //for GENOPER_OK constant
12
13#ifdef DMALLOC
14#include <dmalloc.h>
15#endif
16
17
18GenoConv_f40::GenoConv_f40()
19{
20        name = "Developmental encoding";
21        in_format = '4';
22        out_format = '0';
23        mapsupport = 1;
24}
25
26
27SString GenoConv_f40::convert(SString &in, MultiMap *map, bool using_checkpoints)
28{
29        int res;
30        f4_Model *model = new f4_Model();
31        res = model->buildFromF4(in, using_checkpoints);
32        if (GENOPER_OK != res) return SString();  // oops
33        if (NULL != map)
34                // generate to-f0 conversion map
35                model->getCurrentToF0Map(*map);
36        SString out = model->getF0Geno().getGenes();
37        delete model;
38        return out;
39}
40
41
42GenoConv_F41_TestOnly::GenoConv_F41_TestOnly()
43{
44        name = "test-only approximate f4 -> f1 converter";
45        in_format = '4';
46        out_format = '1';
47        mapsupport = 0;
48        info = "This is for testing.  Do not use in production! (adam)";
49}
50
51
52SString GenoConv_F41_TestOnly::convert(SString &in, MultiMap *map, bool using_checkpoints)
53{
54        int res;
55        f4_Model *model = new f4_Model();
56        res = model->buildFromF4(in, using_checkpoints);
57        if (GENOPER_OK != res) return SString();  // oops
58        SString out;
59        model->toF1Geno(out);
60        delete model;
61        return out;
62}
63
64
65f4_Model::f4_Model() : Model()
66{
67        cells = NULL;
68}
69
70f4_Model::~f4_Model()
71{
72        if (cells) delete cells;
73}
74
75int f4_Model::buildFromF4(SString &geno, bool using_checkpoints)
76{
77        int i;
78
79        error = GENOPER_OK;
80        errorpos = -1;
81
82        // build cells, and simulate
83        if (cells) delete cells;
84        cells = new f4_Cells(geno, 0);
85        if (GENOPER_OK != cells->geterror())
86        {
87                error = cells->geterror();
88                errorpos = cells->geterrorpos();
89                //delete cells;
90                return error;
91        }
92
93        cells->simulate();
94        if (GENOPER_OK != cells->geterror())
95        {
96                error = cells->geterror();
97                errorpos = cells->geterrorpos();
98                return error;
99        }
100
101        // reset recursive traverse flags
102        for (i = 0; i < cells->nc; i++)
103                cells->C[i]->recProcessedFlag = 0;
104
105        open(using_checkpoints); // begin model build
106
107        // process every cell
108        int res;
109        for (i = 0; i < cells->nc; i++)
110        {
111                res = buildModelRec(cells->C[i]);
112                if (res)
113                {
114                        logMessage("f4_Model", "buildModelRec", 2, "Error in building Model");
115                        error = res;
116                        break;
117                }
118        }
119
120        res = close();
121        if (0 == res) // invalid
122                error = -10;
123
124        return error;
125}
126
127
128f4_Cell* f4_Model::getStick(f4_Cell *C)
129{
130        if (T_STICK4 == C->type) return C;
131        if (NULL != C->dadlink)
132                return getStick(C->dadlink);
133        // we have no more dadlinks, find any stick
134        for (int i = 0; i < cells->nc; i++)
135                if (cells->C[i]->type == T_STICK4)
136                        return cells->C[i];
137        // none!
138        logMessage("f4_Model", "getStick", 2, "Not a single stick");
139        return NULL;
140}
141
142
143/// updated by Macko to follow new SDK standards (no more neuroitems)
144int f4_Model::buildModelRec(f4_Cell *C)
145{
146        int partidx;
147        int j, res;
148        MultiRange range;
149
150        if (C->recProcessedFlag)
151                // already processed
152                return 0;
153
154        // mark it processed
155        C->recProcessedFlag = 1;
156
157        // make sure parent is a stick
158        if (NULL != C->dadlink)
159                if (C->dadlink->type != T_STICK4)
160                {
161                C->dadlink = getStick(C->dadlink);
162                }
163
164        // make sure its parent is processed first
165        if (NULL != C->dadlink)
166        {
167                res = buildModelRec(C->dadlink);
168                if (res) return res;
169        }
170
171        char tmpLine[100];
172
173        range = C->genoRange;
174        if (C->type == T_STICK4)
175        {
176                int jj_p1_refno;  // save for later
177                // first end is connected to dad, or new
178                if (C->dadlink == NULL)
179                {
180                        // new part object for firstend
181                        // coordinates are left to be computed by Model
182                        sprintf(tmpLine, "fr=%g,ing=%g,as=%g",
183                                /*1.0/C->P.mass,*/ C->P.friction, C->P.ingestion, C->P.assimilation
184                                //C->firstend.x, C->firstend.y, C->firstend.z
185                                );
186                        partidx = addFromString(PartType, tmpLine, &range);
187                        if (partidx < 0) return -1;
188                        this->checkpoint();
189                        jj_p1_refno = partidx;
190                }
191                else {
192                        // adjust mass/vol of first endpoint
193                        jj_p1_refno = C->dadlink->p2_refno;
194                        Part *p1 = getPart(jj_p1_refno);
195                        p1->mass += 1.0;
196                        //      p1->volume += 1.0/C->P.mass;
197                }
198                // new part object for lastend
199                sprintf(tmpLine, "fr=%g,ing=%g,as=%g",
200                        //C->lastend.x, C->lastend.y, C->lastend.z
201                        /*"vol=" 1.0/C->P.mass,*/ C->P.friction, C->P.ingestion, C->P.assimilation
202                        );
203                partidx = addFromString(PartType, tmpLine, &range);
204                if (partidx < 0) return -2;
205                this->checkpoint();
206                C->p2_refno = partidx;
207
208                // new joint object
209                // check that the part references are valid
210                int jj_p2_refno = C->p2_refno;
211                if ((jj_p1_refno < 0) || (jj_p1_refno >= getPartCount())) return -11;
212                if ((jj_p2_refno < 0) || (jj_p2_refno >= getPartCount())) return -12;
213                sprintf(tmpLine, "p1=%ld,p2=%ld,dx=%g,dy=0,dz=0,rx=%g,ry=0,rz=%g"\
214                        ",stam=%g",
215                        jj_p1_refno, jj_p2_refno,
216                        // relative position -- always (len, 0, 0), along the stick
217                        // this is optional!
218                        C->P.length,
219                        // relative rotation
220                        C->xrot, C->zrot,
221                        //C->P.ruch,   // rotstif
222                        C->P.stamina
223                        );
224                partidx = addFromString(JointType, tmpLine, &range);
225                if (partidx < 0) return -13;
226                this->checkpoint();
227                C->joint_refno = partidx;
228        }
229
230        if (C->type == T_NEURON4) ///<this case was updated by MacKo
231        {
232                const char* nclass = C->neuclass->name.c_str();
233                int partno, jointno;
234                if (C->neuclass->getPreferredLocation() == 0)
235                {
236                        if (strcmp(nclass, "N") == 0)
237                        {
238                                partno = C->dadlink->p2_refno;
239                                if ((partno < 0) || (partno >= getPartCount())) return -21;
240                                else sprintf(tmpLine, "p=%ld,d=\"N:in=%g,fo=%g,si=%g\"", partno, C->inertia, C->force, C->sigmo);
241                        }
242                        else
243                        {
244                                sprintf(tmpLine, "d=\"%s\"", nclass);
245                        }
246                        partidx = addFromString(NeuronType, tmpLine, &range);
247                        if (partidx < 0) return -22;
248                        this->checkpoint();
249                        C->neuro_refno = partidx;
250                }
251                else if (C->neuclass->getPreferredLocation() == 1) // attached to Part or have no required attachment - also part
252                {
253                        partno = C->dadlink->p2_refno;
254                        if ((partno < 0) || (partno >= getPartCount())) return -21;
255                        if (strcmp(nclass, "N") == 0)
256                        {
257                                sprintf(tmpLine, "d=\"N:in=%g,fo=%g,si=%g\"", partno, C->inertia, C->force, C->sigmo);
258                        }
259                        else
260                        {
261                                sprintf(tmpLine, "p=%ld,d=\"%s\"", partno, nclass);
262                        }
263                        partidx = addFromString(NeuronType, tmpLine, &range);
264                        if (partidx < 0) return -22;
265                        this->checkpoint();
266                        C->neuro_refno = partidx;
267                }
268                else // attached to Joint
269                {
270                        jointno = C->dadlink->joint_refno;
271                        sprintf(tmpLine, "n:j=%d,d=\"%s\"", jointno, nclass);
272                        partidx = addFromString(NeuronType, tmpLine, &range);
273                        if (partidx < 0) return -32;
274                        this->checkpoint();
275                }
276                C->neuro_refno = partidx;
277                int n_refno = C->neuro_refno;
278
279                if ((strcmp(nclass,"N") == 0) && C->ctrl)
280                {
281                        if (1 == C->ctrl)
282                                sprintf(tmpLine, "j=%d,d=\"@:p=%g\"", C->dadlink->joint_refno, C->P.muscle_power);
283                        else
284                                sprintf(tmpLine, "j=%d,d=\"|:p=%g,r=%g\"", C->dadlink->joint_refno, C->P.muscle_power, C->mz);
285                        partidx = addFromString(NeuronType, tmpLine, &range);
286                        if (partidx < 0) return -32;
287                        sprintf(tmpLine, "%d,%d", partidx, n_refno);
288                        if (addFromString(NeuronConnectionType, tmpLine, &range) < 0) return -33;
289                        this->checkpoint();
290                }
291
292                for (j = 0; j < C->nolink; j++)
293                {
294                        if (NULL != C->links[j]->from)
295                                buildModelRec(C->links[j]->from);
296
297                        tmpLine[0] = 0;
298                        if (C->links[j]->from == NULL)
299                        {
300                                const char* nclass = C->links[j]->t.c_str();
301                                char* temp = (char*)C->links[j]->t.c_str();
302                                NeuroClass *sensortest = GenoOperators::parseNeuroClass(temp);
303                                //backward compatibility for G*TS
304                                if (C->links[j]->t == "*" || C->links[j]->t == "S" || C->links[j]->t == "T")
305                                {
306                                        sprintf(tmpLine, "p=%d,d=\"%s\"", partno, nclass);
307                                }
308                                else if (C->links[j]->t == "G")
309                                {
310                                        jointno = C->dadlink->joint_refno;
311                                        sprintf(tmpLine, "j=%d,d=\"%s\"", jointno, nclass);
312                                }                               
313                                else if (sensortest->getPreferredLocation() == 0)
314                                {
315                                        sprintf(tmpLine, "d=\"%s\"",nclass);
316                                }
317                                else if (sensortest->getPreferredLocation() == 1)
318                                {
319                                        sprintf(tmpLine, "p=%d,d=\"%s\"", partno, nclass);
320                                }
321                                else
322                                {
323                                        jointno = C->dadlink->joint_refno;
324                                        sprintf(tmpLine, "j=%d,d=\"%s\"", jointno, nclass);
325                                }
326
327                        }
328                        int from = -1;
329                        if (tmpLine[0]) //input from receptor
330                        {
331                                from = addFromString(NeuronType, tmpLine, &range);
332                                if (from < 0) return -34;
333                                this->checkpoint();
334                        } /*could be 'else'...*/
335
336                        if (NULL != C->links[j]->from) // input from another neuron
337                                from = C->links[j]->from->neuro_refno;
338                        if (from >= 0)
339                        {
340                                sprintf(tmpLine, "%d,%d,%g", n_refno, from, C->links[j]->w);
341                                if (addFromString(NeuronConnectionType, tmpLine, &range) < 0) return -35;
342                                this->checkpoint();
343                        }
344                }
345        }
346        return 0;
347}
348
349
350void f4_Model::toF1Geno(SString &out)
351{
352        cells->toF1Geno(out);
353}
Note: See TracBrowser for help on using the repository browser.