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 | /**
|
---|
11 | \class GenoOper_fn
|
---|
12 |
|
---|
13 | This genetic representation only stores a vector of real numbers. A fitness function must be provided
|
---|
14 | for the gene pool, for example the "Booth function" would be:
|
---|
15 |
|
---|
16 | var X = String.deserialize(this.geno.rawgenotype); //a vector of real values
|
---|
17 | var result = Math.pow(X[0]+2*X[1]-7,2) + Math.pow(2*X[0]+X[1]-5,2);
|
---|
18 | return -result; //negation because Framsticks assumes maximization, and the original function needs to be minimized
|
---|
19 | */
|
---|
20 |
|
---|
21 |
|
---|
22 |
|
---|
23 | #define FIELDSTRUCT GenoOper_fn
|
---|
24 | static ParamEntry GENOfnparam_tab[] =
|
---|
25 | {
|
---|
26 | { "Genetics: fn", 1, 4, },
|
---|
27 | { "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).", },
|
---|
28 | { "fn_mut_bound_low", 1, 0, "Lower bounds for mutation", "s 0 0 [-10.0, -10.0]", FIELD(mut_bound_low), "A vector of lower bounds (one real value for each variable)", },
|
---|
29 | { "fn_mut_bound_high", 1, 0, "Higher bounds for mutation", "s 0 0 [10.0, 10.0]", FIELD(mut_bound_high), "A vector of higher bounds (one real value for each variable)", },
|
---|
30 | { "fn_mut_stddev", 1, 0, "Standard deviations for mutation", "s 0 0 [0.1, 0.1]", FIELD(mut_stddev), "A vector of standard deviations (one real value for each variable)", },
|
---|
31 | { 0, },
|
---|
32 | };
|
---|
33 | #undef FIELDSTRUCT
|
---|
34 |
|
---|
35 |
|
---|
36 |
|
---|
37 | GenoOper_fn::GenoOper_fn()
|
---|
38 | {
|
---|
39 | par.setParamTab(GENOfnparam_tab);
|
---|
40 | par.select(this);
|
---|
41 | par.setDefault();
|
---|
42 | supported_format = 'n';
|
---|
43 | }
|
---|
44 |
|
---|
45 | int GenoOper_fn::checkValidity(const char* gene, const char *genoname)
|
---|
46 | {
|
---|
47 | vector<double> values = GenoConv_fn0::stringToVector(gene);
|
---|
48 | return values.size() > 0 ? GENOPER_OK : 1;
|
---|
49 | }
|
---|
50 |
|
---|
51 | int GenoOper_fn::validate(char *&gene, const char *genoname)
|
---|
52 | {
|
---|
53 | vector<double> values = GenoConv_fn0::stringToVector(gene);
|
---|
54 | if (values.size() == 0)
|
---|
55 | values.push_back(0.0);
|
---|
56 | string validated = GenoConv_fn0::vectorToString(values);
|
---|
57 | free(gene);
|
---|
58 | gene = strdup(validated.c_str()); //reallocate
|
---|
59 | return GENOPER_OK;
|
---|
60 | }
|
---|
61 |
|
---|
62 | //Creep-mutate one property
|
---|
63 | int GenoOper_fn::mutate(char *&gene, float &chg, int &method)
|
---|
64 | {
|
---|
65 | method = 0;
|
---|
66 | vector<double> values = GenoConv_fn0::stringToVector(gene);
|
---|
67 | if (values.size() == 0)
|
---|
68 | return GENOPER_OPFAIL;
|
---|
69 | vector<double> bound_low = GenoConv_fn0::stringToVector(mut_bound_low.c_str());
|
---|
70 | vector<double> bound_high = GenoConv_fn0::stringToVector(mut_bound_high.c_str());
|
---|
71 | vector<double> stddev = GenoConv_fn0::stringToVector(mut_stddev.c_str());
|
---|
72 | if (bound_low.size() != bound_high.size() || bound_high.size() != stddev.size() || stddev.size() != values.size())
|
---|
73 | {
|
---|
74 | logPrintf("GenoOper_fn", "mutate", LOG_ERROR, "The solution vector, bound vectors, and standard deviation vectors must all have the same number of values");
|
---|
75 | return GENOPER_OPFAIL;
|
---|
76 | }
|
---|
77 |
|
---|
78 | int which = randomN(values.size());
|
---|
79 | values[which] = GenoOperators::mutateCreep('f', values[which], bound_low[which], bound_high[which], stddev[which], false);
|
---|
80 | string saved = GenoConv_fn0::vectorToString(values);
|
---|
81 | free(gene);
|
---|
82 | gene = strdup(saved.c_str()); //reallocate
|
---|
83 | chg = 1.0f / values.size();
|
---|
84 | return GENOPER_OK;
|
---|
85 | }
|
---|
86 |
|
---|
87 | ///Averaging crossover
|
---|
88 | int GenoOper_fn::crossOver(char *&g1, char *&g2, float& chg1, float& chg2)
|
---|
89 | {
|
---|
90 | //g1 = strdup("[1,0.5,0.5,0.5,0.5,1,1]"); //testing...
|
---|
91 | //g2 = strdup("[4,1, 1, 1, 1, 2,2]"); //testing...
|
---|
92 | //xover_proportion = 0.1; //testing...
|
---|
93 |
|
---|
94 | chg1 = xover_proportion;
|
---|
95 | chg2 = 1 - xover_proportion;
|
---|
96 |
|
---|
97 | vector<double> v1 = GenoConv_fn0::stringToVector(g1);
|
---|
98 | vector<double> v2 = GenoConv_fn0::stringToVector(g2);
|
---|
99 |
|
---|
100 | if (v1.size() != v2.size())
|
---|
101 | {
|
---|
102 | logPrintf("GenoOper_fn", "crossOver", LOG_ERROR, "Tried to cross over solutions with a differing number of variables (%d and %d)", v1.size(), v2.size());
|
---|
103 | return GENOPER_OPFAIL;
|
---|
104 | }
|
---|
105 |
|
---|
106 | GenoOperators::linearMix(v1, v2, xover_proportion);
|
---|
107 |
|
---|
108 | string saved = GenoConv_fn0::vectorToString(v1);
|
---|
109 | free(g1);
|
---|
110 | g1 = strdup(saved.c_str()); //reallocate
|
---|
111 | saved = GenoConv_fn0::vectorToString(v2);
|
---|
112 | free(g2);
|
---|
113 | g2 = strdup(saved.c_str()); //reallocate
|
---|
114 | return GENOPER_OK;
|
---|
115 | }
|
---|
116 |
|
---|
117 | ///Applying some colors and font styles...
|
---|
118 | uint32_t GenoOper_fn::style(const char *g, int pos)
|
---|
119 | {
|
---|
120 | char ch = g[pos];
|
---|
121 | uint32_t style = GENSTYLE_CS(0, GENSTYLE_INVALID); //default, should be changed below
|
---|
122 | if (strchr("-.e 0123456789", ch) != NULL)
|
---|
123 | style = GENSTYLE_CS(GENCOLOR_NUMBER, GENSTYLE_NONE);
|
---|
124 | else if (strchr("[,]", ch) != NULL)
|
---|
125 | style = GENSTYLE_RGBS(0, 0, 0, GENSTYLE_BOLD);
|
---|
126 | return style;
|
---|
127 | }
|
---|