source: cpp/frams/genetics/fF/oper_fF.cpp @ 567

Last change on this file since 567 was 513, checked in by Maciej Komosinski, 9 years ago

checkValidity() and validate() are now aware of genotype name

  • Property svn:eol-style set to native
File size: 2.7 KB
RevLine 
[286]1// This file is a part of Framsticks SDK.  http://www.framsticks.com/
2// Copyright (C) 1999-2015  Maciej Komosinski and Szymon Ulatowski.
3// See LICENSE.txt for details.
[140]4
5#include "oper_fF.h"
[145]6#include "fF_genotype.h"
[140]7#include <common/nonstd.h> //randomN, rnd01
8
9
10#define FIELDSTRUCT GenoOper_fF
[145]11static ParamEntry GENOfFparam_tab[] =
[140]12{
[145]13        { "Genetics: fF", 1, 1, },
14        { "fF_xover", 0, 0, "Averaging crossover proportion", "f 0 0.5 0.5", FIELD(xover_proportion), "0.5 => children are averaged parents.\n0.2 => children are only 20% different from parents.\n0 => each child is identical to one parent (no crossover).", },
15        { 0, },
[140]16};
17#undef FIELDSTRUCT
18
19
20GenoOper_fF::GenoOper_fF()
21{
[145]22        par.setParamTab(GENOfFparam_tab);
[140]23        par.select(this);
24        par.setDefault();
[145]25        supported_format = 'F';
[140]26}
27
[513]28int GenoOper_fF::checkValidity(const char* gene, const char *genoname)
[140]29{
[145]30        fF_growth_params par;
31        return par.load(gene) ? GENOPER_OK : 1;
[140]32}
33
[513]34int GenoOper_fF::validate(char *&gene, const char *genoname)
[140]35{
[145]36        fF_growth_params par; //is initialized with default values
37        par.load(gene); //loads as much as possible, other fields remain with default values
38        string validated = par.save();
[140]39        free(gene);
[145]40        gene = strdup(validated.c_str()); //reallocate
[140]41        return GENOPER_OK;
42}
43
[145]44//Creep-mutate one property
45int GenoOper_fF::mutate(char *&gene, float &chg, int &method)
[140]46{
[145]47        method = 0;
48        fF_growth_params par;
49        par.load(gene);
50        int which = randomN(par.param.getPropCount());
51        bool mutated_ok = GenoOperators::mutatePropertyNaive(par.param, which);
52        if (mutated_ok)
[140]53        {
[145]54                string saved = par.save();
55                free(gene);
56                gene = strdup(saved.c_str()); //reallocate
57                chg = 1.0f / par.param.getPropCount();
58                return GENOPER_OK;
[140]59        }
[145]60        else
[140]61        {
[145]62                chg = 0;
63                return GENOPER_OPFAIL;
[140]64        }
65}
66
[145]67///Averaging crossover
68int GenoOper_fF::crossOver(char *&g1, char *&g2, float& chg1, float& chg2)
[140]69{
[145]70        //g1 = strdup("1,0.5,0.5,0.5,0.5,1,1"); //testing...
71        //g2 = strdup("4,1,  1,  1,  1,  2,2"); //testing...
72        //xover_proportion = 0.1; //testing...
73        fF_growth_params par1;
74        par1.load(g1);
75        fF_growth_params par2;
76        par2.load(g2);
77        chg1 = xover_proportion;
78        chg2 = 1 - xover_proportion;
79        for (int i = 0; i < par1.param.getPropCount(); i++)
80                GenoOperators::linearMix(par1.param, i, par2.param, i, xover_proportion);
81        string saved = par1.save();
82        free(g1);
83        g1 = strdup(saved.c_str()); //reallocate
84        saved = par2.save();
85        free(g2);
86        g2 = strdup(saved.c_str()); //reallocate
[140]87        return GENOPER_OK;
88}
89
90///Applying some colors and font styles...
[247]91uint32_t GenoOper_fF::style(const char *g, int pos)
[140]92{
[145]93        char ch = g[pos];
[247]94        uint32_t style = GENSTYLE_CS(0, GENSTYLE_INVALID); //default, should be changed below
[145]95        if (strchr("-.e 0123456789", ch) != NULL)
96                style = GENSTYLE_CS(GENCOLOR_NUMBER, GENSTYLE_NONE);
97        else if (ch == ',')
98                style = GENSTYLE_RGBS(0, 0, 0, GENSTYLE_BOLD);
[140]99        return style;
100}
Note: See TracBrowser for help on using the repository browser.