Changeset 168 for cpp/frams/genetics/genman.cpp
- Timestamp:
- 03/11/14 14:45:29 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
cpp/frams/genetics/genman.cpp
r145 r168 227 227 Geno GenMan::Validate(const Geno& geny) 228 228 { 229 char format =geny.getFormat();230 GenoOperators *gf =getOper_f(format);231 if (gf ==NULL)232 return Geno(SString::empty(), -1,SString::empty(),SString::sprintf("GENOPER_NOOPER: Validate(): don't know how to handle genetic format %c",format));233 char *g2 =strdup(geny.getGene()); //copy for validation234 int res =gf->validate(g2);235 SString sg2 =g2;229 char format = geny.getFormat(); 230 GenoOperators *gf = getOper_f(format); 231 if (gf == NULL) 232 return Geno(SString::empty(), -1, SString::empty(), SString::sprintf("GENOPER_NOOPER: Validate(): don't know how to handle genetic format %c", format)); 233 char *g2 = strdup(geny.getGene()); //copy for validation 234 int res = gf->validate(g2); 235 SString sg2 = g2; 236 236 free(g2); 237 if (res ==GENOPER_OK)238 return Geno(sg2, format,geny.getName(),geny.getComment());237 if (res == GENOPER_OK) 238 return Geno(sg2, format, geny.getName(), geny.getComment()); 239 239 else 240 return Geno(SString::empty(), -1,SString::empty(),SString::sprintf("GENOPER_NOOPER: validate() for format %c returned invalid value",format));240 return Geno(SString::empty(), -1, SString::empty(), SString::sprintf("GENOPER_NOOPER: validate() for format %c returned invalid value", format)); 241 241 } 242 242 … … 245 245 float chg; //how many changes 246 246 int method; //mutation method 247 char format =g.getFormat();248 GenoOperators *gf =getOper_f(format);249 if (gf ==NULL)250 return Geno(SString::empty(), -1,SString::empty(),SString::sprintf("GENOPER_NOOPER: Mutate(): don't know how to handle genetic format %c",format));251 Geno gv =g;252 bool canvalidate =true;253 if (testValidity(gv, canvalidate)>0 && canvalidate==false)254 return Geno("", -1,"","GENOPER_OPFAIL: Mutate(): cannot validate invalid source genotype");255 bool ok =false;256 int pcount =count;247 char format = g.getFormat(); 248 GenoOperators *gf = getOper_f(format); 249 if (gf == NULL) 250 return Geno(SString::empty(), -1, SString::empty(), SString::sprintf("GENOPER_NOOPER: Mutate(): don't know how to handle genetic format %c", format)); 251 Geno gv = g; 252 bool canvalidate = true; 253 if (testValidity(gv, canvalidate) > 0 && canvalidate == false) 254 return Geno("", -1, "", "GENOPER_OPFAIL: Mutate(): cannot validate invalid source genotype"); 255 bool ok = false; 256 int pcount = count; 257 257 while (!ok) 258 258 { 259 char *gn =strdup(gv.getGene()); //copy for mutation260 chg =0;261 if (gf->mutate(gn, chg,method)==GENOPER_OK)259 char *gn = strdup(gv.getGene()); //copy for mutation 260 chg = 0; 261 if (gf->mutate(gn, chg, method) == GENOPER_OK) 262 262 { 263 263 ErrorHandler eh(ErrorHandler::StoreFirstMessage); //mute testValidity() 264 Geno G(gn,gv.getFormat(),"",""); 265 canvalidate=true; 266 int res=testValidity(G,canvalidate); 267 if (res==GENOPER_OK && canvalidate==false) {valid_m++; ok=true;} else 268 if (res>0 && canvalidate==false) invalid_m++; else 269 {validated_m++; ok=true;} 270 if (ok) gv=G; 271 } else failed_m++; 264 Geno G(gn, gv.getFormat(), "", ""); 265 canvalidate = true; 266 int res = testValidity(G, canvalidate); 267 if (res == GENOPER_OK && canvalidate == false) { valid_m++; ok = true; } 268 else 269 if (res > 0 && canvalidate == false) invalid_m++; else 270 { 271 validated_m++; ok = true; 272 } 273 if (ok) gv = G; 274 } 275 else failed_m++; 272 276 free(gn); 273 277 count++; 274 if (!ok && (count -pcount>100))275 { 276 FMprintf("GenMan", "Mutate",2,"Tried 100x and failed: %s",(const char*)g.getGene());277 return Geno("", -1,"","GENOPER_OPFAIL: Mutate() tried 100x and failed");278 } 279 } 280 mutchg +=chg;281 if (history) saveLink((const char*)g.getGene(), (const char*)gv.getGene(),chg);278 if (!ok && (count - pcount > 100)) 279 { 280 FMprintf("GenMan", "Mutate", 2, "Tried 100x and failed: %s", (const char*)g.getGene()); 281 return Geno("", -1, "", "GENOPER_OPFAIL: Mutate() tried 100x and failed"); 282 } 283 } 284 mutchg += chg; 285 if (history) saveLink((const char*)g.getGene(), (const char*)gv.getGene(), chg); 282 286 SString mutinfo; 283 if (extmutinfo == 0) mutinfo = SString::sprintf("%.2f%% mutation of '%s'", 100*chg,(const char*)g.getName()); else287 if (extmutinfo == 0) mutinfo = SString::sprintf("%.2f%% mutation of '%s'", 100 * chg, (const char*)g.getName()); else 284 288 if (extmutinfo == 1) mutinfo = SString::sprintf("%.2f%% mutation(%d) of '%s'", 100 * chg, method, (const char*)g.getName()); else 285 289 mutinfo = SString::sprintf("%.2f%% mutation(%s) of '%s'", 100 * chg, gf->mutation_method_names ? gf->mutation_method_names[method] : "unspecified method name", (const char*)g.getName()); … … 320 324 { 321 325 char *gn; 322 if (g1n[0] && g2n[0]) if (randomN(2) == 0) g1n[0] = 0; else g2n[0] = 0; // we want only one326 if (g1n[0] && g2n[0]) if (randomN(2) == 0) g1n[0] = 0; else g2n[0] = 0; //both provided? we want only one 323 327 if (g1n[0]) { gn = g1n; chg = chg1; } 324 328 else { gn = g2n; chg = chg2; } … … 474 478 int ind = operformats.find(format); 475 479 if (ind == -1) return NULL; 476 int ktoryopformatu= seloper[ind];480 int which_oper_of_format = seloper[ind]; 477 481 for (unsigned int i = 0; i < oper_fx_list.size(); i++) 478 482 if (oper_fx_list[i]->supported_format == format) 479 if ( ktoryopformatu == 0) return oper_fx_list[i]; else ktoryopformatu--;483 if (which_oper_of_format == 0) return oper_fx_list[i]; else which_oper_of_format--; 480 484 return NULL; //should never happen 481 485 }
Note: See TracChangeset
for help on using the changeset viewer.