Changeset 168 for cpp/frams/genetics/oper_fx.cpp
 Timestamp:
 03/11/14 14:45:29 (10 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

cpp/frams/genetics/oper_fx.cpp
r158 r168 9 9 #include <frams/util/rndutil.h> 10 10 11 static double distrib_force[] = // for '!'12 { 13 3,// distribution 0 __/ +114 0.001, 0.2,// "slow" neurons15 0.001, 1,16 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 17 }; 18 static double distrib_inertia[] = // for '='19 { 20 2,// distribution 0 .. +121 0, 0,// "fast" neurons22 0.7, 0.98,18 static double distrib_inertia[] = // for '=' 19 { 20 2, // distribution 0 .. +1 21 0, 0, // "fast" neurons 22 0.7, 0.98, 23 23 }; 24 static double distrib_sigmo[] = // for '/'25 { 26 5,// distribution 999 ..^.. +99927 999, 999,//"perceptron"28 29 5, 1,// nonlinear30 31 1, 1,// ~linear24 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 32 }; 33 33 34 34 35 int GenoOperators::roulette(const double *probtab, const int count)36 { 37 double sum=0;38 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 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=default55 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 long _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=default64 if (defined<3) _def=(_mn+_mx)/2;65 mn=_mn; mx=_mx; def=_def;66 67 return defined==3;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 long _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 68 } 69 69 70 70 int GenoOperators::selectRandomProperty(Neuro* n) 71 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 neuron75 int index=randomN(neuext+neucls);76 if (index>=neuext) index=indexneuext+100;77 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 connection83 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 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 mutate95 getMinMaxDef(&p,i,mn,mx,df);96 97 98 p.get(i,ev);99 ev.setDouble(mutateCreep(p.type(i)[0],ev.getDouble(),mn,mx));100 p.set(i,ev);101 102 } 103 104 bool GenoOperators::mutateProperty(ParamInterface &p, int i)105 { 106 107 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 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 mutate118 const char *n=p.id(i),*na=p.name(i);119 if (strcmp(n,"si")==0 && strcmp(na,"Sigmoid")==0) newval=CustomRnd(distrib_sigmo); else120 if (strcmp(n,"in")==0 && strcmp(na,"Inertia")==0) newval=CustomRnd(distrib_inertia); else121 if (strcmp(n,"fo")==0 && strcmp(na,"Force")==0) newval=CustomRnd(distrib_force); else122 123 double mn,mx,df;124 getMinMaxDef(&p,i,mn,mx,df);125 newval=mutateCreep(p.type(i)[0],oldval,mn,mx);126 127 128 } 129 130 double GenoOperators::mutateCreepNoLimit(char type, double current,double mn,double mx)131 { 132 double result=RndGen.Gauss(current,(mxmn)/2/5); // /halfinterval, 5 times narrower133 if (type=='d') {result=int(result+0.5); if (result==current) result+=randomN(2)*21;}134 else result=floor(result*1000+0.5)/1000.0; //round135 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 136 } 137 137 … … 180 180 { 181 181 SListTempl<NeuroClass*> active; 182 for(int i=0;i<Neuro::getClassCount();i++)183 if (Neuro::getClass(i)>genactive) active+=Neuro::getClass(i);184 if (!active==0) return NULL; else return active(randomN(!active));182 for (int i = 0; i < Neuro::getClassCount(); i++) 183 if (Neuro::getClass(i)>genactive) active += Neuro::getClass(i); 184 if (!active == 0) return NULL; else return active(randomN(!active)); 185 185 } 186 186 187 187 NeuroClass* GenoOperators::parseNeuroClass(char*& s) 188 188 { 189 int len=strlen(s);190 int Len=0;191 NeuroClass *I=NULL;192 for(int i=0;i<Neuro::getClassCount();i++)193 194 const char *n=Neuro::getClass(i)>name;195 int l=strlen(n);196 if (len>=l && l>Len && (strncmp(s,n,l)==0)) {I=Neuro::getClass(i); Len=l;}197 198 s+=Len;199 200 } 201 202 Neuro* GenoOperators::findNeuro(const Model *m, const NeuroClass *nc)203 { 204 205 for(int i=0;i<m>getNeuroCount();i++)206 if (m>getNeuro(i)>getClass()==nc) return m>getNeuro(i);207 208 } 209 210 int GenoOperators::neuroClassProp(char*& s, NeuroClass *nc,bool also_v1_N_props)211 { 212 int len=strlen(s);213 int Len=0,I=1;214 215 216 Param p=nc>getProperties();217 for(int i=0;i<p.getPropCount();i++)218 219 const char *n=p.id(i);220 int l=strlen(n);221 if (len>=l && l>Len && (strncmp(s,n,l)==0)) {I=100+i; Len=l;}222 223 224 if (strcmp(n,"si")==0) n="/"; else225 if (strcmp(n,"in")==0) n="="; else226 if (strcmp(n,"fo")==0) n="!";227 l=strlen(n);228 if (len>=l && l>Len && (strncmp(s,n,l)==0)) {I=100+i; Len=l;}229 230 231 232 233 Param p=n.extraProperties();234 for(int i=0;i<p.getPropCount();i++)235 236 const char *n=p.id(i);237 int l=strlen(n);238 if (len>=l && l>Len && (strncmp(s,n,l)==0)) {I=i; Len=l;}239 240 s+=Len;241 189 int len = strlen(s); 190 int Len = 0; 191 NeuroClass *I = NULL; 192 for (int i = 0; i<Neuro::getClassCount(); i++) 193 { 194 const char *n = Neuro::getClass(i)>name; 195 int l = strlen(n); 196 if (len >= l && l>Len && (strncmp(s, n, l) == 0)) { I = Neuro::getClass(i); Len = l; } 197 } 198 s += Len; 199 return I; 200 } 201 202 Neuro* GenoOperators::findNeuro(const Model *m, const NeuroClass *nc) 203 { 204 if (!m) return NULL; 205 for (int i = 0; i < m>getNeuroCount(); i++) 206 if (m>getNeuro(i)>getClass() == nc) return m>getNeuro(i); 207 return NULL; //neuron of class 'nc' was not found 208 } 209 210 int GenoOperators::neuroClassProp(char*& s, NeuroClass *nc, bool also_v1_N_props) 211 { 212 int len = strlen(s); 213 int Len = 0, I = 1; 214 if (nc) 215 { 216 Param p = nc>getProperties(); 217 for (int i = 0; i<p.getPropCount(); i++) 218 { 219 const char *n = p.id(i); 220 int l = strlen(n); 221 if (len >= l && l>Len && (strncmp(s, n, l) == 0)) { I = 100 + i; Len = l; } 222 if (also_v1_N_props) //recognize old properties symbols /=! 223 { 224 if (strcmp(n, "si") == 0) n = "/"; else 225 if (strcmp(n, "in") == 0) n = "="; else 226 if (strcmp(n, "fo") == 0) n = "!"; 227 l = strlen(n); 228 if (len >= l && l > Len && (strncmp(s, n, l) == 0)) { I = 100 + i; Len = l; } 229 } 230 } 231 } 232 Neuro n; 233 Param p = n.extraProperties(); 234 for (int i = 0; i<p.getPropCount(); i++) 235 { 236 const char *n = p.id(i); 237 int l = strlen(n); 238 if (len >= l && l>Len && (strncmp(s, n, l) == 0)) { I = i; Len = l; } 239 } 240 s += Len; 241 return I; 242 242 } 243 243 244 244 bool GenoOperators::isWS(const char c) 245 {return c==' '  c=='\n'  c=='\t'  c=='\r';} 245 { 246 return c == ' '  c == '\n'  c == '\t'  c == '\r'; 247 } 246 248 247 249 void GenoOperators::skipWS(char *&s) 248 250 { 249 if (s ==NULL)251 if (s == NULL) 250 252 FramMessage("GenoOperators", "skipWS", "NULL reference!", FMLV_WARN); 251 253 else … … 253 255 } 254 256 255 bool GenoOperators::areAlike(char *g1, char *g2)257 bool GenoOperators::areAlike(char *g1, char *g2) 256 258 { 257 259 while (*g1  *g2) … … 260 262 skipWS(g2); 261 263 if (*g1 != *g2) return false; //when difference 262 263 264 264 if (!*g1 && !*g2) break; //both end 265 g1++; 266 g2++; 265 267 } 266 268 return true; //equal 267 269 } 268 270 269 char* GenoOperators::strchrn0(const char *str,char ch) 270 { return ch==0?NULL:strchr((char*)str,ch); } 271 char* GenoOperators::strchrn0(const char *str, char ch) 272 { 273 return ch == 0 ? NULL : strchr((char*)str, ch); 274 } 271 275 272 276 bool GenoOperators::isNeuroClassName(const char firstchar) 273 277 { 274 return isupper(firstchar)  firstchar==''  firstchar=='@'  firstchar=='*';275 } 276 278 return isupper(firstchar)  firstchar == ''  firstchar == '@'  firstchar == '*'; 279 } 280
Note: See TracChangeset
for help on using the changeset viewer.