KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > examples > grid > fitnessDistributed > MyGAWorker


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.fitnessDistributed;
11
12 import org.homedns.dade.jcgrid.*;
13 import org.jgap.*;
14 import org.jgap.distr.grid.*;
15
16 /**
17  * Receives work, computes a solution and returns the solution to the requester.
18  *
19  * @author Klaus Meffert
20  * @since 3.01
21  */

22 public class MyGAWorker
23     extends JGAPWorker {
24   /** String containing the CVS revision. Read out via reflection!*/
25   private final static String JavaDoc CVS_REVISION = "$Revision: 1.4 $";
26
27   /**
28    * Executes the evolution and returns the result.
29    *
30    * @param work WorkRequest
31    * @param workDir String
32    * @return WorkResult
33    * @throws Exception
34    *
35    * @author Klaus Meffert
36    * @since 3.01
37    */

38   public WorkResult doWork(WorkRequest work, String JavaDoc workDir)
39       throws Exception JavaDoc {
40     // Here we could use our own class descended from JGAPRequest
41
// MyRequest req = ( (MyRequest) work);
42
// For the result we could also use an individual class such as MyResult
43
// It would be also possible to do different computations than in
44
// super.doWork(...)
45
// MyResult res = (MyResult)super.doWork(work, workDir);
46

47     // Doing the evolution as always just means:
48
// return super.doWork(work, workDir);
49

50     // But we want to only calculate the fitness value of the chromosomes
51
// passed. In our case this is only one chromosome.
52
// ------------------------------------------------------------------
53
JGAPRequest req = ( (JGAPRequest) work);
54     IChromosome chrom = req.getPopulation().getChromosome(0);
55     // Do the actual fitness computation here.
56
// ---------------------------------------
57
chrom.getFitnessValue();
58     Population pop = new Population(req.getConfiguration(), chrom);
59     MyResult result = new MyResult(req.getSessionName(), req.getRID(), pop, 1);
60     return result;
61   }
62
63 }
64
Popular Tags