Ignore:
Timestamp:
06/25/20 00:34:29 (4 years ago)
Author:
Maciej Komosinski
Message:

Genetic format ID becomes a string (no longer limited to a single character)

File:
1 edited

Legend:

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

    r896 r955  
    11// This file is a part of Framsticks SDK.  http://www.framsticks.com/
    2 // Copyright (C) 1999-2018  Maciej Komosinski and Szymon Ulatowski.
     2// Copyright (C) 1999-2020  Maciej Komosinski and Szymon Ulatowski.
    33// See LICENSE.txt for details.
    44
     
    6868#ifdef USE_GENMAN_fL
    6969#include "fL/fL_oper.h"
     70#endif
     71#ifdef USE_GENMAN_fS
     72#include "fS/fS_oper.h"
    7073#endif
    7174
     
    177180        oper_fx_list.push_back(new Geno_fL);
    178181#endif
     182#ifdef USE_GENMAN_fS
     183        oper_fx_list.push_back(new GenoOper_fS);
     184#endif
    179185
    180186        seloper = new int[oper_fx_list.size()]; //may result in a little overhead if some of the operators on the oper_fx_list concern the same genetic format
     
    182188        for (unsigned int i = 0; i < oper_fx_list.size(); i++)
    183189        {
    184                 if (operformats.find(oper_fx_list[i]->supported_format) != -1) continue;
     190                if (findOperFormatIndex(oper_fx_list[i]->supported_format) != -1) continue;
    185191                string type = string("~") + oper_fx_list[i]->name;
    186192                int dup = 0;
     
    188194                        if (oper_fx_list[i]->supported_format == oper_fx_list[j]->supported_format)
    189195                        {
    190                         type += "~";
    191                         type += oper_fx_list[j]->name;
    192                         dup++;
     196                                type += "~";
     197                                type += oper_fx_list[j]->name;
     198                                dup++;
    193199                        }
    194200                type = ssprintf("d 0 %d ", dup) + type;
    195                 string id = ssprintf("genoper_f%c", oper_fx_list[i]->supported_format);
    196                 string name = ssprintf("Operators for f%c", oper_fx_list[i]->supported_format);
     201                string id = ssprintf("genoper_f%s", oper_fx_list[i]->supported_format.c_str());
     202                string name = ssprintf("Operators for f%s", oper_fx_list[i]->supported_format.c_str());
    197203                seloper[selopercount] = 0;
    198                 operformats += oper_fx_list[i]->supported_format;
     204                operformats += &oper_fx_list[i]->supported_format;
    199205                //printf("%x %s %s %s\n",&seloper[selopercount],(const char*)id,(const char*)type,(const char*)name);
    200                 seloperpar.addProperty(&seloper[selopercount++], id.c_str(), type.c_str(), name.c_str(), "", PARAM_READONLY*(dup == 0));
     206                seloperpar.addProperty(&seloper[selopercount++], id.c_str(), type.c_str(), name.c_str(), "", PARAM_READONLY * (dup == 0));
    201207        }
    202208
     
    214220        for (unsigned int i = 0; i < oper_fx_list.size(); i++) delete oper_fx_list[i];
    215221        delete[] seloper;
     222}
     223
     224int GenMan::findOperFormatIndex(const SString& format)
     225{
     226        for (int i = 0; i < operformats.size(); i++)
     227                if (*operformats(i) == format)
     228                        return i;
     229        return -1;
    216230}
    217231
     
    265279Geno GenMan::validate(const Geno& geny)
    266280{
    267         char format = geny.getFormat();
     281        SString format = geny.getFormat();
    268282        GenoOperators *gf = getOper_f(format);
    269283        if (gf == NULL)
    270                 return Geno(SString::empty(), -1, SString::empty(), SString::sprintf("GENOPER_NOOPER: Validate(): don't know how to handle genetic format %c", format));
     284                return Geno("", Geno::INVALID_FORMAT, "", SString::sprintf("GENOPER_NOOPER: Validate(): don't know how to handle genetic format %s", format.c_str()));
    271285        char *g2 = strdup(geny.getGenes().c_str()); //copy for validation
    272286        int res = gf->validate(g2, geny.getName().c_str());
     
    276290                return Geno(sg2, format, geny.getName(), geny.getComment());
    277291        else
    278                 return Geno(SString::empty(), -1, SString::empty(), SString::sprintf("GENOPER_NOOPER: validate() for format %c returned invalid value", format));
     292                return Geno("", Geno::INVALID_FORMAT, "", SString::sprintf("GENOPER_NOOPER: validate() for format %s returned invalid value", format.c_str()));
    279293}
    280294
     
    283297        float chg; //how many changes
    284298        int method; //mutation method
    285         char format = g.getFormat();
     299        SString format = g.getFormat();
    286300        GenoOperators *gf = getOper_f(format);
    287301        if (gf == NULL)
    288                 return Geno(SString::empty(), -1, SString::empty(), SString::sprintf("GENOPER_NOOPER: Mutate(): don't know how to handle genetic format %c", format));
     302                return Geno("", Geno::INVALID_FORMAT, "", SString::sprintf("GENOPER_NOOPER: Mutate(): don't know how to handle genetic format %s", format.c_str()));
    289303        Geno gv = g;
    290304        bool canvalidate = true;
    291305        if (testValidity(gv, canvalidate) > 0 && canvalidate == false)
    292                 return Geno("", -1, "", "GENOPER_OPFAIL: Mutate(): cannot validate invalid source genotype");
     306                return Geno("", Geno::INVALID_FORMAT, "", "GENOPER_OPFAIL: Mutate(): cannot validate invalid source genotype");
    293307        bool ok = false;
    294308        int pcount = count;
     
    307321                                if (res > 0 && canvalidate == false) invalid_m++; else
    308322                                {
    309                                 validated_m++; ok = true;
     323                                        validated_m++; ok = true;
    310324                                }
    311325                        if (ok) gv = G;
     
    316330                if (!ok && (count - pcount > GENMAN_REPEAT_FAILED))
    317331                {
    318                         logPrintf("GenMan", "Mutate", 2, "Tried " GENMAN_REPEAT_FAILED_STR "x and failed: %s", g.getGenes().c_str());
     332                        logPrintf("GenMan", "Mutate", LOG_WARN, "Tried " GENMAN_REPEAT_FAILED_STR "x and failed: %s", g.getGenes().c_str());
    319333                        return Geno("", -1, "", "GENOPER_OPFAIL: Mutate() tried " GENMAN_REPEAT_FAILED_STR "x and failed");
    320334                }
     
    332346Geno GenMan::crossOver(const Geno& g1, const Geno& g2)
    333347{
    334         char format = g1.getFormat();
    335         if (format != g2.getFormat()) return Geno(SString::empty(), -1, SString::empty(), SString::sprintf("GENOPER_NOOPER: CrossOver(): does not work for parents with differing genetic formats (%c and %c)", format, g2.getFormat()));
     348        SString format = g1.getFormat();
     349        if (format != g2.getFormat()) return Geno("", Geno::INVALID_FORMAT, "", SString::sprintf("GENOPER_NOOPER: CrossOver(): does not work for parents with differing genetic formats (%s and %s)", format.c_str(), g2.getFormat().c_str()));
    336350        GenoOperators *gf = getOper_f(format);
    337351        if (gf == NULL)
    338                 return Geno(SString::empty(), -1, SString::empty(), SString::sprintf("GENOPER_NOOPER: CrossOver(): no operators found for genetic format %c", format));
     352                return Geno("", Geno::INVALID_FORMAT, "", SString::sprintf("GENOPER_NOOPER: CrossOver(): no operators found for genetic format %s", format.c_str()));
    339353        Geno g1v = g1, g2v = g2;
    340354
     
    343357                bool canvalidate = true;
    344358                if (testValidity(g1v, canvalidate) > 0 && canvalidate == false)
    345                         return Geno("", -1, "", "GENOPER_OPFAIL: CrossOver(): cannot validate invalid source genotype #1");
     359                        return Geno("", Geno::INVALID_FORMAT, "", "GENOPER_OPFAIL: CrossOver(): cannot validate invalid source genotype #1");
    346360                canvalidate = true;
    347361                if (testValidity(g2v, canvalidate) > 0 && canvalidate == false)
    348                         return Geno("", -1, "", "GENOPER_OPFAIL: CrossOver(): cannot validate invalid source genotype #2");
     362                        return Geno("", Geno::INVALID_FORMAT, "", "GENOPER_OPFAIL: CrossOver(): cannot validate invalid source genotype #2");
    349363        }
    350364
     
    373387                                if (res > 0 && canvalidate == false) invalid_xo++; else
    374388                                {
    375                                 validated_xo++; ok = true;
     389                                        validated_xo++; ok = true;
    376390                                }
    377391                        if (ok) g1v = G;
     
    383397                if (!ok && (count - pcount > GENMAN_REPEAT_FAILED))
    384398                {
    385                         logPrintf("GenMan", "CrossOver", 2, "Tried " GENMAN_REPEAT_FAILED_STR "x and failed: %s and %s", g1.getGenes().c_str(), g2.getGenes().c_str());
    386                         return Geno("", -1, "", "GENOPER_OPFAIL: CrossOver() tried " GENMAN_REPEAT_FAILED_STR "x and failed");
     399                        logPrintf("GenMan", "CrossOver", LOG_WARN, "Tried " GENMAN_REPEAT_FAILED_STR "x and failed: %s and %s", g1.getGenes().c_str(), g2.getGenes().c_str());
     400                        return Geno("", Geno::INVALID_FORMAT, "", "GENOPER_OPFAIL: CrossOver() tried " GENMAN_REPEAT_FAILED_STR "x and failed");
    387401                }
    388402        }
     
    398412float GenMan::similarity(const Geno& g1, const Geno& g2)
    399413{
    400         char format = g1.getFormat();
     414        SString format = g1.getFormat();
    401415        if (format != g2.getFormat()) return GENOPER_NOOPER;
    402416        GenoOperators *gf = getOper_f(format);
     
    406420uint32_t GenMan::getStyle(const char *g, const Geno *G, int pos)
    407421{
    408         char format = G->getFormat();
     422        SString format = G->getFormat();
    409423        if (format == Geno::INVALID_FORMAT)
    410424                return GENSTYLE_RGBS(64, 64, 64, 0); // gray & "valid" (unknown format so we don't know what is valid and what is not)
     
    423437void GenMan::getFullStyle(const char *g, const Geno *G, uint32_t *styletab)
    424438{
    425         char format = G->getFormat();
     439        SString format = G->getFormat();
    426440        if (format == Geno::INVALID_FORMAT)
    427441        {
     
    438452                else if (!gf) styletab[pos] = GENSTYLE_CS(0, 0); //black & valid
    439453                else styletab[pos] = gf->style(geny.c_str(), posmapped);
    440                 //logPrintf("GenMan", "getFullStyle", 0, "%d  char='%c' (%d)  format=0x%08x", pos, g[pos], g[pos], styletab[pos]);
     454                //logPrintf("GenMan", "getFullStyle", LOG_INFO, "%d  char='%c' (%d)  format=0x%08x", pos, g[pos], g[pos], styletab[pos]);
    441455        }
    442456}
     
    473487                color = GENGETCOLOR(styletab[i]);
    474488                if ((i != 0 && (color != prevcolor))) html += "</font>";
    475                 if ((style&GENSTYLE_INVALID) != (prevstyle&GENSTYLE_INVALID))
    476                 {
    477                         html += "<"; if (!(style&GENSTYLE_INVALID)) html += "/"; html += "u>";
    478                 }
    479                 if ((style&GENSTYLE_BOLD) != (prevstyle&GENSTYLE_BOLD))
    480                 {
    481                         html += "<"; if (!(style&GENSTYLE_BOLD)) html += "/"; html += "b>";
    482                 }
    483                 if ((style&GENSTYLE_ITALIC) != (prevstyle&GENSTYLE_ITALIC))
    484                 {
    485                         html += "<"; if (!(style&GENSTYLE_ITALIC)) html += "/"; html += "i>";
     489                if ((style & GENSTYLE_INVALID) != (prevstyle & GENSTYLE_INVALID))
     490                {
     491                        html += "<"; if (!(style & GENSTYLE_INVALID)) html += "/"; html += "u>";
     492                }
     493                if ((style & GENSTYLE_BOLD) != (prevstyle & GENSTYLE_BOLD))
     494                {
     495                        html += "<"; if (!(style & GENSTYLE_BOLD)) html += "/"; html += "b>";
     496                }
     497                if ((style & GENSTYLE_ITALIC) != (prevstyle & GENSTYLE_ITALIC))
     498                {
     499                        html += "<"; if (!(style & GENSTYLE_ITALIC)) html += "/"; html += "i>";
    486500                }
    487501                if ((i == 0 || (color != prevcolor)))
     
    509523}
    510524
    511 Geno GenMan::getSimplest(char format)
     525Geno GenMan::getSimplest(const SString& format)
    512526{
    513527        GenoOperators *gf = getOper_f(format);
    514528        if (!gf) return Geno();
    515         string info = "The simplest genotype of format f"; info += format;
     529        string info = "The simplest genotype of format f"; info += format.c_str();
    516530        info += " for operators '"; info += gf->name; info += "'.";
    517531        return Geno(gf->getSimplest(), format, "Root", info.c_str());
     
    520534void GenMan::p_getsimplest(ExtValue *args, ExtValue *ret)
    521535{
    522         int format = GenoObj::formatFromExtValue(args[0]);
     536        SString format = GenoObj::formatFromExtValue(args[0]);
    523537        if (!getOper_f(format))
    524538                ret->setEmpty();
     
    527541}
    528542
    529 const char *GenMan::getOpName(char format)
     543const char *GenMan::getOpName(const SString& format)
    530544{
    531545        GenoOperators *gf = getOper_f(format);
     
    533547}
    534548
    535 GenoOperators* GenMan::getOper_f(char format)
    536 {
    537         int ind = operformats.find(format);
     549GenoOperators* GenMan::getOper_f(const SString& format)
     550{
     551        int ind = findOperFormatIndex(format);
    538552        if (ind == -1) return NULL;
    539553        int which_oper_of_format = seloper[ind];
     
    602616                }
    603617                //      if (oper_fx_list[i]->similarity("","")!=GENOPER_NOOPER) l+=" similarity";
    604                 logPrintf("GenMan", "Report", 0, "format f%c (%s):%s",
    605                         oper_fx_list[i]->supported_format, oper_fx_list[i]->name.c_str(), l.c_str());
     618                logPrintf("GenMan", "Report", LOG_INFO, "format f%s (%s):%s",
     619                        oper_fx_list[i]->supported_format.c_str(), oper_fx_list[i]->name.c_str(), l.c_str());
    606620        }
    607621}
Note: See TracChangeset for help on using the changeset viewer.