org.uma.jmetal.runner.multiobjective.SMPSORPWithMultipleReferencePointsAndChartsRunner Maven / Gradle / Ivy
package org.uma.jmetal.runner.multiobjective;
import org.knowm.xchart.BitmapEncoder;
import org.uma.jmetal.algorithm.Algorithm;
import org.uma.jmetal.algorithm.multiobjective.smpso.SMPSORP;
import org.uma.jmetal.measure.MeasureListener;
import org.uma.jmetal.measure.MeasureManager;
import org.uma.jmetal.measure.impl.BasicMeasure;
import org.uma.jmetal.measure.impl.CountingMeasure;
import org.uma.jmetal.operator.MutationOperator;
import org.uma.jmetal.operator.impl.mutation.PolynomialMutation;
import org.uma.jmetal.problem.DoubleProblem;
import org.uma.jmetal.solution.DoubleSolution;
import org.uma.jmetal.util.AlgorithmRunner;
import org.uma.jmetal.util.JMetalException;
import org.uma.jmetal.util.JMetalLogger;
import org.uma.jmetal.util.ProblemUtils;
import org.uma.jmetal.util.archivewithreferencepoint.ArchiveWithReferencePoint;
import org.uma.jmetal.util.archivewithreferencepoint.impl.CrowdingDistanceArchiveWithReferencePoint;
import org.uma.jmetal.util.chartcontainer.ChartContainerWithReferencePoints;
import org.uma.jmetal.util.evaluator.impl.SequentialSolutionListEvaluator;
import org.uma.jmetal.util.fileoutput.SolutionListOutput;
import org.uma.jmetal.util.fileoutput.impl.DefaultFileOutputContext;
import org.uma.jmetal.util.pseudorandom.JMetalRandom;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class SMPSORPWithMultipleReferencePointsAndChartsRunner {
/**
* Program to run the SMPSORP algorithm with three reference points and plotting a graph during the algorithm
* execution. SMPSORP is described in "Extending the Speed-constrained Multi-Objective PSO (SMPSO) With
* Reference Point Based Preference Articulation". Accepted in PPSN 2018.
*
* @author Antonio J. Nebro
*/
public static void main(String[] args) throws JMetalException, IOException {
DoubleProblem problem;
Algorithm> algorithm;
MutationOperator mutation;
String referenceParetoFront = "" ;
//JMetalRandom.getInstance().setSeed(3);
String problemName ;
if (args.length == 1) {
problemName = args[0];
} else if (args.length == 2) {
problemName = args[0] ;
referenceParetoFront = args[1] ;
} else {
problemName = "org.uma.jmetal.problem.multiobjective.zdt.ZDT1" ;
referenceParetoFront = "jmetal-problem/src/test/resources/pareto_fronts/ZDT1.pf" ;
}
problem = (DoubleProblem) ProblemUtils. loadProblem(problemName);
List> referencePoints;
referencePoints = new ArrayList<>();
referencePoints.add(Arrays.asList(0.0, 0.0)) ;
//referencePoints.add(Arrays.asList(0.3, 0.3)) ;
//referencePoints.add(Arrays.asList(0.8, 0.2)) ;
double mutationProbability = 1.0 / problem.getNumberOfVariables() ;
double mutationDistributionIndex = 20.0 ;
mutation = new PolynomialMutation(mutationProbability, mutationDistributionIndex) ;
int maxIterations = 250;
int swarmSize = 100 ;
List> archivesWithReferencePoints = new ArrayList<>();
for (int i = 0 ; i < referencePoints.size(); i++) {
archivesWithReferencePoints.add(
new CrowdingDistanceArchiveWithReferencePoint(
swarmSize/referencePoints.size(), referencePoints.get(i))) ;
}
algorithm = new SMPSORP(problem,
swarmSize,
archivesWithReferencePoints,
referencePoints,
mutation,
maxIterations,
0.0, 1.0,
0.0, 1.0,
2.5, 1.5,
2.5, 1.5,
0.1, 0.1,
-1.0, -1.0,
new SequentialSolutionListEvaluator<>() );
/* Measure management */
MeasureManager measureManager = ((SMPSORP) algorithm).getMeasureManager();
BasicMeasure> solutionListMeasure = (BasicMeasure>) measureManager
.>getPushMeasure("currentPopulation");
CountingMeasure iterationMeasure = (CountingMeasure) measureManager.getPushMeasure("currentIteration");
ChartContainerWithReferencePoints chart = new ChartContainerWithReferencePoints(algorithm.getName(), 80);
chart.setFrontChart(0, 1, referenceParetoFront);
chart.setReferencePoint(referencePoints);
chart.initChart();
solutionListMeasure.register(new ChartListener(chart));
iterationMeasure.register(new IterationListener(chart));
/* End of measure management */
AlgorithmRunner algorithmRunner = new AlgorithmRunner.Executor(algorithm)
.execute() ;
chart.saveChart("RSMPSO", BitmapEncoder.BitmapFormat.PNG);
List population = algorithm.getResult() ;
long computingTime = algorithmRunner.getComputingTime() ;
JMetalLogger.logger.info("Total execution time: " + computingTime + "ms");
new SolutionListOutput(population)
.setSeparator("\t")
.setVarFileOutputContext(new DefaultFileOutputContext("VAR.tsv"))
.setFunFileOutputContext(new DefaultFileOutputContext("FUN.tsv"))
.print();
for (int i = 0 ; i < archivesWithReferencePoints.size(); i++) {
new SolutionListOutput(archivesWithReferencePoints.get(i).getSolutionList())
.setSeparator("\t")
.setVarFileOutputContext(new DefaultFileOutputContext("VAR" + i + ".tsv"))
.setFunFileOutputContext(new DefaultFileOutputContext("FUN" + i + ".tsv"))
.print();
}
System.exit(0);
}
private static class ChartListener implements MeasureListener> {
private ChartContainerWithReferencePoints chart;
private int iteration = 0;
public ChartListener(ChartContainerWithReferencePoints chart) {
this.chart = chart;
this.chart.getFrontChart().setTitle("Iteration: " + this.iteration);
}
private void refreshChart(List solutionList) {
if (this.chart != null) {
iteration++;
this.chart.getFrontChart().setTitle("Iteration: " + this.iteration);
this.chart.updateFrontCharts(solutionList);
this.chart.refreshCharts();
new SolutionListOutput(solutionList)
.setSeparator("\t")
.setVarFileOutputContext(new DefaultFileOutputContext("VAR." + iteration + ".tsv"))
.setFunFileOutputContext(new DefaultFileOutputContext("FUN." + iteration + ".tsv"))
.print();
}
}
@Override
synchronized public void measureGenerated(List solutions) {
refreshChart(solutions);
}
}
private static class IterationListener implements MeasureListener {
ChartContainerWithReferencePoints chart;
public IterationListener(ChartContainerWithReferencePoints chart) {
this.chart = chart;
this.chart.getFrontChart().setTitle("Iteration: " + 0);
}
@Override
synchronized public void measureGenerated(Long iteration) {
if (this.chart != null) {
this.chart.getFrontChart().setTitle("Iteration: " + iteration);
}
}
}
}