01: /*
02: * This file is part of JGAP.
03: *
04: * JGAP offers a dual license model containing the LGPL as well as the MPL.
05: *
06: * For licencing information please see the file license.txt included with JGAP
07: * or have a look at the top of class org.jgap.Chromosome which representatively
08: * includes the JGAP license policy applicable for any file delivered with JGAP.
09: */
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 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() throws Exception {
38: m_localconfig = new GridConfiguration();
39: m_localconfig.initialize(null);
40: m_clientEvolver = m_localconfig.getClientEvolveStrategy();
41: if (m_clientEvolver != null) {
42: m_clientEvolver.initialize(null, m_localconfig
43: .getConfiguration(), m_localconfig
44: .getClientFeedback());
45: }
46: m_req = assembleWorkRequest();
47: evolve();
48: }
49:
50: protected MyRequest assembleWorkRequest() {
51: MyRequest req = new MyRequest("Local session", 0, m_localconfig
52: .getConfiguration());
53: req.setWorkerReturnStrategy(m_localconfig
54: .getWorkerReturnStrategy());
55: req.setGenotypeInitializer(m_localconfig
56: .getGenotypeInitializer());
57: req.setEvolveStrategy(m_localconfig.getWorkerEvolveStrategy());
58: req.setConfiguration(m_localconfig.getConfiguration());
59: // Evolution takes place on client only!
60: // -------------------------------------
61: req.setEvolveStrategy(null);
62: return req;
63: }
64:
65: protected void evolve() throws Exception {
66: // JGAPClient
67: // m_localconfig.getClientFeedback().beginWork();
68: // m_localconfig.getClientFeedback().endWork();
69: m_clientEvolver.generateWorkRequests(m_req, m_localconfig
70: .getRequestSplitStrategy(), null);
71: m_clientEvolver.evolve();
72: // JGAPWorker
73: Genotype genotype = m_localconfig.getGenotypeInitializer()
74: .setupGenotype(m_req, null);
75: if (m_localconfig.getWorkerEvolveStrategy() != null) {
76: m_localconfig.getWorkerEvolveStrategy().evolve(genotype);
77: }
78: if (m_localconfig.getWorkerReturnStrategy() == null) {
79: throw new IllegalStateException(
80: "Worker return strategy expected, but was null!");
81: }
82: JGAPResult res = m_localconfig.getWorkerReturnStrategy()
83: .assembleResult(m_req, genotype);
84: System.out.println(res.getPopulation()
85: .determineFittestChromosome());
86: }
87:
88: public static void main(String[] args) {
89: try {
90: new LocalEvolutionDemo();
91: } catch (Throwable t) {
92: t.printStackTrace();
93: System.exit(1);
94: }
95: }
96: }
|