source: cpp/frams/genetics/fn/oper_fn.cpp @ 752

Last change on this file since 752 was 752, checked in by Maciej Komosinski, 6 years ago

Mutation in the 'fn' encoding respects custom min,max,stddev for each variable

File size: 4.0 KB
Line 
1// This file is a part of Framsticks SDK.  http://www.framsticks.com/
2// Copyright (C) 1999-2018  Maciej Komosinski and Szymon Ulatowski.
3// See LICENSE.txt for details.
4
5#include "oper_fn.h"
6#include "conv_fn.h"
7#include <common/nonstd.h> //randomN, rnd01
8
9
10#define FIELDSTRUCT GenoOper_fn
11static ParamEntry GENOfnparam_tab[] =
12{
13        { "Genetics: fn", 1, 4, },
14        { "fn_xover", 0, 0, "Inherited in linear mix crossover", "f 0.5 1.0 0.9", 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        { "fn_mut_bound_low", 1, 0, "Lower bounds for mutation", "s 0 0 [-1.0]", FIELD(mut_bound_low), "A vector of lower bounds (one real value for each variable)", },
16        { "fn_mut_bound_high", 1, 0, "Higher bounds for mutation", "s 0 0 [1.0]", FIELD(mut_bound_high), "A vector of higher bounds (one real value for each variable)", },
17        { "fn_mut_stddev", 1, 0, "Standard deviations for mutation", "s 0 0 [0.1]", FIELD(mut_stddev), "A vector of standard deviations (one real value for each variable)", },
18        { 0, },
19};
20#undef FIELDSTRUCT
21
22
23
24GenoOper_fn::GenoOper_fn()
25{
26        par.setParamTab(GENOfnparam_tab);
27        par.select(this);
28        par.setDefault();
29        supported_format = 'n';
30}
31
32int GenoOper_fn::checkValidity(const char* gene, const char *genoname)
33{
34        vector<double> values = GenoConv_fn0::stringToVector(gene);
35        return values.size() > 0 ? GENOPER_OK : 1;
36}
37
38int GenoOper_fn::validate(char *&gene, const char *genoname)
39{
40        vector<double> values = GenoConv_fn0::stringToVector(gene);
41        if (values.size() == 0)
42                values.push_back(0.0);
43        string validated = GenoConv_fn0::vectorToString(values);
44        free(gene);
45        gene = strdup(validated.c_str()); //reallocate
46        return GENOPER_OK;
47}
48
49//Creep-mutate one property
50int GenoOper_fn::mutate(char *&gene, float &chg, int &method)
51{
52        method = 0;
53        vector<double> values = GenoConv_fn0::stringToVector(gene);
54        if (values.size() == 0)
55                return GENOPER_OPFAIL;
56        vector<double> bound_low = GenoConv_fn0::stringToVector(mut_bound_low.c_str());
57        vector<double> bound_high = GenoConv_fn0::stringToVector(mut_bound_high.c_str());
58        vector<double> stddev = GenoConv_fn0::stringToVector(mut_stddev.c_str());
59        if (bound_low.size() != bound_high.size() || bound_high.size() != stddev.size() || stddev.size() != values.size())
60        {
61                logPrintf("GenoOper_fn", "mutate", LOG_ERROR, "The solution vector, bound vectors, and standard deviation vectors must all have the same number of values");
62                return GENOPER_OPFAIL;
63        }
64
65        int which = randomN(values.size());
66        values[which] = GenoOperators::mutateCreep('f', values[which], bound_low[which], bound_high[which], stddev[which], false);
67        string saved = GenoConv_fn0::vectorToString(values);
68        free(gene);
69        gene = strdup(saved.c_str()); //reallocate
70        chg = 1.0f / values.size();
71        return GENOPER_OK;
72}
73
74///Averaging crossover
75int GenoOper_fn::crossOver(char *&g1, char *&g2, float& chg1, float& chg2)
76{
77        //g1 = strdup("[1,0.5,0.5,0.5,0.5,1,1]"); //testing...
78        //g2 = strdup("[4,1,  1,  1,  1,  2,2]"); //testing...
79        //xover_proportion = 0.1; //testing...
80
81        vector<double> v1 = GenoConv_fn0::stringToVector(g1);
82        vector<double> v2 = GenoConv_fn0::stringToVector(g2);
83
84        chg1 = xover_proportion;
85        chg2 = 1 - xover_proportion;
86
87        GenoOperators::linearMix(v1, v2, xover_proportion);
88
89        string saved = GenoConv_fn0::vectorToString(v1);
90        free(g1);
91        g1 = strdup(saved.c_str()); //reallocate
92        saved = GenoConv_fn0::vectorToString(v2);
93        free(g2);
94        g2 = strdup(saved.c_str()); //reallocate
95        return GENOPER_OK;
96}
97
98///Applying some colors and font styles...
99uint32_t GenoOper_fn::style(const char *g, int pos)
100{
101        char ch = g[pos];
102        uint32_t style = GENSTYLE_CS(0, GENSTYLE_INVALID); //default, should be changed below
103        if (strchr("-.e 0123456789", ch) != NULL)
104                style = GENSTYLE_CS(GENCOLOR_NUMBER, GENSTYLE_NONE);
105        else if (strchr("[,]", ch) != NULL)
106                style = GENSTYLE_RGBS(0, 0, 0, GENSTYLE_BOLD);
107        return style;
108}
Note: See TracBrowser for help on using the repository browser.