[286] | 1 | // This file is a part of Framsticks SDK. http://www.framsticks.com/ |
---|
[974] | 2 | // Copyright (C) 1999-2020 Maciej Komosinski and Szymon Ulatowski. |
---|
[286] | 3 | // See LICENSE.txt for details. |
---|
[140] | 4 | |
---|
[779] | 5 | #include "fF_oper.h" |
---|
[145] | 6 | #include "fF_genotype.h" |
---|
[899] | 7 | #include <common/nonstd.h> //rndUint, rndDouble |
---|
[140] | 8 | |
---|
| 9 | |
---|
| 10 | #define FIELDSTRUCT GenoOper_fF |
---|
[974] | 11 | static ParamEntry geno_fF_paramtab[] = |
---|
[140] | 12 | { |
---|
[145] | 13 | { "Genetics: fF", 1, 1, }, |
---|
[750] | 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).", }, |
---|
[145] | 15 | { 0, }, |
---|
[140] | 16 | }; |
---|
| 17 | #undef FIELDSTRUCT |
---|
| 18 | |
---|
| 19 | |
---|
| 20 | GenoOper_fF::GenoOper_fF() |
---|
| 21 | { |
---|
[974] | 22 | par.setParamTab(geno_fF_paramtab); |
---|
[140] | 23 | par.select(this); |
---|
| 24 | par.setDefault(); |
---|
[145] | 25 | supported_format = 'F'; |
---|
[140] | 26 | } |
---|
| 27 | |
---|
[513] | 28 | int GenoOper_fF::checkValidity(const char* gene, const char *genoname) |
---|
[140] | 29 | { |
---|
[145] | 30 | fF_growth_params par; |
---|
| 31 | return par.load(gene) ? GENOPER_OK : 1; |
---|
[140] | 32 | } |
---|
| 33 | |
---|
[513] | 34 | int GenoOper_fF::validate(char *&gene, const char *genoname) |
---|
[140] | 35 | { |
---|
[145] | 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(); |
---|
[140] | 39 | free(gene); |
---|
[145] | 40 | gene = strdup(validated.c_str()); //reallocate |
---|
[140] | 41 | return GENOPER_OK; |
---|
| 42 | } |
---|
| 43 | |
---|
[145] | 44 | //Creep-mutate one property |
---|
| 45 | int GenoOper_fF::mutate(char *&gene, float &chg, int &method) |
---|
[140] | 46 | { |
---|
[145] | 47 | method = 0; |
---|
| 48 | fF_growth_params par; |
---|
| 49 | par.load(gene); |
---|
[667] | 50 | static const int propsToMutate[] = fF_PROPS_TO_MUTATE; |
---|
[896] | 51 | int which = rndUint(ARRAY_LENGTH(propsToMutate)); |
---|
[667] | 52 | bool mutated_ok = GenoOperators::mutatePropertyNaive(par.param, propsToMutate[which]); |
---|
[145] | 53 | if (mutated_ok) |
---|
[140] | 54 | { |
---|
[145] | 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; |
---|
[140] | 60 | } |
---|
[145] | 61 | else |
---|
[140] | 62 | { |
---|
[145] | 63 | chg = 0; |
---|
| 64 | return GENOPER_OPFAIL; |
---|
[140] | 65 | } |
---|
| 66 | } |
---|
| 67 | |
---|
[145] | 68 | ///Averaging crossover |
---|
| 69 | int GenoOper_fF::crossOver(char *&g1, char *&g2, float& chg1, float& chg2) |
---|
[140] | 70 | { |
---|
[145] | 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 |
---|
[140] | 88 | return GENOPER_OK; |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | ///Applying some colors and font styles... |
---|
[247] | 92 | uint32_t GenoOper_fF::style(const char *g, int pos) |
---|
[140] | 93 | { |
---|
[145] | 94 | char ch = g[pos]; |
---|
[247] | 95 | uint32_t style = GENSTYLE_CS(0, GENSTYLE_INVALID); //default, should be changed below |
---|
[145] | 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); |
---|
[140] | 100 | return style; |
---|
| 101 | } |
---|