org.jgap.impl
Class FixedBinaryGene

java.lang.Object
  extended byorg.jgap.impl.FixedBinaryGene
All Implemented Interfaces:
java.lang.Comparable, Gene, java.io.Serializable

public class FixedBinaryGene
extends java.lang.Object
implements Gene

A Gene implementation that supports two possible values (alleles, 1 and 0) with a fixed length of alleles

NOTE: Since this Gene implementation only supports two different values (1 and 0), there's only a 50% chance that invocation of the setToRandomValue() method will actually change the value of this Gene (if it has a value). As a result, it may be desirable to use a higher overall mutation rate when this Gene implementation is in use.

Partly adapted stuff from the JAGA (Java API for Genetic Algorithms) package (see http://www.jaga.org).

Since:
2.0
Author:
Klaus Meffert
See Also:
Serialized Form

Field Summary
 
Fields inherited from interface org.jgap.Gene
PERSISTENT_FIELD_DELIMITER
 
Constructor Summary
FixedBinaryGene(FixedBinaryGene toCopy)
           
FixedBinaryGene(int a_length)
           
 
Method Summary
 void applyMutation(int index, double a_percentage)
          Applies a mutation of a given intensity (percentage) onto the atomic element at given index
 void cleanup()
          Executed by the genetic engine when this Gene instance is no longer needed and should perform any necessary resource cleanup.
 java.lang.Object clone()
           
 int compareTo(java.lang.Object other)
          Compares this Gene with the specified object for order.
 boolean equals(java.lang.Object other)
          Compares this Gene with the given object and returns true if the other object is a FixedBinaryGene and has the same value as this Gene.
 void flip(int index)
           
 java.lang.Object getAllele()
          Retrieves the value represented by this Gene.
 boolean getBit(int m_index)
           
 IGeneConstraintChecker getConstraintChecker()
           
 int[] getIntValues()
           
 int getLength()
           
 java.lang.String getPersistentRepresentation()
          Retrieves a string representation of the value of this Gene instance that includes any information required to reconstruct it at a later time, such as its value and internal state.
 int hashCode()
          Retrieves the hash code value of this Gene.
 Gene newGene()
          Provides an implementation-independent means for creating new Gene instances.
 void setAllele(java.lang.Object a_newValue)
          Sets the value of this Gene to the new given value.
 void setBit(int m_index, boolean m_value)
           
 void setBit(int m_index, int m_value)
           
 void setBit(int m_from, int m_to, boolean m_value)
           
 void setBit(int from, int to, FixedBinaryGene values)
           
 void setConstraintChecker(IGeneConstraintChecker a_constraintChecker)
          Sets the constraint checker to be used for this gene whenever method setAllele(Object a_newValue) is called
 void setToRandomValue(RandomGenerator a_numberGenerator)
          Sets the value of this Gene to a random legal value for the implementation.
 void setUnchecked(int index, boolean value)
           
 void setValueFromPersistentRepresentation(java.lang.String a_representation)
          Sets the value and internal state of this Gene from the string representation returned by a previous invocation of the getPersistentRepresentation() method.
 int size()
           
 FixedBinaryGene substring(int m_from, int m_to)
           
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

FixedBinaryGene

public FixedBinaryGene(int a_length)
Parameters:
a_length - the fixed length of the gene
Since:
2.0

FixedBinaryGene

public FixedBinaryGene(FixedBinaryGene toCopy)
Method Detail

newGene

public Gene newGene()
Description copied from interface: Gene
Provides an implementation-independent means for creating new Gene instances. The new instance that is created and returned should be setup with any implementation-dependent configuration that this Gene instance is setup with (aside from the actual value, of course). For example, if this Gene were setup with bounds on its value, then the Gene instance returned from this method should also be setup with those same bounds. This is important, as the JGAP core will invoke this method on each Gene in the sample Chromosome in order to create each new Gene in the same respective gene position for a new Chromosome.

It should be noted that nothing is guaranteed about the actual value of the returned Gene and it should therefore be considered to be undefined.

Specified by:
newGene in interface Gene
Returns:
A new Gene instance of the same type and with the same setup as this concrete Gene.

getLength

public int getLength()

clone

public java.lang.Object clone()

setAllele

public void setAllele(java.lang.Object a_newValue)
Description copied from interface: Gene
Sets the value of this Gene to the new given value. The actual type of the value is implementation-dependent.

Specified by:
setAllele in interface Gene
Parameters:
a_newValue - the new value of this Gene instance.

setConstraintChecker

public void setConstraintChecker(IGeneConstraintChecker a_constraintChecker)
Sets the constraint checker to be used for this gene whenever method setAllele(Object a_newValue) is called

Parameters:
a_constraintChecker - the constraint checker to be set
Since:
2.0

getConstraintChecker

public IGeneConstraintChecker getConstraintChecker()
Returns:
IGeneConstraintChecker the constraint checker to be used whenever method setAllele(Object a_newValue) is called
Since:
2.0

getAllele

public java.lang.Object getAllele()
Description copied from interface: Gene
Retrieves the value represented by this Gene. The actual type of the value is implementation-dependent.

Specified by:
getAllele in interface Gene
Returns:
the value of this Gene.

getIntValues

public int[] getIntValues()

getBit

public boolean getBit(int m_index)

setBit

public void setBit(int m_index,
                   boolean m_value)

setBit

public void setBit(int m_index,
                   int m_value)

setBit

public void setBit(int m_from,
                   int m_to,
                   boolean m_value)

setBit

public void setBit(int from,
                   int to,
                   FixedBinaryGene values)

substring

public FixedBinaryGene substring(int m_from,
                                 int m_to)

flip

public void flip(int index)

setUnchecked

public void setUnchecked(int index,
                         boolean value)

getPersistentRepresentation

public java.lang.String getPersistentRepresentation()
                                             throws java.lang.UnsupportedOperationException
Description copied from interface: Gene
Retrieves a string representation of the value of this Gene instance that includes any information required to reconstruct it at a later time, such as its value and internal state. This string will be used to represent this Gene instance in XML persistence. This is an optional method but, if not implemented, XML persistence and possibly other features will not be available. An UnsupportedOperationException should be thrown if no implementation is provided.

Specified by:
getPersistentRepresentation in interface Gene
Returns:
A string representation of this Gene's current state.
Throws:
java.lang.UnsupportedOperationException - to indicate that no implementation is provided for this method.

setValueFromPersistentRepresentation

public void setValueFromPersistentRepresentation(java.lang.String a_representation)
                                          throws java.lang.UnsupportedOperationException,
                                                 UnsupportedRepresentationException
Sets the value and internal state of this Gene from the string representation returned by a previous invocation of the getPersistentRepresentation() method. This is an optional method but, if not implemented, XML persistence and possibly other features will not be available. An UnsupportedOperationException should be thrown if no implementation is provided.

Specified by:
setValueFromPersistentRepresentation in interface Gene
Parameters:
a_representation - the string representation retrieved from a prior call to the getPersistentRepresentation() method.
Throws:
java.lang.UnsupportedOperationException - to indicate that no implementation is provided for this method.
UnsupportedRepresentationException - if this Gene implementation does not support the given string representation.
Since:
2.0

setToRandomValue

public void setToRandomValue(RandomGenerator a_numberGenerator)
Description copied from interface: Gene
Sets the value of this Gene to a random legal value for the implementation. This method exists for the benefit of mutation and other operations that simply desire to randomize the value of a gene.

Specified by:
setToRandomValue in interface Gene
Parameters:
a_numberGenerator - The random number generator that should be used to create any random values. It's important to use this generator to maintain the user's flexibility to configure the genetic engine to use the random number generator of their choice.

cleanup

public void cleanup()
Executed by the genetic engine when this Gene instance is no longer needed and should perform any necessary resource cleanup.

Specified by:
cleanup in interface Gene
Since:
2.0

toString

public java.lang.String toString()
Specified by:
toString in interface Gene
Returns:
String
Since:
2.0

size

public int size()
Specified by:
size in interface Gene
Returns:
the size of the gene, i.e the number of atomic elements.
Since:
2.0

hashCode

public int hashCode()
Retrieves the hash code value of this Gene.

Returns:
this Gene's hash code.
Since:
2.0

applyMutation

public void applyMutation(int index,
                          double a_percentage)
Applies a mutation of a given intensity (percentage) onto the atomic element at given index

Specified by:
applyMutation in interface Gene
Parameters:
index - index of atomic element, between 0 and size()-1
a_percentage - percentage of mutation (greater than -1 and smaller than 1).
Since:
2.0

equals

public boolean equals(java.lang.Object other)
Compares this Gene with the given object and returns true if the other object is a FixedBinaryGene and has the same value as this Gene. Otherwise it returns false.

Parameters:
other - the object to compare to this Gene for equality.
Returns:
true if this Gene is equal to the given object, false otherwise.
Since:
2.0

compareTo

public int compareTo(java.lang.Object other)
Compares this Gene with the specified object for order. A 0 value is considered to be less than a 1 value. A null value is considered to be less than any non-null value. A Gene is greater than another one of the same length if it has more 1's than the other one. If there is the same number of 1's the Gene with the highest value (binary to int) is greater.

Specified by:
compareTo in interface java.lang.Comparable
Parameters:
other - the FixedBinaryGene to be compared.
Returns:
a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.
Throws:
java.lang.ClassCastException - if the specified object's type prevents it from being compared to this Gene.
Since:
2.0