Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

Geno_fx Class Reference

Base class for genetic operations on genotypes of some genetic format. More...

Inheritance diagram for Geno_fx:

Geno_ftest List of all members.

Public Methods

 Geno_fx ()
virtual int checkValidity (const char *geno)
virtual int validate (char *&geno)
virtual int mutate (char *&geno, float &chg)
virtual int crossOver (char *&g1, char *&g2, float &chg1, float &chg2)
virtual char * getSimplest ()
virtual unsigned long style (const char *geno, int pos)
virtual float similarity (const char *, const char *)
 currently not used (similarity of two genotypes)

virtual ~Geno_fx ()

Static Public Methods

Some helpful methods for you
int roulette (const double *probtab, int count)
 returns random index according to probabilities in the probtab table or -1 if all probs are zero. count is the number of elements in probtab.

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).

int selectRandomProperty (Neuro *n)
 selects random property (either 0-based extraproperty of Neuro or 100-based property of its NeuroClass).

double mutateNeuProperty (double current, Neuro *n, int propindex)
 returns value current mutated for the property propindex of NeuroClass nc or for extraproperty (propindex - 100) of Neuro. Give propindex == -1 to mutate connection weight (nc is then ignored).

bool mutateProperty (ParamInterface &p, int propindex)
 mutate selected property. Returns true when success.

double mutateValue (double current, double vmin, double vmax)
 returns current value "creep"-mutated with Gaussian distribution within [ vmin , vmax ] interval. 3 digits after comma.

NeuroClass * getRandomNeuroClass ()
 returns random neuroclass or NULL when no active classes.

NeuroClass * parseNeuroClass (char *&s)
 returns neuroclass or NULL if the string does not begin with a valid name. Advance s pointer.

int neuroClassProp (char *&s, NeuroClass *nc, bool also_v1_N_props=false)
 returns 0-based property number for neuroclass, 100-based extraproperty number for Neuro, or -1 if the string does not begin with a valid property name. Advance s pointer if success.

bool isWS (const char c)
 is c a whitespace char?

void skipWS (char *&s)
 advances pointer s skipping whitespaces.

bool areAlike (char *, char *)
 compares two text strings skipping whitespaces. Returns 1 when equal, 0 when different.

char * strchrn0 (const char *str, char ch)
 like strchr, but does not find zero char in str.

bool isNeuroClassName (const char firstchar)
 determines if firstchar may start NeuroClass name. If not, it may start NeuroClass' (or Neuro's) property name.


Public Attributes

Param par
char supported_format
 genotype format which is supported by this class ('6' for Geno_f6, etc.). Must be initialized in constructor

SString name
 name of this set of genetic operators


Detailed Description

Base class for genetic operations on genotypes of some genetic format.

Author:
Maciej Komosinski
When designing your genetic representation, inherit your class (for example Geno_fMy) from Geno_fx. Define some methods, like mutate() and validite(), in your class, to allow for evolution. Ensure they have the same names and arguments as the corresponding virtual methods in Geno_fx. Whenever arguments are genotypes, they are without trailing characters which describe genetic format (for example, "p:", not "//0\np:"). When allocating/reallocating char* parameters, use malloc, free, realloc, strdup, etc. Do not use new and delete.

All the methods you might define are:

Your code must not cause errors (like invalid memory access, memory leaks) on any arguments, even 'random' ones. GENOPER_OPFAIL should be returned when an operator cannot cope with its argument genotype.

To compile your code, you may also need some GDK files. A simple example is Geno_ftest class (see C++ code for details). A more realistic example is Geno_f4 derived from Geno_fx: refer to the available source on developmental encoding and f4 genotype format.


Member Function Documentation

virtual int Geno_fx::checkValidity const char *    geno [virtual]
 

Checks a genotype for minor mistakes and major errors.

Parameters:
geno  genotype to be checked
Return values:
error_position  1-based (or 1 if no exact error position known)
GENOPER_OK  when the genotype is fully valid, and can be translated by the converter with no modifications nor tweaks

Reimplemented in Geno_ftest.

virtual int Geno_fx::crossOver char *&    g1,
char *&    g2,
float &    chg1,
float &    chg2
[virtual]
 

Crosses over two genotypes. It is sufficient to return only one child (in g1) and set chg1 only, then g2 must equal "".

Avoid unnecessary calls in your code. Every genotype argument passed to this function is first checked, and validated if checkValidity() reported an error (or if there is no checkValidity() implemented). Every resulting genotype is subject to the same procedure, unless GENOPER_OPFAIL was returned. Thus you do not have to call these functions on input and output genotypes, because they are validated if needed.

Parameters:
g1  input/output: parent1 genotype, initialize with child1
g2  input/output: parent2 genotype, initialize with child2 if both children are available
chg1  output: initialize with the fraction of parent1 genes in child1 (parent2 has the rest)
chg2  output: initialize with the fraction of parent2 genes in child2 (parent1 has the rest)
Return values:
GENOPER_OK 
GENOPER_OPFAIL 
See also:
mutate() for an example

Reimplemented in Geno_ftest.

virtual char* Geno_fx::getSimplest   [virtual]
 

Returns :
a pointer to the simplest genotype string

virtual int Geno_fx::mutate char *&    geno,
float &    chg
[virtual]
 

Mutates a genotype. Mutation should always change something.

Avoid unnecessary calls in your code. Every genotype argument passed to this function is first checked, and validated if checkValidity() reported an error (or if there is no checkValidity() implemented). Every resulting genotype is subject to the same procedure, unless GENOPER_OPFAIL was returned. Thus you do not have to call these functions on input and output genotypes, because they are validated if needed.

Parameters:
geno  input/output: genotype to be mutated
chg  output: initialize with a value (in most cases 0..1) corresponding to the amount of genotype mutated. For example, it could be the number of changed genes divided by the total number of genes before mutation.
Return values:
GENOPER_OK 
GENOPER_OPFAIL 
See also:
Mutation example to illustrate the exchange of pointers for geno. The mutation adds random letter at the beginning or removes last letter from geno.
{
 int len=strlen(geno);
 if (len==0 || random(2)==0) //add
 {
    char* mutated=(char*)malloc(mutated,len+2); //allocate for mutated genotype
    mutated[0]='A'+random(10); //first char random
    strcpy(mutated+1,geno); //the rest is original
    free(geno); //must take care of the original allocation
    geno=mutated;
 } else geno[len-1]=0; //simply shorten the string - remove last char
 chg=1.0/max(len,1); //estimation of mutation strength, divby0-safe
} 

Reimplemented in Geno_ftest.

virtual unsigned long Geno_fx::style const char *    geno,
int    pos
[virtual]
 

You may want to have your genotype colored. This method provides desired character styles for genes.

Parameters:
geno  genotype
pos  0-based char offset
Return values:
number-encoded  visual style (and validity) of the genotype char at geno[pos]. Assume white background.
See also:
GENSTYLE_* macros, like GENSTYLE_BOLD

Reimplemented in Geno_ftest.

virtual int Geno_fx::validate char *&    geno [virtual]
 

Validates a genotype. The purpose of this function is to validate obvious/minor errors (range overruns, invalid links, etc.). Do not try to introduce entirely new genes in place of an error.

Parameters:
geno  input/output: genotype to be validated
Return values:
GENOPER_OK  must be returned in any case ("did my best to validate")

Reimplemented in Geno_ftest.


Generated on Sun Sep 15 00:58:40 2002 for Framsticks GenoFX by doxygen1.2.17