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

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

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

  • Property svn:eol-style set to native
File size: 2.7 KB
Line 
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.
4
5#include "oper_fF.h"
6#include "fF_genotype.h"
7#include <common/nonstd.h> //randomN, rnd01
8
9
10#define FIELDSTRUCT GenoOper_fF
11static ParamEntry GENOfFparam_tab[] =
12{
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, },
16};
17#undef FIELDSTRUCT
18
19
20GenoOper_fF::GenoOper_fF()
21{
22        par.setParamTab(GENOfFparam_tab);
23        par.select(this);
24        par.setDefault();
25        supported_format = 'F';
26}
27
28int GenoOper_fF::checkValidity(const char* gene, const char *genoname)
29{
30        fF_growth_params par;
31        return par.load(gene) ? GENOPER_OK : 1;
32}
33
34int GenoOper_fF::validate(char *&gene, const char *genoname)
35{
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();
39        free(gene);
40        gene = strdup(validated.c_str()); //reallocate
41        return GENOPER_OK;
42}
43
44//Creep-mutate one property
45int GenoOper_fF::mutate(char *&gene, float &chg, int &method)
46{
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)
53        {
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;
59        }
60        else
61        {
62                chg = 0;
63                return GENOPER_OPFAIL;
64        }
65}
66
67///Averaging crossover
68int GenoOper_fF::crossOver(char *&g1, char *&g2, float& chg1, float& chg2)
69{
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
87        return GENOPER_OK;
88}
89
90///Applying some colors and font styles...
91uint32_t GenoOper_fF::style(const char *g, int pos)
92{
93        char ch = g[pos];
94        uint32_t style = GENSTYLE_CS(0, GENSTYLE_INVALID); //default, should be changed below
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);
99        return style;
100}
Note: See TracBrowser for help on using the repository browser.