JGAP

org.jgap.impl
Class IntegerGene

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

public class IntegerGene
extends NumberGene
implements IPersistentRepresentation

A Gene implementation that supports an 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.

Since:
1.0
See Also:
Serialized Form

Field Summary
protected static long INTEGER_RANGE
          Represents the constant range of values supported by integers.
 
Fields inherited from class org.jgap.BaseGene
DELTA, S_APPLICATION_DATA
 
Fields inherited from interface org.jgap.Gene
PERSISTENT_FIELD_DELIMITER
 
Constructor Summary
IntegerGene()
          Constructs a new IntegerGene with default settings.
IntegerGene(Configuration a_config)
          Constructs a new IntegerGene with default settings.
IntegerGene(Configuration a_config, 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
 void applyMutation(int a_index, double a_percentage)
          See interface Gene for description.
protected  int compareToNative(java.lang.Object a_o1, java.lang.Object a_o2)
          Compares to objects by first casting them into their expected type (e.g.
 int getLowerBounds()
           
 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.
 int getUpperBounds()
           
 int hashCode()
          Modified hashCode() function to return different hashcodes for differently ordered genes in a chromosome.
 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.
protected  Gene newGeneInternal()
          Provides implementation-independent means for creating new Gene instances.
 void setToRandomValue(RandomGenerator 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 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 Gene's value that may be useful for display purposes.
 
Methods inherited from class org.jgap.impl.NumberGene
compareTo, getInternalValue, setAllele
 
Methods inherited from class org.jgap.BaseGene
cleanup, compareApplicationData, decode, encode, equals, getAllele, getApplicationData, getBusinessKey, getConfiguration, getConstraintChecker, getEnergy, getUniqueID, getUniqueIDTemplate, isCompareApplicationData, newGene, setApplicationData, setCompareApplicationData, setConstraintChecker, setEnergy, setUniqueIDTemplate, size
 
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
Constructor Detail

IntegerGene

public IntegerGene()
            throws InvalidConfigurationException
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.

Attention: The configuration used is the one set with the static method Genotype.setConfiguration.

Throws:
InvalidConfigurationException
Since:
1.0

IntegerGene

public IntegerGene(Configuration a_config)
            throws InvalidConfigurationException
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.

Parameters:
a_config - the configuration to use
Throws:
InvalidConfigurationException
Since:
3.0

IntegerGene

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

Parameters:
a_config - the configuration to use
a_lowerBounds - the lowest value that this Gene may possess, inclusive
a_upperBounds - the highest value that this Gene may possess, inclusive
Throws:
InvalidConfigurationException
Since:
2.0
Method Detail

newGeneInternal

protected Gene newGeneInternal()
Provides implementation-independent means for creating new Gene instances.

Specified by:
newGeneInternal in class BaseGene
Returns:
a new Gene instance of the same type and with the same setup as this concrete Gene
Since:
2.6 (was newGene since 1.0, moved to BaseGene)

getPersistentRepresentation

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

Specified by:
getPersistentRepresentation in interface Gene
Specified by:
getPersistentRepresentation in interface IPersistentRepresentation
Returns:
string representation of this Gene's current state
Since:
1.0

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:
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:
1.0

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
Since:
1.0

setToRandomValue

public void setToRandomValue(RandomGenerator 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 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
Since:
1.0

compareToNative

protected int compareToNative(java.lang.Object a_o1,
                              java.lang.Object a_o2)
Compares to objects by first casting them into their expected type (e.g. Integer for IntegerGene) and then calling the compareTo-method of the casted type.

Specified by:
compareToNative in class NumberGene
Parameters:
a_o1 - first object to be compared, always is not null
a_o2 - second object to be compared, always is not null
Returns:
a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the object provided for comparison
Since:
1.0

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.

Specified by:
mapValueToWithinBounds in class NumberGene
Since:
1.0

applyMutation

public void applyMutation(int a_index,
                          double a_percentage)
See interface Gene for description.

Specified by:
applyMutation in interface Gene
Parameters:
a_index - ignored (because there is only 1 atomic element)
a_percentage - percentage of mutation (greater than -1 and smaller than 1)
Since:
1.1

hashCode

public int hashCode()
Modified hashCode() function to return different hashcodes for differently ordered genes in a chromosome.

Overrides:
hashCode in class BaseGene
Returns:
-1 if no allele set, otherwise value return by BaseGene.hashCode()
Since:
2.2

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 of this Gene's value that may be useful for display purposes
Since:
2.4

getLowerBounds

public int getLowerBounds()
Returns:
the lower bounds of the integer gene
Since:
2.6

getUpperBounds

public int getUpperBounds()
Returns:
the upper bounds of the integer gene
Since:
2.6

JGAP