Permalink
Browse files

Extracted duplicated logger class in examples.

Reverted generation numbering change from 0.7.0 (resulted in two generation zeros, the first one being the initial population).
  • Loading branch information...
1 parent 59b06c1 commit 619386adc279eebee8f1f4ee3baa5f2b69ac3dc9 @dwdyer committed Jan 8, 2010
@@ -0,0 +1,33 @@
+//=============================================================================
+// Copyright 2006-2010 Daniel W. Dyer
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//=============================================================================
+package org.uncommons.watchmaker.examples;
+
+import org.uncommons.watchmaker.framework.EvolutionObserver;
+import org.uncommons.watchmaker.framework.PopulationData;
+
+/**
+ * Trivial evolution observer for displaying information at the end
+ * of each generation.
+ * @param <T> The type of entity being evolved.
+ * @author Daniel Dyer
+ */
+public class EvolutionLogger<T> implements EvolutionObserver<T>
+{
+ public void populationUpdate(PopulationData<? extends T> data)
+ {
+ System.out.println("Generation " + data.getGenerationNumber() + ": " + data.getBestCandidateFitness());
+ }
+}
@@ -20,10 +20,9 @@
import org.uncommons.maths.binary.BitString;
import org.uncommons.maths.random.MersenneTwisterRNG;
import org.uncommons.maths.random.Probability;
-import org.uncommons.watchmaker.framework.EvolutionObserver;
+import org.uncommons.watchmaker.examples.EvolutionLogger;
import org.uncommons.watchmaker.framework.EvolutionaryOperator;
import org.uncommons.watchmaker.framework.GenerationalEvolutionEngine;
-import org.uncommons.watchmaker.framework.PopulationData;
import org.uncommons.watchmaker.framework.factories.BitStringFactory;
import org.uncommons.watchmaker.framework.operators.BitStringCrossover;
import org.uncommons.watchmaker.framework.operators.BitStringMutation;
@@ -60,22 +59,9 @@ public static BitString evolveBits(int length)
new RouletteWheelSelection(),
new MersenneTwisterRNG());
engine.setSingleThreaded(true); // Performs better for very trivial fitness evaluations.
- engine.addEvolutionObserver(new EvolutionLogger());
+ engine.addEvolutionObserver(new EvolutionLogger<BitString>());
return engine.evolve(100, // 100 individuals in each generation.
0, // Don't use elitism.
new TargetFitness(length, true)); // Continue until a perfect match is found.
}
-
-
- /**
- * Trivial evolution observer for displaying information at the end
- * of each generation.
- */
- private static class EvolutionLogger implements EvolutionObserver<BitString>
- {
- public void populationUpdate(PopulationData<? extends BitString> data)
- {
- System.out.println("Generation " + data.getGenerationNumber() + ": " + data.getBestCandidate());
- }
- }
}
@@ -21,11 +21,10 @@
import java.util.Map;
import org.uncommons.maths.random.MersenneTwisterRNG;
import org.uncommons.maths.random.Probability;
+import org.uncommons.watchmaker.examples.EvolutionLogger;
import org.uncommons.watchmaker.framework.EvolutionEngine;
-import org.uncommons.watchmaker.framework.EvolutionObserver;
import org.uncommons.watchmaker.framework.EvolutionaryOperator;
import org.uncommons.watchmaker.framework.GenerationalEvolutionEngine;
-import org.uncommons.watchmaker.framework.PopulationData;
import org.uncommons.watchmaker.framework.operators.EvolutionPipeline;
import org.uncommons.watchmaker.framework.selection.RouletteWheelSelection;
import org.uncommons.watchmaker.framework.termination.TargetFitness;
@@ -80,20 +79,7 @@ public static Node evolveProgram(Map<double[], Double> data)
evaluator,
new RouletteWheelSelection(),
new MersenneTwisterRNG());
- engine.addEvolutionObserver(new EvolutionLogger());
+ engine.addEvolutionObserver(new EvolutionLogger<Node>());
return engine.evolve(1000, 5, new TargetFitness(0d, evaluator.isNatural()));
}
-
-
- /**
- * Trivial evolution observer for displaying information at the end
- * of each generation.
- */
- private static class EvolutionLogger implements EvolutionObserver<Node>
- {
- public void populationUpdate(PopulationData<? extends Node> data)
- {
- System.out.println("Generation " + data.getGenerationNumber() + ": " + data.getBestCandidateFitness());
- }
- }
}
@@ -150,6 +150,7 @@ public T evolve(int populationSize,
List<TerminationCondition> satisfiedConditions = EvolutionUtils.shouldContinue(data, conditions);
while (satisfiedConditions == null)
{
+ ++currentGenerationIndex;
evaluatedPopulation = nextEvolutionStep(evaluatedPopulation, eliteCount, rng);
EvolutionUtils.sortEvaluatedPopulation(evaluatedPopulation, fitnessEvaluator.isNatural());
data = EvolutionUtils.getPopulationData(evaluatedPopulation,
@@ -160,7 +161,6 @@ public T evolve(int populationSize,
// Notify observers of the state of the population.
notifyPopulationChange(data);
satisfiedConditions = EvolutionUtils.shouldContinue(data, conditions);
- ++currentGenerationIndex;
}
this.satisfiedTerminationConditions = satisfiedConditions;
return evaluatedPopulation;

0 comments on commit 619386a

Please sign in to comment.