KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgap > distr > grid > JGAPWorker


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 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
26     implements Worker {
27   /** String containing the CVS revision. Read out via reflection!*/
28   private final static String JavaDoc CVS_REVISION = "$Revision: 1.7 $";
29
30   /**
31    * Executes the evolution and returns the result.
32    *
33    * @param work WorkRequest
34    * @param workDir String
35    * @return WorkResult
36    * @throws Exception
37    *
38    * @author Klaus Meffert
39    * @since 3.01
40    */

41   public WorkResult doWork(WorkRequest work, String JavaDoc workDir)
42       throws Exception JavaDoc {
43     JGAPRequest req = ( (JGAPRequest) work);
44     /**@todo set gridworkerfeedback in class GridWorker*/
45
46     // Setup configuration.
47
// --------------------
48
Configuration conf = req.getConfiguration();
49     conf = conf.newInstance(conf.getId() + "_1", conf.getName() + "_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, initialPop);
61       if (req.getWorkerEvolveStrategy() != null) {
62         // Execute evolution via registered strategy.
63
// ------------------------------------------
64
req.getWorkerEvolveStrategy().evolve(gen);
65       }
66     }
67     // Assemble result according to registered strategy.
68
// -------------------------------------------------
69
WorkResult res = req.getWorkerReturnStrategy().assembleResult(req, gen);
70     return res;
71   }
72
73   /**
74    * Convenience method to start the worker.
75    *
76    * @param args command-line arguments, such as server address. See
77    * @throws Exception
78    *
79    * @author Klaus Meffert
80    * @since 3.01
81    */

82   public static void main(String JavaDoc[] args)
83       throws Exception JavaDoc {
84     // Start worker.
85
// -------------
86
new JGAPWorkers(args);
87   }
88 }
89
Popular Tags