1 10 package examples.grid.fitnessDistributed; 11 12 import org.homedns.dade.jcgrid.client.*; 13 import org.jgap.*; 14 import org.jgap.distr.grid.*; 15 16 22 public class ClientEvolveStrategy 23 implements IClientEvolveStrategy { 24 25 public final static String CVS_REVISION = "$Revision: 1.1 $"; 26 27 29 private Configuration m_config; 30 31 private IClientFeedback m_clientFeedback; 32 33 private final int m_maxEvolutions = 3; 34 35 private Population m_pop; 36 37 49 public void initialize(GridClient a_gc, Configuration a_config, 50 IClientFeedback a_clientFeedback) 51 throws Exception { 52 m_clientFeedback = a_clientFeedback; 53 m_config = a_config; 54 Genotype gen = Genotype.randomInitialGenotype(a_config); 58 m_pop = gen.getPopulation(); 59 } 60 61 public void afterWorkRequestsSent() 62 throws Exception { 63 m_pop = new Population(m_config, m_config.getPopulationSize()); 64 } 65 66 public boolean isEvolutionFinished(int a_evolutionsDone) { 67 if (a_evolutionsDone > m_maxEvolutions) { 70 return true; 71 } 72 else { 73 return false; 74 } 75 } 76 77 public void onFinished() { 78 IChromosome best = m_pop.determineFittestChromosome(); 79 m_clientFeedback.info("Best solution evolved: " + best); 80 } 81 82 public void evolve() 83 throws Exception { 84 Genotype gen = new Genotype(m_config, m_pop); 88 gen.evolve(); 89 m_pop = gen.getPopulation(); 93 } 94 95 public JGAPRequest[] generateWorkRequests(JGAPRequest m_workReq, 96 IRequestSplitStrategy m_splitStrategy, Object data) 97 throws Exception { 98 JGAPRequest[] workList; 102 m_workReq.setPopulation(m_pop); 103 workList = m_splitStrategy.split(m_workReq); 104 return workList; 105 } 106 107 public void resultReceived(JGAPResult a_result) 108 throws Exception { 109 m_pop.addChromosomes(a_result.getPopulation()); 110 } 111 } 112 | Popular Tags |