Last active
July 27, 2016 13:25
-
-
Save dhadka/50c13c0cce249a6753bce72565e58e2f to your computer and use it in GitHub Desktop.
Demonstrates how to use a collector to record the population at each generation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Copyright 2009-2016 David Hadka | |
* | |
* This file is part of the MOEA Framework. | |
* | |
* The MOEA Framework is free software: you can redistribute it and/or modify | |
* it under the terms of the GNU Lesser General Public License as published by | |
* the Free Software Foundation, either version 3 of the License, or (at your | |
* option) any later version. | |
* | |
* The MOEA Framework is distributed in the hope that it will be useful, but | |
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | |
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public | |
* License for more details. | |
* | |
* You should have received a copy of the GNU Lesser General Public License | |
* along with the MOEA Framework. If not, see <http://www.gnu.org/licenses/>. | |
*/ | |
import java.io.IOException; | |
import java.util.ArrayList; | |
import java.util.List; | |
import org.moeaframework.Executor; | |
import org.moeaframework.Instrumenter; | |
import org.moeaframework.analysis.collector.Accumulator; | |
import org.moeaframework.analysis.collector.AttachPoint; | |
import org.moeaframework.analysis.collector.Collector; | |
import org.moeaframework.core.EvolutionaryAlgorithm; | |
import org.moeaframework.core.Population; | |
import org.moeaframework.core.Solution; | |
/** | |
* Demonstrates the use of the {@code Instrumenter} to collect the population | |
* of an evolutionary algorithm. | |
*/ | |
public class PopulationCollectorExample { | |
/** | |
* Collects the population from an {@link EvolutionaryAlgorithm}. | |
*/ | |
public static class PopulationCollector implements Collector { | |
/** | |
* The algorithm instance used by this collector; or {@code null} if this | |
* collector has not yet been attached. | |
*/ | |
private final EvolutionaryAlgorithm algorithm; | |
/** | |
* Constructs an unattached collector for recording the population used by | |
* an {@code EvolutionaryAlgorithm}. | |
*/ | |
public PopulationCollector() { | |
this(null); | |
} | |
/** | |
* Constructs a collector for recording the population used by the specified | |
* {@code EvolutionaryAlgorithm}. | |
* | |
* @param algorithm the algorithm this collector records data from | |
*/ | |
public PopulationCollector(EvolutionaryAlgorithm algorithm) { | |
super(); | |
this.algorithm = algorithm; | |
} | |
@Override | |
public AttachPoint getAttachPoint() { | |
return AttachPoint.isSubclass(EvolutionaryAlgorithm.class).and( | |
AttachPoint.not(AttachPoint.isNestedIn( | |
EvolutionaryAlgorithm.class))); | |
} | |
@Override | |
public Collector attach(Object object) { | |
return new PopulationCollector( | |
(EvolutionaryAlgorithm)object); | |
} | |
@Override | |
public void collect(Accumulator accumulator) { | |
ArrayList<Solution> list = new ArrayList<Solution>(); | |
Population population = algorithm.getPopulation(); | |
for (Solution solution : population) { | |
list.add(solution); | |
} | |
accumulator.add("Population", list); | |
} | |
} | |
public static void main(String[] args) throws IOException { | |
// setup the instrumenter to record the population | |
Instrumenter instrumenter = new Instrumenter() | |
.withFrequency(100) | |
.attach(new PopulationCollector()); | |
// use the executor to run the algorithm with the instrumenter | |
new Executor() | |
.withProblem("UF1") | |
.withAlgorithm("NSGAII") | |
.withMaxEvaluations(10000) | |
.withInstrumenter(instrumenter) | |
.run(); | |
Accumulator accumulator = instrumenter.getLastAccumulator(); | |
// print the population at each generation | |
for (int i = 0; i < accumulator.size("NFE"); i++) { | |
System.out.println("NFE: " + accumulator.get("NFE", i)); | |
for (Solution solution : (List<Solution>)accumulator.get("Population", i)) { | |
System.out.print(" V:"); | |
for (int j = 0; j < solution.getNumberOfVariables(); j++) { | |
System.out.print(" "); | |
System.out.print(solution.getVariable(j)); | |
} | |
System.out.print(" O:"); | |
for (int j = 0; j < solution.getNumberOfObjectives(); j++) { | |
System.out.print(" "); | |
System.out.print(solution.getObjective(j)); | |
} | |
System.out.print(" C:"); | |
for (int j = 0; j < solution.getNumberOfConstraints(); j++) { | |
System.out.print(" "); | |
System.out.print(solution.getConstraint(j)); | |
} | |
System.out.println(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment