1 | // This file is a part of Framsticks SDK. http://www.framsticks.com/ |
---|
2 | // Copyright (C) 1999-2015 Maciej Komosinski and Szymon Ulatowski. |
---|
3 | // See LICENSE.txt for details. |
---|
4 | |
---|
5 | #include <ctype.h> //isupper() |
---|
6 | #include "oper_fx.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 = rnd01*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->getMinMax(i, _mn, _mx, _def); |
---|
53 | if (defined == 1) _mx = _mn + 1.0; |
---|
54 | if (_mx < _mn && defined == 3) _mn = _mx = _def; //only default was defined, let's assume min=max=default |
---|
55 | if (defined < 3) _def = (_mn + _mx) / 2.0; |
---|
56 | mn = _mn; mx = _mx; def = _def; |
---|
57 | } |
---|
58 | if (p->type(i)[0] == 'd') |
---|
59 | { |
---|
60 | paInt _mn = 0, _mx = 1, _def = 0; |
---|
61 | defined = p->getMinMax(i, _mn, _mx, _def); |
---|
62 | if (defined == 1) _mx = _mn + 1; |
---|
63 | if (_mx < _mn && defined == 3) _mn = _mx = _def; //only default was defined, let's assume min=max=default |
---|
64 | if (defined < 3) _def = (_mn + _mx) / 2; |
---|
65 | mn = _mn; mx = _mx; def = _def; |
---|
66 | } |
---|
67 | return defined == 3; |
---|
68 | } |
---|
69 | |
---|
70 | int GenoOperators::selectRandomProperty(Neuro* n) |
---|
71 | { |
---|
72 | int neuext = n->extraProperties().getPropCount(), |
---|
73 | neucls = n->getClass() == NULL ? 0 : n->getClass()->getProperties().getPropCount(); |
---|
74 | if (neuext + neucls == 0) return -1; //no properties in this neuron |
---|
75 | int index = randomN(neuext + neucls); |
---|
76 | if (index >= neuext) index = index - neuext + 100; |
---|
77 | return index; |
---|
78 | } |
---|
79 | |
---|
80 | double GenoOperators::mutateNeuProperty(double current, Neuro *n, int i) |
---|
81 | { |
---|
82 | if (i == -1) return mutateCreepNoLimit('f', current, -10, 10); //i==-1: mutating weight of neural connection |
---|
83 | Param p; |
---|
84 | if (i >= 100) { i -= 100; p = n->getClass()->getProperties(); } |
---|
85 | else p = n->extraProperties(); |
---|
86 | double newval = current; |
---|
87 | /*bool ok=*/getMutatedProperty(p, i, current, newval); |
---|
88 | return newval; |
---|
89 | } |
---|
90 | |
---|
91 | bool GenoOperators::mutatePropertyNaive(ParamInterface &p, int i) |
---|
92 | { |
---|
93 | double mn, mx, df; |
---|
94 | if (p.type(i)[0] != 'f' && p.type(i)[0] != 'd') return false; //don't know how to mutate |
---|
95 | getMinMaxDef(&p, i, mn, mx, df); |
---|
96 | |
---|
97 | ExtValue ev; |
---|
98 | p.get(i, ev); |
---|
99 | ev.setDouble(mutateCreep(p.type(i)[0], ev.getDouble(), mn, mx)); |
---|
100 | p.set(i, ev); |
---|
101 | return true; |
---|
102 | } |
---|
103 | |
---|
104 | bool GenoOperators::mutateProperty(ParamInterface &p, int i) |
---|
105 | { |
---|
106 | double newval; |
---|
107 | ExtValue ev; |
---|
108 | p.get(i, ev); |
---|
109 | bool ok = getMutatedProperty(p, i, ev.getDouble(), newval); |
---|
110 | if (ok) { ev.setDouble(newval); p.set(i, ev); } |
---|
111 | return ok; |
---|
112 | } |
---|
113 | |
---|
114 | bool GenoOperators::getMutatedProperty(ParamInterface &p, int i, double oldval, double &newval) |
---|
115 | { |
---|
116 | newval = 0; |
---|
117 | if (p.type(i)[0] != 'f' && p.type(i)[0] != 'd') return false; //don't know how to mutate |
---|
118 | const char *n = p.id(i), *na = p.name(i); |
---|
119 | if (strcmp(n, "si") == 0 && strcmp(na, "Sigmoid") == 0) newval = CustomRnd(distrib_sigmo); else |
---|
120 | if (strcmp(n, "in") == 0 && strcmp(na, "Inertia") == 0) newval = CustomRnd(distrib_inertia); else |
---|
121 | if (strcmp(n, "fo") == 0 && strcmp(na, "Force") == 0) newval = CustomRnd(distrib_force); else |
---|
122 | { |
---|
123 | double mn, mx, df; |
---|
124 | getMinMaxDef(&p, i, mn, mx, df); |
---|
125 | newval = mutateCreep(p.type(i)[0], oldval, mn, mx); |
---|
126 | } |
---|
127 | return true; |
---|
128 | } |
---|
129 | |
---|
130 | double GenoOperators::mutateCreepNoLimit(char type, double current, double mn, double mx) |
---|
131 | { |
---|
132 | double result = RndGen.Gauss(current, (mx - mn) / 2 / 5); // /halfinterval, 5 times narrower |
---|
133 | if (type == 'd') { result = int(result + 0.5); if (result == current) result += randomN(2) * 2 - 1; } |
---|
134 | else result = floor(result * 1000 + 0.5) / 1000.0; //round |
---|
135 | return result; |
---|
136 | } |
---|
137 | |
---|
138 | double GenoOperators::mutateCreep(char type, double current, double mn, double mx) |
---|
139 | { |
---|
140 | double result = mutateCreepNoLimit(type, current, mn, mx); //TODO consider that when boundary is touched (reflect/absorb below), the default precision (3 digits) may change. Is it good or bad? |
---|
141 | //reflect: |
---|
142 | if (result > mx) result = mx - (result - mx); else |
---|
143 | if (result < mn) result = mn + (mn - result); |
---|
144 | //absorb (just in case 'result' exceeded the allowed range so much): |
---|
145 | if (result > mx) result = mx; else |
---|
146 | if (result < mn) result = mn; |
---|
147 | return result; |
---|
148 | } |
---|
149 | |
---|
150 | void GenoOperators::setIntFromDoubleWithProbabilisticDithering(ParamInterface &p, int index, double value) //TODO |
---|
151 | { |
---|
152 | p.setInt(index, (paInt)value); //TODO value=2.5 will result in 2 but we want it to be 2 or 3 with equal probability. value=2.1 would be mostly 2, rarely 3. Careful with negative values (test it!) |
---|
153 | } |
---|
154 | |
---|
155 | void GenoOperators::linearMix(ParamInterface &p1, int i1, ParamInterface &p2, int i2, double proportion) |
---|
156 | { |
---|
157 | char type1 = p1.type(i1)[0]; |
---|
158 | char type2 = p2.type(i2)[0]; |
---|
159 | if (type1 == 'f' && type2 == 'f') |
---|
160 | { |
---|
161 | double v1 = p1.getDouble(i1); |
---|
162 | double v2 = p2.getDouble(i2); |
---|
163 | p1.setDouble(i1, v1*proportion + v2*(1 - proportion)); |
---|
164 | p2.setDouble(i2, v2*proportion + v1*(1 - proportion)); |
---|
165 | } |
---|
166 | else |
---|
167 | if (type1 == 'd' && type2 == 'd') |
---|
168 | { |
---|
169 | int v1 = p1.getInt(i1); |
---|
170 | int v2 = p2.getInt(i2); |
---|
171 | setIntFromDoubleWithProbabilisticDithering(p1, i1, v1*proportion + v2*(1 - proportion)); |
---|
172 | setIntFromDoubleWithProbabilisticDithering(p2, i2, v2*proportion + v1*(1 - proportion)); |
---|
173 | } |
---|
174 | else |
---|
175 | logPrintf("GenoOperators", "linearMix", LOG_WARN, "Cannot mix values of types '%c' and '%c'", type1, type2); |
---|
176 | } |
---|
177 | |
---|
178 | NeuroClass* GenoOperators::getRandomNeuroClass() |
---|
179 | { |
---|
180 | vector<NeuroClass*> active; |
---|
181 | for (int i = 0; i < Neuro::getClassCount(); i++) |
---|
182 | if (Neuro::getClass(i)->genactive) |
---|
183 | active.push_back(Neuro::getClass(i)); |
---|
184 | if (active.size() == 0) return NULL; else return active[randomN(active.size())]; |
---|
185 | } |
---|
186 | |
---|
187 | int GenoOperators::getRandomNeuroClassWithOutput(const vector<NeuroClass*>& NClist) |
---|
188 | { |
---|
189 | vector<int> allowed; |
---|
190 | for (size_t i = 0; i < NClist.size(); i++) |
---|
191 | if (NClist[i]->getPreferredOutput() != 0) //this NeuroClass provides output |
---|
192 | allowed.push_back(i); |
---|
193 | if (allowed.size() == 0) return -1; else return allowed[randomN(allowed.size())]; |
---|
194 | } |
---|
195 | |
---|
196 | int GenoOperators::getRandomNeuroClassWithInput(const vector<NeuroClass*>& NClist) |
---|
197 | { |
---|
198 | vector<int> allowed; |
---|
199 | for (size_t i = 0; i < NClist.size(); i++) |
---|
200 | if (NClist[i]->getPreferredInputs() != 0) //this NeuroClass wants one input connection or more |
---|
201 | allowed.push_back(i); |
---|
202 | if (allowed.size() == 0) return -1; else return allowed[randomN(allowed.size())]; |
---|
203 | } |
---|
204 | |
---|
205 | int GenoOperators::getRandomChar(const char *choices, const char *excluded) |
---|
206 | { |
---|
207 | int allowed_count = 0; |
---|
208 | for (size_t i = 0; i < strlen(choices); i++) if (!strchrn0(excluded, choices[i])) allowed_count++; |
---|
209 | if (allowed_count == 0) return -1; //no char is allowed |
---|
210 | int rnd_index = randomN(allowed_count) + 1; |
---|
211 | allowed_count = 0; |
---|
212 | for (size_t i = 0; i < strlen(choices); i++) |
---|
213 | { |
---|
214 | if (!strchrn0(excluded, choices[i])) allowed_count++; |
---|
215 | if (allowed_count == rnd_index) return i; |
---|
216 | } |
---|
217 | return -1; //never happens |
---|
218 | } |
---|
219 | |
---|
220 | NeuroClass* GenoOperators::parseNeuroClass(char*& s) |
---|
221 | { |
---|
222 | int maxlen = (int)strlen(s); |
---|
223 | int NClen = 0; |
---|
224 | NeuroClass *NC = NULL; |
---|
225 | for (int i = 0; i<Neuro::getClassCount(); i++) |
---|
226 | { |
---|
227 | const char *ncname = Neuro::getClass(i)->name.c_str(); |
---|
228 | int ncnamelen = (int)strlen(ncname); |
---|
229 | if (maxlen >= ncnamelen && ncnamelen>NClen && (strncmp(s, ncname, ncnamelen) == 0)) |
---|
230 | { |
---|
231 | NC = Neuro::getClass(i); |
---|
232 | NClen = ncnamelen; |
---|
233 | } |
---|
234 | } |
---|
235 | s += NClen; |
---|
236 | return NC; |
---|
237 | } |
---|
238 | |
---|
239 | Neuro* GenoOperators::findNeuro(const Model *m, const NeuroClass *nc) |
---|
240 | { |
---|
241 | if (!m) return NULL; |
---|
242 | for (int i = 0; i < m->getNeuroCount(); i++) |
---|
243 | if (m->getNeuro(i)->getClass() == nc) return m->getNeuro(i); |
---|
244 | return NULL; //neuron of class 'nc' was not found |
---|
245 | } |
---|
246 | |
---|
247 | int GenoOperators::neuroClassProp(char*& s, NeuroClass *nc, bool also_v1_N_props) |
---|
248 | { |
---|
249 | int len = (int)strlen(s); |
---|
250 | int Len = 0, I = -1; |
---|
251 | if (nc) |
---|
252 | { |
---|
253 | Param p = nc->getProperties(); |
---|
254 | for (int i = 0; i<p.getPropCount(); i++) |
---|
255 | { |
---|
256 | const char *n = p.id(i); |
---|
257 | int l = (int)strlen(n); |
---|
258 | if (len >= l && l>Len && (strncmp(s, n, l) == 0)) { I = 100 + i; Len = l; } |
---|
259 | if (also_v1_N_props) //recognize old properties symbols /=! |
---|
260 | { |
---|
261 | if (strcmp(n, "si") == 0) n = "/"; else |
---|
262 | if (strcmp(n, "in") == 0) n = "="; else |
---|
263 | if (strcmp(n, "fo") == 0) n = "!"; |
---|
264 | l = (int)strlen(n); |
---|
265 | if (len >= l && l > Len && (strncmp(s, n, l) == 0)) { I = 100 + i; Len = l; } |
---|
266 | } |
---|
267 | } |
---|
268 | } |
---|
269 | Neuro n; |
---|
270 | Param p = n.extraProperties(); |
---|
271 | for (int i = 0; i<p.getPropCount(); i++) |
---|
272 | { |
---|
273 | const char *n = p.id(i); |
---|
274 | int l = (int)strlen(n); |
---|
275 | if (len >= l && l>Len && (strncmp(s, n, l) == 0)) { I = i; Len = l; } |
---|
276 | } |
---|
277 | s += Len; |
---|
278 | return I; |
---|
279 | } |
---|
280 | |
---|
281 | bool GenoOperators::isWS(const char c) |
---|
282 | { |
---|
283 | return c == ' ' || c == '\n' || c == '\t' || c == '\r'; |
---|
284 | } |
---|
285 | |
---|
286 | void GenoOperators::skipWS(char *&s) |
---|
287 | { |
---|
288 | if (s == NULL) |
---|
289 | logMessage("GenoOperators", "skipWS", LOG_WARN, "NULL reference!"); |
---|
290 | else |
---|
291 | while (isWS(*s)) s++; |
---|
292 | } |
---|
293 | |
---|
294 | bool GenoOperators::areAlike(char *g1, char *g2) |
---|
295 | { |
---|
296 | while (*g1 || *g2) |
---|
297 | { |
---|
298 | skipWS(g1); |
---|
299 | skipWS(g2); |
---|
300 | if (*g1 != *g2) return false; //when difference |
---|
301 | if (!*g1 && !*g2) break; //both end |
---|
302 | g1++; |
---|
303 | g2++; |
---|
304 | } |
---|
305 | return true; //equal |
---|
306 | } |
---|
307 | |
---|
308 | char* GenoOperators::strchrn0(const char *str, char ch) |
---|
309 | { |
---|
310 | return ch == 0 ? NULL : strchr((char*)str, ch); |
---|
311 | } |
---|
312 | |
---|
313 | bool GenoOperators::isNeuroClassName(const char firstchar) |
---|
314 | { |
---|
315 | return isupper(firstchar) || firstchar == '|' || firstchar == '@' || firstchar == '*'; |
---|
316 | } |
---|
317 | |
---|