JGAP

org.jgap
Class BaseGene

java.lang.Object
  extended by org.jgap.BaseGene
All Implemented Interfaces:
java.io.Serializable, java.lang.Comparable, Gene, IBusinessKey, IUniqueKey
Direct Known Subclasses:
AbstractSupergene, BooleanGene, CompositeGene, FixedBinaryGene, MapGene, NumberGene, SetGene, StringGene

public abstract class BaseGene
extends java.lang.Object
implements Gene, IBusinessKey

Abstract base class for all genes. Provides default implementations.

Since:
2.2
See Also:
Serialized Form

Field Summary
static double DELTA
          Delta, useful for comparing doubles and floats.
static java.lang.String S_APPLICATION_DATA
          Constants for toString()
 
Fields inherited from interface org.jgap.Gene
PERSISTENT_FIELD_DELIMITER
 
Constructor Summary
BaseGene(Configuration a_configuration)
           
 
Method Summary
 void cleanup()
          Executed by the genetic engine when this Gene instance is no longer needed and should perform any necessary resource cleanup.
protected  int compareApplicationData(java.lang.Object a_appdata1, java.lang.Object a_appdata2)
           
protected  java.lang.String decode(java.lang.String a_string)
           
protected  java.lang.String encode(java.lang.String a_string)
           
 boolean equals(java.lang.Object a_other)
          Compares this Gene with the given object and returns true if the other object is a Gene of the same type and has the same value (allele) as this Gene.
 java.lang.Object getAllele()
          Retrieves the allele value represented by this Gene.
 java.lang.Object getApplicationData()
          Retrieves the application-specific data that is attached to this Gene.
 java.lang.String getBusinessKey()
           
 Configuration getConfiguration()
           
 IGeneConstraintChecker getConstraintChecker()
           
 double getEnergy()
           
protected abstract  java.lang.Object getInternalValue()
          Each Gene implementation holds its own m_value object keeping the allele value.
 java.lang.String getUniqueID()
           
 java.lang.String getUniqueIDTemplate(int a_index)
           
 int hashCode()
          Retrieves the hash code value for a Gene.
 boolean isCompareApplicationData()
           
 Gene newGene()
          Provides implementation-independent means for creating new Gene instances.
protected abstract  Gene newGeneInternal()
           
 void setApplicationData(java.lang.Object a_newData)
          This sets the application-specific data that is attached to this Gene.
 void setCompareApplicationData(boolean a_doCompare)
          Should we also consider the application data when comparing?
 void setConstraintChecker(IGeneConstraintChecker a_constraintChecker)
          Sets the constraint checker to be used for this gene whenever method setAllele(Object) is called.
 void setEnergy(double a_energy)
          Sets the energy of the gene
 void setUniqueIDTemplate(java.lang.String a_templateID, int a_index)
          A template is a gene that is the logical predecessor of the current gene.
 int size()
           
 java.lang.String toString()
          Retrieves a string representation of this Gene's value that may be useful for display purposes.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.jgap.Gene
applyMutation, getPersistentRepresentation, setAllele, setToRandomValue, setValueFromPersistentRepresentation
 
Methods inherited from interface java.lang.Comparable
compareTo
 

Field Detail

S_APPLICATION_DATA

public static final java.lang.String S_APPLICATION_DATA
Constants for toString()

See Also:
Constant Field Values

DELTA

public static final double DELTA
Delta, useful for comparing doubles and floats.

See Also:
Constant Field Values
Constructor Detail

BaseGene

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

getAllele

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

Specified by:
getAllele in interface Gene
Returns:
the allele value of this Gene
Since:
1.0

hashCode

public int hashCode()
Retrieves the hash code value for a Gene. Override if another hashCode() implementation is necessary or more appropriate than this default implementation.

Overrides:
hashCode in class java.lang.Object
Returns:
this Gene's hash code
Since:
1.0

cleanup

public void cleanup()
Executed by the genetic engine when this Gene instance is no longer needed and should perform any necessary resource cleanup. If you need a special cleanup, override this method.

Specified by:
cleanup in interface Gene
Since:
1.0

toString

public java.lang.String toString()
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 java.lang.Object
Returns:
a string representation of this Gene's value
Since:
1.0

size

public int size()
Specified by:
size in interface Gene
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
Since:
1.0

equals

public boolean equals(java.lang.Object a_other)
Compares this Gene with the given object and returns true if the other object is a Gene of the same type and has the same value (allele) as this Gene. Otherwise it returns false.

Overrides:
equals in class java.lang.Object
Parameters:
a_other - the object to compare to this Gene for equality
Returns:
true if this Gene is equal to the given object, false otherwise
Since:
1.1

getInternalValue

protected abstract java.lang.Object getInternalValue()
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)

Returns:
the m_value object
Since:
2.2

getEnergy

public double getEnergy()
Specified by:
getEnergy in interface Gene
Returns:
energy of the gene
Since:
2.3

setEnergy

public void setEnergy(double a_energy)
Sets the energy of the gene

Specified by:
setEnergy in interface Gene
Parameters:
a_energy - the energy to set
Since:
2.3

setApplicationData

public void setApplicationData(java.lang.Object a_newData)
This sets the application-specific data that is attached to this Gene. Attaching application-specific data may be useful for some applications when it comes time to distinguish a Gene from another. JGAP ignores this data functionally.

Specified by:
setApplicationData in interface Gene
Parameters:
a_newData - the new application-specific data to attach to this Gene
Since:
2.4

getApplicationData

public java.lang.Object getApplicationData()
Retrieves the application-specific data that is attached to this Gene. Attaching application-specific data may be useful for some applications when it comes time to distinguish a Gene from another. JGAP ignores this data functionally.

Specified by:
getApplicationData in interface Gene
Returns:
the application-specific data previously attached to this Gene, or null if there is no data attached
Since:
2.4

setCompareApplicationData

public void setCompareApplicationData(boolean a_doCompare)
Should we also consider the application data when comparing? Default is "false" as "true" means a Gene is losing its identity when application data is set differently!

Specified by:
setCompareApplicationData in interface Gene
Parameters:
a_doCompare - true: consider application data in method compareTo
Since:
2.4

isCompareApplicationData

public boolean isCompareApplicationData()
Specified by:
isCompareApplicationData in interface Gene

compareApplicationData

protected int compareApplicationData(java.lang.Object a_appdata1,
                                     java.lang.Object a_appdata2)

setConstraintChecker

public void setConstraintChecker(IGeneConstraintChecker a_constraintChecker)
Sets the constraint checker to be used for this gene whenever method setAllele(Object) is called.

Specified by:
setConstraintChecker in interface Gene
Parameters:
a_constraintChecker - the constraint checker to be set
Since:
2.5 (moved from CompositeGene, where it was since 2.0)

getConstraintChecker

public IGeneConstraintChecker getConstraintChecker()
Returns:
IGeneConstraintChecker the constraint checker to be used whenever method setAllele(Object) is called.
Since:
2.5 (moved from CompositeGene, where it was since 2.0)

newGene

public Gene newGene()
Provides implementation-independent means for creating new Gene instances. The new instance that is created and returned should be setup with any implementation-dependent configuration that this Gene instance is setup with (aside from the actual value, of course). For example, if this Gene were setup with bounds on its value, then the Gene instance returned from this method should also be setup with those same bounds. This is important, as the JGAP core will invoke this method on each Gene in the sample Chromosome in order to create each new Gene in the same respective gene position for a new Chromosome.

Specified by:
newGene in interface Gene
Returns:
a new Gene instance of the same type and with the same setup as this concrete Gene
Since:
2.6 (since 1.0 in IntegerGene)

newGeneInternal

protected abstract Gene newGeneInternal()

getConfiguration

public Configuration getConfiguration()
Specified by:
getConfiguration in interface Gene
Returns:
the configuration set
Since:
3.0

getBusinessKey

public java.lang.String getBusinessKey()
Specified by:
getBusinessKey in interface IBusinessKey
Returns:
business key

encode

protected java.lang.String encode(java.lang.String a_string)

decode

protected java.lang.String decode(java.lang.String a_string)

getUniqueID

public java.lang.String getUniqueID()
Specified by:
getUniqueID in interface IUniqueKey
Returns:
unique ID of the gene, which allows to distinct this instance from others, in the best case worldwide
Since:
3.5

setUniqueIDTemplate

public void setUniqueIDTemplate(java.lang.String a_templateID,
                                int a_index)
A template is a gene that is the logical predecessor of the current gene. A template can occur in mutation or crossing over. In the latter case can be at least two template genes. This is why in this setter method the parameter a_index exists.

Specified by:
setUniqueIDTemplate in interface IUniqueKey
Parameters:
a_templateID - the unique ID of the template
a_index - the index of the template, e.g. in crossing over for the second candidate gene this is 2
Since:
3.5

getUniqueIDTemplate

public java.lang.String getUniqueIDTemplate(int a_index)
Specified by:
getUniqueIDTemplate in interface IUniqueKey
Parameters:
a_index - the index of the template to retrieve the key for
Returns:
String
Since:
3.5

JGAP