Changeset 348 for cpp/frams/genetics


Ignore:
Timestamp:
04/09/15 23:51:28 (5 years ago)
Author:
Maciej Komosinski
Message:
  • explicit c_str() in SString instead of (const char*) cast
  • genetic converters and GenMan? are now thread-local which enables multi-threaded simulator separation
Location:
cpp/frams/genetics
Files:
11 edited

Legend:

Unmodified
Added
Removed
  • cpp/frams/genetics/f1/conv_f1.cpp

    r319 r348  
    102102SString GenoConv_f1::convert(SString &i,MultiMap *map)
    103103{
    104 const char* g=(const char*)i;
     104const char* g=i.c_str();
    105105Builder builder(g,map?1:0);
    106106builder.model.open();
     
    403403                                        lastNeuroClassParam();
    404404                                        //special handling: muscle properties (can be overwritten by subsequent property assignments)
    405                                         if (!strcmp(cls->getName(),"|"))
     405                                        if (!strcmp(cls->getName().c_str(),"|"))
    406406                                                {
    407407                                                neuro_cls_param->setDoubleById("p",lastjoint_muscle_power);
    408408                                                neuro_cls_param->setDoubleById("r",props.bendrange);
    409409                                                }
    410                                         else if (!strcmp(cls->getName(),"@"))
     410                                        else if (!strcmp(cls->getName().c_str(),"@"))
    411411                                                {
    412412                                                neuro_cls_param->setDoubleById("p",lastjoint_muscle_power);
     
    485485        SString name(begin,colon-begin);
    486486        SString value(colon+1,end-(colon+1));
    487         addClassParam(name,value);
    488         }
    489 }
     487        addClassParam(name.c_str(),value.c_str());
     488        }
     489}
  • cpp/frams/genetics/f4/f4_general.cpp

    r287 r348  
    714714        // transform geno from string to nodes
    715715        f4rootnode = new f4_node();
    716         res = f4_processrec((const char*)genome, (unsigned)0, f4rootnode);
     716        res = f4_processrec(genome.c_str(), (unsigned)0, f4rootnode);
    717717        if ((res < 0) || (1 != f4rootnode->childCount()))
    718718        {
     
    11971197                buf = (char*)realloc(buf, len + 1);
    11981198        }
    1199         strcpy(buf, (const char*)out);
     1199        strcpy(buf, out.c_str());
    12001200}
    12011201
  • cpp/frams/genetics/f9/oper_f9.cpp

    r319 r348  
    4242                if (strchr(turtle_commands_f9, gene[i])) validated += gene[i];  //validated contains only turtle_commands_f9
    4343        free(gene);
    44         gene = strdup(validated); //reallocate
     44        gene = strdup(validated.c_str()); //reallocate
    4545        return GENOPER_OK;
    4646}
     
    8484                }
    8585                free(gene);
    86                 gene = strdup(newgeno); //reallocate
     86                gene = strdup(newgeno.c_str()); //reallocate
    8787        }
    8888
  • cpp/frams/genetics/fF/conv_fF.cpp

    r319 r348  
    3939{
    4040        fF_growth_params gp;
    41         if (!gp.load(in)) //invalid input genotype?
     41        if (!gp.load(in.c_str())) //invalid input genotype?
    4242                return ""; //so we return an invalid f0 genotype
    4343
  • cpp/frams/genetics/fF/fF_genotype.h

    r286 r348  
    4343                SString tmp;
    4444                param.save2(tmp, NULL/*object containing default values for comparison*/, false/*add CR*/, false/*force field names*/);
    45                 return string((const char*)tmp);
     45                return string(tmp.c_str());
    4646        }
    4747};
  • cpp/frams/genetics/fT/oper_fTest.cpp

    r319 r348  
    7777                if (strchr("ATGC", gene[i])) validated += gene[i];  //validated contains only ATGC
    7878        free(gene);
    79         gene = strdup(validated); //reallocate
     79        gene = strdup(validated.c_str()); //reallocate
    8080        return GENOPER_OK;
    8181}
  • cpp/frams/genetics/genman.cpp

    r344 r348  
    199199{
    200200        SString ggs=g.getGene();
    201         const char *gg = ggs;
     201        const char *gg = ggs.c_str();
    202202        GenoOperators *gf = getOper_f(g.getFormat());
    203203        int check1;
     
    237237        if (gf == NULL)
    238238                return Geno(SString::empty(), -1, SString::empty(), SString::sprintf("GENOPER_NOOPER: Validate(): don't know how to handle genetic format %c", format));
    239         char *g2 = strdup(geny.getGene()); //copy for validation
     239        char *g2 = strdup(geny.getGene().c_str()); //copy for validation
    240240        int res = gf->validate(g2);
    241241        SString sg2 = g2;
     
    263263        while (!ok)
    264264        {
    265                 char *gn = strdup(gv.getGene()); //copy for mutation
     265                char *gn = strdup(gv.getGene().c_str()); //copy for mutation
    266266                chg = 0;
    267267                if (gf->mutate(gn, chg, method) == GENOPER_OK)
     
    284284                if (!ok && (count - pcount > 100))
    285285                {
    286                         FMprintf("GenMan", "Mutate", 2, "Tried 100x and failed: %s", (const char*)g.getGene());
     286                        FMprintf("GenMan", "Mutate", 2, "Tried 100x and failed: %s", g.getGene().c_str());
    287287                        return Geno("", -1, "", "GENOPER_OPFAIL: Mutate() tried 100x and failed");
    288288                }
    289289        }
    290290        mutchg += chg;
    291         if (history) saveLink((const char*)g.getGene(), "", (const char*)gv.getGene(), chg);
     291        if (history) saveLink(g.getGene().c_str(), "", gv.getGene().c_str(), chg);
    292292        SString mutinfo;
    293         if (extmutinfo == 0) mutinfo = SString::sprintf("%.2f%% mutation of '%s'", 100 * chg, (const char*)g.getName()); else
    294                 if (extmutinfo == 1) mutinfo = SString::sprintf("%.2f%% mutation(%d) of '%s'", 100 * chg, method, (const char*)g.getName()); else
    295                         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());
     293        if (extmutinfo == 0) mutinfo = SString::sprintf("%.2f%% mutation of '%s'", 100 * chg, g.getName().c_str()); else
     294                if (extmutinfo == 1) mutinfo = SString::sprintf("%.2f%% mutation(%d) of '%s'", 100 * chg, method, g.getName().c_str()); else
     295                        mutinfo = SString::sprintf("%.2f%% mutation(%s) of '%s'", 100 * chg, gf->mutation_method_names ? gf->mutation_method_names[method] : "unspecified method name", g.getName().c_str());
    296296        gv.setComment(mutinfo);
    297297        return gv;
     
    324324        {
    325325                float chg1, chg2;
    326                 char *g1n = strdup(g1.getGene()); //copy for crossover
    327                 char *g2n = strdup(g2.getGene()); //copy for crossover
     326                char *g1n = strdup(g1.getGene().c_str()); //copy for crossover
     327                char *g2n = strdup(g2.getGene().c_str()); //copy for crossover
    328328                chg1 = chg2 = 0;
    329329                if (gf->crossOver(g1n, g2n, chg1, chg2) == GENOPER_OK)
     
    351351                if (!ok && (count - pcount > 100))
    352352                {
    353                         FMprintf("GenMan", "CrossOver", 2, "Tried 100x and failed: %s and %s", (const char*)g1.getGene(), (const char*)g2.getGene());
     353                        FMprintf("GenMan", "CrossOver", 2, "Tried 100x and failed: %s and %s", g1.getGene().c_str(), g2.getGene().c_str());
    354354                        return Geno("", -1, "", "GENOPER_OPFAIL: CrossOver() tried 100x and failed");
    355355                }
     
    357357        // result in g1v
    358358        xochg += chg;
    359         if (history) saveLink((const char*)g1.getGene(), (const char*)g2.getGene(), (const char*)g1v.getGene(), chg);
     359        if (history) saveLink(g1.getGene().c_str(), g2.getGene().c_str(), g1v.getGene().c_str(), chg);
    360360        SString xoinfo = SString::sprintf("Crossing over of '%s' (%.2f%%) and '%s' (%.2f%%)",
    361                 (const char*)g1.getName(), 100 * chg, (const char*)g2.getName(), 100 * (1 - chg));
     361                g1.getName().c_str(), 100 * chg, g2.getName().c_str(), 100 * (1 - chg));
    362362        g1v.setComment(xoinfo);
    363363        return g1v;
     
    369369        if (format != g2.getFormat()) return GENOPER_NOOPER;
    370370        GenoOperators *gf = getOper_f(format);
    371         if (!gf) return GENOPER_NOOPER; else return gf->similarity(g1.getGene(), g2.getGene());
     371        if (!gf) return GENOPER_NOOPER; else return gf->similarity(g1.getGene().c_str(), g2.getGene().c_str());
    372372}
    373373
     
    378378        GenoOperators *gf = getOper_f(G.getFormat());
    379379        if (!gf) return GENSTYLE_CS(0, 0); //black & valid
    380         else return gf->style(G.getGene(), pos);
     380        else return gf->style(G.getGene().c_str(), pos);
    381381}
    382382
     
    391391                if (posmapped == -1) styletab[pos] = GENSTYLE_COMMENT;
    392392                else if (!gf) styletab[pos] = GENSTYLE_CS(0, 0); //black & valid
    393                 else styletab[pos] = gf->style(geny, posmapped);
     393                else styletab[pos] = gf->style(geny.c_str(), posmapped);
    394394        }
    395395}
     
    448448void GenMan::p_htmlize(ExtValue *args, ExtValue *ret)
    449449{
    450         ret->setString(HTMLize(args->getString()));
     450        ret->setString(HTMLize(args->getString().c_str()));
    451451}
    452452
    453453void GenMan::p_htmlizeshort(ExtValue *args, ExtValue *ret)
    454454{
    455         ret->setString(HTMLizeShort(args->getString()));
     455        ret->setString(HTMLizeShort(args->getString().c_str()));
    456456}
    457457
  • cpp/frams/genetics/geno.cpp

    r346 r348  
    77#include <frams/model/model.h>
    88
    9 GenoConvManager *Geno::converters = NULL;
    10 
    11 THREAD_LOCAL_DEF(Geno::Validators, geno_validators);
    12 
    13 Geno::Validators& Geno::getValidators() {return tlsGetRef(geno_validators);}
     9THREAD_LOCAL_DEF_PTR(Geno::Validators, geno_validators);
     10THREAD_LOCAL_DEF_PTR(GenoConvManager, geno_converters);
     11
     12Geno::Validators* Geno::getValidators() {return tlsGetPtr(geno_validators);}
     13GenoConvManager* Geno::getConverters() {return tlsGetPtr(geno_converters);}
     14
     15Geno::Validators* Geno::useValidators(Validators* val)
     16{return tlsSetPtr(geno_validators,val);}
     17GenoConvManager* Geno::useConverters(GenoConvManager* gcm)
     18{return tlsSetPtr(geno_converters,gcm);}
    1419
    1520void Geno::init(const SString& genstring, char genformat, const SString& genname, const SString& comment)
     
    7883        if (!name.len()) name = genname;
    7984        if (!txt.len()) txt = comment;
    80         multiline = (strchr((const char*)gen, '\n') != 0);
     85        multiline = (strchr(gen.c_str(), '\n') != 0);
    8186        // mapoutshift...?
    8287}
     
    237242        if (isvalid >= 0) return;
    238243        if (gen.len() == 0) { isvalid = 0; return; }
    239         Validators& vals=getValidators();
    240         FOREACH(GenoValidator*, v, vals)
     244        Validators* vals=getValidators();
     245        if (vals!=NULL)
     246                {
     247        FOREACH(GenoValidator*, v, (*vals))
    241248                if ((isvalid = v->testGenoValidity(*this)) >= 0)
    242249                        return;
     250                }
    243251        isvalid = 0;
    244252        FMprintf("Geno", "validate", FMLV_WARN, "Wrong configuration? No genotype validators defined for genetic format f%c.", format);
     
    255263        if (otherformat == getFormat()) { if (converter_missing) *converter_missing = false; return *this; }
    256264#ifndef NO_GENOCONVMANAGER
     265        GenoConvManager *converters=getConverters();
    257266        if (converters)
    258267        {
  • cpp/frams/genetics/geno.h

    r346 r348  
    118118
    119119        // managing global Geno-related objects (used for validation and conversion)
    120         static Validators& getValidators();
    121         static void addValidator(GenoValidator* gv,int at_position=9999) { getValidators().insert(at_position,gv); }
    122         static void removeValidator(GenoValidator* gv) { getValidators() -= gv; }
    123         static void useConverters(GenoConvManager& gcm) { converters = &gcm; }
    124         static GenoConvManager &getConverters() { return *converters; }
    125 protected:
    126         static GenoConvManager *converters;
     120        static Validators* useValidators(Validators* val);
     121        static Validators* getValidators();
     122
     123        static GenoConvManager* useConverters(GenoConvManager* gcm);
     124        static GenoConvManager* getConverters();
    127125};
    128126
  • cpp/frams/genetics/oper_fx.cpp

    r286 r348  
    192192        for (int i = 0; i<Neuro::getClassCount(); i++)
    193193        {
    194                 const char *n = Neuro::getClass(i)->name;
     194                const char *n = Neuro::getClass(i)->name.c_str();
    195195                int l = (int)strlen(n);
    196196                if (len >= l && l>Len && (strncmp(s, n, l) == 0)) { I = Neuro::getClass(i); Len = l; }
  • cpp/frams/genetics/preconfigured.h

    r346 r348  
    1818DefaultGenoConvManager gcm;
    1919GenMan genman;
     20Geno::Validators validators;
    2021ModelGenoValidator model_validator; //validation through conversion
    2122
     
    2324        {
    2425        gcm.addDefaultConverters(); //without converters, the application would only handle "format 0" genotypes
    25         Geno::useConverters(gcm);
     26        Geno::useConverters(&gcm);
    2627
    27         Geno::addValidator(&genman); //primary validation: use the extended validity checking (through dedicated genetic operators)
    28         Geno::addValidator(&model_validator); //secondary validation: this simple validator handles all cases when there is no dedicated genetic validation operator, but a converter for a particular format is available. Converters may be less strict in detecting invalid genotypes but using them and checking whether they produced a valid f0 genotype is also some way to tell whether the initial genotype was valid. Otherwise, without dedicated genetic validation operator, we would have no validity check at all.
    29         }
    30 };
    31 
    32 /** Initialization procedure for applications adding their own validators */
    33 class PreconfiguredGenetics_NoValidators
    34 {
    35   public:
    36 DefaultGenoConvManager gcm;
    37 
    38 PreconfiguredGenetics_NoValidators()
    39         {
    40         gcm.addDefaultConverters(); //without converters, the application would only handle "format 0" genotypes
    41         Geno::useConverters(gcm);
     28        Geno::useValidators(&validators);
     29        validators+=&genman; //primary validation: use the extended validity checking (through dedicated genetic operators)
     30        validators+=&model_validator; //secondary validation: this simple validator handles all cases when there is no dedicated genetic validation operator, but a converter for a particular format is available. Converters may be less strict in detecting invalid genotypes but using them and checking whether they produced a valid f0 genotype is also some way to tell whether the initial genotype was valid. Otherwise, without dedicated genetic validation operator, we would have no validity check at all.
    4231        }
    4332};
    4433
    4534#endif
    46 
Note: See TracChangeset for help on using the changeset viewer.