KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > examples > grid > evolutionDistributed > 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.evolutionDistributed;
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 number of chromosome from the original population (determined here)
40    * plus the rest of the chromosomes to be initialized randomly at each worker.
41    *
42    * @param a_request the request to split
43    * @return single requests to be computed by workers
44    * @throws Exception
45    *
46    * @author Klaus Meffert
47    * @since 3.2
48    */

49   public JGAPRequest[] split(JGAPRequest a_request)
50       throws Exception JavaDoc {
51     Population pop = a_request.getPopulation();
52     // Is evolution started the first time?
53
// ------------------------------------
54
boolean firstTime;
55     if (pop == null || pop.size() < 1) {
56       firstTime = true;
57     }
58     else {
59       firstTime = false;
60     }
61     if (!firstTime) {
62       // Sort Population to have the fittest ones at the beginning.
63
// ----------------------------------------------------------
64
pop.sortByFitness();
65     }
66     // Generate 20 work requests.
67
// --------------------------
68
int requests = 20; // number of requests to create
69
JGAPRequest[] result = new JGAPRequest[requests];
70     // Only send 10% of the population to the workers.
71
// -----------------------------------------------
72
int count = getConfiguration().getPopulationSize() / 10;
73     for (int j = 0; j < requests; j++) {
74       result[j] = (JGAPRequest) a_request.newInstance("Population " + j, j);
75       // Setup JGAP configuration for worker.
76
// ------------------------------------
77
Configuration config = getConfiguration().newInstance(j + "",
78           "population " + j);
79       // Assemble population for one request.
80
// ------------------------------------
81
RandomGenerator rand = getConfiguration().getRandomGenerator();
82       Population workPop = new Population(config, count);
83       if (!firstTime) {
84         for (int i = 0; i < count; i++) {
85           IChromosome chrom;
86           if (rand.nextDouble() > 0.2d) {
87             // Take one of the best chromosomes.
88
// ---------------------------------
89
chrom = pop.getChromosome(i);
90           }
91           else {
92             // Take one of the ordinary chromosomes.
93
// -------------------------------------
94
chrom = pop.getChromosome(i + count);
95           }
96           workPop.addChromosome(chrom);
97         }
98       }
99       result[j].setPopulation(workPop);
100     }
101     return result;
102   }
103 }
104
Popular Tags