KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > examples > grid > evolutionDistributed > 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.evolutionDistributed;
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 Configuration m_config;
28
29   private IClientFeedback m_clientFeedback;
30
31   private final int m_maxEvolutions = 3;
32
33   private Population m_pop;
34
35   /**
36    * Called at the very beginning and only once before distributed evolution
37    * starts.
38    *
39    * @param a_gc GridClient
40    * @param a_config Configuration
41    * @param a_clientFeedback IClientFeedback
42    * @throws Exception
43    *
44    * @author Klaus Meffert
45    * @since 3.2
46    */

47   public void initialize(GridClient a_gc, Configuration a_config,
48                          IClientFeedback a_clientFeedback)
49       throws Exception JavaDoc {
50     m_clientFeedback = a_clientFeedback;
51     m_config = a_config;
52     // Start with an empty population.
53
// ------------------------------
54
m_pop = new Population(m_config);
55   }
56
57   public void afterWorkRequestsSent()
58       throws Exception JavaDoc {
59     // Important: clear population, otherwise it would grow
60
// endlessly.
61
// ----------------------------------------------------
62
m_pop.clear();
63   }
64
65   public boolean isEvolutionFinished(int a_evolutionsDone) {
66     // Check if best solution is satisfying.
67
// -------------------------------------
68
IChromosome fittest = m_pop.determineFittestChromosome();
69     if (fittest.getFitnessValue() > 50000) {
70       return true;
71     }
72     // Do the complete evolution cycle 3 times.
73
// ----------------------------------------
74
if (a_evolutionsDone > m_maxEvolutions) {
75       return true;
76     }
77     else {
78       return false;
79     }
80   }
81
82   /**
83    * Called after evolution has finished.
84    */

85   public void onFinished() {
86     IChromosome best = m_pop.determineFittestChromosome();
87     m_clientFeedback.info("Best solution evolved: " + best);
88   }
89
90   public void evolve()
91       throws Exception JavaDoc {
92     // Nothing to do here in this example as evolution takes
93
// place on behalf of the workers .
94
// -----------------------------------------------------
95
}
96
97   public JGAPRequest[] generateWorkRequests(JGAPRequest m_workReq,
98       IRequestSplitStrategy m_splitStrategy, Object JavaDoc data)
99       throws Exception JavaDoc {
100     JGAPRequest[] workList;
101     m_workReq.setPopulation(m_pop);
102     m_workReq.setConfiguration(m_config);
103     workList = m_splitStrategy.split(m_workReq);
104     return workList;
105   }
106
107   /**
108    * Merge the received results as a basis for the next evolution.
109    *
110    * @param a_result JGAPResult
111    * @throws Exception
112    */

113   public void resultReceived(JGAPResult a_result)
114       throws Exception JavaDoc {
115     m_pop.addChromosomes(a_result.getPopulation());
116   }
117 }
118
Popular Tags