JGAP

org.jgap.supergenes
Class AbstractSupergene

java.lang.Object
  extended by org.jgap.BaseGene
      extended by org.jgap.supergenes.AbstractSupergene
All Implemented Interfaces:
java.io.Serializable, java.lang.Comparable, Gene, IBusinessKey, ICompositeGene, IPersistentRepresentation, IUniqueKey, Supergene, SupergeneValidator
Direct Known Subclasses:
NickelsPenniesSupergene

public abstract class AbstractSupergene
extends BaseGene
implements Supergene, SupergeneValidator, IPersistentRepresentation

Combined implementation of both Supergene and SupergeneValidator. A working supergene can be easily created from this class just by adding genes and overriding isValid (Gene [], Supergene) method. For more complex cases, you may need to set your own Validator.

Since:
2.0
See Also:
Serialized Form

Field Summary
static java.lang.String GENE_DELIMITER
          This field separates gene class name from the gene persistent representation string.
static java.lang.String GENE_DELIMITER_CLOSING
          Represents the closing delimiter that is used to separate genes in the persistent representation of CompositeGene instances.
static java.lang.String GENE_DELIMITER_HEADING
          Represents the heading delimiter that is used to separate genes in the persistent representation of CompositeGene instances.
protected  SupergeneValidator m_validator
          A validator (initially set to this
static int MAX_IMMUTABLE_GENES
          Maximal number of notes about immutable genes per single gene position
static int MAX_RETRIES
          Maximal number of retries for applyMutation and setToRandomValue.
 
Fields inherited from class org.jgap.BaseGene
DELTA, S_APPLICATION_DATA
 
Fields inherited from interface org.jgap.Gene
PERSISTENT_FIELD_DELIMITER
 
Constructor Summary
AbstractSupergene()
          Default constructor for dynamic instantiation.
AbstractSupergene(Configuration a_config)
          Constructor for dynamic instantiation.
AbstractSupergene(Configuration a_conf, Gene[] a_genes)
          Constructs abstract supergene with the given gene list.
 
Method Summary
 void addGene(Gene a_gene)
          Append a new gene to the gene array.
 void applyMutation(int a_index, double a_percentage)
          Applies a mutation of a given intensity (percentage) onto the gene at the given index.
 void cleanup()
          Calls cleanup() for each subgene.
 int compareTo(java.lang.Object o)
          Calls compareTo() for all subgenes.
protected  Gene createGene(java.lang.String a_geneClassName, java.lang.String a_persistentRepresentation)
          Creates a new instance of gene.
protected  SupergeneValidator createValidator(java.lang.String a_rep)
          Create validator from the string representation.
 boolean equals(java.lang.Object a_gene)
          Calls equals() for each pair of genes.
 Gene geneAt(int a_index)
          Returns the Gene at the given index (locus) within the Chromosome.
 java.lang.Object getAllele()
          Retrieves the allele value represented by this Supergene.
 Gene[] getGenes()
          Get the array of genes - components of this supergene.
 java.lang.Object getInternalValue()
          Each Gene implementation holds its own m_value object keeping the allele value.
 java.lang.String getPersistent()
           The default implementation returns an empty string.
 java.lang.String getPersistentRepresentation()
          Retrieves a string representation of the value of this Gene instance that includes any information required to reconstruct it at a later time, such as its value and internal state.
 SupergeneValidator getValidator()
          Gets an object, responsible for deciding if the Supergene allele combination is valid.
 int hashCode()
          Returns sum of hashCode() of the genes-components.
 boolean isValid()
          Test the allele combination of this supergene for validity.
 boolean isValid(Gene[] a_case, Supergene a_forSupergene)
          Test the given gene list for validity.
protected  Gene newGeneInternal()
          Creates a new instance of this Supergene class with the same number of genes, calling newGene() for each subgene.
static void reset()
          Discards all internal caches, ensuring correct repetetive tests of performance.
 void setAllele(java.lang.Object a_superAllele)
          Sets the allele.
 void setFromPersistent(java.lang.String a_from)
          Set a persistend string representation (if needed) for this validator.
 void setToRandomValue(RandomGenerator a_numberGenerator)
          Sets the value of this Gene to a random legal value for the implementation.
 void setValidator(SupergeneValidator a_validator)
          Sets an object, responsible for deciding if the Supergene allele combination is valid.
 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()
          Returns the number of the genes-components of this supergene.
protected static java.util.List split(java.lang.String a_string)
          Splits the string a_x into individual gene representations
 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.BaseGene
compareApplicationData, decode, encode, getApplicationData, getBusinessKey, getConfiguration, getConstraintChecker, getEnergy, getUniqueID, getUniqueIDTemplate, isCompareApplicationData, newGene, setApplicationData, setCompareApplicationData, setConstraintChecker, setEnergy, setUniqueIDTemplate
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.jgap.Gene
getApplicationData, getConfiguration, getEnergy, isCompareApplicationData, newGene, setApplicationData, setCompareApplicationData, setConstraintChecker, setEnergy
 
Methods inherited from interface org.jgap.IUniqueKey
getUniqueID, getUniqueIDTemplate, setUniqueIDTemplate
 

Field Detail

GENE_DELIMITER

public static final java.lang.String GENE_DELIMITER
This field separates gene class name from the gene persistent representation string.

See Also:
Constant Field Values

GENE_DELIMITER_HEADING

public static final java.lang.String GENE_DELIMITER_HEADING
Represents the heading delimiter that is used to separate genes in the persistent representation of CompositeGene instances.

See Also:
Constant Field Values

GENE_DELIMITER_CLOSING

public static final java.lang.String GENE_DELIMITER_CLOSING
Represents the closing delimiter that is used to separate genes in the persistent representation of CompositeGene instances.

See Also:
Constant Field Values

MAX_RETRIES

public static final int MAX_RETRIES
Maximal number of retries for applyMutation and setToRandomValue. If the valid supergen cannot be created after this number of iterations, the error message is printed and the unchanged instance is returned.

See Also:
Constant Field Values

MAX_IMMUTABLE_GENES

public static final int MAX_IMMUTABLE_GENES
Maximal number of notes about immutable genes per single gene position

See Also:
Constant Field Values

m_validator

protected SupergeneValidator m_validator
A validator (initially set to this

Constructor Detail

AbstractSupergene

public AbstractSupergene()
                  throws InvalidConfigurationException
Default constructor for dynamic instantiation.

Throws:
InvalidConfigurationException
Since:
3.0

AbstractSupergene

public AbstractSupergene(Configuration a_config)
                  throws InvalidConfigurationException
Constructor for dynamic instantiation.

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

AbstractSupergene

public AbstractSupergene(Configuration a_conf,
                         Gene[] a_genes)
                  throws InvalidConfigurationException
Constructs abstract supergene with the given gene list.

Parameters:
a_conf - the configuration to use
a_genes - array of genes for this Supergene
Throws:
InvalidConfigurationException
Method Detail

getGenes

public Gene[] getGenes()
Description copied from interface: Supergene
Get the array of genes - components of this supergene. The supergene components may be supergenes itself.

Specified by:
getGenes in interface Supergene
Returns:
the array of genes - components of this supergene. The supergene components may be supergenes itself

geneAt

public final Gene geneAt(int a_index)
Returns the Gene at the given index (locus) within the Chromosome. The first gene is at index zero and the last gene is at the index equal to the size of this Chromosome - 1. This seems to be one of the bottlenecks, so it is declared final. I cannot imagine the reason for overriding this trivial single line method.

Specified by:
geneAt in interface ICompositeGene
Specified by:
geneAt in interface Supergene
Parameters:
a_index - the index of the gene value to be returned
Returns:
the Gene at the given index

isValid

public boolean isValid()
Test the allele combination of this supergene for validity. This method calls isValid for the current gene list.

Specified by:
isValid in interface Supergene
Returns:
true only if the supergene allele combination is valid or the setValidator (null) has been previously called

isValid

public boolean isValid(Gene[] a_case,
                       Supergene a_forSupergene)
Test the given gene list for validity. The genes must exactly the same as inside this supergene. At least about 5 % of the randomly generated Supergene suparallele values should be valid. If the valid combinations represents too small part of all possible combinations, it can take too long to find the suitable mutation that does not brake a supergene. If you face this problem, try to split the supergene into several sub-supergenes. This method is only called if you have not set any alternative validator (including null).

Specified by:
isValid in interface SupergeneValidator
Parameters:
a_case - ignored here
a_forSupergene - ignored here
Returns:
true only if the supergene allele combination is valid
Throws:
java.lang.Error - by default. If you do not set external validator, you should always override this method

newGeneInternal

protected Gene newGeneInternal()
Creates a new instance of this Supergene class with the same number of genes, calling newGene() for each subgene. The class, derived from this abstract supergene will be instantiated (not the instance of abstractSupergene itself). If the external validator is set, the same validator will be set for the new gene.

Specified by:
newGeneInternal in class BaseGene
Returns:
the new Gene
Throws:
java.lang.Error - if the instance of this cannot be instantiated (for example, if it is not public or the parameterless constructor is not provided).

applyMutation

public void applyMutation(int a_index,
                          double a_percentage)
Applies a mutation of a given intensity (percentage) onto the gene at the given index. Retries while isValid() returns true for the supergene. The method is delegated to the first element of the gene, indexed by a_index. See org.jgap.supergenes.AbstractSupergene.isValid()

Specified by:
applyMutation in interface Gene
Parameters:
a_index - index of atomic element, between 0 and size()-1
a_percentage - percentage of mutation (greater than -1 and smaller than 1).

reset

public static void reset()
Discards all internal caches, ensuring correct repetetive tests of performance. Differently from cleanup(), discards also static references, that are assumed to be useful for the multiple instances of the Supergene. Clears the set of the alleles that are known to be immutable.


setToRandomValue

public void setToRandomValue(RandomGenerator a_numberGenerator)
Sets the value of this Gene to a random legal value for the implementation. It calls setToRandomValue for all subgenes and then validates. With a large number of subgenes and low percent of valid combinations this may take too long to complete. We think, at lease several % of the all possible combintations must be valid.

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

setAllele

public void setAllele(java.lang.Object a_superAllele)
Sets the allele.

Specified by:
setAllele in interface Gene
Parameters:
a_superAllele - must be an array of objects, size matching the number of genes

getAllele

public java.lang.Object getAllele()
Retrieves the allele value represented by this Supergene.

Specified by:
getAllele in interface Gene
Overrides:
getAllele in class BaseGene
Returns:
array of objects, each matching the subgene in this Supergene

getPersistentRepresentation

public java.lang.String getPersistentRepresentation()
                                             throws java.lang.UnsupportedOperationException
Description copied from interface: Gene
Retrieves a string representation of the value of this Gene instance 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 instance 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:
a string representation of the value of this Supergene instance, using calls to the Supergene components. Supports other (nested) supergenes in this supergene
Throws:
java.lang.UnsupportedOperationException

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. If the validator is not THIS and not null, a new validator is created using Class.forName(..).newInstance.

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

createValidator

protected SupergeneValidator createValidator(java.lang.String a_rep)
Create validator from the string representation.


createGene

protected Gene createGene(java.lang.String a_geneClassName,
                          java.lang.String a_persistentRepresentation)
                   throws java.lang.Exception
Creates a new instance of gene.

Throws:
java.lang.Exception

cleanup

public void cleanup()
Calls cleanup() for each subgene.

Specified by:
cleanup in interface Gene
Overrides:
cleanup in class BaseGene

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:
a string representation of the supergene, providing class name and calling toString() for all subgenes.

size

public int size()
Returns the number of the genes-components of this supergene.

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

compareTo

public int compareTo(java.lang.Object o)
Calls compareTo() for all subgenes. The passed parameter must be an instance of AbstractSupergene.

Specified by:
compareTo in interface java.lang.Comparable

equals

public boolean equals(java.lang.Object a_gene)
Calls equals() for each pair of genes. If the supplied object is an instance of the different class, returns false. Also, the genes are assumed to be different if they have different validator classes (or only one of the validators is set to null).

Overrides:
equals in class BaseGene
Parameters:
a_gene - the object to compare to this Gene for equality
Returns:
true if this Gene is equal to the given object, false otherwise

hashCode

public int hashCode()
Returns sum of hashCode() of the genes-components.

Overrides:
hashCode in class BaseGene
Returns:
this Gene's hash code

split

protected static final java.util.List split(java.lang.String a_string)
                                     throws UnsupportedRepresentationException
Splits the string a_x into individual gene representations

Parameters:
a_string - the string to split
Returns:
the elements of the returned array are the persistent representation strings of the genes - components
Throws:
UnsupportedRepresentationException

addGene

public void addGene(Gene a_gene)
Append a new gene to the gene array.

Specified by:
addGene in interface ICompositeGene
Parameters:
a_gene - the gene to add

setValidator

public void setValidator(SupergeneValidator a_validator)
Sets an object, responsible for deciding if the Supergene allele combination is valid. If it is set to null, no validation is performed (all combinations are assumed to be valid). If no validator is set, the method isValid (Gene [] ) is called.

Specified by:
setValidator in interface Supergene

getValidator

public SupergeneValidator getValidator()
Gets an object, responsible for deciding if the Supergene allele combination is valid. If no external validator was set and the class uses its own internal validation method, it returns this

Specified by:
getValidator in interface Supergene

getPersistent

public java.lang.String getPersistent()
The default implementation returns an empty string.

Specified by:
getPersistent in interface SupergeneValidator
Returns:
persistent string representation (if needed) of this validator. The method name is different allowing the same class to implement both Supergene and supergeneValidator.

setFromPersistent

public void setFromPersistent(java.lang.String a_from)
Set a persistend string representation (if needed) for this validator. The method name is different allowing the same class to implement both Supergene and supergeneValidator. The default implementation does nothing.

Specified by:
setFromPersistent in interface SupergeneValidator

getInternalValue

public 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:
not needed for abstract supergene

JGAP