jenes.chromosome
Class BitwiseChromosome

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

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

This class provides chromosomes made of bits. Its genome contains objects coded according to a specified BitwiseChromosome.BitCoding. Typically objects coded by this chromosome are numeric values. The default integer representation is used when no BitCoding is specified.

A BitwiseChromosome has size and length attributes. The size is the number of coded value contained by its genoma; the length is the number of bits. The length depends on which coding is used for translating objects into bits. The relation between size and length is shown below:

  aBitwiseChromosome.length() = aBitwiseChromosome.getSize() *  aCoding.SIZE.BITS
 
where aCoding.SIZE.BITS is the number of bits required for coding one object.

This Chromosome performs genetic operations at bit level, processing an array of integer, thus ensuring a high throughput and minimal memory occupation. Using a 16 bit representation, a chromosome holding 4 objects (size) will be represented by 32 bits (length), thus by 2 integers (as each of integer is represented by 32 bit)

Since:
1.0
Version:
1.2
Author:
Luigi Troiano, Pierpaolo Lombardi, Giuseppe Pascale, Thierry Bodhuin

Nested Class Summary
static class BitwiseChromosome.BitCoding<T>
          Represents the coding used by a BitwiseChromosome.
static class BitwiseChromosome.BitSize
          Definition of the number bit length and mask to use in the coding operations.
 
Constructor Summary
BitwiseChromosome(BitwiseChromosome chromosome)
          Creates a new BitwiseChromosome using the specified one as prototype
BitwiseChromosome(int size)
          Creates a new BitwiseChromosome with the specified number of objects.
BitwiseChromosome(int size, BitwiseChromosome.BitCoding coding)
          Creates a new BitwiseChromosome with the specified number of objects and coding.
 
Method Summary
 BitwiseChromosome clone()
          Makes a chromosome copy
 void cross(BitwiseChromosome chromosome, int from)
          Exchanges the chromosome bits from the specified cross site to the final position
 void cross(BitwiseChromosome chromosome, int from, int to)
          Exchanges the chromosome bits in the range [from,to].
 boolean equals(BitwiseChromosome chromosome)
          Compares the chromosome with another.
 int getBitValueAt(int index)
          Returns the bit value at the specified position.
 int getIntLength()
          Returns the chromosome length expressed in integers
 int getIntSize()
          Returns the number of integer used by the chromosome for coding the objects
 int getIntValueAt(int index)
          Returns the int value at the specified position
 int getSize()
          Returns the number of coded objects contained by this chromosome.
 BitwiseChromosome.BitCoding getType()
          Returns the BitwiseChromosome.BitCoding used by this chromosome
 java.lang.Object getValueAt(int index)
          Returns the object value at the specified position in the chromosome.
 void leftShift(int from, int to)
          Executes the left shift of bits within the specified range.
 int length()
          Returns the chromosome length expressed in bits.
 void randomize()
          Randomizes each chromosome bit
 void randomize(int pos)
          Randomizes the bit at the given position
 void rightShift(int from, int to)
          Executes the right shift of bits within the specified range.
 void setAs(BitwiseChromosome chromosome)
          Sets this chromosome as a copy of another.
 void setBitValueAt(int index, int bit)
          Sets the bit at a given position.
 void setDefaultValueAt(int pos)
          Sets the default bit value at the a given position
 void setIntValueAt(int index, int value)
          Sets the int value at the specified position
 void setValue(int index, java.lang.Object value)
          Sets the specified object value at the given position.
 void swap(int pos1, int pos2)
          Swaps two bits at given positions
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BitwiseChromosome

public BitwiseChromosome(int size)
Creates a new BitwiseChromosome with the specified number of objects. The chromosome is made of a bit string encoding objects.

Parameters:
size - the number of objects the chromosone represents

BitwiseChromosome

public BitwiseChromosome(int size,
                         BitwiseChromosome.BitCoding coding)
Creates a new BitwiseChromosome with the specified number of objects and coding.

Parameters:
size - the number of chromosome coded objects
coding - the coding to use

BitwiseChromosome

public BitwiseChromosome(BitwiseChromosome chromosome)
Creates a new BitwiseChromosome using the specified one as prototype

Parameters:
chromosome - the chromosome to copy
Method Detail

getType

public final BitwiseChromosome.BitCoding getType()
Returns the BitwiseChromosome.BitCoding used by this chromosome

Returns:
the bit coding used

getSize

public final int getSize()
Returns the number of coded objects contained by this chromosome.

Returns:
the number of coded objects

getIntValueAt

public final int getIntValueAt(int index)
Returns the int value at the specified position

Parameters:
index - the index of the value to return
Returns:
the int value at the specified position

setIntValueAt

public final void setIntValueAt(int index,
                                int value)
Sets the int value at the specified position

Parameters:
index - the index of the element to be modify
value - the value to set

getIntSize

public final int getIntSize()
Returns the number of integer used by the chromosome for coding the objects

Returns:
the number of integers

getValueAt

public final java.lang.Object getValueAt(int index)
Returns the object value at the specified position in the chromosome. The value is decoded and returned.

Parameters:
index - the position
Returns:
the object

setValue

public final void setValue(int index,
                           java.lang.Object value)
Sets the specified object value at the given position. The object value is encoded and then placed in the chromosome.

Parameters:
index - the position
value - the object value to be placed

getBitValueAt

public final int getBitValueAt(int index)
Returns the bit value at the specified position. This position takes into account the usage of integers made by the coding specification. For instance, if the coding requires 5 bits for each value, index 31 does not point to the last bit of the first integer, but to the second bit of the second integer, as described below [01101 01110 00011 11011 10101 10010 --] [01100 11010 ...

Parameters:
index - the position
Returns:
the bit value

setBitValueAt

public final void setBitValueAt(int index,
                                int bit)
Sets the bit at a given position. Position is related to the bits actually used and not to those occupied by the chromosome data structure.

Parameters:
index - the position
bit - the bit value

cross

public final void cross(BitwiseChromosome chromosome,
                        int from,
                        int to)
Exchanges the chromosome bits in the range [from,to]. The range is referred to positions of bits effectively used by the chromosome and not to thoae occupied by the underlying data structure.

Specified by:
cross in interface Chromosome<BitwiseChromosome>
Parameters:
chromosome - the chromosome to cross with
from - the initial cross site
to - the final cross site

cross

public final void cross(BitwiseChromosome chromosome,
                        int from)
Exchanges the chromosome bits from the specified cross site to the final position

Specified by:
cross in interface Chromosome<BitwiseChromosome>
Parameters:
chromosome - the chromosome to cross with
from - the initial cross site

equals

public final boolean equals(BitwiseChromosome chromosome)
Compares the chromosome with another.

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

length

public final int length()
Returns the chromosome length expressed in bits. This value can be different from the effective chromosome length (the latter can contain bits used to make the former multiple of Integer.SIZE)

Specified by:
length in interface Chromosome<BitwiseChromosome>
Returns:
the chromosome length

getIntLength

public final int getIntLength()
Returns the chromosome length expressed in integers

Returns:
the number of integers

randomize

public final void randomize(int pos)
Randomizes the bit at the given position

Specified by:
randomize in interface Chromosome<BitwiseChromosome>
Parameters:
pos - the position of bit to alter

randomize

public final void randomize()
Randomizes each chromosome bit

Specified by:
randomize in interface Chromosome<BitwiseChromosome>

leftShift

public final void leftShift(int from,
                            int to)
Executes the left shift of bits within the specified range. The shift is circular, so the most right-side bit becomes the first bit on left.

Specified by:
leftShift in interface Chromosome<BitwiseChromosome>
Parameters:
from - the lower range limit
to - the upper range limit

rightShift

public final void rightShift(int from,
                             int to)
Executes the right shift of bits within the specified range. The shift is circular, so the most left-side bit becomes the last bit on right.

Specified by:
rightShift in interface Chromosome<BitwiseChromosome>
Parameters:
from - the lower range limit
to - the upper range limit

setAs

public final void setAs(BitwiseChromosome chromosome)
Sets this chromosome as a copy of another.

Specified by:
setAs in interface Chromosome<BitwiseChromosome>
Parameters:
chromosome - the chromosome to copy

setDefaultValueAt

public final void setDefaultValueAt(int pos)
Sets the default bit value at the a given position

Specified by:
setDefaultValueAt in interface Chromosome<BitwiseChromosome>
Parameters:
pos - bit position

swap

public final void swap(int pos1,
                       int pos2)
Swaps two bits at given positions

Specified by:
swap in interface Chromosome<BitwiseChromosome>
Parameters:
pos1 - first bit position
pos2 - second bit position

clone

public final BitwiseChromosome clone()
Makes a chromosome copy

Specified by:
clone in interface Chromosome<BitwiseChromosome>
Overrides:
clone in class java.lang.Object
Returns:
the chromsome clone