org.generation5.demos
Class EvoDiophantine

java.lang.Object
  extended byorg.generation5.bio.EvolveStandard
      extended byorg.generation5.demos.EvoDiophantine
All Implemented Interfaces:
java.lang.Comparable, Evolvable

public final class EvoDiophantine
extends EvolveStandard

EvoDiophantine is an example of a genetic algorithm solving a diophantine equation. A diophantine equation is an integer-only equation of the form ax+by+cz...=t. The class solves for the variables given the coefficients a, b, c etc. and target t.


Field Summary
 
Fields inherited from class org.generation5.bio.EvolveStandard
fitnessValue, minimizeFitness, random
 
Constructor Summary
EvoDiophantine()
          Creates a new instance of EvoDiophantine
 
Method Summary
 void calculateFitness()
          Diophantine fitness is calculated as the error between the target value and the calculated value.
 double getFitness()
          Return the fitness of this object.
static void main(java.lang.String[] args)
          Run the genetic algorithm on the specified coefficients and target values.
 Evolvable mate(Evolvable partner)
          One-point crossover of the equation values.
 void mutate()
          Mutates one of the equation values by a random value between -2 and 2.
 void randomInitialize()
          Initialize the equation values to a number within the specified range.
static void setCoefficients(int[] coeff, int target)
          Set the coefficients and target of the equation.
 void setRange(int min, int max)
          Set the range the genetic algorithm checks between.
 java.lang.String toString()
           
 
Methods inherited from class org.generation5.bio.EvolveStandard
compareTo
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

EvoDiophantine

public EvoDiophantine()
Creates a new instance of EvoDiophantine

Method Detail

calculateFitness

public void calculateFitness()
Diophantine fitness is calculated as the error between the target value and the calculated value.

Specified by:
calculateFitness in interface Evolvable
Specified by:
calculateFitness in class EvolveStandard

getFitness

public double getFitness()
Return the fitness of this object.

Specified by:
getFitness in interface Evolvable
Overrides:
getFitness in class EvolveStandard
Returns:
the fitness value of this instance.

mate

public Evolvable mate(Evolvable partner)
One-point crossover of the equation values.

Specified by:
mate in interface Evolvable
Specified by:
mate in class EvolveStandard
Parameters:
partner - the other parent equation.
Returns:
the new child equation.

mutate

public void mutate()
Mutates one of the equation values by a random value between -2 and 2.

Specified by:
mutate in interface Evolvable
Specified by:
mutate in class EvolveStandard

randomInitialize

public void randomInitialize()
Initialize the equation values to a number within the specified range.

Specified by:
randomInitialize in interface Evolvable
Specified by:
randomInitialize in class EvolveStandard
See Also:
setRange(int, int)

setRange

public void setRange(int min,
                     int max)
Set the range the genetic algorithm checks between.

Parameters:
min - minimum value.
max - maximum value.

toString

public java.lang.String toString()
Returns:
a string with the equation and current fitness.

setCoefficients

public static void setCoefficients(int[] coeff,
                                   int target)
Set the coefficients and target of the equation. As a static member function, this specifies the values for all members of the population.

Parameters:
coeff - the coefficients.
target - target value.

main

public static void main(java.lang.String[] args)
Run the genetic algorithm on the specified coefficients and target values. If nothing is specified, a+2b+3c+4d=50 is attempted. The GA runs for a maximum of 1000 iterations or until fitness reaches zero.

Parameters:
args - coefficients and target value to run the GA on.

This documentation is part of the Generation5 JDK.