JGAP

org.jgap.impl
Class FixedBinaryGene

java.lang.Object
  extended by org.jgap.BaseGene
      extended by org.jgap.impl.FixedBinaryGene
All Implemented Interfaces:
java.io.Serializable, java.lang.Comparable, Gene, IBusinessKey, IPersistentRepresentation, IUniqueKey

public class FixedBinaryGene
extends BaseGene
implements IPersistentRepresentation

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 jaga).

Since:
2.0
See Also:
Serialized Form

Field Summary
 
Fields inherited from class org.jgap.BaseGene
DELTA, S_APPLICATION_DATA
 
Fields inherited from interface org.jgap.Gene
PERSISTENT_FIELD_DELIMITER
 
Constructor Summary
FixedBinaryGene(Configuration a_config, FixedBinaryGene a_toCopy)
           
FixedBinaryGene(Configuration a_config, int a_length)
           
 
Method Summary
 void applyMutation(int a_index, double a_percentage)
          Applies a mutation of a given intensity (percentage) onto the atomic element at given index
protected  void checkIndex(int a_index)
           
protected  int checkSubLength(int a_from, int a_to)
           
 java.lang.Object clone()
           
 int compareTo(java.lang.Object a_other)
          Compares this Gene with the specified object for order.
 void flip(int a_index)
           
 java.lang.Object getAllele()
          Retrieves the allele value represented by this Gene.
 boolean getBit(int a_index)
           
 java.lang.String getBusinessKey()
           
protected  java.lang.Object getInternalValue()
          Not called as getAllele() is overridden.
 int[] getIntValues()
           
 int getLength()
           
 java.lang.String getPersistentRepresentation()
          Returns a persistent representation of an entity (such as a chromosome or a gene).
protected  boolean getUnchecked(int a_index)
           
protected  int[] getValue()
           
 int hashCode()
          Modified hashCode() function to return different hashcodes for differently ordered genes in a chromosome --> does not work as internal value always initialized!
protected  Gene newGeneInternal()
           
 void setAllele(java.lang.Object a_newValue)
          Sets the value of this Gene to the new given value.
 void setBit(int a_index, boolean a_value)
           
 void setBit(int a_index, int a_value)
           
 void setBit(int a_from, int a_to, boolean a_value)
           
 void setBit(int a_from, int a_to, FixedBinaryGene a_values)
           
 void setToRandomValue(RandomGenerator a_numberGenerator)
          Sets the value of this Gene to a random legal value for the implementation.
 void setUnchecked(int a_index, boolean a_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 a_from, int a_to)
           
 java.lang.String toString()
          Retrieves a string representation of this Gene's value that may be useful for display purposes.
 
Methods inherited from class org.jgap.BaseGene
cleanup, compareApplicationData, decode, encode, equals, getApplicationData, getConfiguration, getConstraintChecker, getEnergy, getUniqueID, getUniqueIDTemplate, isCompareApplicationData, newGene, setApplicationData, setCompareApplicationData, setConstraintChecker, setEnergy, setUniqueIDTemplate
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

FixedBinaryGene

public FixedBinaryGene(Configuration a_config,
                       int a_length)
                throws InvalidConfigurationException
Parameters:
a_config - the configuration to use
a_length - the fixed length of the gene
Throws:
InvalidConfigurationException
Since:
2.0

FixedBinaryGene

public FixedBinaryGene(Configuration a_config,
                       FixedBinaryGene a_toCopy)
                throws InvalidConfigurationException
Throws:
InvalidConfigurationException
Method Detail

newGeneInternal

protected Gene newGeneInternal()
Specified by:
newGeneInternal in class BaseGene

getValue

protected int[] getValue()

getLength

public int getLength()

clone

public java.lang.Object clone()
Overrides:
clone in class java.lang.Object

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

getAllele

public java.lang.Object getAllele()
Description copied from class: BaseGene
Retrieves the allele value represented by this Gene.

Specified by:
getAllele in interface Gene
Overrides:
getAllele in class BaseGene
Returns:
the allele value of this Gene

getIntValues

public int[] getIntValues()
Returns:
internal representation of the gene's state. Use getBit for reading bits!

getBit

public boolean getBit(int a_index)

setBit

public void setBit(int a_index,
                   boolean a_value)

setBit

public void setBit(int a_index,
                   int a_value)

setBit

public void setBit(int a_from,
                   int a_to,
                   boolean a_value)

setBit

public void setBit(int a_from,
                   int a_to,
                   FixedBinaryGene a_values)

substring

public FixedBinaryGene substring(int a_from,
                                 int a_to)

flip

public void flip(int a_index)

checkSubLength

protected int checkSubLength(int a_from,
                             int a_to)

checkIndex

protected void checkIndex(int a_index)

getUnchecked

protected boolean getUnchecked(int a_index)

setUnchecked

public void setUnchecked(int a_index,
                         boolean a_value)

getPersistentRepresentation

public java.lang.String getPersistentRepresentation()
Description copied from interface: IPersistentRepresentation
Returns a persistent representation of an entity (such as a chromosome or a gene).

Specified by:
getPersistentRepresentation in interface Gene
Specified by:
getPersistentRepresentation in interface IPersistentRepresentation
Returns:
string representation of current state

setValueFromPersistentRepresentation

public void setValueFromPersistentRepresentation(java.lang.String a_representation)
                                          throws 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
Specified by:
setValueFromPersistentRepresentation in interface IPersistentRepresentation
Parameters:
a_representation - the string representation retrieved from a prior call to the getPersistentRepresentation() method
Throws:
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

toString

public java.lang.String toString()
Description copied from class: BaseGene
Retrieves a string representation of this Gene's value that may be useful for display purposes.

Specified by:
toString in interface Gene
Overrides:
toString in class BaseGene
Returns:
String representation
Since:
2.0

getBusinessKey

public java.lang.String getBusinessKey()
Specified by:
getBusinessKey in interface IBusinessKey
Overrides:
getBusinessKey in class BaseGene
Returns:
business key

size

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

applyMutation

public void applyMutation(int a_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:
a_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

compareTo

public int compareTo(java.lang.Object a_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:
a_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

getInternalValue

protected java.lang.Object getInternalValue()
Not called as getAllele() is overridden.

Specified by:
getInternalValue in class BaseGene
Returns:
same as getAllele()

hashCode

public int hashCode()
Modified hashCode() function to return different hashcodes for differently ordered genes in a chromosome --> does not work as internal value always initialized!

Overrides:
hashCode in class BaseGene
Returns:
this Gene's hash code
Since:
2.2

JGAP