# PRINCIPLEGENETICSGENERATION

## PRINCIPLE

Objects of anyflo type genetic are used to define populations managed by genetic algorithms.
Each gene is composed of individuals, called particles, constructed from a certain alphabet.
Each gene may receive an evaluation function.. d´évaluation.
A new population can be generated by selection, crossover and mutation (favoring individuals of maximum assessment).

## GENETICS

### Genetic type object

`genetic(id);`
Defines the genetic id.id.
Examples:
`genetic(1);`
Builds genetic number 1.
`genetic "G");`
Builds the genetic name G.

### Alphabet

Individuals of a population are constructed from an alphabet, which by default is reduced to two characters 0 and 1.
`alphabet genetic(id)="ABC"`
Defines the alphabet ABC on the genetic id.

### Individuals

An initial population can be defined randomly:
Example:
`generate alea particle(100,12)genetic(1);`
Randomly generate 100 individuals size 12 from the alphabet of genetic 1.
Or individually:
`particle(0) genetic(1)="00011";`
Defines a new individual.
`particle(p) genetic(id)="10110";`
Redefines individual p of genetic id.
All individuals of the same genetic must be character strings of the same size:

### Fitness function

Population evolves by:
1) Selection of individuals for reproduction. They are chosen optimal in that they maximize a certain evaluation function.
2) Crossing two such individuals by randomly cutting and constructing a new individual by taking the first part of the 1st and 2nd part of the 2nd.
3) Mutation (low probability) arbitrarily changing one or more elements.

The evaluation can be done in different ways:
1) Written anyflo language anyflo.:
`func genetic(id)="f";`
This function must be of the form:
``` f(g,num,p)          {          x=normalized avaluation of particue p (between 0.0 and 1.0)          return(x);          } ```
g = number of the evluated genetic.
num = number of the evaluated individual.
p = value of this individual.

2) Written C language function:
`func genetic(id)=num;`
num is the number passed to function `func_genetic_utilisateur` (of utilb.c), it will change, then it will link anyflo.

`validate genetic(id);`
Runs genetic id, ie computes the returnss of the evaluation function for each individual of population id.
`scale particle genetic(g);`
Returns these values.

Cross overs are stored in `cut genetic` in the form:
ind,p1,p2 with ind=index of cut, p1 et p2 = number of the particles crossed.
cut(n) genetic(id)
Returns ind,p1,p2 for individual n. (This command is a purely didactic).
`c=cut(1)genetic(1);`
Returns the first element of cut.

### Eddition

`edit genetic(id);`
Edits genetic id.
`edit particle genetic(id);`
Edits individuals of genetic id.

### Removing

`rem genetic(id);`
Removes genetic id.
`ini genetic;`
Deletes all genetics.

## GENERATION

### Regeneration of a population

`generate genetic(id);`
Regenerates the genetic id by selection, crossover and mutation of its individuals, favoring individuals of maximum evaluation.
`generate(n) genetic(id);`
Regenerates n times the genetic id..
Population improves as we regenerate it.
the evaluation values of individuals are stored in:
`scale particle genetic;`
Examples:
`edit scale particle genetic(1);`
Edits the evaluations.
`\$scale particle(1) genetic(1);`
Prints the evaluation of individual 1.
`scale particle(1) genetic(1)=0.1;`
Changes the evaluation of individual 1.

### Validation

`validate genetic(id);`
Recomputes the evaluations of individuals of genetic id.
`validate genetic;`
Recomputes the evaluations of individuals of all genetics.
`validate particle(n) genetic(id);`
Recomputes the evaluations of individual n of genetic id.
For a simple example showing the resolution of a problem by a genetic algorithm see demo1_genetic.func: