// This file is a part of the Framsticks GDK. // Copyright (C) 2002-2014 Maciej Komosinski and Szymon Ulatowski. See LICENSE.txt for details. // Refer to http://www.framsticks.com/ for further information. #include "oper_fF.h" #include #include //randomN, rnd01 //THIS FILE NEEDS UPDATE. OLD, UNRELATED SOURCES BELOW (COPIED FROM f9). #define FIELDSTRUCT GenoOper_fF static ParamEntry GENOf9param_tab[]= { {"Genetics: fF",1,1,}, {"fF_mut",0,0,"Mutation probability","f 0 1 0.1",FIELD(mut_prob),"How many genes should be mutated during single mutation (1=all genes, 0.1=ten percent)",}, {0,}, }; #undef FIELDSTRUCT GenoOper_fF::GenoOper_fF() { par.setParamTab(GENOf9param_tab); par.select(this); par.setDefault(); supported_format='F'; } int GenoOper_fF::checkValidity(const char* gene) { return GENOPER_OK; if (!gene[0]) return 1; //empty is not valid bool ok=true; int i; for(i=0;i1) //delete { int p=randomN(len); //random location //printf("before delete: %s\n",(const char*)newgeno); newgeno=newgeno.substr(0,p)+newgeno.substr(p+1); //printf("after delete: %s\n",(const char*)newgeno); changes++; } free(gene); gene=strdup(newgeno); //reallocate } chg=(float)changes/len; return changes>0?GENOPER_OK:GENOPER_OPFAIL; //no changes => OPFAIL so that genman will call mutate again } ///A simple one-point crossover int GenoOper_fF::crossOver(char *&g1,char *&g2,float& chg1,float& chg2) { int len1=strlen(g1),len2=strlen(g2); int p1=randomN(len1); //random cut point for first genotype int p2=randomN(len2); //random cut point for second genotype char *child1=(char*)malloc(p1+len2-p2+1); char *child2=(char*)malloc(p2+len1-p1+1); strncpy(child1,g1,p1); strcpy(child1+p1,g2+p2); strncpy(child2,g2,p2); strcpy(child2+p2,g1+p1); free(g1); g1=child1; free(g2); g2=child2; chg1=(float)p1/strlen(child1); chg2=(float)p2/strlen(child2); return GENOPER_OK; } ///Applying some colors and font styles... unsigned long GenoOper_fF::style(const char *g, int pos) { char ch=g[pos]; unsigned long style=GENSTYLE_CS(0,GENSTYLE_INVALID); //default, should be changed below char *ptr=strchr((char*)turtle_commands_f9,ch); if (ptr) { int pos=ptr-turtle_commands_f9; int axis=pos/2; style=GENSTYLE_RGBS(axis==0?200:0,axis==1?200:0,axis==2?200:0,GENSTYLE_NONE); } return style; }