KickJava   Java API By Example, From Geeks To Geeks.

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


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.
17  *
18  * @author Klaus Meffert
19  * @since 3.2
20  */

21 public class FitnessSplitStrategy
22     implements IRequestSplitStrategy {
23   /** String containing the CVS revision. Read out via reflection!*/
24   private final static String JavaDoc CVS_REVISION = "$Revision: 1.3 $";
25
26   private Configuration m_config;
27
28   public FitnessSplitStrategy(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)
48       throws Exception JavaDoc {
49     Population pop = a_request.getPopulation();
50     // Sort Population to only work with the most fittest individuals.
51
// This is necessary as a Population can grow further than given
52
// with the Configuration (it has to do with performance, sorry).
53
// ---------------------------------------------------------------
54
pop.sortByFitness();
55     int count = getConfiguration().getPopulationSize();
56     JGAPRequest[] result = new JGAPRequest[count];
57     for (int i = 0; i < count; i++) {
58       // Setup JGAP configuration for worker.
59
// ------------------------------------
60
Configuration config = getConfiguration().newInstance(i + "",
61           "chromosome " + i);
62       // Create single worker request.
63
// -----------------------------
64
IChromosome chrom = pop.getChromosome(i);
65       result[i] = (JGAPRequest) a_request.newInstance("Chromosome " + i,
66           i);
67       result[i].setPopulation(new Population(config, chrom));
68     }
69     return result;
70   }
71 }
72
Popular Tags