Changeset 532 for cpp/frams


Ignore:
Timestamp:
07/19/16 18:11:24 (8 years ago)
Author:
Maciej Komosinski
Message:

A new set of getStyle/getFullStyle functions in GenMan? and cosmetic rename of other functions (first letter lowercase)

Location:
cpp/frams
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • cpp/frams/_demos/genooper_test.cpp

    r391 r532  
    3232        char format = gsrc.getFormat();
    3333
    34         Geno gmut = genetics.genman.Mutate(gsrc);
     34        Geno gmut = genetics.genman.mutate(gsrc);
    3535        printGenAndTitle(gmut, "mutated (gmut)");
    3636
    37         Geno gxover = genetics.genman.CrossOver(gsrc, gmut);
     37        Geno gxover = genetics.genman.crossOver(gsrc, gmut);
    3838        printGenAndTitle(gxover, "crossed over (gsrc and gmut)");
    3939
    40         Geno gsimplest = genetics.genman.GetSimplest(format);
     40        Geno gsimplest = genetics.genman.getSimplest(format);
    4141        printGenAndTitle(gsimplest, "simplest");
    4242
     
    4444        printGenAndTitle(ginvalid, "invalid");
    4545
    46         Geno gvalidated = genetics.genman.Validate(ginvalid);
     46        Geno gvalidated = genetics.genman.validate(ginvalid);
    4747        printGenAndTitle(gvalidated, "validated");
    4848
  • cpp/frams/genetics/genman.cpp

    r516 r532  
    210210        if (check1 == GENOPER_NOOPER) //disaster: cannot check because there is no check operator
    211211        {
    212                 g.setGene(g2); free(g2); canvalidate = false; return GENOPER_NOOPER;
     212                g.setGeneOnly(g2); free(g2); canvalidate = false; return GENOPER_NOOPER;
    213213        }
    214214        int check2 = gf->checkValidity(g2, "validated");
    215         if (check2 == GENOPER_OK) g.setGene(g2);
     215        if (check2 == GENOPER_OK) g.setGeneOnly(g2);
    216216        free(g2);
    217217        if (check2 == GENOPER_OK) return check1;
     
    231231}
    232232
    233 Geno GenMan::Validate(const Geno& geny)
     233Geno GenMan::validate(const Geno& geny)
    234234{
    235235        char format = geny.getFormat();
     
    247247}
    248248
    249 Geno GenMan::Mutate(const Geno& g)
     249Geno GenMan::mutate(const Geno& g)
    250250{
    251251        float chg; //how many changes
     
    298298}
    299299
    300 Geno GenMan::CrossOver(const Geno& g1, const Geno& g2)
     300Geno GenMan::crossOver(const Geno& g1, const Geno& g2)
    301301{
    302302        char format = g1.getFormat();
     
    364364}
    365365
    366 float GenMan::Similarity(const Geno& g1, const Geno& g2)
     366float GenMan::similarity(const Geno& g1, const Geno& g2)
    367367{
    368368        char format = g1.getFormat();
     
    372372}
    373373
    374 uint32_t GenMan::Style(const char *g, int pos)
     374uint32_t GenMan::getStyle(const char *g, const Geno *G, int pos)
     375{
     376        char format = G->getFormat();
     377        if (format == Geno::INVALID_FORMAT)
     378                return GENSTYLE_RGBS(64, 64, 64, 0); // gray & "valid" (unknown format so we don't know what is valid and what is not)
     379        if ((pos = G->mapStringToGen(pos)) == -1) return GENSTYLE_COMMENT;
     380        GenoOperators *gf = getOper_f(format);
     381        if (!gf) return GENSTYLE_CS(0, 0); //black & valid
     382        else return gf->style(G->getGene().c_str(), pos);
     383}
     384
     385uint32_t GenMan::getStyle(const char *g, int pos)
    375386{
    376387        Geno G(g);
    377         if ((pos = G.mapStringToGen(pos)) == -1) return GENSTYLE_COMMENT;
    378         GenoOperators *gf = getOper_f(G.getFormat());
    379         if (!gf) return GENSTYLE_CS(0, 0); //black & valid
    380         else return gf->style(G.getGene().c_str(), pos);
    381 }
    382 
    383 void GenMan::GetFullStyle(const char *g, uint32_t *styletab)
    384 {
    385         Geno G(g);
    386         GenoOperators *gf = getOper_f(G.getFormat());
    387         SString geny = G.getGene();
     388        return getStyle(g, &G, pos);
     389}
     390
     391void GenMan::getFullStyle(const char *g, const Geno *G, uint32_t *styletab)
     392{
     393        char format = G->getFormat();
     394        if (format == Geno::INVALID_FORMAT)
     395        {
     396                for (unsigned int pos = 0; pos < strlen(g); pos++)
     397                        styletab[pos] = GENSTYLE_RGBS(64, 64, 64, 0); // gray & "valid" (unknown format so we don't know what is valid and what is not)
     398                return;
     399        }
     400        GenoOperators *gf = getOper_f(format);
     401        SString geny=G->getGene();
    388402        for (unsigned int pos = 0; pos < strlen(g); pos++)
    389403        {
    390                 int posmapped = G.mapStringToGen(pos);
     404                int posmapped = G->mapStringToGen(pos);
    391405                if (posmapped == -1) styletab[pos] = GENSTYLE_COMMENT;
    392406                else if (!gf) styletab[pos] = GENSTYLE_CS(0, 0); //black & valid
    393407                else styletab[pos] = gf->style(geny.c_str(), posmapped);
    394         }
     408                //logPrintf("GenMan", "getFullStyle", 0, "%d  char='%c' (%d)  format=0x%08x", pos, g[pos], g[pos], styletab[pos]);
     409        }
     410}
     411
     412void GenMan::getFullStyle(const char *g, uint32_t *styletab)
     413{
     414        Geno G(g);
     415        getFullStyle(g, &G, styletab);
    395416}
    396417
     
    406427        bool shortened = false;
    407428        uint32_t *styletab = new uint32_t[len];
    408         GetFullStyle(g, styletab);
     429        getFullStyle(g, styletab);
    409430        string html = "\n<div style=\"background:white;padding:0.2em;font-family:arial,helvetica,sans-serif;font-size:90%\">";
    410431        uint32_t prevstyle, prevcolor, style = 0, color = 0;
     
    456477}
    457478
    458 Geno GenMan::GetSimplest(char format)
     479Geno GenMan::getSimplest(char format)
    459480{
    460481        GenoOperators *gf = getOper_f(format);
     
    471492                ret->setEmpty();
    472493        else
    473                 *ret = GenoObj::makeDynamicObjectAndDecRef(new Geno(GetSimplest(format)));
    474 }
    475 
    476 const char *GenMan::GetOpName(char format)
     494                *ret = GenoObj::makeDynamicObjectAndDecRef(new Geno(getSimplest(format)));
     495}
     496
     497const char *GenMan::getOpName(char format)
    477498{
    478499        GenoOperators *gf = getOper_f(format);
     
    560581                ret->setEmpty();
    561582        else
    562                 *ret = GenoObj::makeDynamicObjectAndDecRef(new Geno(Validate(*g)));
     583                *ret = GenoObj::makeDynamicObjectAndDecRef(new Geno(validate(*g)));
    563584}
    564585
     
    569590                ret->setEmpty();
    570591        else
    571                 *ret = GenoObj::makeDynamicObjectAndDecRef(new Geno(Mutate(*g)));
     592                *ret = GenoObj::makeDynamicObjectAndDecRef(new Geno(mutate(*g)));
    572593}
    573594
     
    579600                ret->setEmpty();
    580601        else
    581                 *ret = GenoObj::makeDynamicObjectAndDecRef(new Geno(CrossOver(*g1, *g2)));
    582 }
     602                *ret = GenoObj::makeDynamicObjectAndDecRef(new Geno(crossOver(*g1, *g2)));
     603}
     604
  • cpp/frams/genetics/genman.h

    r467 r532  
    11// This file is a part of Framsticks SDK.  http://www.framsticks.com/
    2 // Copyright (C) 1999-2015  Maciej Komosinski and Szymon Ulatowski.
     2// Copyright (C) 1999-2016  Maciej Komosinski and Szymon Ulatowski.
    33// See LICENSE.txt for details.
    44
     
    4242        int testValidity(Geno &g, bool &canvalidate);
    4343        int testGenoValidity(Geno& g); //class GenoValidator (geno.h)
    44         Geno Validate(const Geno&); ///<returns validated (if possible) genotype
    45         Geno Mutate(const Geno&); //returns mutated genotype or empty if errors
    46         Geno CrossOver(const Geno&, const Geno&); //returns xover genotype ("child") or empty if errors
    47         float Similarity(const Geno&, const Geno&); //returns GENOPER_NOOPER or normalized similarity (1: identical, 0: different)
    48         uint32_t Style(const char* g, int pos); //returns Style (and validity) of a genotype char.
    49         void GetFullStyle(const char *g, uint32_t *styletab); //optimized. Fills styletab with styles for all genotype chars. sizeof(*styletab) must be at least strlen(g).
     44        Geno validate(const Geno&); ///<returns validated (if possible) genotype
     45        Geno mutate(const Geno&); //returns mutated genotype or empty if errors
     46        Geno crossOver(const Geno&, const Geno&); //returns xover genotype ("child") or empty if errors
     47        float similarity(const Geno&, const Geno&); //returns GENOPER_NOOPER or normalized similarity (1: identical, 0: different)
     48        uint32_t getStyle(const char* g, int pos); //returns style (and validity) of a genotype char.
     49        uint32_t getStyle(const char *g, const Geno *G, int pos); //returns style (and validity) of a genotype char. Assumes G is created from g.
     50        void getFullStyle(const char *g, uint32_t *styletab); //optimized. Fills styletab with styles for all genotype chars. sizeof(*styletab) must be at least strlen(g).
     51        void getFullStyle(const char *g, const Geno *G, uint32_t *styletab); //optimized. Assumes G is created from g. Fills styletab with styles for all genotype chars. sizeof(*styletab) must be at least strlen(g).
    5052        string HTMLize(const char *g); //returns colored genotype in HTML.
    5153        string HTMLizeShort(const char *g); //returns colored genotype (abbreviated if needed) in HTML.
    52         Geno GetSimplest(char format); ///<returns pointer to the simplest genotype of \e format or empty Geno()
    53         const char *GetOpName(char format); ///<returns pointer to the active operator set for \e format
     54        Geno getSimplest(char format); ///<returns pointer to the simplest genotype of \e format or empty Geno()
     55        const char *getOpName(char format); ///<returns pointer to the active operator set for \e format
    5456        const vector<GenoOperators*>& GetOperators() const { return oper_fx_list; } ///<returns the list of available genetic operators
    5557private:
Note: See TracChangeset for help on using the changeset viewer.