001: /*
002: * This file is part of JGAP.
003: *
004: * JGAP offers a dual license model containing the LGPL as well as the MPL.
005: *
006: * For licensing information please see the file license.txt included with JGAP
007: * or have a look at the top of class org.jgap.Chromosome which representatively
008: * includes the JGAP license policy applicable for any file delivered with JGAP.
009: */
010: package org.jgap.impl.job;
011:
012: import org.jgap.*;
013: import org.jgap.*;
014: import java.util.*;
015:
016: /**
017: * A job that evolves a population. The evolution takes place as given by the
018: * configuration. It operates on a population also provided.
019: * This class utilizes sub jobs to perform its sub-tasks. Each sub job may
020: * either be a local implementation satisfying the IJob interface. Or it may be
021: * a job that eventually is put onto the grid and returns with a result!
022: *
023: * @author Klaus Meffert
024: * @since 3.2
025: */
026: public class EvolveJob extends JobBase implements IEvolveJob {
027: /** String containing the CVS revision. Read out via reflection!*/
028: private final static String CVS_REVISION = "$Revision: 1.12 $";
029:
030: public EvolveJob(JobData a_data) {
031: super (a_data);
032: }
033:
034: /**
035: * Execute the evolution via JGAP.
036: *
037: * @param a_data input parameter of type EvolveData
038: * @return result of the evolution
039: *
040: * @throws Exception in case of any error
041: *
042: * @author Klaus Meffert
043: * @since 3.2
044: */
045: public JobResult execute(JobData a_data) throws Exception {
046: EvolveData data = (EvolveData) a_data;
047: return evolve(data);
048: }
049:
050: /**
051: * Does the genetic evolution.
052: *
053: * @param a_evolveData parameters for the evolution
054: * @return result of the evolution
055: *
056: * @author Klaus Meffert
057: * @since 3.2
058: */
059: public EvolveResult evolve(EvolveData a_evolveData) {
060: EvolveResult result = new EvolveResult();
061: Configuration config = a_evolveData.getConfiguration();
062: result.setConfiguration(config);
063: Population pop = a_evolveData.getPopulation();
064: // Breed a new population by performing evolution.
065: // -----------------------------------------------
066: IBreeder breeder = a_evolveData.getBreeder();
067: pop = breeder.evolve(pop, config);
068: //
069: result.setPopulation(pop);
070: return result;
071: }
072:
073: /**
074: * Applies all NaturalSelectors registered with the Configuration.
075: *
076: * @param a_processBeforeGeneticOperators true apply NaturalSelectors
077: * applicable before GeneticOperators, false: apply the ones applicable
078: * after GeneticOperators
079: *
080: * @author Klaus Meffert
081: * @since 3.2
082: */
083: protected Population applyNaturalSelectors(Configuration a_config,
084: Population a_pop, boolean a_processBeforeGeneticOperators) {
085: /**@todo optionally use working pool*/
086: try {
087: // Process all natural selectors applicable before executing the
088: // genetic operators (reproduction, crossing over, mutation...).
089: // -------------------------------------------------------------
090: int selectorSize = a_config
091: .getNaturalSelectorsSize(a_processBeforeGeneticOperators);
092: if (selectorSize > 0) {
093: int m_population_size = a_config.getPopulationSize();
094: int m_single_selection_size;
095: Population new_population = new Population(a_config,
096: m_population_size);
097: NaturalSelector selector;
098: // Repopulate the population of chromosomes with those selected
099: // by the natural selector. Iterate over all natural selectors.
100: // ------------------------------------------------------------
101: for (int i = 0; i < selectorSize; i++) {
102: selector = a_config.getNaturalSelector(
103: a_processBeforeGeneticOperators, i);
104: if (i == selectorSize - 1 && i > 0) {
105: // Ensure the last NaturalSelector adds the remaining Chromosomes.
106: // ---------------------------------------------------------------
107: m_single_selection_size = m_population_size
108: - a_pop.size();
109: } else {
110: m_single_selection_size = m_population_size
111: / selectorSize;
112: }
113: // Do selection of chromosomes.
114: // ----------------------------
115: /**@todo utilize jobs: integrate job into NaturalSelector!*/
116: selector.select(m_single_selection_size, a_pop,
117: new_population);
118: // Clean up the natural selector.
119: // ------------------------------
120: selector.empty();
121: }
122: // Population result = new Population(a_config);
123: // result.addChromosomes(new_population);
124: return new_population;
125: } else {
126: return a_pop;
127: }
128: } catch (InvalidConfigurationException iex) {
129: // This exception should never be reached
130: throw new IllegalStateException(iex.getMessage());
131: }
132: }
133:
134: /**
135: * Applies all GeneticOperators registered with the Configuration.
136: *
137: * @author Klaus Meffert
138: * @since 3.2
139: */
140: protected void applyGeneticOperators(Configuration a_config,
141: Population a_pop) {
142: List geneticOperators = a_config.getGeneticOperators();
143: Iterator operatorIterator = geneticOperators.iterator();
144: while (operatorIterator.hasNext()) {
145: GeneticOperator operator = (GeneticOperator) operatorIterator
146: .next();
147: /**@todo utilize jobs: integrate job into GeneticOperator*/
148: operator.operate(a_pop, a_pop.getChromosomes());
149: }
150: }
151: }
|