source: cpp/frams/genetics/oper_fx.cpp @ 753

Last change on this file since 753 was 751, checked in by Maciej Komosinski, 7 years ago

More versatile mutation function for numbers

  • Property svn:eol-style set to native
File size: 10.6 KB
RevLine 
[286]1// This file is a part of Framsticks SDK.  http://www.framsticks.com/
[749]2// Copyright (C) 1999-2018  Maciej Komosinski and Szymon Ulatowski.
[286]3// See LICENSE.txt for details.
[109]4
5#include <ctype.h>  //isupper()
[121]6#include "oper_fx.h"
[375]7#include <common/log.h>
[109]8#include <common/nonstd_math.h>
9#include <frams/util/rndutil.h>
10
[168]11static double distrib_force[] =   // for '!'
[109]12{
[168]13        3,             // distribution 0 -__/ +1
14        0.001, 0.2,    // "slow" neurons
15        0.001, 1,
16        1, 1,          // "fast" neurons
[109]17};
[168]18static double distrib_inertia[] =  // for '='
[109]19{
[168]20        2,             // distribution 0 |..- +1
21        0, 0,          // "fast" neurons
22        0.7, 0.98,
[109]23};
[168]24static double distrib_sigmo[] =  // for '/'
[109]25{
[168]26        5,             // distribution -999 -..-^-..- +999
27        -999, -999,    //"perceptron"
28        999, 999,
29        -5, -1,        // nonlinear
30        1, 5,
31        -1, 1,         // ~linear
[109]32};
33
34
[168]35int GenoOperators::roulette(const double *probtab, const int count)
[109]36{
[168]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;
[109]43}
44
[168]45bool GenoOperators::getMinMaxDef(ParamInterface *p, int i, double &mn, double &mx, double &def)
[109]46{
[168]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;
[743]52                defined = p->getMinMaxDouble(i, _mn, _mx, _def);
[168]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        {
[247]60                paInt _mn = 0, _mx = 1, _def = 0;
[743]61                defined = p->getMinMaxInt(i, _mn, _mx, _def);
[168]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;
[109]68}
69
[121]70int GenoOperators::selectRandomProperty(Neuro* n)
[109]71{
[168]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;
[109]78}
79
[168]80double GenoOperators::mutateNeuProperty(double current, Neuro *n, int i)
[109]81{
[751]82        if (i == -1) return mutateCreepNoLimit('f', current, 2, true); //i==-1: mutating weight of neural connection
[168]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;
[109]89}
90
[168]91bool GenoOperators::mutatePropertyNaive(ParamInterface &p, int i)
[109]92{
[168]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);
[109]96
[168]97        ExtValue ev;
98        p.get(i, ev);
[751]99        ev.setDouble(mutateCreep(p.type(i)[0], ev.getDouble(), mn, mx, true));
[168]100        p.set(i, ev);
101        return true;
[109]102}
103
[168]104bool GenoOperators::mutateProperty(ParamInterface &p, int i)
[109]105{
[168]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;
[109]112}
113
[168]114bool GenoOperators::getMutatedProperty(ParamInterface &p, int i, double oldval, double &newval)
[109]115{
[168]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                        {
[670]123                double mn, mx, df;
124                getMinMaxDef(&p, i, mn, mx, df);
[751]125                newval = mutateCreep(p.type(i)[0], oldval, mn, mx, true);
[168]126                        }
127        return true;
[109]128}
129
[751]130double GenoOperators::mutateCreepNoLimit(char type, double current, double stddev, bool limit_precision_3digits)
[109]131{
[751]132        double result = RndGen.Gauss(current, stddev);
133        if (type == 'd')
134        {
135                result = int(result + 0.5);
136                if (result == current) result += randomN(2) * 2 - 1; //force some change
137        }
138        else
139        {
140                if (limit_precision_3digits)
141                        result = floor(result * 1000 + 0.5) / 1000.0; //round
142        }
[168]143        return result;
[109]144}
145
[751]146double GenoOperators::mutateCreep(char type, double current, double mn, double mx, double stddev, bool limit_precision_3digits)
[109]147{
[751]148        double result = mutateCreepNoLimit(type, current, stddev, limit_precision_3digits);
149        //TODO consider that when boundary is touched (reflect+absorb below), the requested precision (3 digits) may change. Is it good or bad?
[146]150        //reflect:
151        if (result > mx) result = mx - (result - mx); else
[670]152                if (result < mn) result = mn + (mn - result);
[146]153        //absorb (just in case 'result' exceeded the allowed range so much):
[670]154        if (result > mx) result = mx; else
[146]155                if (result < mn) result = mn;
156        return result;
[109]157}
158
[751]159double GenoOperators::mutateCreep(char type, double current, double mn, double mx, bool limit_precision_3digits)
160{
161        double stddev = (mx - mn) / 2 / 5; // magic arbitrary formula for stddev, which becomes /halfinterval, 5 times narrower
162        return mutateCreep(type, current, mn, mx, stddev, limit_precision_3digits);
163}
164
[146]165void GenoOperators::setIntFromDoubleWithProbabilisticDithering(ParamInterface &p, int index, double value) //TODO
166{
[749]167        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!)
[146]168}
169
[749]170void GenoOperators::linearMix(vector<double> &p1, vector<double> &p2, double proportion)
171{
172        if (p1.size() != p2.size())
173        {
174                logPrintf("GenoOperators", "linearMix", LOG_ERROR, "Cannot mix vectors of different length (%d and %d)", p1.size(), p2.size());
175                return;
176        }
177        for (unsigned int i = 0; i < p1.size(); i++)
178        {
179                double v1 = p1[i];
180                double v2 = p2[i];
181                p1[i] = v1*proportion + v2*(1 - proportion);
182                p2[i] = v2*proportion + v1*(1 - proportion);
183        }
184}
185
[146]186void GenoOperators::linearMix(ParamInterface &p1, int i1, ParamInterface &p2, int i2, double proportion)
187{
[158]188        char type1 = p1.type(i1)[0];
189        char type2 = p2.type(i2)[0];
190        if (type1 == 'f' && type2 == 'f')
[146]191        {
192                double v1 = p1.getDouble(i1);
193                double v2 = p2.getDouble(i2);
194                p1.setDouble(i1, v1*proportion + v2*(1 - proportion));
195                p2.setDouble(i2, v2*proportion + v1*(1 - proportion));
196        }
[158]197        else
198                if (type1 == 'd' && type2 == 'd')
199                {
[670]200                int v1 = p1.getInt(i1);
201                int v2 = p2.getInt(i2);
202                setIntFromDoubleWithProbabilisticDithering(p1, i1, v1*proportion + v2*(1 - proportion));
203                setIntFromDoubleWithProbabilisticDithering(p2, i2, v2*proportion + v1*(1 - proportion));
[158]204                }
205                else
[375]206                        logPrintf("GenoOperators", "linearMix", LOG_WARN, "Cannot mix values of types '%c' and '%c'", type1, type2);
[146]207}
208
[121]209NeuroClass* GenoOperators::getRandomNeuroClass()
[109]210{
[673]211        vector<NeuroClass*> active;
[168]212        for (int i = 0; i < Neuro::getClassCount(); i++)
[673]213                if (Neuro::getClass(i)->genactive)
214                        active.push_back(Neuro::getClass(i));
215        if (active.size() == 0) return NULL; else return active[randomN(active.size())];
[109]216}
217
[673]218int GenoOperators::getRandomNeuroClassWithOutput(const vector<NeuroClass*>& NClist)
219{
220        vector<int> allowed;
221        for (size_t i = 0; i < NClist.size(); i++)
222                if (NClist[i]->getPreferredOutput() != 0) //this NeuroClass provides output
223                        allowed.push_back(i);
224        if (allowed.size() == 0) return -1; else return allowed[randomN(allowed.size())];
225}
226
227int GenoOperators::getRandomNeuroClassWithInput(const vector<NeuroClass*>& NClist)
228{
229        vector<int> allowed;
230        for (size_t i = 0; i < NClist.size(); i++)
231                if (NClist[i]->getPreferredInputs() != 0) //this NeuroClass wants one input connection or more                 
232                        allowed.push_back(i);
233        if (allowed.size() == 0) return -1; else return allowed[randomN(allowed.size())];
234}
235
236int GenoOperators::getRandomChar(const char *choices, const char *excluded)
237{
238        int allowed_count = 0;
239        for (size_t i = 0; i < strlen(choices); i++) if (!strchrn0(excluded, choices[i])) allowed_count++;
240        if (allowed_count == 0) return -1; //no char is allowed
241        int rnd_index = randomN(allowed_count) + 1;
242        allowed_count = 0;
243        for (size_t i = 0; i < strlen(choices); i++)
244        {
245                if (!strchrn0(excluded, choices[i])) allowed_count++;
246                if (allowed_count == rnd_index) return i;
247        }
248        return -1; //never happens
249}
250
[121]251NeuroClass* GenoOperators::parseNeuroClass(char*& s)
[109]252{
[670]253        int maxlen = (int)strlen(s);
254        int NClen = 0;
255        NeuroClass *NC = NULL;
[168]256        for (int i = 0; i<Neuro::getClassCount(); i++)
257        {
[670]258                const char *ncname = Neuro::getClass(i)->name.c_str();
259                int ncnamelen = (int)strlen(ncname);
260                if (maxlen >= ncnamelen && ncnamelen>NClen && (strncmp(s, ncname, ncnamelen) == 0))
261                {
262                        NC = Neuro::getClass(i);
263                        NClen = ncnamelen;
264                }
[168]265        }
[670]266        s += NClen;
267        return NC;
[109]268}
269
[168]270Neuro* GenoOperators::findNeuro(const Model *m, const NeuroClass *nc)
[109]271{
[168]272        if (!m) return NULL;
273        for (int i = 0; i < m->getNeuroCount(); i++)
274                if (m->getNeuro(i)->getClass() == nc) return m->getNeuro(i);
275        return NULL; //neuron of class 'nc' was not found
[109]276}
277
[168]278int GenoOperators::neuroClassProp(char*& s, NeuroClass *nc, bool also_v1_N_props)
[109]279{
[247]280        int len = (int)strlen(s);
[168]281        int Len = 0, I = -1;
282        if (nc)
283        {
284                Param p = nc->getProperties();
285                for (int i = 0; i<p.getPropCount(); i++)
286                {
287                        const char *n = p.id(i);
[247]288                        int l = (int)strlen(n);
[168]289                        if (len >= l && l>Len && (strncmp(s, n, l) == 0)) { I = 100 + i; Len = l; }
290                        if (also_v1_N_props) //recognize old properties symbols /=!
291                        {
292                                if (strcmp(n, "si") == 0) n = "/"; else
293                                        if (strcmp(n, "in") == 0) n = "="; else
294                                                if (strcmp(n, "fo") == 0) n = "!";
[247]295                                l = (int)strlen(n);
[168]296                                if (len >= l && l > Len && (strncmp(s, n, l) == 0)) { I = 100 + i; Len = l; }
297                        }
298                }
299        }
300        Neuro n;
301        Param p = n.extraProperties();
302        for (int i = 0; i<p.getPropCount(); i++)
303        {
304                const char *n = p.id(i);
[247]305                int l = (int)strlen(n);
[168]306                if (len >= l && l>Len && (strncmp(s, n, l) == 0)) { I = i; Len = l; }
307        }
308        s += Len;
309        return I;
[109]310}
311
[121]312bool GenoOperators::isWS(const char c)
[168]313{
314        return c == ' ' || c == '\n' || c == '\t' || c == '\r';
315}
[109]316
[121]317void GenoOperators::skipWS(char *&s)
[158]318{
[168]319        if (s == NULL)
[375]320                logMessage("GenoOperators", "skipWS", LOG_WARN, "NULL reference!");
[158]321        else
[670]322                while (isWS(*s)) s++;
[109]323}
324
[168]325bool GenoOperators::areAlike(char *g1, char *g2)
[109]326{
327        while (*g1 || *g2)
328        {
329                skipWS(g1);
330                skipWS(g2);
331                if (*g1 != *g2) return false; //when difference
[168]332                if (!*g1 && !*g2) break; //both end
333                g1++;
334                g2++;
[109]335        }
336        return true; //equal
337}
338
[168]339char* GenoOperators::strchrn0(const char *str, char ch)
340{
341        return ch == 0 ? NULL : strchr((char*)str, ch);
342}
[109]343
[121]344bool GenoOperators::isNeuroClassName(const char firstchar)
[109]345{
[168]346        return isupper(firstchar) || firstchar == '|' || firstchar == '@' || firstchar == '*';
[109]347}
348
Note: See TracBrowser for help on using the repository browser.