org.jgap.impl
Class StringGene
- java.lang.Object
-
- org.jgap.BaseGene
-
- 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
Fields Modifier and Type Field and Description static java.lang.StringALPHABET_CHARACTERS_DIGITSstatic java.lang.StringALPHABET_CHARACTERS_LOWERstatic java.lang.StringALPHABET_CHARACTERS_SPECIALstatic java.lang.StringALPHABET_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
Constructors Constructor and Description 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
All Methods Instance Methods Concrete Methods Modifier and Type Method and Description voidapplyMutation(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).intcompareTo(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.StringgetAlphabet()java.lang.StringgetBusinessKey()intgetMaxLength()intgetMinLength()java.lang.StringgetPersistentRepresentation()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.voidsetAllele(java.lang.Object a_newValue)Sets the value (allele) of this Gene to the new given value.voidsetAlphabet(java.lang.String a_alphabet)Sets the valid alphabet of the StringGene.voidsetMaxLength(int m_maxLength)voidsetMinLength(int m_minLength)voidsetToRandomValue(RandomGenerator a_numberGenerator)Sets the value (allele) of this Gene to a random String according to the valid alphabet and boundaries of length.voidsetValueFromPersistentRepresentation(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.intsize()java.lang.StringstringValue()Retrieves the String value of this Gene, which may be more convenient in some cases than the more general getAllele() method.java.lang.StringtoString()Retrieves a string representation of this StringGene's value that may be useful for display purposes.-
Methods inherited from class org.jgap.BaseGene
cleanup, equals, getAllele, getApplicationData, getConfiguration, getConstraintChecker, getEnergy, getUniqueID, getUniqueIDTemplate, hashCode, isCompareApplicationData, newGene, setApplicationData, setCompareApplicationData, setConstraintChecker, setEnergy, setUniqueIDTemplate
-
-
-
-
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 InvalidConfigurationExceptionDefault 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 usea_minLength- minimum valid length of allelea_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 usea_minLength- minimum valid length of an allelea_maxLength- maximum valid length of an allelea_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:
setToRandomValuein interfaceGene- 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 UnsupportedRepresentationExceptionSets 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:
setValueFromPersistentRepresentationin interfaceGene- Specified by:
setValueFromPersistentRepresentationin interfaceIPersistentRepresentation- 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.UnsupportedOperationExceptionRetrieves 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:
getPersistentRepresentationin interfaceGene- Specified by:
getPersistentRepresentationin interfaceIPersistentRepresentation- 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:
getBusinessKeyin interfaceIBusinessKey- Overrides:
getBusinessKeyin classBaseGene- 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.
-
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:
compareToin interfacejava.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()
-
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.
-
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:
applyMutationin interfaceGene- Parameters:
index- index of atomic element, between 0 and size()-1a_percentage- percentage of mutation (greater than -1 and smaller than 1).- Since:
- 1.1
-
-
DMelt 3.0 © DataMelt by jWork.ORG