KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > examples > grid > fitnessDistributed > MyRequestSplitStrategy


1 /*
2  * This file is part of JGAP.
3  *
4  * JGAP offers a dual license model containing the LGPL as well as the MPL.
5  *
6  * For licencing information please see the file license.txt included with JGAP
7  * or have a look at the top of class org.jgap.Chromosome which representatively
8  * includes the JGAP license policy applicable for any file delivered with JGAP.
9  */

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
23     implements IRequestSplitStrategy {
24   /** String containing the CVS revision. Read out via reflection!*/
25   private final static String JavaDoc CVS_REVISION = "$Revision: 1.1 $";
26
27   private Configuration m_config;
28
29   public MyRequestSplitStrategy(Configuration a_config) {
30     m_config = a_config;
31   }
32
33   public Configuration getConfiguration() {
34     return m_config;
35   }
36
37   /**
38    * Creates single requests to be sent to workers. Here, each request consists
39    * of a single chromosome for which to calculate the fitness value.
40    *
41    * @param a_request the request to split
42    * @return single requests to be computed by workers
43    * @throws Exception
44    *
45    * @author Klaus Meffert
46    * @since 3.2
47    */

48   public JGAPRequest[] split(JGAPRequest a_request)
49       throws Exception JavaDoc {
50     Population pop = a_request.getPopulation();
51     // Sort Population to only work with the most fittest individuals.
52
// This is necessary as a Population can grow further than given
53
// with the Configuration (it has to do with performance, sorry).
54
// ---------------------------------------------------------------
55
pop.sortByFitness();
56     int count = getConfiguration().getPopulationSize();
57     JGAPRequest[] result = new JGAPRequest[count];
58     for (int i = 0; i < count; i++) {
59       // Setup JGAP configuration for worker.
60
// ------------------------------------
61
Configuration config = getConfiguration().newInstance(i + "",
62           "chromosome " + i);
63       // Create single worker request.
64
// -----------------------------
65
IChromosome chrom = pop.getChromosome(i);
66       result[i] = (JGAPRequest) a_request.newInstance("Chromosome " + i,
67           i);
68       result[i].setPopulation(new Population(config, chrom));
69     }
70     return result;
71   }
72 }
73
Popular Tags