1 | // This file is a part of Framsticks SDK. http://www.framsticks.com/ |
---|
2 | // Copyright (C) 1999-2021 Maciej Komosinski and Szymon Ulatowski. |
---|
3 | // See LICENSE.txt for details. |
---|
4 | |
---|
5 | #include "f1_conv.h" |
---|
6 | #include <common/log.h> |
---|
7 | #include <frams/util/multirange.h> |
---|
8 | #include <frams/util/multimap.h> |
---|
9 | #include <frams/genetics/geneprops.h> |
---|
10 | #include <ctype.h> |
---|
11 | #include <assert.h> |
---|
12 | #include <algorithm> |
---|
13 | |
---|
14 | //#define v1f1COMPATIBLE //as in ancient Framsticks 1.x |
---|
15 | |
---|
16 | class Builder |
---|
17 | { |
---|
18 | public: |
---|
19 | Builder(const char*g, int mapping = 0) :invalid(0), genbegin(g), usemapping(mapping), first_part_mapping(NULL), own_first_part_mapping(true), model_energy(0), model_energy_count(0) {} |
---|
20 | ~Builder() { if (own_first_part_mapping) SAFEDELETE(first_part_mapping); } |
---|
21 | char tmp[222]; |
---|
22 | bool invalid; |
---|
23 | Model model; |
---|
24 | const char *genbegin; |
---|
25 | SList neuro_f1_to_f0; // neuro_f1_to_f0(f1_refno) = actual neuro pointer |
---|
26 | Neuro *last_f1_neuro; |
---|
27 | SyntParam *neuro_cls_param; |
---|
28 | |
---|
29 | struct Connection |
---|
30 | { |
---|
31 | int n1, n2; double w; |
---|
32 | Connection(int _n1, int _n2, double _w) :n1(_n1), n2(_n2), w(_w) {} |
---|
33 | }; |
---|
34 | |
---|
35 | SListTempl<Connection> connections; |
---|
36 | int usemapping; |
---|
37 | MultiRange range; |
---|
38 | MultiRange *first_part_mapping; |
---|
39 | bool own_first_part_mapping; |
---|
40 | double lastjoint_muscle_power; |
---|
41 | double model_energy; |
---|
42 | int model_energy_count; |
---|
43 | void grow(int part1, const char*g, Pt3D k, GeneProps c, int branching_part); |
---|
44 | void setPartMapping(int p, const char* g); |
---|
45 | int growJoint(int part1, int part2, Pt3D &angle, GeneProps &c, const char *g); |
---|
46 | int growPart(GeneProps &c, const char *g); |
---|
47 | const char *skipNeuro(const char *z); |
---|
48 | const char* growNeuro(const char* t, GeneProps &c, int&); |
---|
49 | void growConnection(const char* begin, const char* colon, const char* end, GeneProps& props); |
---|
50 | int countBranches(const char*g, SList &out); |
---|
51 | SyntParam* lastNeuroClassParam(); |
---|
52 | void addClassParam(const char* name, double value); |
---|
53 | void addClassParam(const char* name, const char* value); |
---|
54 | |
---|
55 | const MultiRange* makeRange(const char*g) { return makeRange(g, g); } |
---|
56 | const MultiRange* makeRange(const char*g, const char*g2); |
---|
57 | Part *getLastPart() { return getLastJoint()->part2; } |
---|
58 | Neuro *getLastNeuro() { return model.getNeuro(model.getNeuroCount() - 1); } |
---|
59 | Joint *getLastJoint() { return model.getJoint(model.getJointCount() - 1); } |
---|
60 | void addOrRememberInput(int n1, int n2, double w) |
---|
61 | { |
---|
62 | //if (!addInput(n1,n2,w,false)) |
---|
63 | connections += Connection(n1, n2, w); |
---|
64 | } |
---|
65 | bool addInput(int n1, int n2, double w, bool final) |
---|
66 | { |
---|
67 | if ((n1 < 0) || (n2 < 0) || (n1 >= neuro_f1_to_f0.size()) || (n2 >= neuro_f1_to_f0.size())) |
---|
68 | { |
---|
69 | if (final) logPrintf("GenoConvF1", "addInput", LOG_WARN, |
---|
70 | "illegal neuron connection %d <- %d (ignored)", n1, n2); |
---|
71 | return 0; |
---|
72 | } |
---|
73 | Neuro *neuro = (Neuro*)neuro_f1_to_f0(n1); |
---|
74 | Neuro *input = (Neuro*)neuro_f1_to_f0(n2); |
---|
75 | neuro->addInput(input, w); |
---|
76 | return 1; |
---|
77 | } |
---|
78 | void addPendingInputs() |
---|
79 | { |
---|
80 | for (int i = 0; i < connections.size(); i++) |
---|
81 | { |
---|
82 | Connection *c = &connections(i); |
---|
83 | addInput(c->n1, c->n2, c->w, true); |
---|
84 | } |
---|
85 | } |
---|
86 | }; |
---|
87 | |
---|
88 | const MultiRange* Builder::makeRange(const char*g, const char*g2) |
---|
89 | { |
---|
90 | if (!usemapping) return 0; |
---|
91 | range.clear(); |
---|
92 | range.add(g - genbegin, g2 - genbegin); |
---|
93 | return ⦥ |
---|
94 | } |
---|
95 | |
---|
96 | /** main conversion function - with conversion map support */ |
---|
97 | SString GenoConv_f1::convert(SString &i, MultiMap *map, bool using_checkpoints) |
---|
98 | { |
---|
99 | const char* g = i.c_str(); |
---|
100 | Builder builder(g, map ? 1 : 0); |
---|
101 | builder.model.open(using_checkpoints); |
---|
102 | builder.grow(-1, g, Pt3D_0, GeneProps::standard_values, -1); // uses Model::addFromString() to create model elements |
---|
103 | if (builder.invalid) return SString(); |
---|
104 | builder.addPendingInputs(); |
---|
105 | builder.model.startenergy = (builder.model_energy_count > 0) ? (builder.model_energy / builder.model_energy_count) : 1.0; |
---|
106 | builder.model.close(); // model is ready to use now |
---|
107 | if (map) builder.model.getCurrentToF0Map(*map); // generate f1-to-f0 conversion map |
---|
108 | return builder.model.getF0Geno().getGenes(); |
---|
109 | } |
---|
110 | |
---|
111 | void Builder::setPartMapping(int p, const char* g) |
---|
112 | { |
---|
113 | if (!usemapping) return; |
---|
114 | const MultiRange *r = makeRange(g); |
---|
115 | if (p < 0) |
---|
116 | { //special case: mapping the part which is not yet created |
---|
117 | if (first_part_mapping) first_part_mapping->add(*r); |
---|
118 | else { first_part_mapping = new MultiRange(*r); own_first_part_mapping = true; } |
---|
119 | } |
---|
120 | else |
---|
121 | model.getPart(p)->addMapping(*r); |
---|
122 | } |
---|
123 | |
---|
124 | void Builder::grow(int part1, const char*g, Pt3D k, GeneProps c, int branching_part) |
---|
125 | { |
---|
126 | int hasmuscles = 0; |
---|
127 | k += Pt3D(c.twist, 0, c.curvedness); |
---|
128 | while (1) |
---|
129 | { |
---|
130 | if (c.executeModifier(*g) == 0) |
---|
131 | { |
---|
132 | setPartMapping(part1, g); |
---|
133 | } |
---|
134 | else |
---|
135 | { |
---|
136 | switch (*g) |
---|
137 | { |
---|
138 | case 0: return; |
---|
139 | case ',': case ')': setPartMapping(branching_part, g); return; |
---|
140 | case 'R': k.x += 0.7853; setPartMapping(part1, g); break; // 45 degrees = pi/4 like in f4 |
---|
141 | case 'r': k.x -= 0.7853; setPartMapping(part1, g); break; |
---|
142 | case '[': //neuron |
---|
143 | // setdebug(g-(char*)geny,DEBUGNEURO | !l_neu); |
---|
144 | if (model.getJointCount()) |
---|
145 | g = growNeuro(g + 1, c, hasmuscles); |
---|
146 | else |
---|
147 | { |
---|
148 | logMessage("GenoConv_F1", "grow", 1, "Illegal neuron position (ignored)"); |
---|
149 | g = skipNeuro(g + 1); |
---|
150 | } |
---|
151 | break; |
---|
152 | case 'X': |
---|
153 | { |
---|
154 | int freshpart = 0; |
---|
155 | //setdebug(g-(char*)geny,DEBUGEST | !l_est); |
---|
156 | if (part1 < 0) //initial grow |
---|
157 | { |
---|
158 | if (model.getPartCount() > 0) |
---|
159 | part1 = 0; |
---|
160 | else |
---|
161 | { |
---|
162 | part1 = growPart(c, g); |
---|
163 | freshpart = 1; |
---|
164 | if (first_part_mapping) |
---|
165 | { |
---|
166 | //mapping was defined before creating this initial Part -> put it into the Part |
---|
167 | assert(own_first_part_mapping); |
---|
168 | model.getPart(part1)->setMapping(*first_part_mapping); |
---|
169 | delete first_part_mapping; |
---|
170 | //first_part_mapping can be still used later but from now on it references the internal Part mapping |
---|
171 | first_part_mapping = model.getPart(part1)->getMapping(); |
---|
172 | own_first_part_mapping = false; |
---|
173 | } |
---|
174 | } |
---|
175 | } |
---|
176 | if (!freshpart) |
---|
177 | { |
---|
178 | Part *part = model.getPart(part1); |
---|
179 | part->density = ((part->mass*part->density) + 1.0 / c.weight) / (part->mass + 1.0); // v=m*d |
---|
180 | // part->volume+=1.0/c.weight; |
---|
181 | part->mass += 1.0; |
---|
182 | } |
---|
183 | model_energy += c.energy; |
---|
184 | model_energy_count++; |
---|
185 | |
---|
186 | int part2 = growPart(c, g); |
---|
187 | growJoint(part1, part2, k, c, g); |
---|
188 | // est* e = new est(*s,*s2,k,c,zz,this); |
---|
189 | |
---|
190 | // attenuate properties as they are propagated along the structure |
---|
191 | c.propagateAlong(true); |
---|
192 | |
---|
193 | model.checkpoint(); |
---|
194 | grow(part2, g + 1, Pt3D_0, c, branching_part); |
---|
195 | return; |
---|
196 | } |
---|
197 | case '(': |
---|
198 | { |
---|
199 | setPartMapping(part1, g); |
---|
200 | SList ga; |
---|
201 | int count = countBranches(g + 1, ga); |
---|
202 | c.muscle_reset_range = false; |
---|
203 | c.muscle_bend_range = 1.0 / count; |
---|
204 | for (int i = 0; i < count; i++) |
---|
205 | grow(part1, (char*)ga(i), k + Pt3D(0, 0, -M_PI + (i + 1)*(2 * M_PI / (count + 1))), c, part1); |
---|
206 | return; |
---|
207 | } |
---|
208 | case ' ': case '\t': case '\n': case '\r': break; |
---|
209 | default: invalid = 1; return; |
---|
210 | } |
---|
211 | } |
---|
212 | g++; |
---|
213 | } |
---|
214 | } |
---|
215 | |
---|
216 | SyntParam* Builder::lastNeuroClassParam() |
---|
217 | { |
---|
218 | if (!neuro_cls_param) |
---|
219 | { |
---|
220 | NeuroClass *cls = last_f1_neuro->getClass(); |
---|
221 | if (cls) |
---|
222 | { |
---|
223 | neuro_cls_param = new SyntParam(last_f1_neuro->classProperties()); |
---|
224 | // this is equivalent to: |
---|
225 | // SyntParam tmp=last_f1_neuro->classProperties(); |
---|
226 | // neuro_cls_param=new SyntParam(tmp); |
---|
227 | // interestingly, some compilers eliminate the call to new SyntParam, |
---|
228 | // realizing that a copy constructor is redundant when the original object is |
---|
229 | // temporary. there are no side effect of such optimization, as long as the |
---|
230 | // copy-constructed object is exact equivalent of the original. |
---|
231 | } |
---|
232 | } |
---|
233 | return neuro_cls_param; |
---|
234 | } |
---|
235 | |
---|
236 | void Builder::addClassParam(const char* name, double value) |
---|
237 | { |
---|
238 | lastNeuroClassParam(); |
---|
239 | if (neuro_cls_param) |
---|
240 | neuro_cls_param->setDoubleById(name, value); |
---|
241 | } |
---|
242 | |
---|
243 | void Builder::addClassParam(const char* name, const char* value) |
---|
244 | { |
---|
245 | lastNeuroClassParam(); |
---|
246 | if (neuro_cls_param) |
---|
247 | { |
---|
248 | ExtValue e(value); |
---|
249 | const ExtValue &re(e); |
---|
250 | neuro_cls_param->setById(name, re); |
---|
251 | } |
---|
252 | } |
---|
253 | |
---|
254 | int Builder::countBranches(const char*g, SList &out) |
---|
255 | { |
---|
256 | int gl = 0; |
---|
257 | out += (void*)g; |
---|
258 | while (gl >= 0) |
---|
259 | { |
---|
260 | switch (*g) |
---|
261 | { |
---|
262 | case 0: gl = -1; break; |
---|
263 | case '(': case '[': ++gl; break; |
---|
264 | case ')': case ']': --gl; break; |
---|
265 | case ',': if (!gl) out += (void*)(g + 1); |
---|
266 | } |
---|
267 | g++; |
---|
268 | } |
---|
269 | return out.size(); |
---|
270 | } |
---|
271 | |
---|
272 | int Builder::growJoint(int part1, int part2, Pt3D &angle, GeneProps &c, const char *g) |
---|
273 | { |
---|
274 | double len = std::min(2.0, c.length); |
---|
275 | sprintf(tmp, "p1=%d,p2=%d,dx=%lg,rx=%lg,ry=%lg,rz=%lg,stam=%lg,vr=%g,vg=%g,vb=%g", |
---|
276 | part1, part2, len, angle.x, angle.y, angle.z, c.stamina, c.cred, c.cgreen, c.cblue); |
---|
277 | lastjoint_muscle_power = c.muscle_power; |
---|
278 | return model.addFromString(Model::JointType, tmp, makeRange(g)); |
---|
279 | } |
---|
280 | |
---|
281 | int Builder::growPart(GeneProps &c, const char *g) |
---|
282 | { |
---|
283 | sprintf(tmp, "dn=%lg,fr=%lg,ing=%lg,as=%lg,vr=%g,vg=%g,vb=%g", |
---|
284 | 1.0 / c.weight, c.friction, c.ingestion, c.assimilation, c.cred, c.cgreen, c.cblue); |
---|
285 | return model.addFromString(Model::PartType, tmp, makeRange(g)); |
---|
286 | } |
---|
287 | |
---|
288 | const char *Builder::skipNeuro(const char *z) |
---|
289 | { |
---|
290 | for (; *z; z++) if ((*z == ']') || (*z == ')')) break; |
---|
291 | return z - 1; |
---|
292 | } |
---|
293 | |
---|
294 | const char* Builder::growNeuro(const char* t, GeneProps& props, int &hasmuscles) |
---|
295 | { |
---|
296 | const char*neuroend = skipNeuro(t); |
---|
297 | last_f1_neuro = model.addNewNeuro(); |
---|
298 | neuro_cls_param = NULL; |
---|
299 | last_f1_neuro->attachToPart(getLastPart()); |
---|
300 | const MultiRange *mr = makeRange(t - 1, neuroend + 1); |
---|
301 | if (mr) last_f1_neuro->addMapping(*mr); |
---|
302 | neuro_f1_to_f0 += last_f1_neuro; |
---|
303 | |
---|
304 | SString clsname; |
---|
305 | bool haveclass = 0; |
---|
306 | while (*t && *t <= ' ') t++; |
---|
307 | const char* next = (*t) ? (t + 1) : t; |
---|
308 | while (*next && *next <= ' ') next++; |
---|
309 | if (*t && *next != ',' && *next != ']') // old style muscles [|rest] or [@rest] |
---|
310 | switch (*t) |
---|
311 | { |
---|
312 | case '@': if (t[1] == ':') break; |
---|
313 | haveclass = 1; |
---|
314 | // if (!(hasmuscles&1)) |
---|
315 | { |
---|
316 | hasmuscles |= 1; |
---|
317 | Neuro *muscle = model.addNewNeuro(); |
---|
318 | sprintf(tmp, "@:p=%lg", lastjoint_muscle_power); |
---|
319 | muscle->addInput(last_f1_neuro); |
---|
320 | muscle->setDetails(tmp); |
---|
321 | muscle->attachToJoint(getLastJoint()); |
---|
322 | if (usemapping) muscle->addMapping(*makeRange(t)); |
---|
323 | } |
---|
324 | t++; |
---|
325 | break; |
---|
326 | case '|': if (t[1] == ':') break; |
---|
327 | haveclass = 1; |
---|
328 | // if (!(hasmuscles&2)) |
---|
329 | { |
---|
330 | hasmuscles |= 2; |
---|
331 | Neuro *muscle = model.addNewNeuro(); |
---|
332 | sprintf(tmp, "|:p=%lg,r=%lg", lastjoint_muscle_power, props.muscle_bend_range); |
---|
333 | muscle->addInput(last_f1_neuro); |
---|
334 | muscle->setDetails(tmp); |
---|
335 | muscle->attachToJoint(getLastJoint()); |
---|
336 | if (usemapping) muscle->addMapping(*makeRange(t)); |
---|
337 | } |
---|
338 | t++; |
---|
339 | break; |
---|
340 | } |
---|
341 | while (*t && *t <= ' ') t++; |
---|
342 | bool finished = 0; |
---|
343 | const char *begin = t; |
---|
344 | const char* colon = 0; |
---|
345 | SString classparams; |
---|
346 | while (!finished) |
---|
347 | { |
---|
348 | switch (*t) |
---|
349 | { |
---|
350 | case ':': colon = t; break; |
---|
351 | case 0: case ']': case ')': finished = 1; |
---|
352 | // NO break! |
---|
353 | case ',': |
---|
354 | if (!haveclass && !colon && t > begin) |
---|
355 | { |
---|
356 | haveclass = 1; |
---|
357 | SString clsname(begin, t - begin); |
---|
358 | clsname = trim(clsname); |
---|
359 | last_f1_neuro->setClassName(clsname); |
---|
360 | NeuroClass *cls = last_f1_neuro->getClass(); |
---|
361 | if (cls) |
---|
362 | { |
---|
363 | if (cls->getPreferredLocation() == 2) |
---|
364 | last_f1_neuro->attachToJoint(getLastJoint()); |
---|
365 | else if (cls->getPreferredLocation() == 1) |
---|
366 | last_f1_neuro->attachToPart(getLastPart()); |
---|
367 | |
---|
368 | lastNeuroClassParam(); |
---|
369 | //special handling: muscle properties (can be overwritten by subsequent property assignments) |
---|
370 | if (!strcmp(cls->getName().c_str(), "|")) |
---|
371 | { |
---|
372 | neuro_cls_param->setDoubleById("p", lastjoint_muscle_power); |
---|
373 | neuro_cls_param->setDoubleById("r", props.muscle_bend_range); |
---|
374 | } |
---|
375 | else if (!strcmp(cls->getName().c_str(), "@")) |
---|
376 | { |
---|
377 | neuro_cls_param->setDoubleById("p", lastjoint_muscle_power); |
---|
378 | } |
---|
379 | } |
---|
380 | } |
---|
381 | else if (colon && (colon > begin) && (t > colon)) |
---|
382 | growConnection(begin, colon, t, props); |
---|
383 | if (t[0] != ',') t--; |
---|
384 | begin = t + 1; colon = 0; |
---|
385 | break; |
---|
386 | } |
---|
387 | t++; |
---|
388 | } |
---|
389 | SAFEDELETE(neuro_cls_param); |
---|
390 | return t; |
---|
391 | } |
---|
392 | void Builder::growConnection(const char* begin, const char* colon, const char* end, GeneProps& props) |
---|
393 | { |
---|
394 | while (*begin && *begin <= ' ') begin++; |
---|
395 | int i; |
---|
396 | if (isdigit(begin[0]) || (begin[0] == '-')) |
---|
397 | { |
---|
398 | double conn_weight = ExtValue::getDouble(trim(SString(colon + 1, end - (colon + 1))).c_str()); |
---|
399 | paInt relative = ExtValue::getInt(trim(SString(begin, colon - begin)).c_str(), false); |
---|
400 | int this_refno = neuro_f1_to_f0.size() - 1; |
---|
401 | addOrRememberInput(this_refno, this_refno + relative, conn_weight); |
---|
402 | } |
---|
403 | else if ((i = last_f1_neuro->extraProperties().findIdn(begin, colon - begin)) >= 0) |
---|
404 | { |
---|
405 | last_f1_neuro->extraProperties().setFromString(i, colon + 1); |
---|
406 | } |
---|
407 | else if (isupper(begin[0]) || strchr("*|@", begin[0])) |
---|
408 | { |
---|
409 | SString clsname(begin, colon - begin); |
---|
410 | trim(clsname); |
---|
411 | Neuro *receptor = model.addNewNeuro(); |
---|
412 | receptor->setClassName(clsname); |
---|
413 | NeuroClass *cls = receptor->getClass(); |
---|
414 | if (cls) |
---|
415 | { |
---|
416 | if (cls->getPreferredLocation() == 2) receptor->attachToJoint(getLastJoint()); |
---|
417 | else if (cls->getPreferredLocation() == 1) receptor->attachToPart(getLastPart()); |
---|
418 | } |
---|
419 | last_f1_neuro->addInput(receptor, ExtValue::getDouble(trim(SString(colon + 1, end - (colon + 1))).c_str())); |
---|
420 | if (usemapping) receptor->addMapping(*makeRange(begin, end - 1)); |
---|
421 | } |
---|
422 | else if ((begin[0] == '>') && (begin[1])) |
---|
423 | { |
---|
424 | Neuro *out = model.addNewNeuro(); |
---|
425 | out->addInput(last_f1_neuro, ExtValue::getDouble(trim(SString(colon + 1, end - (colon + 1))).c_str())); |
---|
426 | out->setClassName(SString(begin + 1, end - colon - 1)); |
---|
427 | if (begin[1] == '@') |
---|
428 | { |
---|
429 | sprintf(tmp, "p=%lg", lastjoint_muscle_power); |
---|
430 | out->setClassParams(tmp); |
---|
431 | } |
---|
432 | else if (begin[1] == '|') |
---|
433 | { |
---|
434 | sprintf(tmp, "p=%lg,r=%lg", lastjoint_muscle_power, props.muscle_bend_range); |
---|
435 | out->setClassParams(tmp); |
---|
436 | } |
---|
437 | NeuroClass *cls = out->getClass(); |
---|
438 | if (cls) |
---|
439 | { |
---|
440 | if (cls->getPreferredLocation() == 2) out->attachToJoint(getLastJoint()); |
---|
441 | else if (cls->getPreferredLocation() == 1) out->attachToPart(getLastPart()); |
---|
442 | } |
---|
443 | if (usemapping) out->addMapping(*makeRange(begin, end - 1)); |
---|
444 | } |
---|
445 | else if (*begin == '!') addClassParam("fo", ExtValue::getDouble(trim(SString(colon + 1, end - (colon + 1))).c_str())); |
---|
446 | else if (*begin == '=') addClassParam("in", ExtValue::getDouble(trim(SString(colon + 1, end - (colon + 1))).c_str())); |
---|
447 | else if (*begin == '/') addClassParam("si", ExtValue::getDouble(trim(SString(colon + 1, end - (colon + 1))).c_str())); |
---|
448 | else if (islower(begin[0])) |
---|
449 | { |
---|
450 | SString name(begin, colon - begin); |
---|
451 | SString value(colon + 1, end - (colon + 1)); |
---|
452 | addClassParam(name.c_str(), value.c_str()); |
---|
453 | } |
---|
454 | } |
---|