// This file is a part of Framsticks SDK. http://www.framsticks.com/ // Copyright (C) 1999-2018 Maciej Komosinski and Szymon Ulatowski. // See LICENSE.txt for details. #include "oper_fn.h" #include "conv_fn.h" #include //randomN, rnd01 #define FIELDSTRUCT GenoOper_fn static ParamEntry GENOfnparam_tab[] = { { "Genetics: fn", 1, 1, }, { "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).", }, { 0, }, }; #undef FIELDSTRUCT GenoOper_fn::GenoOper_fn() { par.setParamTab(GENOfnparam_tab); par.select(this); par.setDefault(); supported_format = 'n'; } int GenoOper_fn::checkValidity(const char* gene, const char *genoname) { vector values = GenoConv_fn0::stringToVector(gene); return values.size() > 0 ? GENOPER_OK : 1; } int GenoOper_fn::validate(char *&gene, const char *genoname) { vector values = GenoConv_fn0::stringToVector(gene); if (values.size() == 0) values.push_back(0.0); string validated = GenoConv_fn0::vectorToString(values); free(gene); gene = strdup(validated.c_str()); //reallocate return GENOPER_OK; } //Creep-mutate one property int GenoOper_fn::mutate(char *&gene, float &chg, int &method) { method = 0; vector values = GenoConv_fn0::stringToVector(gene); if (values.size() == 0) return GENOPER_OPFAIL; int which = randomN(values.size()); values[which] = GenoOperators::mutateCreep('f', values[which], -100, 100); //TODO precision 0.001 is forced! string saved = GenoConv_fn0::vectorToString(values); free(gene); gene = strdup(saved.c_str()); //reallocate chg = 1.0f / values.size(); return GENOPER_OK; } ///Averaging crossover int GenoOper_fn::crossOver(char *&g1, char *&g2, float& chg1, float& chg2) { //g1 = strdup("[1,0.5,0.5,0.5,0.5,1,1]"); //testing... //g2 = strdup("[4,1, 1, 1, 1, 2,2]"); //testing... //xover_proportion = 0.1; //testing... vector v1 = GenoConv_fn0::stringToVector(g1); vector v2 = GenoConv_fn0::stringToVector(g2); chg1 = xover_proportion; chg2 = 1 - xover_proportion; GenoOperators::linearMix(v1, v2, xover_proportion); string saved = GenoConv_fn0::vectorToString(v1); free(g1); g1 = strdup(saved.c_str()); //reallocate saved = GenoConv_fn0::vectorToString(v2); free(g2); g2 = strdup(saved.c_str()); //reallocate return GENOPER_OK; } ///Applying some colors and font styles... uint32_t GenoOper_fn::style(const char *g, int pos) { char ch = g[pos]; uint32_t style = GENSTYLE_CS(0, GENSTYLE_INVALID); //default, should be changed below if (strchr("-.e 0123456789", ch) != NULL) style = GENSTYLE_CS(GENCOLOR_NUMBER, GENSTYLE_NONE); else if (strchr("[,]", ch) != NULL) style = GENSTYLE_RGBS(0, 0, 0, GENSTYLE_BOLD); return style; }