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

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

Added a new "fn" genetic representation (a vector of real numbers for custom numerical optimization)

File size: 2.9 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, 1, },
14        { "fn_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
20
21GenoOper_fn::GenoOper_fn()
22{
23        par.setParamTab(GENOfnparam_tab);
24        par.select(this);
25        par.setDefault();
26        supported_format = 'n';
27}
28
29int GenoOper_fn::checkValidity(const char* gene, const char *genoname)
30{
31        vector<double> values = GenoConv_fn0::stringToVector(gene);
32        return values.size() > 0 ? GENOPER_OK : 1;
33}
34
35int GenoOper_fn::validate(char *&gene, const char *genoname)
36{
37        vector<double> values = GenoConv_fn0::stringToVector(gene);
38        if (values.size() == 0)
39                values.push_back(0.0);
40        string validated = GenoConv_fn0::vectorToString(values);
41        free(gene);
42        gene = strdup(validated.c_str()); //reallocate
43        return GENOPER_OK;
44}
45
46//Creep-mutate one property
47int GenoOper_fn::mutate(char *&gene, float &chg, int &method)
48{
49        method = 0;
50        vector<double> values = GenoConv_fn0::stringToVector(gene);
51        if (values.size() == 0)
52                return GENOPER_OPFAIL;
53        int which = randomN(values.size());
54        values[which] = GenoOperators::mutateCreep('f', values[which], -100, 100); //TODO precision 0.001 is forced!
55        string saved = GenoConv_fn0::vectorToString(values);
56        free(gene);
57        gene = strdup(saved.c_str()); //reallocate
58        chg = 1.0f / values.size();
59        return GENOPER_OK;
60}
61
62///Averaging crossover
63int GenoOper_fn::crossOver(char *&g1, char *&g2, float& chg1, float& chg2)
64{
65        //g1 = strdup("[1,0.5,0.5,0.5,0.5,1,1]"); //testing...
66        //g2 = strdup("[4,1,  1,  1,  1,  2,2]"); //testing...
67        //xover_proportion = 0.1; //testing...
68
69        vector<double> v1 = GenoConv_fn0::stringToVector(g1);
70        vector<double> v2 = GenoConv_fn0::stringToVector(g2);
71
72        chg1 = xover_proportion;
73        chg2 = 1 - xover_proportion;
74
75        GenoOperators::linearMix(v1, v2, xover_proportion);
76
77        string saved = GenoConv_fn0::vectorToString(v1);
78        free(g1);
79        g1 = strdup(saved.c_str()); //reallocate
80        saved = GenoConv_fn0::vectorToString(v2);
81        free(g2);
82        g2 = strdup(saved.c_str()); //reallocate
83        return GENOPER_OK;
84}
85
86///Applying some colors and font styles...
87uint32_t GenoOper_fn::style(const char *g, int pos)
88{
89        char ch = g[pos];
90        uint32_t style = GENSTYLE_CS(0, GENSTYLE_INVALID); //default, should be changed below
91        if (strchr("-.e 0123456789", ch) != NULL)
92                style = GENSTYLE_CS(GENCOLOR_NUMBER, GENSTYLE_NONE);
93        else if (strchr("[,]", ch) != NULL)
94                style = GENSTYLE_RGBS(0, 0, 0, GENSTYLE_BOLD);
95        return style;
96}
Note: See TracBrowser for help on using the repository browser.