source: cpp/geno_fx/geno_fx.h @ 24

Last change on this file since 24 was 3, checked in by Maciej Komosinski, 16 years ago

added geno_fx, a base class for genetic operations on genotypes

File size: 12.3 KB
Line 
1// This file is a part of Framsticks GenoFX library.
2// Copyright (C) 2002-2009  Maciej Komosinski.  See LICENSE.txt for details.
3// Refer to http://www.framsticks.com/ for further information.
4
5#ifndef _GENO_FX_H_
6#define _GENO_FX_H_
7
8#include "nonstd.h"
9#include "model.h"
10
11/** @file */
12
13/** \name Return codes for genetic operators */
14//@{
15#define GENOPER_OK          0 ///<operation successful
16#define GENOPER_OPFAIL     -1 ///<operation failed or could not be completed
17#define GENOPER_REPAIR     -2 ///<do not use in Geno_fx. GenMan uses it in checkValidity()... but will not. only f4 uses it
18#define GENOPER_NOOPER     -3 ///<do not use in Geno_fx. GenMan uses it for "no suitable operator for this genotype format"
19//@}
20
21/** \name gene/character predefined styles (for style() method) */
22//@{
23#define GENSTYLE_NONE       0 ///<no style specified (=normal font)
24#define GENSTYLE_INVALID    1 ///<this char cannot be accepted
25#define GENSTYLE_BOLD       2 ///<bold
26#define GENSTYLE_ITALIC     4 ///<italic
27#define GENSTYLE_STRIKEOUT  8 ///<strikeout (not recommended)
28//UNDERLINE used to mark errors
29//@}
30
31/** \name other useful style/color macros */
32//@{
33#define GENRGB(r,g,b) ((unsigned long)(((unsigned char)(r)|((unsigned short)((unsigned char)(g))<<8))|(((unsigned long)(unsigned char)(b))<<16)))
34#define GENSTYLE_RGBS(r,g,b,s) ((unsigned long)((unsigned char)s)<<24 | GENRGB(r,g,b))
35#define GENSTYLE_CS(rgb,s) ((unsigned long)((unsigned char)s)<<24 | rgb)
36
37#define GENGETSTYLE(style) ((style)>>24)
38#define GENGETCOLOR(style) ((style)&0x00ffffff)
39#define GENGET_R(style) ((style)&0xff)
40#define GENGET_G(style) ((style>>8)&0xff)
41#define GENGET_B(style) ((style>>16)&0xff)
42
43#define GENCOLOR_TEXT    GENRGB(0,0,0) ///<recommended color to use for text genes
44#define GENCOLOR_NUMBER  GENRGB(200,0,0) ///<recommended color to use for number genes
45//@}
46
47///Base class for genetic operations on genotypes of some genetic format
48/**\author Maciej Komosinski
49
50When designing genetic operations on some representation, inherit your class
51(for example Geno_fMy) from Geno_fx. Define some methods,
52like mutate(), in your class, to allow for evolution.
53Ensure they have the same names and arguments as the corresponding
54virtual methods in Geno_fx. Set the 'supported_format' variable to the
55appropriate genetic representation ID.
56Whenever arguments are genotypes, they are without
57trailing characters which describe genetic format
58(for example, "p:", not "//0\np:").
59When allocating/reallocating char* parameters, use malloc, free, realloc, strdup, etc.
60Do not use new and delete.
61
62All the methods you might define are:
63- checkValidity()
64- validate()
65- mutate()
66- crossOver()
67- getSimplest()
68- style()
69
70Your code must not cause errors (like invalid memory access, memory
71leaks) on any arguments, even 'random' ones. GENOPER_OPFAIL should
72be returned when an operator cannot cope with its argument genotype.
73
74To compile your code, you may also need some GDK files.
75A simple example is Geno_ftest class (see \ref geno_ftest_example "C++ code" for details).
76A more realistic example is Geno_f4 derived from Geno_fx: refer to
77the available source on developmental encoding and f4 genotype format.*/
78
79class Geno_fx
80{
81 public:
82   Param par;
83   char supported_format; ///<genotype format which is supported by this class ('6' for Geno_f6, etc.). Must be initialized in constructor
84   SString name; ///<name of this set of genetic operators
85   char **mutation_method_names; ///<array of names for mutation methods. If initialized (by new char*[]), must have entries for each method index returned by mutate(geno,chg,METHOD).  If initialized, it is automatically freed by this destructor.
86   Geno_fx() : par(empty_paramtab) {supported_format='x'; name="Default"; mutation_method_names=NULL; setDefaults();}
87
88/**Used to perform initializations of Param parameters that are not handled by the Param itself
89(i.e. string parameters need to be initialized here)*/
90   virtual void setDefaults() {}
91
92/**Checks a genotype for minor mistakes and major errors.
93\param geno genotype to be checked
94\retval error_position 1-based (or 1 if no exact error position known)
95\retval GENOPER_OK when the genotype is fully valid, and can be translated by the converter with \b no modifications nor tweaks*/
96   virtual int checkValidity(const char *geno) {return GENOPER_NOOPER;}
97
98/**Validates a genotype. The purpose of this function is to validate
99obvious/minor errors (range overruns, invalid links, etc.). Do not try
100to introduce entirely new genes in place of an error.
101\param geno input/output: genotype to be validated
102\retval GENOPER_OK must be returned in any case ("did my best to validate")*/
103   virtual int validate(char *&geno) {return GENOPER_NOOPER;}
104
105/**Mutates a genotype. Mutation should always change something.
106
107Avoid unnecessary calls in your code. Every genotype argument passed to this
108function is first checked, and validated if checkValidity() reported an error (or
109if there is no checkValidity() implemented). Every resulting genotype is subject
110to the same procedure, unless GENOPER_OPFAIL was returned. Thus you do not have
111to call these functions on input and output genotypes, because they are validated
112if needed.
113\param geno input/output: genotype to be mutated
114\param chg output: initialize with a value (in most cases 0..1) corresponding
115to the amount of genotype mutated. For example, it could be the number of changed
116genes divided by the total number of genes before mutation.
117\param chg method: initialize with the ID (number) of mutation method used.
118\retval GENOPER_OK
119\retval GENOPER_OPFAIL
120\sa
121Mutation example to illustrate the exchange of pointers for \e geno.
122The mutation adds random letter at the beginning or removes last letter from \e geno.
123\code
124{
125 int len=strlen(geno);
126 if (len==0 || random(2)==0) //add
127 {
128    method=0;
129    char* mutated=(char*)malloc(mutated,len+2); //allocate for mutated genotype
130    mutated[0]='A'+random(10); //first char random
131    strcpy(mutated+1,geno); //the rest is original
132    free(geno); //must take care of the original allocation
133    geno=mutated;
134 } else
135 {
136    method=1;
137    geno[len-1]=0; //simply shorten the string - remove last char
138 }
139 chg=1.0/max(len,1); //estimation of mutation strength, divby0-safe
140} \endcode
141*/
142   virtual int mutate(char *&geno,float& chg,int &method) {method=-1; chg=-1; return GENOPER_NOOPER;}
143
144/**Crosses over two genotypes. It is sufficient to return only one child (in \e g1) and set \e chg1 only, then \e g2 must equal "".
145
146Avoid unnecessary calls in your code. Every genotype argument passed to this
147function is first checked, and validated if checkValidity() reported an error (or
148if there is no checkValidity() implemented). Every resulting genotype is subject
149to the same procedure, unless GENOPER_OPFAIL was returned. Thus you do not have
150to call these functions on input and output genotypes, because they are validated
151if needed.
152\param g1 input/output: parent1 genotype, initialize with child1
153\param g2 input/output: parent2 genotype, initialize with child2 if both children are available
154\param chg1 output: initialize with the fraction of parent1 genes in child1 (parent2 has the rest)
155\param chg2 output: initialize with the fraction of parent2 genes in child2 (parent1 has the rest)
156\retval GENOPER_OK
157\retval GENOPER_OPFAIL
158\sa mutate() for an example*/
159   virtual int crossOver(char *&g1,char *&g2,float& chg1,float& chg2) {chg1=chg2=-1; return GENOPER_NOOPER;}
160
161/**\return a pointer to the simplest genotype string*/
162   virtual char* getSimplest() {return NULL;}
163
164/**You may want to have your genotype colored. This method provides desired character styles for genes.
165\param geno genotype
166\param pos 0-based char offset
167\retval number-encoded visual style (and validity) of the genotype char at \e geno[pos].
168Assume white background.
169\sa GENSTYLE_* macros, like GENSTYLE_BOLD*/
170   virtual unsigned long style(const char *geno,int pos) {return GENSTYLE_RGBS(0,0,0,GENSTYLE_NONE);}
171
172///currently not used (similarity of two genotypes)
173   virtual float similarity(const char*,const char*) {return GENOPER_NOOPER;}
174   virtual ~Geno_fx() {if (mutation_method_names) {delete []mutation_method_names; mutation_method_names=NULL;}}
175//   virtual char getFormat() {return 255;} //returns supported genotype format, for ex. '1'
176//   virtual int enabled() {return 1;} // should be enabled by default
177
178/** \name Some helpful methods for you */
179//@{
180         static int roulette(const double *probtab,const int count);    ///<returns random index according to probabilities in the \e probtab table or -1 if all probs are zero. \e count is the number of elements in \e probtab.
181   static bool getMinMaxDef(ParamInterface *p,int propindex,double &mn,double &mx,double &def); ///<perhaps a more useful (higher-level) way to obtain min/max/def info for integer and double properties. Returns true if min/max/def was really available (otherwise it is just invented).
182   static int selectRandomProperty(Neuro* n); ///<selects random property (either 0-based extraproperty of Neuro or 100-based property of its NeuroClass). -1 if Neuro has no properties.
183   static double mutateNeuProperty(double current,Neuro *n,int propindex); ///<returns value \e current mutated for the property \e propindex of NeuroClass \e nc or for extraproperty (\e propindex - 100) of Neuro. Neuro is used as read-only. Give \e propindex == -1 to mutate connection weight (\e nc is then ignored).
184   static bool mutatePropertyNaive(ParamInterface &p,int propindex); ///<creep-mutate selected property. Returns true when success. mutateProperty() should be used instead of this function.
185   static bool mutateProperty(ParamInterface &p,int propindex); ///<like mutatePropertyNaive(), but uses special probability distributions for some neuron properties.
186   static bool getMutatedProperty(ParamInterface &p,int i,double oldval,double &newval); ///<like mutateProperty(), but just returns \e newval, does not get nor set it using \e p.
187   static double mutateCreep(char type,double current,double mn,double mx); ///<returns \e current value creep-mutated with Gaussian distribution within [ \e mn , \e mx ] interval. Forced precision: 3 digits after comma. \e type must be either 'd' (integer) or 'f' (float/double).
188   static NeuroClass* getRandomNeuroClass(); ///<returns random neuroclass or NULL when no active classes.
189   static NeuroClass* parseNeuroClass(char *&s); ///<returns neuroclass or NULL if the string does not begin with a valid name. Advance \e s pointer.
190   static Neuro* findNeuro(const Model *m,const NeuroClass *nc); ///<returns pointer to first Neuro of class \e nc, or NULL if there is no such Neuro.
191   static int neuroClassProp(char *&s,NeuroClass *nc,bool also_v1_N_props=false); ///<returns 0-based property number for \e neuroclass, 100-based extraproperty number for Neuro, or -1 if the string does not begin with a valid property name. Advance \e s pointer if success.
192   static bool isWS(const char c); ///<is \e c a whitespace char?
193   static void skipWS(char *&s); ///<advances pointer \e s skipping whitespaces.
194   static bool areAlike(char*,char*); ///<compares two text strings skipping whitespaces. Returns 1 when equal, 0 when different.
195         static char* strchrn0(const char *str,char ch); ///<like strchr, but does not find zero char in \e str.
196   static bool isNeuroClassName(const char firstchar); ///<determines if \e firstchar may start NeuroClass name. If not, it may start NeuroClass' (or Neuro's) property name.
197//@}
198};
199
200
201//
202// custom distributions for mutations of various parameters
203//
204/*
205static double distrib_weight[]=
206{
2075,                 // distribution -999 _-^_^-_ +999
208-999, 999,         // each weight value may be useful, especially...
209  -5, -0.3,        // ...little non-zero values
210  -3, -0.6,
211 0.6, 3,
212 0.3, 5,
213};*/
214static double distrib_force[]=   // for '!'
215{
2163,                 // distribution 0 -__/ +1
2170.001, 0.2,        // "slow" neurons
2180.001, 1,
219    1, 1,          // "fast" neurons
220};
221static double distrib_inertia[]=  // for '='
222{
2232,                 // distribution 0 |..- +1
224  0, 0,            // "fast" neurons
2250.7, 0.98,
226};
227static double distrib_sigmo[]=  // for '/'
228{
2295,                 // distribution -999 -..-^-..- +999
230-999, -999,        //"perceptron"
231 999, 999,
232  -5, -1,          // nonlinear
233   1, 5,
234  -1, 1,           // ~linear
235};
236
237
238#endif
239
Note: See TracBrowser for help on using the repository browser.