source: cpp/frams/genetics/f4/f4_conv.cpp @ 779

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

Unified file names of all files involved in genetic conversions and operations so that they start with "f<format>_"

  • 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 "f4_conv.h"
10#include <common/log.h>
11#include "../genooperators.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                C->p2_refno = partidx;
206
207                // new joint object
208                // check that the part references are valid
209                int jj_p2_refno = C->p2_refno;
210                if ((jj_p1_refno < 0) || (jj_p1_refno >= getPartCount())) return -11;
211                if ((jj_p2_refno < 0) || (jj_p2_refno >= getPartCount())) return -12;
212                sprintf(tmpLine, "p1=%ld,p2=%ld,dx=%g,dy=0,dz=0,rx=%g,ry=0,rz=%g"\
213                        ",stam=%g",
214                        jj_p1_refno, jj_p2_refno,
215                        // relative position -- always (len, 0, 0), along the stick
216                        // this is optional!
217                        C->P.length,
218                        // relative rotation
219                        C->xrot, C->zrot,
220                        //C->P.ruch,   // rotstif
221                        C->P.stamina
222                        );
223                partidx = addFromString(JointType, tmpLine, &range);
224                if (partidx < 0) return -13;
225                this->checkpoint();
226                C->joint_refno = partidx;
227        }
228
229        if (C->type == T_NEURON4) ///<this case was updated by MacKo
230        {
231                const char* nclass = C->neuclass->name.c_str();
232                int partno, jointno;
233                if (C->neuclass->getPreferredLocation() == 0)
234                {
235                        if (strcmp(nclass, "N") == 0)
236                        {
237                                partno = C->dadlink->p2_refno;
238                                if ((partno < 0) || (partno >= getPartCount())) return -21;
239                                else sprintf(tmpLine, "p=%ld,d=\"N:in=%g,fo=%g,si=%g\"", partno, C->inertia, C->force, C->sigmo);
240                        }
241                        else
242                        {
243                                sprintf(tmpLine, "d=\"%s\"", nclass);
244                        }
245                        partidx = addFromString(NeuronType, tmpLine, &range);
246                        if (partidx < 0) return -22;
247                        this->checkpoint();
248                        C->neuro_refno = partidx;
249                }
250                else if (C->neuclass->getPreferredLocation() == 1) // attached to Part or have no required attachment - also part
251                {
252                        partno = C->dadlink->p2_refno;
253                        if ((partno < 0) || (partno >= getPartCount())) return -21;
254                        if (strcmp(nclass, "N") == 0)
255                        {
256                                sprintf(tmpLine, "d=\"N:in=%g,fo=%g,si=%g\"", partno, C->inertia, C->force, C->sigmo);
257                        }
258                        else
259                        {
260                                sprintf(tmpLine, "p=%ld,d=\"%s\"", partno, nclass);
261                        }
262                        partidx = addFromString(NeuronType, tmpLine, &range);
263                        if (partidx < 0) return -22;
264                        this->checkpoint();
265                        C->neuro_refno = partidx;
266                }
267                else // attached to Joint
268                {
269                        jointno = C->dadlink->joint_refno;
270                        sprintf(tmpLine, "n:j=%d,d=\"%s\"", jointno, nclass);
271                        partidx = addFromString(NeuronType, tmpLine, &range);
272                        if (partidx < 0) return -32;
273                        this->checkpoint();
274                }
275                C->neuro_refno = partidx;
276                int n_refno = C->neuro_refno;
277
278                if ((strcmp(nclass,"N") == 0) && C->ctrl)
279                {
280                        if (1 == C->ctrl)
281                                sprintf(tmpLine, "j=%d,d=\"@:p=%g\"", C->dadlink->joint_refno, C->P.muscle_power);
282                        else
283                                sprintf(tmpLine, "j=%d,d=\"|:p=%g,r=%g\"", C->dadlink->joint_refno, C->P.muscle_power, C->mz);
284                        partidx = addFromString(NeuronType, tmpLine, &range);
285                        if (partidx < 0) return -32;
286                        sprintf(tmpLine, "%d,%d", partidx, n_refno);
287                        if (addFromString(NeuronConnectionType, tmpLine, &range) < 0) return -33;
288                        this->checkpoint();
289                }
290
291                for (j = 0; j < C->nolink; j++)
292                {
293                        if (NULL != C->links[j]->from)
294                                buildModelRec(C->links[j]->from);
295
296                        tmpLine[0] = 0;
297                        if (C->links[j]->from == NULL)
298                        {
299                                const char* nclass = C->links[j]->t.c_str();
300                                char* temp = (char*)C->links[j]->t.c_str();
301                                NeuroClass *sensortest = GenoOperators::parseNeuroClass(temp);
302                                //backward compatibility for G*TS
303                                if (C->links[j]->t == "*" || C->links[j]->t == "S" || C->links[j]->t == "T")
304                                {
305                                        sprintf(tmpLine, "p=%d,d=\"%s\"", partno, nclass);
306                                }
307                                else if (C->links[j]->t == "G")
308                                {
309                                        jointno = C->dadlink->joint_refno;
310                                        sprintf(tmpLine, "j=%d,d=\"%s\"", jointno, nclass);
311                                }                               
312                                else if (sensortest->getPreferredLocation() == 0)
313                                {
314                                        sprintf(tmpLine, "d=\"%s\"",nclass);
315                                }
316                                else if (sensortest->getPreferredLocation() == 1)
317                                {
318                                        sprintf(tmpLine, "p=%d,d=\"%s\"", partno, nclass);
319                                }
320                                else
321                                {
322                                        jointno = C->dadlink->joint_refno;
323                                        sprintf(tmpLine, "j=%d,d=\"%s\"", jointno, nclass);
324                                }
325
326                        }
327                        int from = -1;
328                        if (tmpLine[0]) //input from receptor
329                        {
330                                from = addFromString(NeuronType, tmpLine, &range);
331                                if (from < 0) return -34;
332                                this->checkpoint();
333                        } /*could be 'else'...*/
334
335                        if (NULL != C->links[j]->from) // input from another neuron
336                                from = C->links[j]->from->neuro_refno;
337                        if (from >= 0)
338                        {
339                                sprintf(tmpLine, "%d,%d,%g", n_refno, from, C->links[j]->w);
340                                if (addFromString(NeuronConnectionType, tmpLine, &range) < 0) return -35;
341                                this->checkpoint();
342                        }
343                }
344        }
345        return 0;
346}
347
348
349void f4_Model::toF1Geno(SString &out)
350{
351        cells->toF1Geno(out);
352}
Note: See TracBrowser for help on using the repository browser.