Changeset 761 for cpp/frams/genetics


Ignore:
Timestamp:
03/28/18 02:26:29 (6 years ago)
Author:
Maciej Komosinski
Message:

Introduced a macro for the number of times GenMan? tries to repeat a mutation or crossover when the operator does not return acceptable genotype; decreased the number of repetitions from 100 to 10

File:
1 edited

Legend:

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

    r748 r761  
    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-2018  Maciej Komosinski and Szymon Ulatowski.
    33// See LICENSE.txt for details.
    44
     
    1010#include "common/stl-util.h"
    1111#include <common/loggers/loggers.h>
     12
     13
     14#define GENMAN_REPEAT_FAILED 10 //how many times GenMan tries to repeat a mutation or crossover when the operator does not return acceptable genotype
     15#define STRINGIFY_1(x) #x
     16#define STRINGIFY(x) STRINGIFY_1(x) //this second-level macro allows the parameter to be a macro itself and to stringify its value, not its name
     17#define GENMAN_REPEAT_FAILED_STR STRINGIFY(GENMAN_REPEAT_FAILED)
    1218
    1319
     
    164170                        if (oper_fx_list[i]->supported_format == oper_fx_list[j]->supported_format)
    165171                        {
    166                                 type += "~";
    167                                 type += oper_fx_list[j]->name;
    168                                 dup++;
     172                        type += "~";
     173                        type += oper_fx_list[j]->name;
     174                        dup++;
    169175                        }
    170176                type = ssprintf("d 0 %d ", dup) + type;
     
    206212int GenMan::testValidity(Geno &g, bool &canvalidate)
    207213{
    208         SString ggs=g.getGenes();
     214        SString ggs = g.getGenes();
    209215        const char *gg = ggs.c_str();
    210216        GenoOperators *gf = getOper_f(g.getFormat());
    211217        int check1;
    212218        if (!gf) { canvalidate = false; return GENOPER_NOOPER; }
    213         else check1 = gf->checkValidity(gg,g.getName().c_str());
     219        else check1 = gf->checkValidity(gg, g.getName().c_str());
    214220        if (!canvalidate) return check1; //just checking
    215221        if (check1 == GENOPER_OK) { canvalidate = false; return check1; }
    216222        char *g2 = strdup(gg);
    217         if (gf->validate(g2,g.getName().c_str()) == GENOPER_NOOPER) { free(g2); canvalidate = false; return check1; }
     223        if (gf->validate(g2, g.getName().c_str()) == GENOPER_NOOPER) { free(g2); canvalidate = false; return check1; }
    218224        if (check1 == GENOPER_NOOPER) //disaster: cannot check because there is no check operator
    219225        {
     
    283289                                if (res > 0 && canvalidate == false) invalid_m++; else
    284290                                {
    285                                         validated_m++; ok = true;
     291                                validated_m++; ok = true;
    286292                                }
    287293                        if (ok) gv = G;
     
    290296                free(gn);
    291297                count++;
    292                 if (!ok && (count - pcount > 100))
    293                 {
    294                         logPrintf("GenMan", "Mutate", 2, "Tried 100x and failed: %s", g.getGenes().c_str());
    295                         return Geno("", -1, "", "GENOPER_OPFAIL: Mutate() tried 100x and failed");
     298                if (!ok && (count - pcount > GENMAN_REPEAT_FAILED))
     299                {
     300                        logPrintf("GenMan", "Mutate", 2, "Tried " GENMAN_REPEAT_FAILED_STR "x and failed: %s", g.getGenes().c_str());
     301                        return Geno("", -1, "", "GENOPER_OPFAIL: Mutate() tried " GENMAN_REPEAT_FAILED_STR "x and failed");
    296302                }
    297303        }
     
    349355                                if (res > 0 && canvalidate == false) invalid_xo++; else
    350356                                {
    351                                         validated_xo++; ok = true;
     357                                validated_xo++; ok = true;
    352358                                }
    353359                        if (ok) g1v = G;
     
    357363                free(g2n);
    358364                count++;
    359                 if (!ok && (count - pcount > 100))
    360                 {
    361                         logPrintf("GenMan", "CrossOver", 2, "Tried 100x and failed: %s and %s", g1.getGenes().c_str(), g2.getGenes().c_str());
    362                         return Geno("", -1, "", "GENOPER_OPFAIL: CrossOver() tried 100x and failed");
     365                if (!ok && (count - pcount > GENMAN_REPEAT_FAILED))
     366                {
     367                        logPrintf("GenMan", "CrossOver", 2, "Tried " GENMAN_REPEAT_FAILED_STR "x and failed: %s and %s", g1.getGenes().c_str(), g2.getGenes().c_str());
     368                        return Geno("", -1, "", "GENOPER_OPFAIL: CrossOver() tried " GENMAN_REPEAT_FAILED_STR "x and failed");
    363369                }
    364370        }
     
    407413        }
    408414        GenoOperators *gf = getOper_f(format);
    409         SString geny=G->getGenes();
     415        SString geny = G->getGenes();
    410416        for (unsigned int pos = 0; pos < strlen(g); pos++)
    411417        {
     
    438444        string html = "\n<div style=\"background:white;padding:0.2em;font-family:arial,helvetica,sans-serif;font-size:90%\">";
    439445        uint32_t prevstyle, prevcolor, style = 0, color = 0;
    440         for (int i = 0; i<len; i++)
    441         {
    442                 if (shorten && ((lines == 0 && chars>160) || (lines > 5 || chars > 300))) { shortened = true; break; }
     446        for (int i = 0; i < len; i++)
     447        {
     448                if (shorten && ((lines == 0 && chars > 160) || (lines > 5 || chars > 300))) { shortened = true; break; }
    443449                if (g[i] == '\r') continue;
    444450                if (g[i] == '\n') { html += "<br>\n"; lines++; continue; }
     
    566572        {
    567573                string l;
    568                 if (oper_fx_list[i]->checkValidity("","") != GENOPER_NOOPER) l += " checkValidity";
     574                if (oper_fx_list[i]->checkValidity("", "") != GENOPER_NOOPER) l += " checkValidity";
    569575                if (oper_fx_list[i]->getSimplest())
    570576                {
    571577                        g = strdup(oper_fx_list[i]->getSimplest());
    572578                        g2 = strdup(g);
    573                         if (oper_fx_list[i]->validate(g,"") != GENOPER_NOOPER) l += " validate";
     579                        if (oper_fx_list[i]->validate(g, "") != GENOPER_NOOPER) l += " validate";
    574580                        if (oper_fx_list[i]->mutate(g, f1, m) != GENOPER_NOOPER) l += " mutate";
    575581                        if (oper_fx_list[i]->crossOver(g, g2, f1, f2) != GENOPER_NOOPER) l += " crossover";
Note: See TracChangeset for help on using the changeset viewer.