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.evolutionDistributed;
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 Configuration m_config;
027:
028: private IClientFeedback m_clientFeedback;
029:
030: private final int m_maxEvolutions = 3;
031:
032: private Population m_pop;
033:
034: /**
035: * Default constructor is necessary here as it will be called dynamically!
036: * Don't declare any other constructor as it will not be called!
037: */
038: public ClientEvolveStrategy() {
039: }
040:
041: /**
042: * Called at the very beginning and only once before distributed evolution
043: * starts.
044: *
045: * @param a_gc GridClient
046: * @param a_config Configuration
047: * @param a_clientFeedback IClientFeedback
048: * @throws Exception
049: *
050: * @author Klaus Meffert
051: * @since 3.2
052: */
053: public void initialize(GridClient a_gc, Configuration a_config,
054: IClientFeedback a_clientFeedback) throws Exception {
055: m_clientFeedback = a_clientFeedback;
056: m_config = a_config;
057: // Start with an empty population.
058: // ------------------------------
059: m_pop = new Population(m_config);
060: }
061:
062: public void afterWorkRequestsSent() throws Exception {
063: // Important: clear population, otherwise it would grow
064: // endlessly.
065: // ----------------------------------------------------
066: m_pop.clear();
067: }
068:
069: public boolean isEvolutionFinished(int a_evolutionsDone) {
070: // Check if best solution is satisfying.
071: // -------------------------------------
072: IChromosome fittest = m_pop.determineFittestChromosome();
073: if (fittest.getFitnessValue() > 50000) {
074: return true;
075: }
076: // Do the complete evolution cycle 3 times.
077: // ----------------------------------------
078: if (a_evolutionsDone > m_maxEvolutions) {
079: return true;
080: } else {
081: return false;
082: }
083: }
084:
085: /**
086: * Called after evolution has finished.
087: */
088: public void onFinished() {
089: IChromosome best = m_pop.determineFittestChromosome();
090: m_clientFeedback.info("Best solution evolved: " + best);
091: }
092:
093: public void evolve() throws Exception {
094: // Nothing to do here in this example as evolution takes
095: // place on behalf of the workers .
096: // -----------------------------------------------------
097: }
098:
099: public JGAPRequest[] generateWorkRequests(JGAPRequest m_workReq,
100: IRequestSplitStrategy m_splitStrategy, Object data)
101: throws Exception {
102: JGAPRequest[] workList;
103: m_workReq.setPopulation(m_pop);
104: m_workReq.setConfiguration(m_config);
105: workList = m_splitStrategy.split(m_workReq);
106: return workList;
107: }
108:
109: /**
110: * Merge the received results as a basis for the next evolution.
111: *
112: * @param a_result JGAPResult
113: * @throws Exception
114: */
115: public void resultReceived(JGAPResult a_result) throws Exception {
116: m_pop.addChromosomes(a_result.getPopulation());
117: }
118: }
|