dLife Home Page

dlife.ga
Class Individual

java.lang.Object
  extended by dlife.sys.SerializationBase
      extended by dlife.ga.Individual
All Implemented Interfaces:
Serializable, Cloneable, Comparable<Individual>, Iterable<Gene>

public class Individual
extends SerializationBase
implements Comparable<Individual>, Cloneable, Iterable<Gene>

This class represents an individual in an evolving population. An Individual is essentially a collection of Genes with a fitness value. This class implements Comparable to make the Individuals orderable by their fitness values.

Version:
Jan 6, 2007
Author:
Grant Braught, Dickinson College
See Also:
Serialized Form

Constructor Summary
Individual()
          Construct a new empty Individual with 0 fitness.
 
Method Summary
 void addGene(Gene g)
          Add a Gene to the genotype of this Individual.
 int compareTo(Individual ind)
          Compare the fitness of this Individual to the provided Individual.
 Individual copy()
          Produce a new Individual that is an exact copy of this Individual.
 boolean equals(Object obj)
          Compare two Individuals and return true if they have exactly the same Genes.
 double getFitness()
          Get the fitness of this Individual.
 Gene getGene(int index)
          Get the specified Gene from this Individual.
 ArrayList<Gene> getGenes()
          Get all of the Genes from this Individual as an ArrayList.
 int getSize()
          Get the number of Gene's contained in this Individual.
 Iterator<Gene> iterator()
          Get an Iterator for the Gene's in this Individual
 Individual randomCopy()
          Produce a new Individual that has the same type and number of Genes as this Individual but the value of the Genes are randomized.
 void setFitness(double fitness)
          Set the fitness value of this Individual.
 String toString()
          Get a String representation of this Individual.
 
Methods inherited from class dlife.sys.SerializationBase
read, write
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Individual

public Individual()
Construct a new empty Individual with 0 fitness.

Method Detail

addGene

public void addGene(Gene g)
Add a Gene to the genotype of this Individual.

Parameters:
g - the Gene to be added.

getGene

public Gene getGene(int index)
Get the specified Gene from this Individual. If there is no such Gene then null is returned.

Parameters:
index - the index of the Gene to get.
Returns:
the specified Gene or null.

getGenes

public ArrayList<Gene> getGenes()
Get all of the Genes from this Individual as an ArrayList.

Returns:
an ArrayList of the Genes in this Individual.

getSize

public int getSize()
Get the number of Gene's contained in this Individual.

Returns:
the number of Gene's in this Individual.

toString

public String toString()
Get a String representation of this Individual. This representation is essentially a concatenation of the String representation of each of the Gene's in the Indivdual followed by a space and then the fitness of the individual enclosed in parenthesis. For example for a BitGene with 5 bits the representation might be: 10011 (2.234)

Overrides:
toString in class Object
Returns:
a String describing this Individual.

compareTo

public int compareTo(Individual ind)
Compare the fitness of this Individual to the provided Individual. Return -1 if this Individual's fitness is less than that of obj, 0 if the fitnesses are equal and 1 if this Individual's fitness is greater than that of obj.

Specified by:
compareTo in interface Comparable<Individual>
Parameters:
ind - an Individual to be compared to this one.
Returns:
1, 0 or -1.

equals

public boolean equals(Object obj)
Compare two Individuals and return true if they have exactly the same Genes. Note that the fitness value is not considered when comparing two Individuals for equality.

Overrides:
equals in class Object
Parameters:
obj - the Individual to compare to this one.
Returns:
true if the Individuals contain the same Genes and false otherwise.

setFitness

public void setFitness(double fitness)
Set the fitness value of this Individual.

Parameters:
fitness - the new fitness for this Individual.

getFitness

public double getFitness()
Get the fitness of this Individual.

Returns:
this Individual's fitness.

copy

public Individual copy()
Produce a new Individual that is an exact copy of this Individual. All of the Genes will be identical and the fitness value will be the same. Each of the Genes in the new Individual are created by making invoking copy() on the corresponding Gene in the copied Individual. Thus, if the Gene's copy() method produces a deep copy of the Gene then this method produces a deep copy of the Individual.

Returns:
a copy of this Individual.

randomCopy

public Individual randomCopy()
Produce a new Individual that has the same type and number of Genes as this Individual but the value of the Genes are randomized. The fitness of the new Individual will be 0. Each of the Genes in the new Individual are created by invoking copy() on the corresponding Gene in the copied Individual and then invoking randomize() on the copy.

Returns:
a new random copy of this Individual.

iterator

public Iterator<Gene> iterator()
Get an Iterator for the Gene's in this Individual

Specified by:
iterator in interface Iterable<Gene>
Returns:
an Iterator over this Individual's Genes.

dLife Home Page