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

Last change on this file since 151 was 145, checked in by sz, 11 years ago

Genetics reorganization (affects ALL applications!):

  • Converters/Validators? are now configured/initialized in a more verbose but also less confusing way
  • At the same time, the PreconfiguredGenetics? object will help you avoid the increased complexity by creating the ready-to-use environment that is sufficient in 99% of cases (see the demos)
  • Format F genetics updated (work in progress)
  • Property svn:eol-style set to native
File size: 2.8 KB
Line 
1// This file is a part of the Framsticks GDK.
2// Copyright (C) 2002-2014  Maciej Komosinski and Szymon Ulatowski.  See LICENSE.txt for details.
3// Refer to http://www.framsticks.com/ for further information.
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)
29{
30        fF_growth_params par;
31        return par.load(gene) ? GENOPER_OK : 1;
32}
33
34int GenoOper_fF::validate(char *&gene)
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...
91unsigned long GenoOper_fF::style(const char *g, int pos)
92{
93        char ch = g[pos];
94        unsigned long 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.