Not all Encog trainers support simple pause / resume. If they do not support it, they return zero, like this one. The genetic algorithm trainer is much more complex than a simple propagation trainer that supports pause / resume. To preserve the state of the genetic algorithm, you must save the entire population as well as the scoring function (which may or may not be serializable). I modified the Lunar Lander example to show you how you can save / reload your population of neural networks to do this.
You can see that he trains 50 iterations, then makes circular trips (loads / saves) the genetic algorithm, then trains another 50.
package org.encog.examples.neural.lunar; import java.io.File; import java.io.IOException; import org.encog.Encog; import org.encog.engine.network.activation.ActivationTANH; import org.encog.ml.MLMethod; import org.encog.ml.MLResettable; import org.encog.ml.MethodFactory; import org.encog.ml.ea.population.Population; import org.encog.ml.genetic.MLMethodGeneticAlgorithm; import org.encog.ml.genetic.MLMethodGenomeFactory; import org.encog.neural.networks.BasicNetwork; import org.encog.neural.pattern.FeedForwardPattern; import org.encog.util.obj.SerializeObject; public class LunarLander { public static BasicNetwork createNetwork() { FeedForwardPattern pattern = new FeedForwardPattern(); pattern.setInputNeurons(3); pattern.addHiddenLayer(50); pattern.setOutputNeurons(1); pattern.setActivationFunction(new ActivationTANH()); BasicNetwork network = (BasicNetwork)pattern.generate(); network.reset(); return network; } public static void saveMLMethodGeneticAlgorithm(String file, MLMethodGeneticAlgorithm ga ) throws IOException { ga.getGenetic().getPopulation().setGenomeFactory(null); SerializeObject.save(new File(file),ga.getGenetic().getPopulation()); } public static MLMethodGeneticAlgorithm loadMLMethodGeneticAlgorithm(String filename) throws ClassNotFoundException, IOException { Population pop = (Population) SerializeObject.load(new File(filename)); pop.setGenomeFactory(new MLMethodGenomeFactory(new MethodFactory(){ @Override public MLMethod factor() { final BasicNetwork result = createNetwork(); ((MLResettable)result).reset(); return result; }},pop)); MLMethodGeneticAlgorithm result = new MLMethodGeneticAlgorithm(new MethodFactory(){ @Override public MLMethod factor() { return createNetwork(); }},new PilotScore(),1); result.getGenetic().setPopulation(pop); return result; } public static void main(String args[]) { BasicNetwork network = createNetwork(); MLMethodGeneticAlgorithm train; train = new MLMethodGeneticAlgorithm(new MethodFactory(){ @Override public MLMethod factor() { final BasicNetwork result = createNetwork(); ((MLResettable)result).reset(); return result; }},new PilotScore(),500); try { int epoch = 1; for(int i=0;i<50;i++) { train.iteration(); System.out .println("Epoch #" + epoch + " Score:" + train.getError()); epoch++; } train.finishTraining();
source share