This is a simple implementation of a genetic algorithm in PHP. It can either 
be run as a webapplication or as a commandline program.

A genetic algorithm (GA) solves problems by mimicking the time-honoured
system that mother Nature uses to solve problems: sexual
procreation. Essentially this is the recombination of the genetic
material of the parents into new combinations.

We will use the following definitions:

individu   - member of population to be compared by fitness
chromosome - string or array with properties of an individu
gen        - or allele, a specific property in the chromosome
fitness    - the fitness of a given chromosome
target     - an ideal chromosome that is used to decide the fitness of an individu.


iterations - the maximum number of iterations for one initial population
             If the maximum number of iterations is reached before optimum,
             it counts as failure and the program is stopped.
run        - a single run of the program for a given initial population
             until success or the max. number of iterations is reached.
trials     - a number of runs for one initial population.

GA's generally have the following functionality:

* a fitness function. This function returns a 'fitness' of a given
  individu. In our implementation fitness is defined as the number of
  gens that are identical (within tolerances) to the gen of a target 

* a crossover function. Crossover consists of splitting the
  chromosomes of two parents and recombining the parts into two new
  individuals. The place of the split is the same for both parents,
  but choosen at random.

* a mutation function. Ever so often, a gen is changed at random. This
  is necessary to escape local maxima.

Our program implements the above in PHP. We added the following features:

1. a symbol for the exact processing of the gen in the fitness
function. This is in the format of a vector of symbols 
   * apart from '=' we also implement the operators <,>,<=,>= and <> 
   * an 'x' means 'ignore this gen' 
   * a percentage means the tolerance (10% means if the gen is within 10% 
     of the target, it will be a fit)

2 weighting of individual genes. This makes it possible to have some
control over the relative importance of the individual gen.  This also
is in the format of a vector.

Data are read from a csv-like file, but comments (a line starting with
'#') are possible. Also the parameters iterations and mutation can be

The results are appended to a logfile with the same name as the
datafile, but with the .log suffix. It is created when necessary.
It can be run from the commandline as

$ php paai_GA.php filename.csv

or from a browser as


--------------------- example --------------------
comment This is a comment in the datafile that is displayed 
target 3,3,3,3,3 
iterations 5000 
mutation 30 
trials 10 
weights 1,1,1,1,0.5
actions =,=,=,=,= 
# actions <,>,<=,>=,<> 
# actions can be the operators =, <>, <, >, <=, >=. If the comparison 
# between individual and target is true, the fitness of the individual 
# is increased.  
individu 3,3,8,2,6 
individu 2,8,2,5,2 
individu 4,5,9,3,9 
individu 2,6,6,3,1 
individu 5,6,8,1,1 ...


Read the source here.