org.jgap.impl
Class StringGene

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

public class StringGene
extends java.lang.Object
implements Gene

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
Author:
Klaus Meffert, Audrius Meskauskas
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 interface org.jgap.Gene
PERSISTENT_FIELD_DELIMITER
 
Constructor Summary
StringGene()
           
StringGene(int a_minLength, int a_maxLength)
           
StringGene(int a_minLength, int a_maxLength, java.lang.String a_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)
 void cleanup()
          Executed by the genetic engine when this Gene instance is no longer needed and should perform any necessary resource cleanup.
 int compareTo(java.lang.Object 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.
 boolean equals(java.lang.Object other)
          Compares this Gene with the given object and returns true if the other object is a Gene of this type and has the same value (allele) as this Gene.
 java.lang.Object getAllele()
          Retrieves the value (allele) represented by this Gene.
 java.lang.String getAlphabet()
           
 IGeneConstraintChecker getConstraintChecker()
           
 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.
 int hashCode()
          Retrieves the hash code value for this StringGene.
 Gene newGene()
          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 setConstraintChecker(IGeneConstraintChecker a_constraintChecker)
          Sets the constraint checker to be used for this gene whenever method setAllele(Object a_newValue) is called
 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 java.lang.Object
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()
Since:
1.1

StringGene

public StringGene(int a_minLength,
                  int a_maxLength)
Parameters:
a_minLength - minimum valid length of allele
a_maxLength - maximum valid length of allele
Since:
1.1

StringGene

public StringGene(int a_minLength,
                  int a_maxLength,
                  java.lang.String a_alphabet)
Parameters:
a_minLength - minimum valid length of allele
a_maxLength - maximum valid length of allele
a_alphabet - valid aplhabet for allele
Since:
2.0
Method Detail

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

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
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.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
Returns:
A 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

getAllele

public java.lang.Object getAllele()
Retrieves the value (allele) represented by this Gene. All values returned by this class will be String instances.

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

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 throwsn

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

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

newGene

public Gene newGene()
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.
Since:
1.1

compareTo

public int compareTo(java.lang.Object 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:
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
Returns:
the size of the gene, i.e the number of atomic elements. Always 1 for numbers

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

Parameters:
a_alphabet - valid aplhabet 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
Returns:
a string representation of this StringGene's value.
Since:
1.1

equals

public boolean equals(java.lang.Object other)
Compares this Gene with the given object and returns true if the other object is a Gene of this type and has the same value (allele) 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:
1.1

hashCode

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

Returns:
this StringGene's hash code.
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