jenes.chromosome
Class ObjectChromosome

java.lang.Object
  extended by jenes.chromosome.ObjectChromosome
All Implemented Interfaces:
java.lang.Cloneable, Chromosome<ObjectChromosome>

public final class ObjectChromosome
extends java.lang.Object
implements Chromosome<ObjectChromosome>

An ObjectChromosome is made of objects. An AlleleSet is the ObjectChromosome.Gene allele values alphabet. One or more allele sets are required to instantiate an ObjectChromosome. All of genes can have the same allele set or a different one according to ObjectChromosome constructor invoked. An example of code is provided below.

 ObjectChromosome chrom = new ObjectChromosome(
                new GenericAlleleSet(1, 2, 3),
                new GenericAlleleSet(true, false),
                new AnyDoubleGenericAlleleSetSubtype.createRandom(10, 0, 1)  );
 

We instantiated an ObjectChromosome with three genes: the first one with Integer allele values 1, 2 and 3; the second one with Boolean allele values and the last one with Double values randomly distributed in the range [0,1]. GenericAlleleSet is a concrete subclass of AlleleSet interface; AnyDoubleGenericAlleleSetSubtype is a subclass of GenericAlleleSet providing a static method to create randomly double values within a specified range.

Since:
1.0
Version:
1.2
Author:
Luigi Troiano, Pierpaolo Lombardi, Giuseppe Pascale, Thierry Bodhuin
See Also:
AlleleSet, GenericAlleleSet

Nested Class Summary
static class ObjectChromosome.Gene
          An ObjectChromosome's gene.
 
Field Summary
protected  java.lang.Object defaultValue
          The default value for this chromosome.
protected  ObjectChromosome.Gene[] genes
          The chromosome data structure
 
Constructor Summary
ObjectChromosome(AlleleSet... sets)
          Creates a new ObjectChromosome with one gene for each AlleleSet within the specified array.
ObjectChromosome(AlleleSet set, int size)
          Creates a new ObjectChromosome with the specified length and with the same AlleleSet for each Gene.
ObjectChromosome(java.util.List<AlleleSet> list)
          Creates a new ObjectChromosome with one gene for each AlleleSet within the specified list.
ObjectChromosome(ObjectChromosome chromosome)
          Creates a new ObjectChromosome; its genes will be equal to the specified ObjectChromosome's ones.
 
Method Summary
 ObjectChromosome clone()
          Performs a chromosome deep-cloning.
 void cross(ObjectChromosome chromosome, int from)
          Exchanges the genes chromosome starting from a given position
 void cross(ObjectChromosome chromosome, int from, int to)
          Exchanges the genes chromosome within the range [from,to]
 boolean equals(ObjectChromosome chromosome)
          Compares the chromosome with another.
 java.lang.Object getDeafultValue()
          Returns the default value of this chromosome
 ObjectChromosome.Gene getGene(int index)
          Returns the gene at the specified position
 java.lang.Object getValue(int pos)
          Returns the object allele value at the specified position
 java.lang.Object[] getValues()
          Returns the Object values
 java.lang.Object[] getValues(java.lang.Object[] values)
          gets the array of double values by filling the double values passed as parameters
 void leftShift(int from, int to)
          Makes a left-shift of the genes between two positions.
 int length()
          Returns the number of genes contained in the chromosome.
 void randomize()
          Fills the chromosome with random value.
 void randomize(int pos)
          Sets a random value in the specified position
 void rightShift(int from, int to)
          Makes a right-shift of the genes between two positions.
 void setAs(ObjectChromosome chromosome)
          Makes the chromosome a copy of another chromosome.
 void setDefaultValue(java.lang.Object defaultValue)
          Sets the default value of this chromosome.
 void setDefaultValueAt(int pos)
          Sets the default value at a given position
 void setGene(int pos, java.lang.Object value)
          Sets the specified value at the specified position
 void swap(int pos1, int pos2)
          Exchanges two genes at the specified positions
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

genes

protected ObjectChromosome.Gene[] genes
The chromosome data structure


defaultValue

protected java.lang.Object defaultValue
The default value for this chromosome. The default value must be valid for all genes, therefore it should be be null or belonging to every AlleleSet.

Constructor Detail

ObjectChromosome

public ObjectChromosome(AlleleSet set,
                        int size)
Creates a new ObjectChromosome with the specified length and with the same AlleleSet for each Gene.

Parameters:
size - the chromosome length
set - the genes' AlleleSet

ObjectChromosome

public ObjectChromosome(ObjectChromosome chromosome)
Creates a new ObjectChromosome; its genes will be equal to the specified ObjectChromosome's ones.

Parameters:
chromosome - the source ObjectChromosome

ObjectChromosome

public ObjectChromosome(AlleleSet... sets)
Creates a new ObjectChromosome with one gene for each AlleleSet within the specified array. The chromosome genes will contain random allele values.

Parameters:
sets - the alleleset array

ObjectChromosome

public ObjectChromosome(java.util.List<AlleleSet> list)
Creates a new ObjectChromosome with one gene for each AlleleSet within the specified list. The chromosome genes will contain random allele values.

Parameters:
list - the java.util.List with all of AlleleSet
Method Detail

setDefaultValueAt

public final void setDefaultValueAt(int pos)
Description copied from interface: Chromosome
Sets the default value at a given position

Specified by:
setDefaultValueAt in interface Chromosome<ObjectChromosome>
Parameters:
pos - the position where to set the default value

clone

public final ObjectChromosome clone()
Description copied from interface: Chromosome
Performs a chromosome deep-cloning.

Specified by:
clone in interface Chromosome<ObjectChromosome>
Overrides:
clone in class java.lang.Object
Returns:
a chromosome clone.

randomize

public final void randomize()
Description copied from interface: Chromosome
Fills the chromosome with random value.

Specified by:
randomize in interface Chromosome<ObjectChromosome>

randomize

public final void randomize(int pos)
Description copied from interface: Chromosome
Sets a random value in the specified position

Specified by:
randomize in interface Chromosome<ObjectChromosome>
Parameters:
pos - a position where to set the random value

swap

public final void swap(int pos1,
                       int pos2)
Description copied from interface: Chromosome
Exchanges two genes at the specified positions

Specified by:
swap in interface Chromosome<ObjectChromosome>
Parameters:
pos1 - the position of first gene
pos2 - the position of second gene

leftShift

public final void leftShift(int from,
                            int to)
Description copied from interface: Chromosome
Makes a left-shift of the genes between two positions. The shift is circular, so the most left-side gene becomes the last gene on right.

Specified by:
leftShift in interface Chromosome<ObjectChromosome>
Parameters:
from - the start position to be shifted
to - the final position to be shifted

rightShift

public final void rightShift(int from,
                             int to)
Description copied from interface: Chromosome
Makes a right-shift of the genes between two positions. The shift is circular, so the most right-side gene becomes the first gene on left.

Specified by:
rightShift in interface Chromosome<ObjectChromosome>
Parameters:
from - the start position to be shifted
to - the final position to be shifted

length

public final int length()
Description copied from interface: Chromosome
Returns the number of genes contained in the chromosome.

Specified by:
length in interface Chromosome<ObjectChromosome>
Returns:
the length of chromosome.

setAs

public final void setAs(ObjectChromosome chromosome)
Description copied from interface: Chromosome
Makes the chromosome a copy of another chromosome.

Specified by:
setAs in interface Chromosome<ObjectChromosome>
Parameters:
chromosome - the chromosome to be copied

cross

public final void cross(ObjectChromosome chromosome,
                        int from)
Description copied from interface: Chromosome
Exchanges the genes chromosome starting from a given position

Specified by:
cross in interface Chromosome<ObjectChromosome>
Parameters:
chromosome - the chromosome to cross with
from - the start position of the genes to exchange

cross

public final void cross(ObjectChromosome chromosome,
                        int from,
                        int to)
Description copied from interface: Chromosome
Exchanges the genes chromosome within the range [from,to]

Specified by:
cross in interface Chromosome<ObjectChromosome>
Parameters:
chromosome - the chromosome to cross with
from - the start position of the genes to exchange
to - the final position of the genes to exchange

getValue

public final java.lang.Object getValue(int pos)
Returns the object allele value at the specified position

Parameters:
pos - a position into this population
Returns:
the object allele value at the specified position

getValues

public final java.lang.Object[] getValues()
Returns the Object values

Returns:
the Object values

getValues

public final java.lang.Object[] getValues(java.lang.Object[] values)
gets the array of double values by filling the double values passed as parameters

Parameters:
values -
Returns:
the array of chromosome genes

setDefaultValue

public final void setDefaultValue(java.lang.Object defaultValue)
Sets the default value of this chromosome. The default value must be valid for all genes, therefore it should be be null or belonging to every AlleleSet.

Parameters:
defaultValue - the new default value to be used

getDeafultValue

public final java.lang.Object getDeafultValue()
Returns the default value of this chromosome

Returns:
the object default value of this chromosome

setGene

public final void setGene(int pos,
                          java.lang.Object value)
Sets the specified value at the specified position

Parameters:
pos - the position to be modify
value - the value to be set

getGene

public final ObjectChromosome.Gene getGene(int index)
Returns the gene at the specified position

Parameters:
index - the index of the gene
Returns:
the gene at the specified position

toString

public final java.lang.String toString()
Overrides:
toString in class java.lang.Object

equals

public final boolean equals(ObjectChromosome chromosome)
Description copied from interface: Chromosome
Compares the chromosome with another.

Specified by:
equals in interface Chromosome<ObjectChromosome>
Parameters:
chromosome - the cromosome to compare to.
Returns:
true if the two chromosome are equal, false otherwise.