JGAP

org.jgap.impl
Class StringGene

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

public class StringGene
extends BaseGene
implements IPersistentRepresentation, IBusinessKey

A Gene implementation that supports a string for its allele. The valid alphabet as well as the minimum and maximum length of the string can be specified.

An alphabet == null indicates that all characters are seen as valid.
An alphabet == "" indicates that no character is seen to be valid.

Partly copied from IntegerGene.

Since:
1.1
See Also:
Serialized Form

Field Summary
static java.lang.String ALPHABET_CHARACTERS_DIGITS
           
static java.lang.String ALPHABET_CHARACTERS_LOWER
           
static java.lang.String ALPHABET_CHARACTERS_SPECIAL
           
static java.lang.String ALPHABET_CHARACTERS_UPPER
           
 
Fields inherited from class org.jgap.BaseGene
DELTA, S_APPLICATION_DATA
 
Fields inherited from interface org.jgap.Gene
PERSISTENT_FIELD_DELIMITER
 
Constructor Summary
StringGene()
          Default constructor, sets minimum and maximum length to arbitrary.
StringGene(Configuration a_config)
          Default constructor, sets minimum and maximum length to arbitrary.
StringGene(Configuration a_config, int a_minLength, int a_maxLength)
          Constructor, allows to specify minimum and maximum lengths of the string held by this gene.
StringGene(Configuration a_config, int a_minLength, int a_maxLength, java.lang.String a_alphabet)
          Constructor, allows to specify minimum and maximum lengths of the string held by this gene, as well as the valid alphabet.
 
Method Summary
 void applyMutation(int index, double a_percentage)
          Applies a mutation of a given intensity (percentage) onto the atomic element at given index (NumberGenes only have one atomic element).
 int compareTo(java.lang.Object a_other)
          Compares this StringGene with the specified object (which must also be a StringGene) for order, which is determined by the String value of this Gene compared to the one provided for comparison.
 java.lang.String getAlphabet()
           
 java.lang.String getBusinessKey()
           
protected  java.lang.Object getInternalValue()
          Each Gene implementation holds its own m_value object keeping the allele value.
 int getMaxLength()
           
 int getMinLength()
           
 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.
protected  Gene newGeneInternal()
          Provides an implementation-independent means for creating new Gene instances.
 void setAllele(java.lang.Object a_newValue)
          Sets the value (allele) of this Gene to the new given value.
 void setAlphabet(java.lang.String a_alphabet)
          Sets the valid alphabet of the StringGene.
 void setMaxLength(int m_maxLength)
           
 void setMinLength(int m_minLength)
           
 void setToRandomValue(RandomGenerator a_numberGenerator)
          Sets the value (allele) of this Gene to a random String according to the valid alphabet and boundaries of length.
 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()
           
 java.lang.String stringValue()
          Retrieves the String value of this Gene, which may be more convenient in some cases than the more general getAllele() method.
 java.lang.String toString()
          Retrieves a string representation of this StringGene's value that may be useful for display purposes.
 
Methods inherited from class org.jgap.BaseGene
cleanup, compareApplicationData, decode, encode, equals, getAllele, getApplicationData, getConfiguration, getConstraintChecker, getEnergy, getUniqueID, getUniqueIDTemplate, hashCode, isCompareApplicationData, newGene, setApplicationData, setCompareApplicationData, setConstraintChecker, setEnergy, setUniqueIDTemplate
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

ALPHABET_CHARACTERS_UPPER

public static final java.lang.String ALPHABET_CHARACTERS_UPPER
See Also:
Constant Field Values

ALPHABET_CHARACTERS_LOWER

public static final java.lang.String ALPHABET_CHARACTERS_LOWER
See Also:
Constant Field Values

ALPHABET_CHARACTERS_DIGITS

public static final java.lang.String ALPHABET_CHARACTERS_DIGITS
See Also:
Constant Field Values

ALPHABET_CHARACTERS_SPECIAL

public static final java.lang.String ALPHABET_CHARACTERS_SPECIAL
See Also:
Constant Field Values
Constructor Detail

StringGene

public StringGene()
           throws InvalidConfigurationException
Default constructor, sets minimum and maximum length to arbitrary. You need to set the valid alphabet later!

Do not use this constructor with a sample chromosome set in the configuration.

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

Throws:
InvalidConfigurationException
Since:
1.1

StringGene

public StringGene(Configuration a_config)
           throws InvalidConfigurationException
Default constructor, sets minimum and maximum length to arbitrary. You need to set the valid alphabet later!

Do not use this constructor with a sample chromosome set in the configuration.

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

StringGene

public StringGene(Configuration a_config,
                  int a_minLength,
                  int a_maxLength)
           throws InvalidConfigurationException
Constructor, allows to specify minimum and maximum lengths of the string held by this gene. You need to set the valid alphabet later!

Do not use this constructor with a sample chromosome set in the configuration.

Parameters:
a_config - the configuration to use
a_minLength - minimum valid length of allele
a_maxLength - maximum valid length of allele
Throws:
InvalidConfigurationException
Since:
1.1

StringGene

public StringGene(Configuration a_config,
                  int a_minLength,
                  int a_maxLength,
                  java.lang.String a_alphabet)
           throws InvalidConfigurationException
Constructor, allows to specify minimum and maximum lengths of the string held by this gene, as well as the valid alphabet. This constructor can be used to construct a sample chromosome with a configuration.

Parameters:
a_config - the configuration to use
a_minLength - minimum valid length of an allele
a_maxLength - maximum valid length of an allele
a_alphabet - valid alphabet for an allele
Throws:
InvalidConfigurationException
Since:
2.0
Method Detail

setToRandomValue

public void setToRandomValue(RandomGenerator a_numberGenerator)
Sets the value (allele) of this Gene to a random String according to the valid alphabet and boundaries of length.

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

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

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.

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

getBusinessKey

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

setAllele

public void setAllele(java.lang.Object a_newValue)
Sets the value (allele) of this Gene to the new given value. This class expects the value to be a String instance. If the value is shorter or longer than the minimum or maximum length or any character is not within the valid alphabet an exception is thrown.

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

newGeneInternal

protected Gene newGeneInternal()
Provides an 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:
1.1

compareTo

public int compareTo(java.lang.Object a_other)
Compares this StringGene with the specified object (which must also be a StringGene) for order, which is determined by the String value of this Gene compared to the one provided for comparison.

Specified by:
compareTo in interface java.lang.Comparable
Parameters:
a_other - the StringGene to be compared to this StringGene
Returns:
a negative int, zero, or a positive int as this object is less than, equal to, or greater than the object provided for comparison
Throws:
java.lang.ClassCastException - if the specified object's type prevents it from being compared to this StringGene
Since:
1.1

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. Always 1 for non-composed Gene types. Override for composed Gene types

getMaxLength

public int getMaxLength()

getMinLength

public int getMinLength()

setMinLength

public void setMinLength(int m_minLength)

setMaxLength

public void setMaxLength(int m_maxLength)

getAlphabet

public java.lang.String getAlphabet()

setAlphabet

public void setAlphabet(java.lang.String a_alphabet)
Sets the valid alphabet of the StringGene. The caller needs to care that there are no doublettes in the alphabet. Otherwise there is no guarantee for correct functioning of the class!

Parameters:
a_alphabet - valid alphabet for allele
Since:
1.1

toString

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

Specified by:
toString in interface Gene
Overrides:
toString in class BaseGene
Returns:
a string representation of this StringGene's value
Since:
1.1

stringValue

public java.lang.String stringValue()
Retrieves the String value of this Gene, which may be more convenient in some cases than the more general getAllele() method.

Returns:
the String value of this Gene
Since:
1.1

applyMutation

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

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

getInternalValue

protected java.lang.Object getInternalValue()
Description copied from class: BaseGene
Each Gene implementation holds its own m_value object keeping the allele value. In your Gene implementation, just return it with this method (see BooleanGene for example)

Specified by:
getInternalValue in class BaseGene
Returns:
the m_value object

JGAP