Ignore:
Timestamp:
03/11/14 14:45:29 (10 years ago)
Author:
sz
Message:

reformatting

File:
1 edited

Legend:

Unmodified
Added
Removed
  • cpp/frams/genetics/genman.cpp

    r145 r168  
    227227Geno GenMan::Validate(const Geno& geny)
    228228{
    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;
     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;
    236236        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());
    239239        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));
    241241}
    242242
     
    245245        float chg; //how many changes
    246246        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;
    257257        while (!ok)
    258258        {
    259                 char *gn=strdup(gv.getGene()); //copy for mutation
    260                 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)
    262262                {
    263263                        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++;
    272276                free(gn);
    273277                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);
    282286        SString mutinfo;
    283         if (extmutinfo == 0) mutinfo = SString::sprintf("%.2f%% mutation of '%s'",100*chg,(const char*)g.getName()); else
     287        if (extmutinfo == 0) mutinfo = SString::sprintf("%.2f%% mutation of '%s'", 100 * chg, (const char*)g.getName()); else
    284288                if (extmutinfo == 1) mutinfo = SString::sprintf("%.2f%% mutation(%d) of '%s'", 100 * chg, method, (const char*)g.getName()); else
    285289                        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());
     
    320324                {
    321325                        char *gn;
    322                         if (g1n[0] && g2n[0]) if (randomN(2) == 0) g1n[0] = 0; else g2n[0] = 0; //we want only one
     326                        if (g1n[0] && g2n[0]) if (randomN(2) == 0) g1n[0] = 0; else g2n[0] = 0; //both provided? we want only one
    323327                        if (g1n[0]) { gn = g1n; chg = chg1; }
    324328                        else { gn = g2n; chg = chg2; }
     
    474478        int ind = operformats.find(format);
    475479        if (ind == -1) return NULL;
    476         int ktoryopformatu = seloper[ind];
     480        int which_oper_of_format = seloper[ind];
    477481        for (unsigned int i = 0; i < oper_fx_list.size(); i++)
    478482                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--;
    480484        return NULL; //should never happen
    481485}
Note: See TracChangeset for help on using the changeset viewer.