org.jgap
Class Configuration

java.lang.Object
  extended byorg.jgap.Configuration
Direct Known Subclasses:
DefaultConfiguration

public class Configuration
extends java.lang.Object

The Configuration class represents the current configuration of plugins and flags necessary to execute the genetic algorithm (such as fitness function, natural selector, genetic operators, and so on).

Note that, while during setup, the settings, flags, and other values may be set multiple times. But once the lockSettings() method is invoked, they cannot be changed. The default behavior of the Genotype constructor is to invoke this method, meaning that once a Configuration object is passed to a Genotype, it cannot be subsequently modified. There is no mechanism for unlocking the settings once they are locked.

Not all configuration options are required. See the documentation for each of the respective mutator methods to determine whether it is required to provide a value for that setting, and what the setting will default to if not.

Since:
1.0
Author:
Neil Rotstan, Klaus Meffert

Constructor Summary
Configuration()
           
 
Method Summary
 void addGeneticOperator(GeneticOperator a_operatorToAdd)
          Adds a genetic operator for use in this algorithm.
 void addNaturalSelector(NaturalSelector a_selector, boolean processBeforeGeneticOperators)
          Adds a NaturalSelector to the ordered chain of registered NaturalSelector's.
 BulkFitnessFunction getBulkFitnessFunction()
          Retrieves the bulk fitness function previously setup in this Configuration object.
 ChromosomePool getChromosomePool()
          Retrieves the ChromosomePool instance, if any, that is associated with this configuration.
 int getChromosomeSize()
          Retrieves the chromosome size being used by this genetic algorithm.
 EventManager getEventManager()
          Retrieves the EventManager associated with this configuration.
 FitnessEvaluator getFitnessEvaluator()
           
 FitnessFunction getFitnessFunction()
          Retrieves the fitness function previously setup in this Configuration object.
 java.util.List getGeneticOperators()
          Retrieves the genetic operators setup in this Configuration instance.
 int getMinimumPopSizePercent()
           
 NaturalSelector getNaturalSelector()
          Deprecated. use getNaturalSelectors(true) or getNaturalSelectors(false) to obtain the relevant chain of NaturalSelector's and then call the chain's get(index) method
 NaturalSelector getNaturalSelector(boolean processBeforeGeneticOperators, int index)
           
 ChainOfSelectors getNaturalSelectors(boolean processBeforeGeneticOperators)
           
 int getNaturalSelectorsSize(boolean processBeforeGeneticOperators)
           
 int getPopulationSize()
          Retrieves the population size setup in this Configuration instance.
 RandomGenerator getRandomGenerator()
          Retrieves the random generator setup in this Configuration instance.
 Chromosome getSampleChromosome()
          Retrieves the sample Chromosome that contains the desired Gene setup for each respective gene position (locus).
 boolean isLocked()
          Retrieves the lock status of this object.
 void lockSettings()
          Locks all of the settings in this configuration object.
 void setBulkFitnessFunction(BulkFitnessFunction a_functionToSet)
          Sets the bulk fitness function to be used for this genetic algorithm.
 void setChromosomePool(ChromosomePool a_chromosomePoolToSet)
          Sets the ChromosomePool that is to be associated with this configuration.
 void setEventManager(EventManager a_eventManagerToSet)
          Sets the EventManager that is to be associated with this configuration.
 void setFitnessEvaluator(FitnessEvaluator a_fitnessEvaluator)
          Set the fitness evaluator (deciding if a given fitness value is better when it's higher or better when it's lower).
 void setFitnessFunction(FitnessFunction a_functionToSet)
          Sets the fitness function to be used for this genetic algorithm.
 void setMinimumPopSizePercent(int a_minimumSizeGuaranteedPercent)
           
 void setNaturalSelector(NaturalSelector a_selectorToSet)
          Deprecated. use addNaturalSelector(false) instead
 void setPopulationSize(int a_sizeOfPopulation)
          Sets the population size to be used for this genetic algorithm.
 void setRandomGenerator(RandomGenerator a_generatorToSet)
          Sets the random generator to be used for this genetic algorithm.
 void setSampleChromosome(Chromosome a_sampleChromosomeToSet)
          Sets the sample Chromosome that is to be used as a guide for the construction of other Chromosomes.
 void verifyStateIsValid()
          Tests the state of this Configuration object to make sure it's valid.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Configuration

public Configuration()
Since:
1.0
Method Detail

setFitnessFunction

public void setFitnessFunction(FitnessFunction a_functionToSet)
                        throws InvalidConfigurationException
Sets the fitness function to be used for this genetic algorithm. The fitness function is responsible for evaluating a given Chromosome and returning a positive integer that represents its worth as a candidate solution. These values are used as a guide by the natural to determine which Chromosome instances will be allowed to move on to the next round of evolution, and which will instead be eliminated.

Note that it is illegal to set both this fitness function and a bulk fitness function. Although one or the other must be set, the two are mutually exclusive.

Parameters:
a_functionToSet - The fitness function to be used.
Throws:
InvalidConfigurationException - if the fitness function is null, a bulk fitness function has already been set, or if this Configuration object is locked.
Since:
1.1

getFitnessFunction

public FitnessFunction getFitnessFunction()
Retrieves the fitness function previously setup in this Configuration object.

Returns:
The fitness function.
Since:
1.0

setBulkFitnessFunction

public void setBulkFitnessFunction(BulkFitnessFunction a_functionToSet)
                            throws InvalidConfigurationException
Sets the bulk fitness function to be used for this genetic algorithm. The bulk fitness function may be used to evaluate and assign fitness values to the entire group of candidate Chromosomes in a single batch. This can be useful in cases where it's difficult to assign fitness values to a Chromosome in isolation from the other candidate Chromosomes.

Note that it is illegal to set both a bulk fitness function and a normal fitness function. Although one or the other is required, the two are mutually exclusive.

Parameters:
a_functionToSet - The bulk fitness function to be used.
Throws:
InvalidConfigurationException - if the bulk fitness function is null, the normal fitness function has already been set, or if this Configuration object is locked.
Since:
1.0

getBulkFitnessFunction

public BulkFitnessFunction getBulkFitnessFunction()
Retrieves the bulk fitness function previously setup in this Configuration object.

Returns:
The bulk fitness function.
Since:
1.0

setSampleChromosome

public void setSampleChromosome(Chromosome a_sampleChromosomeToSet)
                         throws InvalidConfigurationException
Sets the sample Chromosome that is to be used as a guide for the construction of other Chromosomes. The Chromosome should be setup with each gene represented by the desired concrete Gene implementation for that gene position (locus). Anytime a new Chromosome is created, it will be constructed with the same Gene setup as that provided in this sample Chromosome.

Parameters:
a_sampleChromosomeToSet - The Chromosome to be used as the sample.
Throws:
InvalidConfigurationException - if the given Chromosome is null or this Configuration object is locked.
Since:
1.0

getSampleChromosome

public Chromosome getSampleChromosome()
Retrieves the sample Chromosome that contains the desired Gene setup for each respective gene position (locus).

Returns:
the sample Chromosome instance.
Since:
1.0

getChromosomeSize

public int getChromosomeSize()
Retrieves the chromosome size being used by this genetic algorithm. This value is set automatically when the sample Chromosome is provided.

Returns:
The chromosome size used in this Configuration.
Since:
1.0

setNaturalSelector

public void setNaturalSelector(NaturalSelector a_selectorToSet)
                        throws InvalidConfigurationException
Deprecated. use addNaturalSelector(false) instead

Sets the natural selector to be used for this genetic algorithm. The natural selector is responsible for actually selecting which Chromosome instances are allowed to move on to the next round of evolution (usually guided by the fitness values provided by the fitness function). This setting is required.

Parameters:
a_selectorToSet - The natural selector to be used.
Throws:
InvalidConfigurationException - if the natural selector is null or this Configuration object is locked.
Since:
1.0

getNaturalSelector

public NaturalSelector getNaturalSelector()
Deprecated. use getNaturalSelectors(true) or getNaturalSelectors(false) to obtain the relevant chain of NaturalSelector's and then call the chain's get(index) method

Retrieves the natural selector setup in this Configuration instance.

Returns:
The natural selector.
Since:
1.0

getNaturalSelector

public NaturalSelector getNaturalSelector(boolean processBeforeGeneticOperators,
                                          int index)
Parameters:
processBeforeGeneticOperators - boolean
index - int
Returns:
NaturalSelector
Since:
1.1

getNaturalSelectors

public ChainOfSelectors getNaturalSelectors(boolean processBeforeGeneticOperators)
Parameters:
processBeforeGeneticOperators - boolean
Returns:
ChainOfSelectors
Since:
1.1

getNaturalSelectorsSize

public int getNaturalSelectorsSize(boolean processBeforeGeneticOperators)
Parameters:
processBeforeGeneticOperators - boolean
Returns:
int
Since:
1.1

setRandomGenerator

public void setRandomGenerator(RandomGenerator a_generatorToSet)
                        throws InvalidConfigurationException
Sets the random generator to be used for this genetic algorithm. The random generator is responsible for generating random numbers, which are used throughout the process of genetic evolution and selection. This setting is required.

Parameters:
a_generatorToSet - The random generator to be used.
Throws:
InvalidConfigurationException - if the random generator is null or this object is locked.
Since:
1.0

getRandomGenerator

public RandomGenerator getRandomGenerator()
Retrieves the random generator setup in this Configuration instance.

Returns:
The random generator.
Since:
1.0

addGeneticOperator

public void addGeneticOperator(GeneticOperator a_operatorToAdd)
                        throws InvalidConfigurationException
Adds a genetic operator for use in this algorithm. Genetic operators represent evolutionary steps that, when combined, make up the evolutionary process. Examples of genetic operators are reproduction, crossover, and mutation. During the evolution process, all of the genetic operators added via this method are invoked in the order they were added. At least one genetic operator must be provided.

Parameters:
a_operatorToAdd - The genetic operator to add.
Throws:
InvalidConfigurationException - if the genetic operator is null of if this Configuration object is locked.
Since:
1.0

getGeneticOperators

public java.util.List getGeneticOperators()
Retrieves the genetic operators setup in this Configuration instance. Note that once this Configuration instance is locked, a new, immutable list of operators is used and any lists previously retrieved with this method will no longer reflect the actual list in use.

Returns:
The list of genetic operators.
Since:
1.0

setPopulationSize

public void setPopulationSize(int a_sizeOfPopulation)
                       throws InvalidConfigurationException
Sets the population size to be used for this genetic algorithm. The population size is a fixed value that represents the number of Chromosomes contained within the Genotype (population). This setting is required.

Parameters:
a_sizeOfPopulation - The population size to be used.
Throws:
InvalidConfigurationException - if the population size is not positive or this object is locked.
Since:
1.0

getPopulationSize

public int getPopulationSize()
Retrieves the population size setup in this Configuration instance.

Returns:
The population size.

setEventManager

public void setEventManager(EventManager a_eventManagerToSet)
                     throws InvalidConfigurationException
Sets the EventManager that is to be associated with this configuration. The EventManager is responsible for the management of event subscribers and event notifications.

Parameters:
a_eventManagerToSet - the EventManager instance to use in this configuration.
Throws:
InvalidConfigurationException - if the event manager is null or this Configuration object is locked.
Since:
1.0

getEventManager

public EventManager getEventManager()
Retrieves the EventManager associated with this configuration. The EventManager is responsible for the management of event subscribers and event notifications.

Returns:
the actively configured EventManager instance.

setChromosomePool

public void setChromosomePool(ChromosomePool a_chromosomePoolToSet)
                       throws InvalidConfigurationException
Sets the ChromosomePool that is to be associated with this configuration. The ChromosomePool is used to pool discarded Chromosome instances so that they may be recycled later, thereby memory and the time to construct them from scratch. The presence of a ChromosomePool is optional. If none exists, then a new Chromosome will be constructed each time one is needed.

Parameters:
a_chromosomePoolToSet - The ChromosomePool instance to use.
Throws:
InvalidConfigurationException - if this object is locked.
Since:
1.0

getChromosomePool

public ChromosomePool getChromosomePool()
Retrieves the ChromosomePool instance, if any, that is associated with this configuration. The ChromosomePool is used to pool discarded Chromosome instances so that they may be recycled later, thereby saving memory and the time to construct them from scratch. The presence of a ChromosomePool instance is optional. If none exists, then new Chromosomes should be constructed each time one is needed.

Returns:
The ChromosomePool instance associated this configuration, or null if none has been provided.
Since:
1.0

lockSettings

public void lockSettings()
                  throws InvalidConfigurationException
Locks all of the settings in this configuration object. Once this method is successfully invoked, none of the settings may be changed. There is no way to unlock this object once it is locked.

Prior to returning successfully, this method will first invoke the verifyStateIsValid() method to make sure that any required configuration options have been properly set. If it detects a problem, it will throw an InvalidConfigurationException and leave the object unlocked.

It's possible to test whether is object is locked through the isLocked() method.

It is ok to lock an object more than once. In that case, this method does nothing and simply returns.

Throws:
InvalidConfigurationException - if this Configuration object is in an invalid state at the time of invocation.
Since:
1.0

isLocked

public boolean isLocked()
Retrieves the lock status of this object.

Returns:
true if this object has been locked by a previous successful call to the lockSettings() method, false otherwise.
Since:
1.0

verifyStateIsValid

public void verifyStateIsValid()
                        throws InvalidConfigurationException
Tests the state of this Configuration object to make sure it's valid. This generally consists of verifying that required settings have, in fact, been set. If this object is not in a valid state, then an exception will be thrown detailing the reason the state is not valid.

Throws:
InvalidConfigurationException - if the state of this Configuration is not valid. The error message in the exception will detail the reason for invalidity.
Since:
1.0

addNaturalSelector

public void addNaturalSelector(NaturalSelector a_selector,
                               boolean processBeforeGeneticOperators)
                        throws InvalidConfigurationException
Adds a NaturalSelector to the ordered chain of registered NaturalSelector's. It's possible to execute the NaturalSelector before or after (registered) genetic operations have been applied.

Parameters:
a_selector - the selector to be added to the chain
processBeforeGeneticOperators - true: execute NaturalSelector before any genetic operator will be applied, false: .. after..
Throws:
InvalidConfigurationException
Since:
1.1

setMinimumPopSizePercent

public void setMinimumPopSizePercent(int a_minimumSizeGuaranteedPercent)

getMinimumPopSizePercent

public int getMinimumPopSizePercent()

getFitnessEvaluator

public FitnessEvaluator getFitnessEvaluator()
Returns:
the assigned FitnessEvaluator
Since:
2.0

setFitnessEvaluator

public void setFitnessEvaluator(FitnessEvaluator a_fitnessEvaluator)
Set the fitness evaluator (deciding if a given fitness value is better when it's higher or better when it's lower).

Parameters:
a_fitnessEvaluator - the FitnessEvaluator to be used
Since:
2.0