org.jgap.impl
Class IntegerAllele

java.lang.Object
  extended byorg.jgap.Allele
      extended byorg.jgap.impl.IntegerAllele
All Implemented Interfaces:
java.lang.Comparable

public class IntegerAllele
extends Allele

A Gene implementation that supports a integer values for its allele. Upper and lower bounds may optionally be provided to restrict the range of legal values allowed by this Gene instance.


Field Summary
protected static long INTEGER_RANGE
          Represents the constant range of values supported by integers.
protected  Configuration m_activeConfiguration
          The current active configuration that is in use.
protected  long m_boundsUnitsToIntegerUnits
          Stores the number of integer range units that a single bounds-range unit represents.
protected  int m_lowerBounds
          The lower bounds of values represented by this Gene.
protected  int m_upperBounds
          The upper bounds of values represented by this Gene.
protected  java.lang.Integer m_value
          References the internal integer value (allele) of this Gene.
protected static java.lang.String PERSISTENT_FIELD_DELIMITER
          Represents the delimiter that is used to separate fields in the persistent representation of IntegerGene instances.
 
Constructor Summary
IntegerAllele()
          Constructs a new IntegerGene with default settings.
IntegerAllele(Configuration a_activeConfiguration)
          Constructs a new IntegerGene according to the given active configuration.
IntegerAllele(Configuration a_activeConfiguration, int a_lowerBounds, int a_upperBounds)
          Constructs a new IntegerGene with the given active configuration and the specified lower and upper bounds for values represented by this Gene.
IntegerAllele(int a_lowerBounds, int a_upperBounds)
          Constructs a new IntegerGene with the specified lower and upper bounds for values (alleles) of this Gene instance.
 
Method Summary
protected  void calculateBoundsUnitsToIntegerUnitsRatio()
          Calculates and sets the m_boundsUnitsToIntegerUnits field based on the current lower and upper bounds of this IntegerGene.
 void cleanup()
          Executed by the genetic engine when this Gene instance is no longer needed and should perform any necessary resource cleanup.
 Allele cloneAllele()
           
 double distance(Allele target)
           
 java.lang.String getPersistentRepresentation()
          Retrieves a string representation of this Gene that includes any information required to reconstruct it at a later time, such as its value and internal state.
 java.lang.Integer getValue()
           
 int hashCode()
          Retrieves the hash code value for this IntegerGene.
 int intValue()
          Retrieves the int value of this Gene, which may be more convenient in some cases than the more general getAllele() method.
protected  void mapValueToWithinBounds()
          Maps the value of this IntegerGene to within the bounds specified by the m_upperBounds and m_lowerBounds instance variables.
 Allele newAllele(Configuration a_activeConfiguration)
          Provides an implementation-independent means for creating new Gene instances.
 void setToRandomValue(java.util.Random a_numberGenerator)
          Sets the value (allele) of this Gene to a random Integer value between the lower and upper bounds (if any) of this Gene.
 void setValue(java.lang.Integer a_newValue)
          Sets the value (allele) of this Gene to the new given 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.
 java.lang.String toString()
          Retrieves a string representation of this IntegerGene's value that may be useful for display purposes.
 java.lang.String toXml()
           
 
Methods inherited from class org.jgap.Allele
compareTo, equals, getGene, getGenes, getInnovationId
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

INTEGER_RANGE

protected static final long INTEGER_RANGE
Represents the constant range of values supported by integers.

See Also:
Constant Field Values

PERSISTENT_FIELD_DELIMITER

protected static final java.lang.String PERSISTENT_FIELD_DELIMITER
Represents the delimiter that is used to separate fields in the persistent representation of IntegerGene instances.

See Also:
Constant Field Values

m_value

protected java.lang.Integer m_value
References the internal integer value (allele) of this Gene.


m_upperBounds

protected int m_upperBounds
The upper bounds of values represented by this Gene. If not explicitly provided by the user, this should be set to Integer.MAX_VALUE.


m_lowerBounds

protected int m_lowerBounds
The lower bounds of values represented by this Gene. If not explicitly provided by the user, this should be set to Integer.MIN_VALUE


m_boundsUnitsToIntegerUnits

protected long m_boundsUnitsToIntegerUnits
Stores the number of integer range units that a single bounds-range unit represents. For example, if the integer range is -2 billion to +2 billion and the bounds range is -1 billion to +1 billion, then each unit in the bounds range would map to 2 units in the integer range. The value of this variable would therefore be 2. This mapping unit is used to map illegal allele values that are outside of the bounds to legal allele values that are within the bounds.


m_activeConfiguration

protected Configuration m_activeConfiguration
The current active configuration that is in use.

Constructor Detail

IntegerAllele

public IntegerAllele()
Constructs a new IntegerGene with default settings. No bounds will be put into effect for values (alleles) of this Gene instance, other than the standard range of integer values.


IntegerAllele

public IntegerAllele(int a_lowerBounds,
                     int a_upperBounds)
Constructs a new IntegerGene with the specified lower and upper bounds for values (alleles) of this Gene instance.

Parameters:
a_lowerBounds - The lowest value that this Gene may possess, inclusive.
a_upperBounds - The highest value that this Gene may possess, inclusive.

IntegerAllele

public IntegerAllele(Configuration a_activeConfiguration)
Constructs a new IntegerGene according to the given active configuration.

Parameters:
a_activeConfiguration - The current active configuration.

IntegerAllele

public IntegerAllele(Configuration a_activeConfiguration,
                     int a_lowerBounds,
                     int a_upperBounds)
Constructs a new IntegerGene with the given active configuration and the specified lower and upper bounds for values represented by this Gene.

Parameters:
a_activeConfiguration - The current active configuration.
a_lowerBounds - The lowest value that this Gene may represent, inclusive.
a_upperBounds - The highest value that this Gene may represent, inclusive.
Method Detail

toXml

public java.lang.String toXml()
Returns:
XML representation of object

distance

public double distance(Allele target)
Specified by:
distance in class Allele
Parameters:
target -
Returns:
compatibility distance based on intValue; always positive
See Also:
Allele.distance(Allele)

newAllele

public Allele newAllele(Configuration a_activeConfiguration)
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.

Parameters:
a_activeConfiguration - The current active configuration.
Returns:
A new Gene instance of the same type and with the same setup as this concrete Gene.

cloneAllele

public Allele cloneAllele()
Specified by:
cloneAllele in class Allele
Returns:
clone
See Also:
Allele.cloneAllele()

setValue

public void setValue(java.lang.Integer a_newValue)
Sets the value (allele) of this Gene to the new given value. This class expects the value to be an Integer instance. If the value is above or below the upper or lower bounds, it will be mappped to within the allowable range.

Parameters:
a_newValue - the new value of this Gene instance.

getValue

public java.lang.Integer getValue()
Returns:
value

getPersistentRepresentation

public java.lang.String getPersistentRepresentation()
                                             throws java.lang.UnsupportedOperationException
Retrieves a string representation of this Gene 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 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.

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

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.

intValue

public int intValue()
Retrieves the int value of this Gene, which may be more convenient in some cases than the more general getAllele() method.

Returns:
the int value of this Gene.

setToRandomValue

public void setToRandomValue(java.util.Random a_numberGenerator)
Sets the value (allele) of this Gene to a random Integer value between the lower and upper bounds (if any) of this Gene.

Specified by:
setToRandomValue in class Allele
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.

hashCode

public int hashCode()
Retrieves the hash code value for this IntegerGene.

Overrides:
hashCode in class Allele
Returns:
this IntegerGene's hash code.
See Also:
Object.hashCode()

toString

public java.lang.String toString()
Retrieves a string representation of this IntegerGene's value that may be useful for display purposes.

Overrides:
toString in class Allele
Returns:
a string representation of this IntegerGene's value.
See Also:
Object.toString()

cleanup

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


mapValueToWithinBounds

protected void mapValueToWithinBounds()
Maps the value of this IntegerGene to within the bounds specified by the m_upperBounds and m_lowerBounds instance variables. The value's relative position within the integer range will be preserved within the bounds range (in other words, if the value is about halfway between the integer max and min, then the resulting value will be about halfway between the upper bounds and lower bounds). If the value is null or is already within the bounds, it will be left unchanged.


calculateBoundsUnitsToIntegerUnitsRatio

protected void calculateBoundsUnitsToIntegerUnitsRatio()
Calculates and sets the m_boundsUnitsToIntegerUnits field based on the current lower and upper bounds of this IntegerGene. For example, if the integer range is -2 billion to +2 billion and the bounds range is -1 billion to +1 billion, then each unit in the bounds range would map to 2 units in the integer range. The m_boundsUnitsToIntegerUnits field would therefore be 2. This mapping unit is used to map illegal allele values that are outside of the bounds to legal allele values that are within the bounds.