source: cpp/frams/genetics/fF/fF_oper.cpp @ 1125

Last change on this file since 1125 was 974, checked in by Maciej Komosinski, 4 years ago

Renamed all genooper paramtab variables to be consistent with the standard naming convention

  • Property svn:eol-style set to native
File size: 2.8 KB
Line 
1// This file is a part of Framsticks SDK.  http://www.framsticks.com/
2// Copyright (C) 1999-2020  Maciej Komosinski and Szymon Ulatowski.
3// See LICENSE.txt for details.
4
5#include "fF_oper.h"
6#include "fF_genotype.h"
7#include <common/nonstd.h> //rndUint, rndDouble
8
9
10#define FIELDSTRUCT GenoOper_fF
11static ParamEntry geno_fF_paramtab[] =
12{
13        { "Genetics: fF", 1, 1, },
14        { "fF_xover", 0, 0, "Inherited in linear mix crossover", "f 0.5 1.0 0.5", FIELD(xover_proportion), "0.5 => children are averaged parents.\n0.8 => children are only 20% different from parents.\n1.0 => 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(geno_fF_paramtab);
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        static const int propsToMutate[] = fF_PROPS_TO_MUTATE;
51        int which = rndUint(ARRAY_LENGTH(propsToMutate));
52        bool mutated_ok = GenoOperators::mutatePropertyNaive(par.param, propsToMutate[which]);
53        if (mutated_ok)
54        {
55                string saved = par.save();
56                free(gene);
57                gene = strdup(saved.c_str()); //reallocate
58                chg = 1.0f / par.param.getPropCount();
59                return GENOPER_OK;
60        }
61        else
62        {
63                chg = 0;
64                return GENOPER_OPFAIL;
65        }
66}
67
68///Averaging crossover
69int GenoOper_fF::crossOver(char *&g1, char *&g2, float& chg1, float& chg2)
70{
71        //g1 = strdup("1,0.5,0.5,0.5,0.5,1,1"); //testing...
72        //g2 = strdup("4,1,  1,  1,  1,  2,2"); //testing...
73        //xover_proportion = 0.1; //testing...
74        fF_growth_params par1;
75        par1.load(g1);
76        fF_growth_params par2;
77        par2.load(g2);
78        chg1 = xover_proportion;
79        chg2 = 1 - xover_proportion;
80        for (int i = 0; i < par1.param.getPropCount(); i++)
81                GenoOperators::linearMix(par1.param, i, par2.param, i, xover_proportion);
82        string saved = par1.save();
83        free(g1);
84        g1 = strdup(saved.c_str()); //reallocate
85        saved = par2.save();
86        free(g2);
87        g2 = strdup(saved.c_str()); //reallocate
88        return GENOPER_OK;
89}
90
91///Applying some colors and font styles...
92uint32_t GenoOper_fF::style(const char *g, int pos)
93{
94        char ch = g[pos];
95        uint32_t style = GENSTYLE_CS(0, GENSTYLE_INVALID); //default, should be changed below
96        if (strchr("-.e 0123456789", ch) != NULL)
97                style = GENSTYLE_CS(GENCOLOR_NUMBER, GENSTYLE_NONE);
98        else if (ch == ',')
99                style = GENSTYLE_RGBS(0, 0, 0, GENSTYLE_BOLD);
100        return style;
101}
Note: See TracBrowser for help on using the repository browser.