org.jgap.supergenes
Class AbstractSupergene
- java.lang.Object
-
- org.jgap.BaseGene
-
- org.jgap.supergenes.AbstractSupergene
-
- All Implemented Interfaces:
- java.io.Serializable, java.lang.Comparable, Gene, IBusinessKey, ICompositeGene, IPersistentRepresentation, IUniqueKey, Supergene, SupergeneValidator
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 overridingisValid (Gene [], Supergene)method. For more complex cases, you may need to set your ownValidator.- Since:
- 2.0
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field and Description static java.lang.StringGENE_DELIMITERThis field separates gene class name from the gene persistent representation string.static java.lang.StringGENE_DELIMITER_CLOSINGRepresents the closing delimiter that is used to separate genes in the persistent representation of CompositeGene instances.static java.lang.StringGENE_DELIMITER_HEADINGRepresents the heading delimiter that is used to separate genes in the persistent representation of CompositeGene instances.static intMAX_IMMUTABLE_GENESMaximal number of notes about immutable genes per single gene positionstatic intMAX_RETRIESMaximal 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
Constructors Constructor and Description 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
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method and Description voidaddGene(Gene a_gene)Append a new gene to the gene array.voidapplyMutation(int a_index, double a_percentage)Applies a mutation of a given intensity (percentage) onto the gene at the given index.voidcleanup()Calls cleanup() for each subgene.intcompareTo(java.lang.Object o)Calls compareTo() for all subgenes.booleanequals(java.lang.Object a_gene)Calls equals() for each pair of genes.GenegeneAt(int a_index)Returns the Gene at the given index (locus) within the Chromosome.java.lang.ObjectgetAllele()Retrieves the allele value represented by this Supergene.Gene[]getGenes()Get the array of genes - components of this supergene.java.lang.ObjectgetInternalValue()Each Gene implementation holds its own m_value object keeping the allele value.java.lang.StringgetPersistent()The default implementation returns an empty string.java.lang.StringgetPersistentRepresentation()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.SupergeneValidatorgetValidator()Gets an object, responsible for deciding if the Supergene allele combination is valid.inthashCode()Returns sum of hashCode() of the genes-components.booleanisValid()Test the allele combination of this supergene for validity.booleanisValid(Gene[] a_case, Supergene a_forSupergene)Test the given gene list for validity.static voidreset()Discards all internal caches, ensuring correct repetetive tests of performance.voidsetAllele(java.lang.Object a_superAllele)Sets the allele.voidsetFromPersistent(java.lang.String a_from)Set a persistend string representation (if needed) for this validator.voidsetToRandomValue(RandomGenerator a_numberGenerator)Sets the value of this Gene to a random legal value for the implementation.voidsetValidator(SupergeneValidator a_validator)Sets an object, responsible for deciding if the Supergene allele combination is valid.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()Returns the number of the genes-components of this supergene.java.lang.StringtoString()Retrieves a string representation of this Gene's value that may be useful for display purposes.-
Methods inherited from class org.jgap.BaseGene
getApplicationData, getBusinessKey, getConfiguration, getConstraintChecker, getEnergy, getUniqueID, getUniqueIDTemplate, isCompareApplicationData, newGene, setApplicationData, setCompareApplicationData, setConstraintChecker, setEnergy, setUniqueIDTemplate
-
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
-
-
Constructor Detail
-
AbstractSupergene
public AbstractSupergene() throws InvalidConfigurationExceptionDefault 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 usea_genes- array of genes for this Supergene- Throws:
InvalidConfigurationException
-
-
Method Detail
-
getGenes
public Gene[] getGenes()
Description copied from interface:SupergeneGet 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:
geneAtin interfaceICompositeGene- Specified by:
geneAtin interfaceSupergene- 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.
-
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:
isValidin interfaceSupergeneValidator- Parameters:
a_case- ignored herea_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
-
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:
applyMutationin interfaceGene- Parameters:
a_index- index of atomic element, between 0 and size()-1a_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:
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
-
setAllele
public void setAllele(java.lang.Object a_superAllele)
Sets the allele.
-
getAllele
public java.lang.Object getAllele()
Retrieves the allele value represented by this Supergene.
-
getPersistentRepresentation
public java.lang.String getPersistentRepresentation() throws java.lang.UnsupportedOperationExceptionDescription copied from interface:GeneRetrieves 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:
getPersistentRepresentationin interfaceGene- Specified by:
getPersistentRepresentationin interfaceIPersistentRepresentation- 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 UnsupportedRepresentationExceptionSets 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:
setValueFromPersistentRepresentationin interfaceGene- Specified by:
setValueFromPersistentRepresentationin interfaceIPersistentRepresentation- 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.
-
toString
public java.lang.String toString()
Description copied from class:BaseGeneRetrieves a string representation of this Gene's value that may be useful for display purposes.
-
size
public int size()
Returns the number of the genes-components of this supergene.
-
compareTo
public int compareTo(java.lang.Object o)
Calls compareTo() for all subgenes. The passed parameter must be an instance of AbstractSupergene.- Specified by:
compareToin interfacejava.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 a_gene)
Append a new gene to the gene array.- Specified by:
addGenein interfaceICompositeGene- 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 methodisValid (Gene [] )is called.- Specified by:
setValidatorin interfaceSupergene
-
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:
getValidatorin interfaceSupergene
-
getPersistent
public java.lang.String getPersistent()
The default implementation returns an empty string.- Specified by:
getPersistentin interfaceSupergeneValidator- 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:
setFromPersistentin interfaceSupergeneValidator
-
getInternalValue
public java.lang.Object getInternalValue()
Description copied from class:BaseGeneEach Gene implementation holds its own m_value object keeping the allele value. In your Gene implementation, just return it with this method (seeBooleanGenefor example)- Returns:
- not needed for abstract supergene
-
-
DMelt 3.0 © DataMelt by jWork.ORG