JGAP

org.jgap.impl
Class SetGene

java.lang.Object
  extended by org.jgap.BaseGene
      extended by org.jgap.impl.SetGene
All Implemented Interfaces:
java.io.Serializable, java.lang.Comparable, Gene, IBusinessKey, IPersistentRepresentation

public class SetGene
extends BaseGene
implements IPersistentRepresentation

ATTENTION: This class is preliminary and subject of future adaptations! Use with care or wait for a more mature version we are working on.

Creates a gene instance in which individual alleles have both a label (key) and a value with a distinct meaning. For example, IntegerGene only allows for values having a continuous range, and does not have a function where it is possible to specify setValue...

Since:
2.4
See Also:
Serialized Form

Field Summary
 
Fields inherited from class org.jgap.BaseGene
DELTA, S_APPLICATION_DATA
 
Fields inherited from interface org.jgap.Gene
PERSISTENT_FIELD_DELIMITER
 
Constructor Summary
SetGene()
          Default constructor.
SetGene(Configuration a_conf)
           
 
Method Summary
 void addAllele(java.lang.Object a_value)
          Adds a potential allele value to the collection.
 void addAlleles(java.util.Collection a_alleles)
          Add a set of potential allele values to the collection
 void applyMutation(int a_index, double a_percentage)
          See interface Gene for description of applyMutation.
 int compareTo(java.lang.Object other)
          Compares this NumberGene with the specified object (which must also be a NumberGene) for order, which is determined by the number value of this Gene compared to the one provided for comparison.
protected  java.lang.Object getInternalValue()
          Each Gene implementation holds its own m_value object keeping the allele value.
 java.lang.String getPersistentRepresentation()
          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.
 int hashCode()
          Modified hashCode() function to return different hashcodes for differently ordered genes in a chromosome
protected  Gene newGeneInternal()
           
 void removeAlleles(java.lang.Object a_key)
          Removes a potential allele or set of alleles from the collection.
 void setAllele(java.lang.Object a_newValue)
          Sets the value (allele) of this Gene to the new given value.
 void setToRandomValue(RandomGenerator a_numberGenerator)
          Sets the allele value to be a random value using a defined random number generator.
 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.
 
Methods inherited from class org.jgap.BaseGene
cleanup, compareApplicationData, decode, encode, equals, getAllele, getApplicationData, getBusinessKey, getConfiguration, getConstraintChecker, getEnergy, isCompareApplicationData, newGene, setApplicationData, setCompareApplicationData, setConstraintChecker, setEnergy, size, toString
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

SetGene

public SetGene()
        throws InvalidConfigurationException
Default constructor.

Attention: The configuration used is the one set with the static method Genotype.setConfiguration.

Throws:
InvalidConfigurationException

SetGene

public SetGene(Configuration a_conf)
        throws InvalidConfigurationException
Parameters:
a_conf - the configuration to use
Throws:
InvalidConfigurationException
Since:
3.0
Method Detail

newGeneInternal

protected Gene newGeneInternal()
Specified by:
newGeneInternal in class BaseGene

addAllele

public void addAllele(java.lang.Object a_value)
Adds a potential allele value to the collection.

Parameters:
a_value - the Integer value to be added

addAlleles

public void addAlleles(java.util.Collection a_alleles)
Add a set of potential allele values to the collection

Parameters:
a_alleles - the set of alleles to be added

removeAlleles

public void removeAlleles(java.lang.Object a_key)
Removes a potential allele or set of alleles from the collection.

Parameters:
a_key - the unique value(s) of the object(s) to be removed

setToRandomValue

public void setToRandomValue(RandomGenerator a_numberGenerator)
Sets the allele value to be a random value using a defined random number generator.

Specified by:
setToRandomValue in interface Gene
Parameters:
a_numberGenerator - RandomGenerator

applyMutation

public void applyMutation(int a_index,
                          double a_percentage)
See interface Gene for description of applyMutation. For this kind of gene, providing an index and a magnitude have no significance because the individual allele forms are independent of one another. In mutating, they can only cange from one form to another. It may be possible to weight the likelihood of mutation to different forms, but that will not be implemented here.

Specified by:
applyMutation in interface Gene
Parameters:
a_index - ignored here
a_percentage - ignored here
Since:
2.4

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. 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:
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 - if this Gene implementation does not support the given string representation
Since:
1.0

getPersistentRepresentation

public java.lang.String getPersistentRepresentation()
                                             throws java.lang.UnsupportedOperationException
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. 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:
getPersistentRepresentation in interface Gene
Specified by:
getPersistentRepresentation in interface IPersistentRepresentation
Returns:
a string representation of this Gene's current state
Throws:
java.lang.UnsupportedOperationException - to indicate that no implementation is provided for this method
Since:
1.0

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 an instance of current type (e.g. Integer).

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

compareTo

public int compareTo(java.lang.Object other)
Compares this NumberGene with the specified object (which must also be a NumberGene) for order, which is determined by the number value of this Gene compared to the one provided for comparison.

Specified by:
compareTo in interface java.lang.Comparable
Parameters:
other - the NumberGene to be compared to this NumberGene
Returns:
a negative integer, zero, or a positive integer 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 NumberGene
Since:
2.4

getInternalValue

protected 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:
the internal value of the gene
Since:
2.4

hashCode

public int hashCode()
Modified hashCode() function to return different hashcodes for differently ordered genes in a chromosome

Overrides:
hashCode in class BaseGene
Returns:
-67 if no allele set, otherwise value return by BaseGene.hashCode()
Since:
2.4

JGAP