1 | // This file is a part of Framsticks SDK. http://www.framsticks.com/ |
---|
2 | // Copyright (C) 1999-2020 Maciej Komosinski and Szymon Ulatowski. |
---|
3 | // See LICENSE.txt for details. |
---|
4 | |
---|
5 | #include <ctype.h> //isupper() |
---|
6 | #include "genooperators.h" |
---|
7 | #include <common/log.h> |
---|
8 | #include <common/nonstd_math.h> |
---|
9 | #include <frams/util/rndutil.h> |
---|
10 | |
---|
11 | static double distrib_force[] = // for '!' |
---|
12 | { |
---|
13 | 3, // distribution 0 -__/ +1 |
---|
14 | 0.001, 0.2, // "slow" neurons |
---|
15 | 0.001, 1, |
---|
16 | 1, 1, // "fast" neurons |
---|
17 | }; |
---|
18 | static double distrib_inertia[] = // for '=' |
---|
19 | { |
---|
20 | 2, // distribution 0 |..- +1 |
---|
21 | 0, 0, // "fast" neurons |
---|
22 | 0.7, 0.98, |
---|
23 | }; |
---|
24 | static double distrib_sigmo[] = // for '/' |
---|
25 | { |
---|
26 | 5, // distribution -999 -..-^-..- +999 |
---|
27 | -999, -999, //"perceptron" |
---|
28 | 999, 999, |
---|
29 | -5, -1, // nonlinear |
---|
30 | 1, 5, |
---|
31 | -1, 1, // ~linear |
---|
32 | }; |
---|
33 | |
---|
34 | |
---|
35 | int GenoOperators::roulette(const double *probtab, const int count) |
---|
36 | { |
---|
37 | double sum = 0; |
---|
38 | int i; |
---|
39 | for (i = 0; i < count; i++) sum += probtab[i]; |
---|
40 | double sel = rndDouble(sum); |
---|
41 | for (sum = 0, i = 0; i < count; i++) { sum += probtab[i]; if (sel < sum) return i; } |
---|
42 | return -1; |
---|
43 | } |
---|
44 | |
---|
45 | bool GenoOperators::getMinMaxDef(ParamInterface *p, int i, double &mn, double &mx, double &def) |
---|
46 | { |
---|
47 | mn = mx = def = 0; |
---|
48 | int defined = 0; |
---|
49 | if (p->type(i)[0] == 'f') |
---|
50 | { |
---|
51 | double _mn = 0, _mx = 1, _def = 0.5; |
---|
52 | defined = p->getMinMaxDouble(i, _mn, _mx, _def); |
---|
53 | if (defined == 1) _mx = _mn + 1000.0; //only min was defined, so let's set some arbitrary range, just to have some freedom. Assumes _mn is not close to maxdouble... |
---|
54 | if (_mx < _mn && defined == 3) //only default was defined, so let's assume some arbitrary range. Again, no check for min/maxdouble... |
---|
55 | { |
---|
56 | _mn = _def - 500.0; |
---|
57 | _mx = _def + 500.0; |
---|
58 | } |
---|
59 | if (defined < 3) _def = (_mn + _mx) / 2.0; |
---|
60 | mn = _mn; mx = _mx; def = _def; |
---|
61 | } |
---|
62 | if (p->type(i)[0] == 'd') |
---|
63 | { |
---|
64 | paInt _mn = 0, _mx = 1, _def = 0; |
---|
65 | defined = p->getMinMaxInt(i, _mn, _mx, _def); |
---|
66 | if (defined == 1) _mx = _mn + 1000; //only min was defined, so let's set some arbitrary range, just to have some freedom. Assumes _mn is not close to maxint... |
---|
67 | if (_mx < _mn && defined == 3) //only default was defined, so let's assume some arbitrary range. Again, no check for min/maxint... |
---|
68 | { |
---|
69 | _mn = _def - 500; |
---|
70 | _mx = _def + 500; |
---|
71 | } |
---|
72 | if (defined < 3) _def = (_mn + _mx) / 2; |
---|
73 | mn = _mn; mx = _mx; def = _def; |
---|
74 | } |
---|
75 | return defined == 3; |
---|
76 | } |
---|
77 | |
---|
78 | int GenoOperators::selectRandomProperty(Neuro *n) |
---|
79 | { |
---|
80 | int neuext = n->extraProperties().getPropCount(), |
---|
81 | neucls = n->getClass() == NULL ? 0 : n->getClass()->getProperties().getPropCount(); |
---|
82 | if (neuext + neucls == 0) return -1; //no properties in this neuron |
---|
83 | int index = rndUint(neuext + neucls); |
---|
84 | if (index >= neuext) index = index - neuext + 100; |
---|
85 | return index; |
---|
86 | } |
---|
87 | |
---|
88 | double GenoOperators::mutateNeuProperty(double current, Neuro *n, int i) |
---|
89 | { |
---|
90 | if (i == -1) return mutateCreepNoLimit('f', current, 2, true); //i==-1: mutating weight of neural connection |
---|
91 | Param p; |
---|
92 | if (i >= 100) { i -= 100; p = n->getClass()->getProperties(); } |
---|
93 | else p = n->extraProperties(); |
---|
94 | double newval = current; |
---|
95 | /*bool ok=*/getMutatedProperty(p, i, current, newval); |
---|
96 | return newval; |
---|
97 | } |
---|
98 | |
---|
99 | bool GenoOperators::mutatePropertyNaive(ParamInterface &p, int i) |
---|
100 | { |
---|
101 | double mn, mx, df; |
---|
102 | if (p.type(i)[0] != 'f' && p.type(i)[0] != 'd') return false; //don't know how to mutate |
---|
103 | getMinMaxDef(&p, i, mn, mx, df); |
---|
104 | |
---|
105 | ExtValue ev; |
---|
106 | p.get(i, ev); |
---|
107 | ev.setDouble(mutateCreep(p.type(i)[0], ev.getDouble(), mn, mx, true)); |
---|
108 | p.set(i, ev); |
---|
109 | return true; |
---|
110 | } |
---|
111 | |
---|
112 | bool GenoOperators::mutateProperty(ParamInterface &p, int i) |
---|
113 | { |
---|
114 | double newval; |
---|
115 | ExtValue ev; |
---|
116 | p.get(i, ev); |
---|
117 | bool ok = getMutatedProperty(p, i, ev.getDouble(), newval); |
---|
118 | if (ok) { ev.setDouble(newval); p.set(i, ev); } |
---|
119 | return ok; |
---|
120 | } |
---|
121 | |
---|
122 | bool GenoOperators::getMutatedProperty(ParamInterface &p, int i, double oldval, double &newval) |
---|
123 | { |
---|
124 | newval = 0; |
---|
125 | if (p.type(i)[0] != 'f' && p.type(i)[0] != 'd') return false; //don't know how to mutate |
---|
126 | const char *n = p.id(i), *na = p.name(i); |
---|
127 | if (strcmp(n, "si") == 0 && strcmp(na, "Sigmoid") == 0) newval = CustomRnd(distrib_sigmo); else |
---|
128 | if (strcmp(n, "in") == 0 && strcmp(na, "Inertia") == 0) newval = CustomRnd(distrib_inertia); else |
---|
129 | if (strcmp(n, "fo") == 0 && strcmp(na, "Force") == 0) newval = CustomRnd(distrib_force); else |
---|
130 | { |
---|
131 | double mn, mx, df; |
---|
132 | getMinMaxDef(&p, i, mn, mx, df); |
---|
133 | newval = mutateCreep(p.type(i)[0], oldval, mn, mx, true); |
---|
134 | } |
---|
135 | return true; |
---|
136 | } |
---|
137 | |
---|
138 | double GenoOperators::mutateCreepNoLimit(char type, double current, double stddev, bool limit_precision_3digits) |
---|
139 | { |
---|
140 | double result = RndGen.Gauss(current, stddev); |
---|
141 | if (type == 'd') |
---|
142 | { |
---|
143 | result = int(result + 0.5); |
---|
144 | if (result == current) result += rndUint(2) * 2 - 1; //force some change |
---|
145 | } |
---|
146 | else |
---|
147 | { |
---|
148 | if (limit_precision_3digits) |
---|
149 | result = floor(result * 1000 + 0.5) / 1000.0; //round |
---|
150 | } |
---|
151 | return result; |
---|
152 | } |
---|
153 | |
---|
154 | double GenoOperators::mutateCreep(char type, double current, double mn, double mx, double stddev, bool limit_precision_3digits) |
---|
155 | { |
---|
156 | double result = mutateCreepNoLimit(type, current, stddev, limit_precision_3digits); |
---|
157 | if (result<mn || result>mx) //exceeds boundary, so bring to the allowed range |
---|
158 | { |
---|
159 | //reflect: |
---|
160 | if (result > mx) result = mx - (result - mx); else |
---|
161 | if (result < mn) result = mn + (mn - result); |
---|
162 | //wrap (just in case 'result' exceeded the allowed range so much that after reflection above it exceeded the other boundary): |
---|
163 | if (result > mx) result = mn + fmod(result - mx, mx - mn); else |
---|
164 | if (result < mn) result = mn + fmod(mn - result, mx - mn); |
---|
165 | if (limit_precision_3digits) |
---|
166 | { |
---|
167 | //reflect and wrap above may have changed the (limited) precision, so try to round again (maybe unnecessarily, because we don't know if reflect+wrap above were triggered) |
---|
168 | double result_try = floor(result * 1000 + 0.5) / 1000.0; //round |
---|
169 | if (mn <= result_try && result_try <= mx) result = result_try; //after rounding still witin allowed range, so keep rounded value |
---|
170 | } |
---|
171 | } |
---|
172 | return result; |
---|
173 | } |
---|
174 | |
---|
175 | double GenoOperators::mutateCreep(char type, double current, double mn, double mx, bool limit_precision_3digits) |
---|
176 | { |
---|
177 | double stddev = (mx - mn) / 2 / 5; // magic arbitrary formula for stddev, which becomes /halfinterval, 5 times narrower |
---|
178 | return mutateCreep(type, current, mn, mx, stddev, limit_precision_3digits); |
---|
179 | } |
---|
180 | |
---|
181 | void GenoOperators::setIntFromDoubleWithProbabilisticDithering(ParamInterface &p, int index, double value) //TODO |
---|
182 | { |
---|
183 | p.setInt(index, (paInt)(value + 0.5)); //TODO value=2.499 will result in 2 and 2.5 will result in 3, but we want these cases to be 2 or 3 with almost equal probability. value=2.1 should be mostly 2, rarely 3. Careful with negative values (test it!) |
---|
184 | } |
---|
185 | |
---|
186 | void GenoOperators::linearMix(vector<double> &p1, vector<double> &p2, double proportion) |
---|
187 | { |
---|
188 | if (p1.size() != p2.size()) |
---|
189 | { |
---|
190 | logPrintf("GenoOperators", "linearMix", LOG_ERROR, "Cannot mix vectors of different length (%d and %d)", p1.size(), p2.size()); |
---|
191 | return; |
---|
192 | } |
---|
193 | for (unsigned int i = 0; i < p1.size(); i++) |
---|
194 | { |
---|
195 | double v1 = p1[i]; |
---|
196 | double v2 = p2[i]; |
---|
197 | p1[i] = v1 * proportion + v2 * (1 - proportion); |
---|
198 | p2[i] = v2 * proportion + v1 * (1 - proportion); |
---|
199 | } |
---|
200 | } |
---|
201 | |
---|
202 | void GenoOperators::linearMix(ParamInterface &p1, int i1, ParamInterface &p2, int i2, double proportion) |
---|
203 | { |
---|
204 | char type1 = p1.type(i1)[0]; |
---|
205 | char type2 = p2.type(i2)[0]; |
---|
206 | if (type1 == 'f' && type2 == 'f') |
---|
207 | { |
---|
208 | double v1 = p1.getDouble(i1); |
---|
209 | double v2 = p2.getDouble(i2); |
---|
210 | p1.setDouble(i1, v1 * proportion + v2 * (1 - proportion)); |
---|
211 | p2.setDouble(i2, v2 * proportion + v1 * (1 - proportion)); |
---|
212 | } |
---|
213 | else |
---|
214 | if (type1 == 'd' && type2 == 'd') |
---|
215 | { |
---|
216 | int v1 = p1.getInt(i1); |
---|
217 | int v2 = p2.getInt(i2); |
---|
218 | setIntFromDoubleWithProbabilisticDithering(p1, i1, v1 * proportion + v2 * (1 - proportion)); |
---|
219 | setIntFromDoubleWithProbabilisticDithering(p2, i2, v2 * proportion + v1 * (1 - proportion)); |
---|
220 | } |
---|
221 | else |
---|
222 | logPrintf("GenoOperators", "linearMix", LOG_WARN, "Cannot mix values of types '%c' and '%c'", type1, type2); |
---|
223 | } |
---|
224 | |
---|
225 | int GenoOperators::getActiveNeuroClassCount(Model::ShapeType for_shape_type) |
---|
226 | { |
---|
227 | int count = 0; |
---|
228 | for (int i = 0; i < Neuro::getClassCount(); i++) |
---|
229 | { |
---|
230 | NeuroClass *nc = Neuro::getClass(i); |
---|
231 | if (nc->isShapeTypeSupported(for_shape_type) && nc->genactive) |
---|
232 | count++; |
---|
233 | } |
---|
234 | return count; |
---|
235 | } |
---|
236 | |
---|
237 | NeuroClass *GenoOperators::getRandomNeuroClass(Model::ShapeType for_shape_type) |
---|
238 | { |
---|
239 | vector<NeuroClass *> active; |
---|
240 | for (int i = 0; i < Neuro::getClassCount(); i++) |
---|
241 | { |
---|
242 | NeuroClass *nc = Neuro::getClass(i); |
---|
243 | if (nc->isShapeTypeSupported(for_shape_type) && nc->genactive) |
---|
244 | active.push_back(nc); |
---|
245 | } |
---|
246 | if (active.size() == 0) return NULL; else return active[rndUint(active.size())]; |
---|
247 | } |
---|
248 | |
---|
249 | NeuroClass *GenoOperators::getRandomNeuroClassWithOutput(Model::ShapeType for_shape_type) |
---|
250 | { |
---|
251 | vector<NeuroClass *> active; |
---|
252 | for (int i = 0; i < Neuro::getClassCount(); i++) |
---|
253 | { |
---|
254 | NeuroClass *nc = Neuro::getClass(i); |
---|
255 | if (nc->isShapeTypeSupported(for_shape_type) && nc->genactive && nc->getPreferredOutput() != 0) |
---|
256 | active.push_back(nc); |
---|
257 | } |
---|
258 | if (active.size() == 0) return NULL; else return active[rndUint(active.size())]; |
---|
259 | } |
---|
260 | |
---|
261 | NeuroClass *GenoOperators::getRandomNeuroClassWithInput(Model::ShapeType for_shape_type) |
---|
262 | { |
---|
263 | vector<NeuroClass *> active; |
---|
264 | for (int i = 0; i < Neuro::getClassCount(); i++) |
---|
265 | { |
---|
266 | NeuroClass *nc = Neuro::getClass(i); |
---|
267 | if (nc->isShapeTypeSupported(for_shape_type) && nc->genactive && nc->getPreferredInputs() != 0) |
---|
268 | active.push_back(nc); |
---|
269 | } |
---|
270 | if (active.size() == 0) return NULL; else return active[rndUint(active.size())]; |
---|
271 | } |
---|
272 | |
---|
273 | NeuroClass *GenoOperators::getRandomNeuroClassWithOutputAndNoInputs(Model::ShapeType for_shape_type) |
---|
274 | { |
---|
275 | vector<NeuroClass *> active; |
---|
276 | for (int i = 0; i < Neuro::getClassCount(); i++) |
---|
277 | { |
---|
278 | NeuroClass *nc = Neuro::getClass(i); |
---|
279 | if (nc->isShapeTypeSupported(for_shape_type) && nc->genactive && nc->getPreferredOutput() != 0 && nc->getPreferredInputs() == 0) |
---|
280 | active.push_back(nc); |
---|
281 | } |
---|
282 | if (active.size() == 0) return NULL; else return active[rndUint(active.size())]; |
---|
283 | } |
---|
284 | |
---|
285 | int GenoOperators::getRandomNeuroClassWithOutput(const vector<NeuroClass *> &NClist) |
---|
286 | { |
---|
287 | vector<int> allowed; |
---|
288 | for (size_t i = 0; i < NClist.size(); i++) |
---|
289 | if (NClist[i]->getPreferredOutput() != 0) //this NeuroClass provides output |
---|
290 | allowed.push_back(i); |
---|
291 | if (allowed.size() == 0) return -1; else return allowed[rndUint(allowed.size())]; |
---|
292 | } |
---|
293 | |
---|
294 | int GenoOperators::getRandomNeuroClassWithInput(const vector<NeuroClass *> &NClist) |
---|
295 | { |
---|
296 | vector<int> allowed; |
---|
297 | for (size_t i = 0; i < NClist.size(); i++) |
---|
298 | if (NClist[i]->getPreferredInputs() != 0) //this NeuroClass wants one input connection or more |
---|
299 | allowed.push_back(i); |
---|
300 | if (allowed.size() == 0) return -1; else return allowed[rndUint(allowed.size())]; |
---|
301 | } |
---|
302 | |
---|
303 | int GenoOperators::getRandomChar(const char *choices, const char *excluded) |
---|
304 | { |
---|
305 | int allowed_count = 0; |
---|
306 | for (size_t i = 0; i < strlen(choices); i++) if (!strchrn0(excluded, choices[i])) allowed_count++; |
---|
307 | if (allowed_count == 0) return -1; //no char is allowed |
---|
308 | int rnd_index = rndUint(allowed_count) + 1; |
---|
309 | allowed_count = 0; |
---|
310 | for (size_t i = 0; i < strlen(choices); i++) |
---|
311 | { |
---|
312 | if (!strchrn0(excluded, choices[i])) allowed_count++; |
---|
313 | if (allowed_count == rnd_index) return i; |
---|
314 | } |
---|
315 | return -1; //never happens |
---|
316 | } |
---|
317 | |
---|
318 | NeuroClass *GenoOperators::parseNeuroClass(char *&s) |
---|
319 | { |
---|
320 | int maxlen = (int)strlen(s); |
---|
321 | int NClen = 0; |
---|
322 | NeuroClass *NC = NULL; |
---|
323 | for (int i = 0; i < Neuro::getClassCount(); i++) |
---|
324 | { |
---|
325 | const char *ncname = Neuro::getClass(i)->name.c_str(); |
---|
326 | int ncnamelen = (int)strlen(ncname); |
---|
327 | if (maxlen >= ncnamelen && ncnamelen > NClen && (strncmp(s, ncname, ncnamelen) == 0)) |
---|
328 | { |
---|
329 | NC = Neuro::getClass(i); |
---|
330 | NClen = ncnamelen; |
---|
331 | } |
---|
332 | } |
---|
333 | s += NClen; |
---|
334 | return NC; |
---|
335 | } |
---|
336 | |
---|
337 | Neuro *GenoOperators::findNeuro(const Model *m, const NeuroClass *nc) |
---|
338 | { |
---|
339 | if (!m) return NULL; |
---|
340 | for (int i = 0; i < m->getNeuroCount(); i++) |
---|
341 | if (m->getNeuro(i)->getClass() == nc) return m->getNeuro(i); |
---|
342 | return NULL; //neuron of class 'nc' was not found |
---|
343 | } |
---|
344 | |
---|
345 | int GenoOperators::neuroClassProp(char *&s, NeuroClass *nc, bool also_v1_N_props) |
---|
346 | { |
---|
347 | int len = (int)strlen(s); |
---|
348 | int Len = 0, I = -1; |
---|
349 | if (nc) |
---|
350 | { |
---|
351 | Param p = nc->getProperties(); |
---|
352 | for (int i = 0; i < p.getPropCount(); i++) |
---|
353 | { |
---|
354 | const char *n = p.id(i); |
---|
355 | int l = (int)strlen(n); |
---|
356 | if (len >= l && l > Len && (strncmp(s, n, l) == 0)) { I = 100 + i; Len = l; } |
---|
357 | if (also_v1_N_props) //recognize old properties symbols /=! |
---|
358 | { |
---|
359 | if (strcmp(n, "si") == 0) n = "/"; else |
---|
360 | if (strcmp(n, "in") == 0) n = "="; else |
---|
361 | if (strcmp(n, "fo") == 0) n = "!"; |
---|
362 | l = (int)strlen(n); |
---|
363 | if (len >= l && l > Len && (strncmp(s, n, l) == 0)) { I = 100 + i; Len = l; } |
---|
364 | } |
---|
365 | } |
---|
366 | } |
---|
367 | Neuro n; |
---|
368 | Param p = n.extraProperties(); |
---|
369 | for (int i = 0; i < p.getPropCount(); i++) |
---|
370 | { |
---|
371 | const char *n = p.id(i); |
---|
372 | int l = (int)strlen(n); |
---|
373 | if (len >= l && l > Len && (strncmp(s, n, l) == 0)) { I = i; Len = l; } |
---|
374 | } |
---|
375 | s += Len; |
---|
376 | return I; |
---|
377 | } |
---|
378 | |
---|
379 | bool GenoOperators::isWS(const char c) |
---|
380 | { |
---|
381 | return c == ' ' || c == '\n' || c == '\t' || c == '\r'; |
---|
382 | } |
---|
383 | |
---|
384 | void GenoOperators::skipWS(char *&s) |
---|
385 | { |
---|
386 | if (s == NULL) |
---|
387 | logMessage("GenoOperators", "skipWS", LOG_WARN, "NULL reference!"); |
---|
388 | else |
---|
389 | while (isWS(*s)) s++; |
---|
390 | } |
---|
391 | |
---|
392 | bool GenoOperators::areAlike(char *g1, char *g2) |
---|
393 | { |
---|
394 | while (*g1 || *g2) |
---|
395 | { |
---|
396 | skipWS(g1); |
---|
397 | skipWS(g2); |
---|
398 | if (*g1 != *g2) return false; //when difference |
---|
399 | if (!*g1 && !*g2) break; //both end |
---|
400 | g1++; |
---|
401 | g2++; |
---|
402 | } |
---|
403 | return true; //equal |
---|
404 | } |
---|
405 | |
---|
406 | char *GenoOperators::strchrn0(const char *str, char ch) |
---|
407 | { |
---|
408 | return ch == 0 ? NULL : strchr((char *)str, ch); |
---|
409 | } |
---|
410 | |
---|
411 | bool GenoOperators::canStartNeuroClassName(const char firstchar) |
---|
412 | { |
---|
413 | return isupper(firstchar) || firstchar == '|' || firstchar == '@' || firstchar == '*'; |
---|
414 | } |
---|