01: /*
02: * This file is part of JGAP.
03: *
04: * JGAP offers a dual license model containing the LGPL as well as the MPL.
05: *
06: * For licensing information please see the file license.txt included with JGAP
07: * or have a look at the top of class org.jgap.Chromosome which representatively
08: * includes the JGAP license policy applicable for any file delivered with JGAP.
09: */
10: package examples.grid.fitnessDistributed;
11:
12: import org.jgap.*;
13: import org.jgap.distr.grid.*;
14:
15: /**
16: * Sample implementation of IRequestSplitStrategy.
17: *
18: * @author Klaus Meffert
19: * @since 3.2
20: */
21: public class FitnessSplitStrategy implements IRequestSplitStrategy {
22: /** String containing the CVS revision. Read out via reflection!*/
23: private final static String CVS_REVISION = "$Revision: 1.4 $";
24:
25: private Configuration m_config;
26:
27: public FitnessSplitStrategy(Configuration a_config) {
28: m_config = a_config;
29: }
30:
31: public Configuration getConfiguration() {
32: return m_config;
33: }
34:
35: /**
36: * Creates single requests to be sent to workers. Here, each request consists
37: * of a single chromosome for which to calculate the fitness value.
38: *
39: * @param a_request the request to split
40: * @return single requests to be computed by workers
41: * @throws Exception
42: *
43: * @author Klaus Meffert
44: * @since 3.2
45: */
46: public JGAPRequest[] split(JGAPRequest a_request) throws Exception {
47: Population pop = a_request.getPopulation();
48: // Sort Population to only work with the most fittest individuals.
49: // This is necessary as a Population can grow further than given
50: // with the Configuration (it has to do with performance, sorry).
51: // ---------------------------------------------------------------
52: pop.sortByFitness();
53: int count = getConfiguration().getPopulationSize();
54: JGAPRequest[] result = new JGAPRequest[count];
55: for (int i = 0; i < count; i++) {
56: // Setup JGAP configuration for worker.
57: // ------------------------------------
58: Configuration config = getConfiguration().newInstance(
59: i + "", "chromosome " + i);
60: // Create single worker request.
61: // -----------------------------
62: IChromosome chrom = pop.getChromosome(i);
63: result[i] = (JGAPRequest) a_request.newInstance(
64: "Chromosome " + i, i);
65: result[i].setPopulation(new Population(config, chrom));
66: }
67: return result;
68: }
69: }
|