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.mathProblemDistributed;
011:
012: import org.homedns.dade.jcgrid.client.*;
013: import org.jgap.*;
014: import org.jgap.gp.*;
015: import org.jgap.gp.impl.*;
016: import org.jgap.distr.grid.*;
017: import org.jgap.distr.grid.gp.*;
018: import org.jgap.gp.function.Subtract;
019: import org.jgap.gp.function.Add3;
020: import org.jgap.gp.impl.GPConfiguration;
021: import org.jgap.gp.function.Divide;
022: import org.jgap.gp.function.Multiply3;
023: import org.jgap.gp.function.Pow;
024: import org.jgap.gp.function.Exp;
025: import org.jgap.gp.terminal.Variable;
026: import org.jgap.gp.function.Sine;
027: import org.jgap.gp.function.Multiply;
028: import org.jgap.gp.function.Add;
029: import org.jgap.gp.impl.*;
030: import org.jgap.gp.terminal.*;
031: import org.apache.log4j.*;
032:
033: /**
034: * Sample implementation of a strategy for evolving a generation on the client.
035: *
036: * @author Klaus Meffert
037: * @since 3.2
038: */
039: public class ClientEvolveStrategy extends GPProblem implements
040: IClientEvolveStrategyGP {
041: /** String containing the CVS revision. Read out via reflection!*/
042: public final static String CVS_REVISION = "$Revision: 1.9 $";
043:
044: private static Logger log = Logger
045: .getLogger(ClientEvolveStrategy.class);
046:
047: // private GPConfiguration m_config;
048:
049: private IClientFeedbackGP m_clientFeedback;
050:
051: private final int m_maxEvolutions = 3;
052:
053: private GPPopulation m_pop;
054:
055: private static Float[] x = new Float[20];
056:
057: private static float[] y = new float[20];
058:
059: /**@todo pass config*/
060: // public ClientEvolveStrategy(GPConfiguration a_conf)
061: // throws InvalidConfigurationException {
062: // super(a_conf);
063: // }
064: /**
065: * Default constructor is necessary here as it will be called dynamically!
066: * Don't declare any other constructor as it will not be called!
067: */
068: public ClientEvolveStrategy() {
069: super ();
070: }
071:
072: /**
073: * Called at the very beginning and only once before distributed evolution
074: * starts.
075: *
076: * @param a_gc GridClient
077: * @param a_config Configuration
078: * @param a_clientFeedback IClientFeedback
079: * @throws Exception
080: *
081: * @author Klaus Meffert
082: * @since 3.2
083: */
084: public void initialize(GridClient a_gc, GPConfiguration a_config,
085: IClientFeedbackGP a_clientFeedback) throws Exception {
086: log.info("Initializing...");
087: super .setGPConfiguration(a_config);
088: m_clientFeedback = a_clientFeedback;
089: // Start with a randomly initialized population.
090: // ---------------------------------------------
091: GPGenotype gen = create();
092: m_pop = gen.getGPPopulation();
093: }
094:
095: public void afterWorkRequestsSent() throws Exception {
096: // Don't clear population here!
097: if (false) {
098: // Important: clear population, otherwise it would grow
099: // endlessly.
100: // ----------------------------------------------------
101: if (m_pop != null) {
102: m_pop.clear();
103: log.warn("Population cleared");
104: }
105: }
106: }
107:
108: public boolean isEvolutionFinished(int a_evolutionsDone) {
109: if (m_pop != null) {
110: // Check if best solution is satisfying.
111: // -------------------------------------
112: IGPProgram fittest = m_pop.determineFittestProgram();
113: if (fittest.getFitnessValue() < 20) {
114: return true;
115: }
116: }
117: // Do the complete evolution cycle 3 times.
118: // ----------------------------------------
119: if (a_evolutionsDone > m_maxEvolutions) {
120: return true;
121: } else {
122: return false;
123: }
124: }
125:
126: /**
127: * Called after evolution has finished.
128: */
129: public void onFinished() {
130: IGPProgram best = m_pop.determineFittestProgram();
131: m_clientFeedback.info("Best solution evolved: "
132: + best.getFitnessValue());
133: }
134:
135: public void evolve() throws Exception {
136: // Nothing to do here in this example as evolution takes
137: // place on behalf of the workers .
138: // -----------------------------------------------------
139: }
140:
141: public JGAPRequestGP[] generateWorkRequests(
142: JGAPRequestGP m_workReq,
143: IRequestSplitStrategyGP m_splitStrategy, Object data)
144: throws Exception {
145: JGAPRequestGP[] workList;
146: if (m_pop == null || m_pop.isFirstEmpty()) {
147: log.warn("Initial population is empty!");
148: }
149: m_workReq.setPopulation(m_pop);
150: m_workReq.setConfiguration(getGPConfiguration());
151: if (getGPConfiguration().getJGAPFactory() == null) {
152: throw new IllegalStateException(
153: "JGAPFactory must not be null!");
154: }
155: workList = m_splitStrategy.split(m_workReq);
156: return workList;
157: }
158:
159: /**
160: * Merge the received results as a basis for the next evolution.
161: *
162: * @param a_result JGAPResult
163: * @throws Exception
164: * @since 3.2
165: */
166: public void resultReceived(JGAPResultGP a_result) throws Exception {
167: // This is a very simplistic implementation!
168: // -----------------------------------------
169: GPPopulation pop = a_result.getPopulation();
170: if (pop == null || pop.isFirstEmpty()) {
171: IGPProgram best = a_result.getFittest();
172: if (best == null) {
173: log.error("Empty result received");
174: } else {
175: m_pop.addFittestProgram(best);
176: }
177: } else {
178: m_pop = pop;
179: }
180: }
181:
182: public GPGenotype create() throws InvalidConfigurationException {
183: GPConfiguration conf = getGPConfiguration();
184: Class[] types = { CommandGene.FloatClass };
185: Class[][] argTypes = { {} };
186: Variable vx;
187: CommandGene[][] nodeSets = { {
188: vx = Variable.create(conf, "X", CommandGene.FloatClass),
189: new Add(conf, CommandGene.FloatClass),
190: new Add3(conf, CommandGene.FloatClass),
191: new Subtract(conf, CommandGene.FloatClass),
192: new Multiply(conf, CommandGene.FloatClass),
193: new Multiply3(conf, CommandGene.FloatClass),
194: new Divide(conf, CommandGene.FloatClass),
195: new Sine(conf, CommandGene.FloatClass),
196: new Exp(conf, CommandGene.FloatClass),
197: new Pow(conf, CommandGene.FloatClass),
198: new Terminal(conf, CommandGene.FloatClass, 2.0d, 10.0d,
199: true), } };
200: // Create genotype with initial population.
201: // ----------------------------------------
202: GPGenotype result = GPGenotype.randomInitialGenotype(conf,
203: types, argTypes, nodeSets, 20, true);
204: result.putVariable(vx);
205: conf.putVariable(vx);
206: return result;
207: }
208: }
|