org.jgap.impl
Class CompositeGene
- java.lang.Object
-
- org.jgap.BaseGene
-
- org.jgap.impl.CompositeGene
-
- All Implemented Interfaces:
- java.io.Serializable, java.lang.Comparable, Gene, IBusinessKey, ICompositeGene, IPersistentRepresentation, IUniqueKey
public class CompositeGene extends BaseGene implements ICompositeGene, IPersistentRepresentation
Ordered container for multiple genes Has the same interface as a single gene and could be used accordingly. Use the addGene(Gene) method to add single genes (possibly CompositeGenes) after construction, an empty CompositeGene without genes makes no sense. Beware that there are two equalities defined for a CompositeGene in respect to its contained genes: a) Two genes are (only) equal if they are identical b) Two genes are (seen as) equal if their equals method returns true This influences several methods such as addGene. Notice that it is safer to use addGene(a_gene, false) than addGene(a_gene, true) because the second variant only allows to add genes not seen as equal to already added genes in respect to their equals function. But: the equals function returns true for two different DoubleGenes (e.g.) just after their creation. If no specific (and hopefully different) allele is set for these DoubleGenes they are seen as equal!- Since:
- 1.1
- 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.-
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 CompositeGene()Default constructor.CompositeGene(Configuration a_config)CompositeGene(Configuration a_config, Gene a_geneTypeAllowed)Allows to specify which Gene implementation is allowed to be added to the CompositeGene.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method and Description voidaddGene(Gene a_gene)Adds a gene to the CompositeGene.voidaddGene(Gene a_gene, boolean a_strict)Adds a gene to the CompositeGene's container.voidapplyMutation(int a_index, double a_percentage)Don't use this method, is makes no sense here.voidcleanup()Executed by the genetic engine when this Gene instance is no longer needed and should perform any necessary resource cleanup.intcompareTo(java.lang.Object a_other)Compares this CompositeGene with the specified object for order.booleancontainsGeneByIdentity(Gene gene)Checks whether a specific gene is already contained.GenegeneAt(int a_index)java.lang.ObjectgetAllele()Retrieves the value represented by this Gene.java.lang.StringgetBusinessKey()java.util.List<Gene>getGenes()GenegetGeneTypeAllowed()java.lang.StringgetPersistentRepresentation()See interface Gene for description.inthashCode()Retrieves the hash code value for this Gene.booleanisEmpty()booleanremoveGene(Gene a_gene)Removes the given gene from the collection of genes.booleanremoveGeneByIdentity(Gene a_gene)Removes the given gene from the collection of genes.voidsetAllele(java.lang.Object a_newValue)Sets the value of the contained Genes to the new given value.voidsetToRandomValue(RandomGenerator a_numberGenerator)See interface Gene for description.voidsetValueFromPersistentRepresentation(java.lang.String a_representation)See interface Gene for description.intsize()java.lang.StringtoString()Retrieves a string representation of this CompositeGene's value that may be useful for display purposes.-
Methods inherited from class org.jgap.BaseGene
equals, getApplicationData, 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. '*' does not work properly with URLEncoder!- 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
-
-
Constructor Detail
-
CompositeGene
public CompositeGene() throws InvalidConfigurationExceptionDefault constructor.Attention: The configuration used is the one set with the static method Genotype.setConfiguration.
- Throws:
InvalidConfigurationException- Since:
- 1.1
-
CompositeGene
public CompositeGene(Configuration a_config) throws InvalidConfigurationException
- Parameters:
a_config- the configuration to use- Throws:
InvalidConfigurationException- Since:
- 3.0
-
CompositeGene
public CompositeGene(Configuration a_config, Gene a_geneTypeAllowed) throws InvalidConfigurationException
Allows to specify which Gene implementation is allowed to be added to the CompositeGene.- Parameters:
a_config- the configuration to usea_geneTypeAllowed- the class of Genes to be allowed to be added to the CompositeGene- Throws:
InvalidConfigurationException- Since:
- 2.0
-
-
Method Detail
-
addGene
public void addGene(Gene a_gene)
Adds a gene to the CompositeGene.- Specified by:
addGenein interfaceICompositeGene- Parameters:
a_gene- the gene to add
-
getGeneTypeAllowed
public Gene getGeneTypeAllowed()
- Returns:
- the gene type allowed, or null if any type allowed
- Since:
- 2.4
-
addGene
public void addGene(Gene a_gene, boolean a_strict)
Adds a gene to the CompositeGene's container. See comments in class header for additional details about equality (concerning "strict" param.)- Parameters:
a_gene- the gene to be addeda_strict- false: add the given gene except the gene itself already is contained within the CompositeGene's container. true: add the gene if there is no other gene being equal to the given gene in request to the Gene's equals method- Since:
- 1.1
-
removeGeneByIdentity
public boolean removeGeneByIdentity(Gene a_gene)
Removes the given gene from the collection of genes. The gene is only removed if an object of the same identity is contained. The equals method is not used here intentionally.- Parameters:
a_gene- the gene to be removed- Returns:
- true: given gene found and removed
- Since:
- 1.1
-
removeGene
public boolean removeGene(Gene a_gene)
Removes the given gene from the collection of genes. The gene is removed if another gene exists that is equal to the given gene in respect to the equals method of the gene.- Parameters:
a_gene- the gene to be removed- Returns:
- true: given gene found and removed
- Since:
- 1.1
-
cleanup
public void cleanup()
Executed by the genetic engine when this Gene instance is no longer needed and should perform any necessary resource cleanup.
-
setToRandomValue
public void setToRandomValue(RandomGenerator a_numberGenerator)
See interface Gene for description.- 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 UnsupportedRepresentationExceptionSee interface Gene for description.- 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:
- 1.1
-
getPersistentRepresentation
public java.lang.String getPersistentRepresentation() throws java.lang.UnsupportedOperationExceptionSee interface Gene for description.- Specified by:
getPersistentRepresentationin interfaceGene- Specified by:
getPersistentRepresentationin interfaceIPersistentRepresentation- Returns:
- string representation of this Gene's current state
- Throws:
java.lang.UnsupportedOperationException- Since:
- 1.1
-
getAllele
public java.lang.Object getAllele()
Retrieves the value represented by this Gene. All values returned by this class will be Vector instances. Each element of the Vector represents the allele of the corresponding gene in the CompositeGene's container
-
setAllele
public void setAllele(java.lang.Object a_newValue)
Sets the value of the contained Genes to the new given value. This class expects the value to be of a Vector type. Each element of the Vector must conform with the type of the gene in the CompositeGene's container at the corresponding position.
-
compareTo
public int compareTo(java.lang.Object a_other)
Compares this CompositeGene with the specified object for order. A false value is considered to be less than a true value. A null value is considered to be less than any non-null value.- Specified by:
compareToin interfacejava.lang.Comparable- Parameters:
a_other- the CompositeGene to be compared- Returns:
- a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object
- Throws:
java.lang.ClassCastException- if the specified object's type prevents it from being compared to this CompositeGene- Since:
- 1.1
-
toString
public java.lang.String toString()
Retrieves a string representation of this CompositeGene's value that may be useful for display purposes.
-
isEmpty
public boolean isEmpty()
- Returns:
- true: no genes contained, false otherwise
- Since:
- 1.1
-
geneAt
public Gene geneAt(int a_index)
- Specified by:
geneAtin interfaceICompositeGene- Parameters:
a_index- index to return the gene at- Returns:
- the gene at the given index
- Since:
- 1.1
-
getGenes
public java.util.List<Gene> getGenes()
- Returns:
- list of genes contained in the CompositeGene
- Since:
- 3.2.2
-
size
public int size()
-
containsGeneByIdentity
public boolean containsGeneByIdentity(Gene gene)
Checks whether a specific gene is already contained. The determination will be done by checking for identity and not using the equal method!- Parameters:
gene- the gene under test- Returns:
- true: the given gene object is contained
- Since:
- 1.1
-
applyMutation
public void applyMutation(int a_index, double a_percentage)Don't use this method, is makes no sense here. It is just there to satisfy the Gene interface. Instead, loop over all contained genes and call their applyMutation method.- Specified by:
applyMutationin interfaceGene- Parameters:
a_index- does not matter herea_percentage- does not matter here- Since:
- 1.1
-
hashCode
public int hashCode()
Retrieves the hash code value for this Gene.
-
getBusinessKey
public java.lang.String getBusinessKey()
- Specified by:
getBusinessKeyin interfaceIBusinessKey- Overrides:
getBusinessKeyin classBaseGene- Returns:
- business key
-
-
DMelt 3.0 © DataMelt by jWork.ORG