KickJava   Java API By Example, From Geeks To Geeks.

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


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.homedns.dade.jcgrid.client.*;
13 import org.jgap.*;
14 import org.jgap.distr.grid.*;
15
16 /**
17  * Sample implementation of a strategy for evolving a generation on the client.
18  *
19  * @author Klaus Meffert
20  * @since 3.2
21  */

22 public class ClientEvolveStrategy
23     implements IClientEvolveStrategy {
24   /** String containing the CVS revision. Read out via reflection!*/
25   public final static String JavaDoc CVS_REVISION = "$Revision: 1.1 $";
26
27 // private GridClient m_gc;
28

29   private Configuration m_config;
30
31   private IClientFeedback m_clientFeedback;
32
33   private final int m_maxEvolutions = 3;
34
35   private Population m_pop;
36
37   /**
38    * Called at the very beginning and only once before distributed evolution
39    * starts.
40    *
41    * @param a_gc GridClient
42    * @param a_config Configuration
43    * @param a_clientFeedback IClientFeedback
44    * @throws Exception
45    *
46    * @author Klaus Meffert
47    * @since 3.2
48    */

49   public void initialize(GridClient a_gc, Configuration a_config,
50                          IClientFeedback a_clientFeedback)
51       throws Exception JavaDoc {
52     m_clientFeedback = a_clientFeedback;
53     m_config = a_config;
54 // m_gc = a_gc;
55
// Initialize population randomly.
56
// -------------------------------
57
Genotype gen = Genotype.randomInitialGenotype(a_config);
58     m_pop = gen.getPopulation();
59   }
60
61   public void afterWorkRequestsSent()
62       throws Exception JavaDoc {
63     m_pop = new Population(m_config, m_config.getPopulationSize());
64   }
65
66   public boolean isEvolutionFinished(int a_evolutionsDone) {
67     // Do the complete evolution cycle 3 times.
68
// ----------------------------------------
69
if (a_evolutionsDone > m_maxEvolutions) {
70       return true;
71     }
72     else {
73       return false;
74     }
75   }
76
77   public void onFinished() {
78     IChromosome best = m_pop.determineFittestChromosome();
79     m_clientFeedback.info("Best solution evolved: " + best);
80   }
81
82   public void evolve()
83       throws Exception JavaDoc {
84     // Do the evolution locally.
85
// Note: It would be easy to also distribute this task.
86
// ----------------------------------------------------
87
Genotype gen = new Genotype(m_config, m_pop);
88     gen.evolve();
89     // Get back the evolved population for further evolutions
90
// (see beginning of the iteration).
91
// ------------------------------------------------------
92
m_pop = gen.getPopulation();
93   }
94
95   public JGAPRequest[] generateWorkRequests(JGAPRequest m_workReq,
96       IRequestSplitStrategy m_splitStrategy, Object JavaDoc data)
97       throws Exception JavaDoc {
98     // Calculate fitness values of chromosomes.
99
// Let each worker compute one fitness value.
100
// ------------------------------------------
101
JGAPRequest[] workList;
102     m_workReq.setPopulation(m_pop);
103     workList = m_splitStrategy.split(m_workReq);
104     return workList;
105   }
106
107   public void resultReceived(JGAPResult a_result)
108       throws Exception JavaDoc {
109     m_pop.addChromosomes(a_result.getPopulation());
110   }
111 }
112
Popular Tags