org.jgap.supergenes
Class abstractSupergene

java.lang.Object
  extended byorg.jgap.supergenes.abstractSupergene
All Implemented Interfaces:
java.lang.Comparable, Gene, java.io.Serializable, Supergene, supergeneValidator
Direct Known Subclasses:
nonValidatingSupergene

public abstract class abstractSupergene
extends java.lang.Object
implements Supergene, supergeneValidator, java.io.Serializable

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
Author:
Audrius Meskauskas
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.
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 interface org.jgap.Gene
PERSISTENT_FIELD_DELIMITER
 
Constructor Summary
abstractSupergene()
          Always provide the parameterless constructor for the derived class.
abstractSupergene(Gene[] a_genes)
          Constructs abstract supergene with the given gene list.
 
Method Summary
 void addGene(Gene g)
          Append a new gene to the gene array.
 void applyMutation(int 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.
 boolean equals(java.lang.Object a_gene)
          Calls equals() for each pair of genes.
 java.lang.Object getAllele()
          Retrieves the allele value represented by this Supergene.
 Gene getGene(int a_index)
          Returns the Gene at the given index (locus) within the Chromosome.
 Gene[] getGenes()
          Get the array of genes - components of this supergene.
 java.lang.String getPersistent()
          Get a persistent string representation (if needed) of this validator. The name is different allowing the same class to implement both Supergene and supergeneValidator. The default implementation returns an empty string.
 java.lang.String getPersistentRepresentation()
          Retrieves a string representation of the value of this Supergene instance, using calls to the Supergene components.
 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.
 Gene newGene()
          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. The name is different allowing the same class to implement both Supergene and supergeneValidator. The default implementation does nothing.
 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.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 

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


MAX_IMMUTABLE_GENES

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

Constructor Detail

abstractSupergene

public abstractSupergene(Gene[] a_genes)
Constructs abstract supergene with the given gene list.

Parameters:
a_genes - array of genes for this Supergene

abstractSupergene

public abstractSupergene()
Always provide the parameterless constructor for the derived class. This is required to create a new instance of supergene and should be used inside newGene only. The parameterless constructor need not (and cannot) assign the private genes array.

Method Detail

getGenes

public Gene[] getGenes()
Get the array of genes - components of this supergene. The supergene components may be supergenes itself.

Specified by:
getGenes in interface Supergene

getGene

public final Gene getGene(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:
getGene in interface Supergene
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.
See Also:
in the interface header. *

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

newGene

public Gene newGene()
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:
newGene in interface Gene
Returns:
A new Gene instance of the same type and with the same setup as this concrete 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 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 [0] of the gene, indexed by index.

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).
See Also:
org.jgap.supergenes.abstractSupergene.isValid()

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.
Throws:
an - error if unable to get a valid random instance in the number of loops, defined by MAX_RETRIES.

setAllele

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

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

getAllele

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

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

getPersistentRepresentation

public java.lang.String getPersistentRepresentation()
                                             throws java.lang.UnsupportedOperationException
Retrieves a string representation of the value of this Supergene instance, using calls to the Supergene components. Supports other (nested) supergenes in this supergene.

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.

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
Parameters:
a_representation - the string representation retrieved from a prior call to the getPersistentRepresentation() method.
Throws:
UnsupportedRepresentationException
Since:
2.0

cleanup

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

Specified by:
cleanup in interface Gene

toString

public java.lang.String toString()
Specified by:
toString in interface Gene
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
Returns:
the size of the gene, i.e the number of atomic elements. Always 1 for numbers

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


hashCode

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


addGene

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


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()
Get a persistent string representation (if needed) of this validator. The name is different allowing the same class to implement both Supergene and supergeneValidator. The default implementation returns an empty string.

Specified by:
getPersistent in interface supergeneValidator

setFromPersistent

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

Specified by:
setFromPersistent in interface supergeneValidator