001: /*
002: * This file is part of JGAP.
003: *
004: * JGAP offers a dual license model containing the LGPL as well as the MPL.
005: *
006: * For licensing information please see the file license.txt included with JGAP
007: * or have a look at the top of class org.jgap.Chromosome which representatively
008: * includes the JGAP license policy applicable for any file delivered with JGAP.
009: */
010: package examples.grid.fitnessDistributed;
011:
012: import org.homedns.dade.jcgrid.client.*;
013: import org.jgap.*;
014: import org.jgap.distr.grid.*;
015:
016: /**
017: * Sample implementation of a strategy for evolving a generation on the client.
018: *
019: * @author Klaus Meffert
020: * @since 3.2
021: */
022: public class ClientEvolveStrategy implements IClientEvolveStrategy {
023: /** String containing the CVS revision. Read out via reflection!*/
024: public final static String CVS_REVISION = "$Revision: 1.3 $";
025:
026: // private GridClient m_gc;
027:
028: private Configuration m_config;
029:
030: private IClientFeedback m_clientFeedback;
031:
032: private final int m_maxEvolutions = 3;
033:
034: private Population m_pop;
035:
036: /**
037: * Default constructor is necessary here as it will be called dynamically!
038: * Don't declare any other constructor as it will not be called!
039: */
040: public ClientEvolveStrategy() {
041: }
042:
043: /**
044: * Called at the very beginning and only once before distributed evolution
045: * starts.
046: *
047: * @param a_gc GridClient
048: * @param a_config Configuration
049: * @param a_clientFeedback IClientFeedback
050: * @throws Exception
051: *
052: * @author Klaus Meffert
053: * @since 3.2
054: */
055: public void initialize(GridClient a_gc, Configuration a_config,
056: IClientFeedback a_clientFeedback) throws Exception {
057: m_clientFeedback = a_clientFeedback;
058: m_config = a_config;
059: // m_gc = a_gc;
060: // Initialize population randomly.
061: // -------------------------------
062: Genotype gen = Genotype.randomInitialGenotype(a_config);
063: m_pop = gen.getPopulation();
064: }
065:
066: public void afterWorkRequestsSent() throws Exception {
067: m_pop = new Population(m_config, m_config.getPopulationSize());
068: }
069:
070: public boolean isEvolutionFinished(int a_evolutionsDone) {
071: // Do the complete evolution cycle 3 times.
072: // ----------------------------------------
073: if (a_evolutionsDone > m_maxEvolutions) {
074: return true;
075: } else {
076: return false;
077: }
078: }
079:
080: public void onFinished() {
081: IChromosome best = m_pop.determineFittestChromosome();
082: m_clientFeedback.info("Best solution evolved: " + best);
083: }
084:
085: public void evolve() throws Exception {
086: // Do the evolution locally.
087: // Note: It would be easy to also distribute this task.
088: // ----------------------------------------------------
089: Genotype gen = new Genotype(m_config, m_pop);
090: gen.evolve();
091: // Get back the evolved population for further evolutions
092: // (see beginning of the iteration).
093: // ------------------------------------------------------
094: m_pop = gen.getPopulation();
095: }
096:
097: public JGAPRequest[] generateWorkRequests(JGAPRequest m_workReq,
098: IRequestSplitStrategy m_splitStrategy, Object data)
099: throws Exception {
100: // Calculate fitness values of chromosomes.
101: // Let each worker compute one fitness value.
102: // ------------------------------------------
103: JGAPRequest[] workList;
104: m_workReq.setPopulation(m_pop);
105: workList = m_splitStrategy.split(m_workReq);
106: return workList;
107: }
108:
109: public void resultReceived(JGAPResult a_result) throws Exception {
110: m_pop.addChromosomes(a_result.getPopulation());
111: }
112: }
|