KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > examples > grid > evolutionDistributed > LocalEvolutionDemo


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.distr.grid.*;
13 import org.jgap.*;
14 import examples.grid.fitnessDistributed.*;
15
16 /**
17  * Demonstrates how the grid configuration can be used to do the whole
18  * evolution locally (i.e. stand alone without server and workers).<p>
19  * Our aim: We can recycle any code written for the grid. We don't have to
20  * rewrite or add a single line of code!<p>
21  * This class is still under development! It will evolve together with the
22  * framework.
23  *
24  * @author Klaus Meffert
25  * @since 3.2
26  */

27 public class LocalEvolutionDemo {
28   /** String containing the CVS revision. Read out via reflection!*/
29   private final static String JavaDoc CVS_REVISION = "$Revision: 1.1 $";
30
31   private GridConfiguration m_localconfig;
32
33   private IClientEvolveStrategy m_clientEvolver;
34
35   private MyRequest m_req;
36
37   public LocalEvolutionDemo()
38       throws Exception JavaDoc {
39     m_localconfig = new GridConfiguration();
40     m_localconfig.initialize(null);
41     m_clientEvolver = m_localconfig.getClientEvolveStrategy();
42     if (m_clientEvolver != null) {
43       m_clientEvolver.initialize(null, m_localconfig.getConfiguration(),
44                                  m_localconfig.getClientFeedback());
45     }
46     m_req = assembleWorkRequest();
47     evolve();
48   }
49
50   protected MyRequest assembleWorkRequest() {
51     MyRequest req = new MyRequest("Local session", 0,
52                                   m_localconfig.getConfiguration());
53     req.setWorkerReturnStrategy(m_localconfig.getWorkerReturnStrategy());
54     req.setGenotypeInitializer(m_localconfig.getGenotypeInitializer());
55     req.setEvolveStrategy(m_localconfig.getWorkerEvolveStrategy());
56     req.setConfiguration(m_localconfig.getConfiguration());
57     // Evolution takes place on client only!
58
// -------------------------------------
59
req.setEvolveStrategy(null);
60     return req;
61   }
62
63   protected void evolve()
64       throws Exception JavaDoc {
65     // JGAPClient
66
// m_localconfig.getClientFeedback().beginWork();
67
// m_localconfig.getClientFeedback().endWork();
68
m_clientEvolver.generateWorkRequests(m_req,
69         m_localconfig.getRequestSplitStrategy(), null);
70     m_clientEvolver.evolve();
71     // JGAPWorker
72
Genotype genotype = m_localconfig.getGenotypeInitializer().
73         setupGenotype(m_req, null);
74     if (m_localconfig.getWorkerEvolveStrategy() != null) {
75       m_localconfig.getWorkerEvolveStrategy().evolve(genotype);
76     }
77     if (m_localconfig.getWorkerReturnStrategy() == null) {
78       throw new IllegalStateException JavaDoc(
79           "Worker return strategy expected, but was null!");
80     }
81     JGAPResult res = m_localconfig.getWorkerReturnStrategy().assembleResult(
82         m_req, genotype);
83     System.out.println(res.getPopulation().determineFittestChromosome());
84   }
85
86   public static void main(String JavaDoc[] args) {
87     try {
88       new LocalEvolutionDemo();
89     } catch (Throwable JavaDoc t) {
90       t.printStackTrace();
91       System.exit(1);
92     }
93   }
94 }
95
Popular Tags