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 org.jgap.distr.grid;
11:
12: import org.homedns.dade.jcgrid.*;
13: import org.homedns.dade.jcgrid.worker.*;
14: import org.jgap.*;
15: import org.jgap.distr.grid.*;
16:
17: /**
18: * A worker receives work units from a JGAPServer and sends back computed
19: * solutions to the same JGAPServer.
20: *
21: * @author Klaus Meffert
22: * @since 3.2 (since 3.01 this class contained something different that is now
23: * in class org.jgap.distr.grid.JGAPWorkers)
24: */
25: public class JGAPWorker implements Worker {
26: /** String containing the CVS revision. Read out via reflection!*/
27: private final static String CVS_REVISION = "$Revision: 1.7 $";
28:
29: /**
30: * Executes the evolution and returns the result.
31: *
32: * @param work WorkRequest
33: * @param workDir String
34: * @return WorkResult
35: * @throws Exception
36: *
37: * @author Klaus Meffert
38: * @since 3.01
39: */
40: public WorkResult doWork(WorkRequest work, String workDir)
41: throws Exception {
42: JGAPRequest req = ((JGAPRequest) work);
43: /**@todo set gridworkerfeedback in class GridWorker*/
44:
45: // Setup configuration.
46: // --------------------
47: Configuration conf = req.getConfiguration();
48: conf = conf.newInstance(conf.getId() + "_1", conf.getName()
49: + "_1");
50: // Important: Re-set the cloned configuration!
51: // -------------------------------------------
52: req.setConfiguration(conf);
53: Genotype gen = null;
54: // It is possible that no evolution happens at the worker.
55: // -------------------------------------------------------
56: if (req.getGenotypeInitializer() != null) {
57: // Setup the genotype to evolve.
58: // -----------------------------
59: Population initialPop = req.getPopulation();
60: gen = req.getGenotypeInitializer().setupGenotype(req,
61: initialPop);
62: if (req.getWorkerEvolveStrategy() != null) {
63: // Execute evolution via registered strategy.
64: // ------------------------------------------
65: req.getWorkerEvolveStrategy().evolve(gen);
66: }
67: }
68: // Assemble result according to registered strategy.
69: // -------------------------------------------------
70: WorkResult res = req.getWorkerReturnStrategy().assembleResult(
71: req, gen);
72: return res;
73: }
74:
75: /**
76: * Convenience method to start the worker.
77: *
78: * @param args command-line arguments, such as server address. See
79: * @throws Exception
80: *
81: * @author Klaus Meffert
82: * @since 3.01
83: */
84: public static void main(String[] args) throws Exception {
85: // Start worker.
86: // -------------
87: new JGAPWorkers(args);
88: }
89: }
|