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 calculateCost()
          Called to calculate the cost for this chromosome.
 int compareTo(Chromosome<GENE_TYPE> other)
          Used to compare two chromosomes.
 double getCost()
           
 GENE_TYPE getGene(int gene)
          Get the specified gene.
 GENE_TYPE[] getGenes()
          Used the get the entire gene array.
 GeneticAlgorithm<GENE_TYPE> getGeneticAlgorithm()
           
 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 setCost(double cost)
          Set the cost for 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.
 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

calculateCost

public abstract void calculateCost()
Called to calculate the cost for this chromosome.


compareTo

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

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 cost to this chromosome; a value less than 0 if the argument is a chromosome with a cost greater than this chromosome; and a value greater than 0 if the argument is a chromosome what a cost less than this chromosome.

getCost

public double getCost()
Returns:
the cost

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.


setCost

public void setCost(double cost)
Set the cost for this chromosome.

Parameters:
cost - the cost 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