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 licencing 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 to split a single request
17: * into multiple requests for workers.
18: *
19: * @author Klaus Meffert
20: * @since 3.2
21: */
22: public class MyRequestSplitStrategy implements IRequestSplitStrategy {
23: /** String containing the CVS revision. Read out via reflection!*/
24: private final static String CVS_REVISION = "$Revision: 1.1 $";
25:
26: private Configuration m_config;
27:
28: public MyRequestSplitStrategy(Configuration a_config) {
29: m_config = a_config;
30: }
31:
32: public Configuration getConfiguration() {
33: return m_config;
34: }
35:
36: /**
37: * Creates single requests to be sent to workers. Here, each request consists
38: * of a single chromosome for which to calculate the fitness value.
39: *
40: * @param a_request the request to split
41: * @return single requests to be computed by workers
42: * @throws Exception
43: *
44: * @author Klaus Meffert
45: * @since 3.2
46: */
47: public JGAPRequest[] split(JGAPRequest a_request) throws Exception {
48: Population pop = a_request.getPopulation();
49: // Sort Population to only work with the most fittest individuals.
50: // This is necessary as a Population can grow further than given
51: // with the Configuration (it has to do with performance, sorry).
52: // ---------------------------------------------------------------
53: pop.sortByFitness();
54: int count = getConfiguration().getPopulationSize();
55: JGAPRequest[] result = new JGAPRequest[count];
56: for (int i = 0; i < count; i++) {
57: // Setup JGAP configuration for worker.
58: // ------------------------------------
59: Configuration config = getConfiguration().newInstance(
60: i + "", "chromosome " + i);
61: // Create single worker request.
62: // -----------------------------
63: IChromosome chrom = pop.getChromosome(i);
64: result[i] = (JGAPRequest) a_request.newInstance(
65: "Chromosome " + i, i);
66: result[i].setPopulation(new Population(config, chrom));
67: }
68: return result;
69: }
70: }
|