1 | // This file is a part of the Framsticks GDK. |
---|
2 | // Copyright (C) 2002-2014 Maciej Komosinski and Szymon Ulatowski. See LICENSE.txt for details. |
---|
3 | // Refer to http://www.framsticks.com/ for further information. |
---|
4 | |
---|
5 | #include "oper_fF.h" |
---|
6 | #include <frams/genetics/f9/conv_f9.h> |
---|
7 | #include <common/nonstd.h> //randomN, rnd01 |
---|
8 | |
---|
9 | |
---|
10 | //THIS FILE NEEDS UPDATE. OLD, UNRELATED SOURCES BELOW (COPIED FROM f9). |
---|
11 | |
---|
12 | #define FIELDSTRUCT GenoOper_fF |
---|
13 | static ParamEntry GENOf9param_tab[]= |
---|
14 | { |
---|
15 | {"Genetics: fF",1,1,}, |
---|
16 | {"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)",}, |
---|
17 | {0,}, |
---|
18 | }; |
---|
19 | #undef FIELDSTRUCT |
---|
20 | |
---|
21 | |
---|
22 | GenoOper_fF::GenoOper_fF() |
---|
23 | { |
---|
24 | par.setParamTab(GENOf9param_tab); |
---|
25 | par.select(this); |
---|
26 | par.setDefault(); |
---|
27 | supported_format='F'; |
---|
28 | } |
---|
29 | |
---|
30 | int GenoOper_fF::checkValidity(const char* gene) |
---|
31 | { |
---|
32 | return GENOPER_OK; |
---|
33 | if (!gene[0]) return 1; //empty is not valid |
---|
34 | bool ok=true; |
---|
35 | int i; |
---|
36 | for(i=0;i<strlen(gene);i++) if (!strchr(turtle_commands_f9,gene[i])) {ok=false; break;} |
---|
37 | return ok ? GENOPER_OK : i+1; |
---|
38 | } |
---|
39 | |
---|
40 | ///Remove all invalid letters from the genotype |
---|
41 | int GenoOper_fF::validate(char *&gene) |
---|
42 | { |
---|
43 | SString validated; //new genotype (everything except turtle_commands_f9 is skipped) |
---|
44 | for(int i=0;i<strlen(gene);i++) |
---|
45 | if (strchr(turtle_commands_f9,gene[i])) validated+=gene[i]; //validated contains only turtle_commands_f9 |
---|
46 | free(gene); |
---|
47 | gene=strdup(validated); //reallocate |
---|
48 | return GENOPER_OK; |
---|
49 | } |
---|
50 | |
---|
51 | ///Very simple mutation |
---|
52 | int GenoOper_fF::mutate(char *&gene,float &chg,int &method) |
---|
53 | { |
---|
54 | method=0; |
---|
55 | int changes=0,len=strlen(gene); |
---|
56 | int symbols=strlen(turtle_commands_f9); |
---|
57 | |
---|
58 | for(int i=0;i<len;i++) |
---|
59 | { |
---|
60 | if (rnd01<mut_prob) //normalize prob with the length of the genotype |
---|
61 | { |
---|
62 | char oldgene=gene[i]; |
---|
63 | gene[i]=turtle_commands_f9[randomN(symbols)]; |
---|
64 | if (gene[i]!=oldgene) changes++; |
---|
65 | } |
---|
66 | } |
---|
67 | |
---|
68 | if (rnd01<mut_prob) //add or delete a random char |
---|
69 | { |
---|
70 | SString newgeno(gene); |
---|
71 | if (randomN(2)==0) //add |
---|
72 | { |
---|
73 | int symbols=strlen(turtle_commands_f9); |
---|
74 | int p=randomN(len+1); //random location |
---|
75 | //printf("before add: %s\n",(const char*)newgeno); |
---|
76 | newgeno=newgeno.substr(0,p)+SString(turtle_commands_f9+randomN(symbols),1)+newgeno.substr(p); |
---|
77 | //printf("after add: %s\n",(const char*)newgeno); |
---|
78 | changes++; |
---|
79 | } else if (len>1) //delete |
---|
80 | { |
---|
81 | int p=randomN(len); //random location |
---|
82 | //printf("before delete: %s\n",(const char*)newgeno); |
---|
83 | newgeno=newgeno.substr(0,p)+newgeno.substr(p+1); |
---|
84 | //printf("after delete: %s\n",(const char*)newgeno); |
---|
85 | changes++; |
---|
86 | } |
---|
87 | free(gene); |
---|
88 | gene=strdup(newgeno); //reallocate |
---|
89 | } |
---|
90 | |
---|
91 | chg=(float)changes/len; |
---|
92 | return changes>0?GENOPER_OK:GENOPER_OPFAIL; //no changes => OPFAIL so that genman will call mutate again |
---|
93 | } |
---|
94 | |
---|
95 | ///A simple one-point crossover |
---|
96 | int GenoOper_fF::crossOver(char *&g1,char *&g2,float& chg1,float& chg2) |
---|
97 | { |
---|
98 | int len1=strlen(g1),len2=strlen(g2); |
---|
99 | int p1=randomN(len1); //random cut point for first genotype |
---|
100 | int p2=randomN(len2); //random cut point for second genotype |
---|
101 | char *child1=(char*)malloc(p1+len2-p2+1); |
---|
102 | char *child2=(char*)malloc(p2+len1-p1+1); |
---|
103 | strncpy(child1,g1,p1); strcpy(child1+p1,g2+p2); |
---|
104 | strncpy(child2,g2,p2); strcpy(child2+p2,g1+p1); |
---|
105 | free(g1); g1=child1; |
---|
106 | free(g2); g2=child2; |
---|
107 | chg1=(float)p1/strlen(child1); |
---|
108 | chg2=(float)p2/strlen(child2); |
---|
109 | return GENOPER_OK; |
---|
110 | } |
---|
111 | |
---|
112 | ///Applying some colors and font styles... |
---|
113 | unsigned long GenoOper_fF::style(const char *g, int pos) |
---|
114 | { |
---|
115 | char ch=g[pos]; |
---|
116 | unsigned long style=GENSTYLE_CS(0,GENSTYLE_INVALID); //default, should be changed below |
---|
117 | char *ptr=strchr((char*)turtle_commands_f9,ch); |
---|
118 | if (ptr) |
---|
119 | { |
---|
120 | int pos=ptr-turtle_commands_f9; |
---|
121 | int axis=pos/2; |
---|
122 | style=GENSTYLE_RGBS(axis==0?200:0,axis==1?200:0,axis==2?200:0,GENSTYLE_NONE); |
---|
123 | } |
---|
124 | return style; |
---|
125 | } |
---|