The Encog Project

org.encog.solve.genetic
Class Chromosome<GENE_TYPE>

java.lang.Object
  extended by org.encog.solve.genetic.Chromosome<GENE_TYPE>
Type Parameters:
GENE_TYPE - The datatype for a gene.
All Implemented Interfaces:
java.lang.Comparable<Chromosome<GENE_TYPE>>
Direct Known Subclasses:
NeuralChromosome

public abstract class Chromosome<GENE_TYPE>
extends java.lang.Object
implements java.lang.Comparable<Chromosome<GENE_TYPE>>

Implements a chromosome to genetic algorithm. This is an abstract class. Other classes are provided in this book that use this base class to train neural networks or provide an answer to the traveling salesman problem. Lifeforms in this genetic algorithm consist of one single chromosome each. Therefore, this class represents a virtual lifeform. The chromosome is a string of objects that represent one solution. For a neural network, this string of objects usually represents the weight and threshold matrix. Chromosomes are made up of genes. These are of the generic type GENE_TYPE. For a neural network this type would most likely be double values.


Constructor Summary
Chromosome()
           
 
Method Summary
abstract  void calculateScore()
          Called to calculate the score for this chromosome.
 int compareTo(Chromosome<GENE_TYPE> other)
          Used to compare two chromosomes.
 GENE_TYPE getGene(int gene)
          Get the specified gene.
 GENE_TYPE[] getGenes()
          Used the get the entire gene array.
 GeneticAlgorithm<GENE_TYPE> getGeneticAlgorithm()
           
 double getScore()
           
 void mate(Chromosome<GENE_TYPE> father, Chromosome<GENE_TYPE> offspring1, Chromosome<GENE_TYPE> offspring2)
          Assuming this chromosome is the "mother" mate with the passed in "father".
abstract  void mutate()
          Called to mutate this chromosome.
 void setGene(int gene, GENE_TYPE value)
          Set the specified gene's value.
 void setGenes(GENE_TYPE[] genes)
          Set the entire gene array.
 void setGenesDirect(GENE_TYPE[] genes)
          Set the genes directly, not allowed to be overridden.
 void setGeneticAlgorithm(GeneticAlgorithm<GENE_TYPE> geneticAlgorithm)
          Set the genetic algorithm.
 void setScore(double score)
          Set the score for this chromosome.
 java.lang.String toString()
          Convert the chromosome to a string.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Chromosome

public Chromosome()
Method Detail

calculateScore

public abstract void calculateScore()
Called to calculate the score for this chromosome.


compareTo

public int compareTo(Chromosome<GENE_TYPE> other)
Used to compare two chromosomes. Used to sort by score.

Specified by:
compareTo in interface java.lang.Comparable<Chromosome<GENE_TYPE>>
Parameters:
other - The other chromosome to compare.
Returns:
The value 0 if the argument is a chromosome that has an equal score to this chromosome; a value less than 0 if the argument is a chromosome with a score greater than this chromosome; and a value greater than 0 if the argument is a chromosome what a score less than this chromosome.

getScore

public double getScore()
Returns:
the score

getGene

public GENE_TYPE getGene(int gene)
Get the specified gene.

Parameters:
gene - The specified gene.
Returns:
The gene specified.

getGenes

public GENE_TYPE[] getGenes()
Used the get the entire gene array.

Returns:
the genes

getGeneticAlgorithm

public GeneticAlgorithm<GENE_TYPE> getGeneticAlgorithm()
Returns:
the geneticAlgorithm

mate

public void mate(Chromosome<GENE_TYPE> father,
                 Chromosome<GENE_TYPE> offspring1,
                 Chromosome<GENE_TYPE> offspring2)
Assuming this chromosome is the "mother" mate with the passed in "father".

Parameters:
father - The father.
offspring1 - Returns the first offspring
offspring2 - Returns the second offspring.

mutate

public abstract void mutate()
Called to mutate this chromosome.


setScore

public void setScore(double score)
Set the score for this chromosome.

Parameters:
score - the score to set

setGene

public void setGene(int gene,
                    GENE_TYPE value)
Set the specified gene's value.

Parameters:
gene - The specified gene.
value - The value to set the specified gene to.

setGenes

public void setGenes(GENE_TYPE[] genes)
Set the entire gene array.

Parameters:
genes - the genes to set

setGenesDirect

public final void setGenesDirect(GENE_TYPE[] genes)
Set the genes directly, not allowed to be overridden.

Parameters:
genes - the genes to set

setGeneticAlgorithm

public void setGeneticAlgorithm(GeneticAlgorithm<GENE_TYPE> geneticAlgorithm)
Set the genetic algorithm.

Parameters:
geneticAlgorithm - the geneticAlgorithm to set

toString

public java.lang.String toString()
Convert the chromosome to a string.

Overrides:
toString in class java.lang.Object
Returns:
The chromosome as a string.

The Encog Project