1 | // This file is a part of Framsticks SDK. http://www.framsticks.com/ |
---|
2 | // Copyright (C) 1999-2023 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 development checkpoints and support for new API for neuron types |
---|
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 | |
---|
18 | GenoConv_f40::GenoConv_f40() |
---|
19 | { |
---|
20 | name = "Developmental encoding"; |
---|
21 | in_format = '4'; |
---|
22 | out_format = '0'; |
---|
23 | mapsupport = 1; |
---|
24 | } |
---|
25 | |
---|
26 | |
---|
27 | SString GenoConv_f40::convert(SString &in, MultiMap *map, bool using_checkpoints) |
---|
28 | { |
---|
29 | f4_Model *model = new f4_Model(); |
---|
30 | int res = model->buildFromF4(in, using_checkpoints); |
---|
31 | if (res != GENOPER_OK) |
---|
32 | { |
---|
33 | delete model; |
---|
34 | return SString(); // oops |
---|
35 | } |
---|
36 | if (NULL != map) |
---|
37 | // generate to-f0 conversion map |
---|
38 | model->getCurrentToF0Map(*map); |
---|
39 | SString out = model->getF0Geno().getGenes(); |
---|
40 | delete model; |
---|
41 | |
---|
42 | /* quick debugging test - print an approximate f1 conversion of every genotype converted to f0: |
---|
43 | GenoConv_F41_TestOnly conv41; |
---|
44 | SString f1 = conv41.convert(in, NULL, false); |
---|
45 | printf("f1 = %s\n", f1.c_str()); |
---|
46 | */ |
---|
47 | |
---|
48 | return out; |
---|
49 | } |
---|
50 | |
---|
51 | |
---|
52 | GenoConv_F41_TestOnly::GenoConv_F41_TestOnly() |
---|
53 | { |
---|
54 | name = "Only for testing, approximate f4->f1 converter"; |
---|
55 | // Why approximate? for example, f1 does not allow to continue after branching: X(X,X)X <-- the last X |
---|
56 | // Some modifier genes are also not perfectly converted. |
---|
57 | // And neuron properties are ignored... |
---|
58 | in_format = '4'; |
---|
59 | out_format = '1'; |
---|
60 | mapsupport = 0; |
---|
61 | } |
---|
62 | |
---|
63 | |
---|
64 | SString GenoConv_F41_TestOnly::convert(SString &in, MultiMap *map, bool using_checkpoints) |
---|
65 | { |
---|
66 | f4_Model *model = new f4_Model(); |
---|
67 | int res = model->buildFromF4(in, using_checkpoints); |
---|
68 | if (res != GENOPER_OK) |
---|
69 | { |
---|
70 | delete model; |
---|
71 | return SString(); // oops |
---|
72 | } |
---|
73 | SString out; |
---|
74 | model->toF1Geno(out); |
---|
75 | delete model; |
---|
76 | return out; |
---|
77 | } |
---|
78 | |
---|
79 | |
---|
80 | f4_Model::f4_Model() : Model() |
---|
81 | { |
---|
82 | cells = NULL; |
---|
83 | } |
---|
84 | |
---|
85 | f4_Model::~f4_Model() |
---|
86 | { |
---|
87 | if (cells) delete cells; |
---|
88 | } |
---|
89 | |
---|
90 | int f4_Model::buildFromF4(SString &geno, bool using_checkpoints) |
---|
91 | { |
---|
92 | error = GENOPER_OK; |
---|
93 | errorpos = -1; |
---|
94 | |
---|
95 | // transform geno from string to nodes |
---|
96 | f4_Node f4rootnode; |
---|
97 | int res = f4_process(geno.c_str(), &f4rootnode); |
---|
98 | if (res || (f4rootnode.childCount() != 1)) //consider any error fatal, preventing building a model |
---|
99 | { |
---|
100 | error = GENOPER_OPFAIL; |
---|
101 | errorpos = res; |
---|
102 | return error; |
---|
103 | } |
---|
104 | |
---|
105 | // build cells, and simulate |
---|
106 | if (cells) delete cells; |
---|
107 | cells = new f4_Cells(f4rootnode.child, false); |
---|
108 | if (cells->getErrorCode() != GENOPER_OK) |
---|
109 | { |
---|
110 | error = cells->getErrorCode(); |
---|
111 | errorpos = cells->getErrorPos(); |
---|
112 | //delete cells; |
---|
113 | return error; |
---|
114 | } |
---|
115 | |
---|
116 | cells->simulate(); |
---|
117 | if (cells->getErrorCode() != GENOPER_OK) |
---|
118 | { |
---|
119 | error = cells->getErrorCode(); |
---|
120 | errorpos = cells->getErrorPos(); |
---|
121 | return error; |
---|
122 | } |
---|
123 | |
---|
124 | // reset recursive traverse flags |
---|
125 | for (int i = 0; i < cells->cell_count; i++) |
---|
126 | cells->C[i]->recurProcessedFlag = false; |
---|
127 | |
---|
128 | open(using_checkpoints); // begin model build |
---|
129 | |
---|
130 | // process every cell |
---|
131 | for (int i = 0; i < cells->cell_count; i++) |
---|
132 | { |
---|
133 | int res = buildModelRecur(cells->C[i]); |
---|
134 | if (res) |
---|
135 | { |
---|
136 | logPrintf("f4_Model", "buildFromF4", LOG_ERROR, "Error %d when building a Model", res); |
---|
137 | error = res; |
---|
138 | break; |
---|
139 | } |
---|
140 | } |
---|
141 | |
---|
142 | int res_close = close(); |
---|
143 | if (res_close == 0) // invalid |
---|
144 | { |
---|
145 | logPrintf("f4_Model", "buildFromF4", LOG_ERROR, "Error %d when closing a Model", res_close); |
---|
146 | error = -10; |
---|
147 | } |
---|
148 | |
---|
149 | return error; |
---|
150 | } |
---|
151 | |
---|
152 | |
---|
153 | f4_Cell* f4_Model::getStick(f4_Cell *C) |
---|
154 | { |
---|
155 | if (C->type == CELL_STICK) return C; |
---|
156 | if (NULL != C->dadlink) |
---|
157 | return getStick(C->dadlink); |
---|
158 | // we have no more dadlinks, find any stick |
---|
159 | for (int i = 0; i < cells->cell_count; i++) |
---|
160 | if (cells->C[i]->type == CELL_STICK) |
---|
161 | return cells->C[i]; |
---|
162 | // none! |
---|
163 | logMessage("f4_Model", "getStick", LOG_ERROR, "Not a single stick"); |
---|
164 | return NULL; |
---|
165 | } |
---|
166 | |
---|
167 | |
---|
168 | int f4_Model::buildModelRecur(f4_Cell *C) |
---|
169 | { |
---|
170 | if (C->recurProcessedFlag) |
---|
171 | // already processed |
---|
172 | return 0; |
---|
173 | |
---|
174 | // mark it processed |
---|
175 | C->recurProcessedFlag = true; |
---|
176 | |
---|
177 | // make sure parent is a stick |
---|
178 | if (C->dadlink != NULL) |
---|
179 | if (C->dadlink->type != CELL_STICK) |
---|
180 | { |
---|
181 | C->dadlink = getStick(C->dadlink); |
---|
182 | } |
---|
183 | |
---|
184 | // make sure its parent is processed first |
---|
185 | if (C->dadlink != NULL) |
---|
186 | { |
---|
187 | int res = buildModelRecur(C->dadlink); |
---|
188 | if (res) return res; |
---|
189 | } |
---|
190 | |
---|
191 | char tmpLine[100]; |
---|
192 | MultiRange range = C->genoRange; |
---|
193 | |
---|
194 | if (C->type == CELL_STICK) |
---|
195 | { |
---|
196 | int jj_p1_refno; // save for later |
---|
197 | // first end is connected to dad, or new |
---|
198 | if (C->dadlink == NULL) |
---|
199 | { |
---|
200 | // new part object for firstend |
---|
201 | // coordinates are left to be computed by Model |
---|
202 | sprintf(tmpLine, "fr=%g,ing=%g,as=%g", |
---|
203 | /*1.0/C->P.mass,*/ C->P.friction, C->P.ingestion, C->P.assimilation |
---|
204 | //C->firstend.x, C->firstend.y, C->firstend.z |
---|
205 | ); |
---|
206 | jj_p1_refno = addFromString(PartType, tmpLine, &range); |
---|
207 | if (jj_p1_refno < 0) return -1; |
---|
208 | this->checkpoint(); |
---|
209 | } |
---|
210 | else { |
---|
211 | // adjust mass/vol of first endpoint |
---|
212 | jj_p1_refno = C->dadlink->p2_refno; |
---|
213 | Part *p1 = getPart(jj_p1_refno); |
---|
214 | p1->mass += 1.0; |
---|
215 | // p1->volume += 1.0/C->P.mass; |
---|
216 | } |
---|
217 | // new part object for lastend |
---|
218 | sprintf(tmpLine, "fr=%g,ing=%g,as=%g", |
---|
219 | //C->lastend.x, C->lastend.y, C->lastend.z |
---|
220 | /*"vol=" 1.0/C->P.mass,*/ C->P.friction, C->P.ingestion, C->P.assimilation |
---|
221 | ); |
---|
222 | C->p2_refno = addFromString(PartType, tmpLine, &range); |
---|
223 | if (C->p2_refno < 0) return -2; |
---|
224 | |
---|
225 | // new joint object |
---|
226 | // check that the part references are valid |
---|
227 | int jj_p2_refno = C->p2_refno; |
---|
228 | if ((jj_p1_refno < 0) || (jj_p1_refno >= getPartCount())) return -11; |
---|
229 | if ((jj_p2_refno < 0) || (jj_p2_refno >= getPartCount())) return -12; |
---|
230 | sprintf(tmpLine, "p1=%d,p2=%d,dx=%g,dy=0,dz=0,rx=%g,ry=0,rz=%g"\ |
---|
231 | ",stam=%g", |
---|
232 | jj_p1_refno, jj_p2_refno, |
---|
233 | // relative position -- always (len, 0, 0), along the stick |
---|
234 | // this is optional! |
---|
235 | C->P.length, |
---|
236 | // relative rotation |
---|
237 | C->xrot, C->zrot, |
---|
238 | //C->P.ruch, // rotstif |
---|
239 | C->P.stamina |
---|
240 | ); |
---|
241 | C->joint_refno = addFromString(JointType, tmpLine, &range); |
---|
242 | if (C->joint_refno < 0) return -13; |
---|
243 | this->checkpoint(); |
---|
244 | } |
---|
245 | |
---|
246 | if (C->type == CELL_NEURON) |
---|
247 | { |
---|
248 | const char* nclass = C->neuclass->name.c_str(); |
---|
249 | if (C->neuclass->getPreferredLocation() == 0) |
---|
250 | { |
---|
251 | if (strcmp(nclass, "N") == 0) //special case just to specify the only neuron properties supported by f4, i.e., the properties for neuron class 'N' |
---|
252 | sprintf(tmpLine, "d=\"N:in=%g,fo=%g,si=%g\"", C->inertia, C->force, C->sigmo); |
---|
253 | else |
---|
254 | sprintf(tmpLine, "d=\"%s\"", nclass); |
---|
255 | |
---|
256 | C->neuro_refno = addFromString(NeuronType, tmpLine, &range); |
---|
257 | if (C->neuro_refno < 0) return -22; |
---|
258 | this->checkpoint(); |
---|
259 | } |
---|
260 | else if (C->neuclass->getPreferredLocation() == 1) // attached to Part or have no required attachment - also part |
---|
261 | { |
---|
262 | int partno = C->dadlink->p2_refno; |
---|
263 | if ((partno < 0) || (partno >= getPartCount())) return -21; |
---|
264 | |
---|
265 | sprintf(tmpLine, "p=%d,d=\"%s\"", partno, nclass); |
---|
266 | |
---|
267 | C->neuro_refno = addFromString(NeuronType, tmpLine, &range); |
---|
268 | if (C->neuro_refno < 0) return -22; |
---|
269 | this->checkpoint(); |
---|
270 | } |
---|
271 | else // attached to Joint, assume there are only three possibilities of getPreferredLocation() |
---|
272 | { |
---|
273 | int jointno = C->dadlink->joint_refno; |
---|
274 | |
---|
275 | if (strcmp(nclass, "@") == 0) |
---|
276 | sprintf(tmpLine, "j=%d,d=\"@:p=%g\"", jointno, C->P.muscle_power); |
---|
277 | else if (strcmp(nclass, "|") == 0) |
---|
278 | { |
---|
279 | sprintf(tmpLine, "j=%d,d=\"|:p=%g,r=%g\"", jointno, C->P.muscle_power, C->dadlink->P.muscle_bend_range); //Macko 2023-05 change: we take muscle_bend_range from dadlink, not from C, because we also assign this neuron to C->dadlink->joint_refno. Without this, for example in /*4*/<<X><<<<X>N:|>X>X>X>X the muscle is attached to the junction with 3 sticks, but gets range=33% as in a four-stick junction. f1 correctly sets range=0.5 for the analogous phenotype: X(X[|],X(X,X,X)) |
---|
280 | } |
---|
281 | else |
---|
282 | sprintf(tmpLine, "j=%d,d=\"%s\"", jointno, nclass); |
---|
283 | |
---|
284 | C->neuro_refno = addFromString(NeuronType, tmpLine, &range); |
---|
285 | if (C->neuro_refno < 0) return -32; |
---|
286 | this->checkpoint(); |
---|
287 | } |
---|
288 | for (int j = 0; j < C->conns_count; j++) |
---|
289 | { |
---|
290 | if (C->conns[j]->from != NULL) |
---|
291 | buildModelRecur(C->conns[j]->from); |
---|
292 | |
---|
293 | tmpLine[0] = 0; |
---|
294 | if (C->conns[j]->from == NULL) |
---|
295 | { |
---|
296 | logMessage("f4_Model", "buildModelRec", LOG_ERROR, "Old code for sensors as inputs embedded in [connection]: C->conns[j]->from == NULL"); |
---|
297 | } |
---|
298 | int from = -1; |
---|
299 | if (C->conns[j]->from != NULL) // input from another neuron |
---|
300 | from = C->conns[j]->from->neuro_refno; |
---|
301 | if (from >= 0) |
---|
302 | { |
---|
303 | sprintf(tmpLine, "%d,%d,%g", C->neuro_refno, from, C->conns[j]->weight); |
---|
304 | if (addFromString(NeuronConnectionType, tmpLine, &range) < 0) return -35; |
---|
305 | this->checkpoint(); |
---|
306 | } |
---|
307 | } |
---|
308 | } |
---|
309 | return 0; |
---|
310 | } |
---|
311 | |
---|
312 | |
---|
313 | void f4_Model::toF1Geno(SString &out) |
---|
314 | { |
---|
315 | cells->toF1Geno(out); |
---|
316 | } |
---|