mirror of
https://github.com/graphhopper/jsprit.git
synced 2020-01-24 07:45:05 +01:00
simplified analysis.toolbox.AlgorithmSearchProgressChartListener.java
with analysis.toolbox.AlgorithmSearchProgressChartBuilder.java
This commit is contained in:
parent
db4c6f0784
commit
c3043a7431
1 changed files with 7 additions and 95 deletions
|
|
@ -16,11 +16,7 @@
|
|||
******************************************************************************/
|
||||
package jsprit.analysis.toolbox;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import jsprit.core.algorithm.VehicleRoutingAlgorithm;
|
||||
import jsprit.core.algorithm.listener.AlgorithmEndsListener;
|
||||
|
|
@ -30,15 +26,6 @@ import jsprit.core.problem.VehicleRoutingProblem;
|
|||
import jsprit.core.problem.solution.VehicleRoutingProblemSolution;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
import org.jfree.chart.ChartFactory;
|
||||
import org.jfree.chart.ChartUtilities;
|
||||
import org.jfree.chart.JFreeChart;
|
||||
import org.jfree.chart.axis.NumberAxis;
|
||||
import org.jfree.chart.plot.PlotOrientation;
|
||||
import org.jfree.chart.plot.XYPlot;
|
||||
import org.jfree.data.Range;
|
||||
import org.jfree.data.xy.XYSeries;
|
||||
import org.jfree.data.xy.XYSeriesCollection;
|
||||
|
||||
|
||||
|
||||
|
|
@ -55,20 +42,10 @@ public class AlgorithmSearchProgressChartListener implements IterationEndsListen
|
|||
|
||||
private static Logger log = Logger.getLogger(AlgorithmSearchProgressChartListener.class);
|
||||
|
||||
private double[] bestResults;
|
||||
|
||||
private double[] worstResults;
|
||||
|
||||
private double[] avgResults;
|
||||
|
||||
private List<Double> bestResultList = new ArrayList<Double>();
|
||||
|
||||
private List<Double> worstResultList = new ArrayList<Double>();
|
||||
|
||||
private List<Double> avgResultList = new ArrayList<Double>();
|
||||
|
||||
private String filename;
|
||||
|
||||
private AlgorithmSearchProgressChartBuilder chartBuilder;
|
||||
|
||||
/**
|
||||
* Constructs chart listener with target png-file (filename plus path).
|
||||
*
|
||||
|
|
@ -82,70 +59,10 @@ public class AlgorithmSearchProgressChartListener implements IterationEndsListen
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void informAlgorithmEnds(VehicleRoutingProblem problem, Collection<VehicleRoutingProblemSolution> solutions) {
|
||||
log.info("create chart " + filename);
|
||||
if(bestResultList.isEmpty()){
|
||||
log.warn("cannot create chart since no results available.");
|
||||
return;
|
||||
}
|
||||
bestResults = new double[bestResultList.size()];
|
||||
worstResults = new double[worstResultList.size()];
|
||||
avgResults = new double[avgResultList.size()];
|
||||
|
||||
double maxValue = 0.0;
|
||||
double minValue = Double.MAX_VALUE;
|
||||
|
||||
double[] iteration = new double[bestResultList.size()];
|
||||
for (int i = 0; i < bestResultList.size(); i++) {
|
||||
if(bestResultList.get(i) < minValue) minValue = bestResultList.get(i);
|
||||
if(worstResultList.get(i) < minValue) minValue = worstResultList.get(i);
|
||||
if(avgResultList.get(i) < minValue) minValue = avgResultList.get(i);
|
||||
|
||||
if(bestResultList.get(i) > maxValue) maxValue = bestResultList.get(i);
|
||||
if(worstResultList.get(i) > maxValue) maxValue = worstResultList.get(i);
|
||||
if(avgResultList.get(i) > maxValue) maxValue = avgResultList.get(i);
|
||||
|
||||
bestResults[i] = bestResultList.get(i);
|
||||
worstResults[i] = worstResultList.get(i);
|
||||
avgResults[i] = avgResultList.get(i);
|
||||
iteration[i] = i;
|
||||
}
|
||||
XYSeriesCollection coll = new XYSeriesCollection();
|
||||
JFreeChart chart = ChartFactory.createXYLineChart("search-progress","iterations", "results",coll, PlotOrientation.VERTICAL,true,true,false);
|
||||
addSeries("bestResults", iteration, bestResults, coll);
|
||||
addSeries("worstResults", iteration, worstResults, coll);
|
||||
addSeries("avgResults", iteration, avgResults, coll);
|
||||
|
||||
XYPlot plot = chart.getXYPlot();
|
||||
|
||||
NumberAxis yAxis = (NumberAxis) plot.getRangeAxis();
|
||||
Range rangeBounds = coll.getRangeBounds(true);
|
||||
double upper = Math.min(rangeBounds.getUpperBound(), rangeBounds.getLowerBound()*5);
|
||||
if(upper == 0.0){ upper = 10000; }
|
||||
if(rangeBounds.getLowerBound() == upper){
|
||||
yAxis.setRangeWithMargins(rangeBounds.getLowerBound()-rangeBounds.getLowerBound()*.1,upper+upper*.1);
|
||||
}
|
||||
else{
|
||||
yAxis.setRangeWithMargins(rangeBounds.getLowerBound(),upper);
|
||||
}
|
||||
|
||||
|
||||
try {
|
||||
ChartUtilities.saveChartAsJPEG(new File(filename), chart, 1000, 600);
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private void addSeries(String string, double[] iteration, double[] results, XYSeriesCollection coll) {
|
||||
XYSeries series = new XYSeries(string, true, true);
|
||||
for(int i=0;i<iteration.length;i++){
|
||||
series.add(iteration[i], results[i]);
|
||||
}
|
||||
coll.addSeries(series);
|
||||
AlgorithmSearchProgressChartBuilder.saveChartAsPNG(chartBuilder.build(), filename);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -158,20 +75,15 @@ public class AlgorithmSearchProgressChartListener implements IterationEndsListen
|
|||
if(sol.getCost() < best) best = sol.getCost();
|
||||
sum += Math.min(sol.getCost(),Double.MAX_VALUE);
|
||||
}
|
||||
bestResultList.add(best);
|
||||
worstResultList.add(worst);
|
||||
avgResultList.add(sum/(double)solutions.size());
|
||||
chartBuilder.addData("best", i, best);
|
||||
chartBuilder.addData("worst", i, worst);
|
||||
chartBuilder.addData("avg", i, sum/(double)solutions.size());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void informAlgorithmStarts(VehicleRoutingProblem problem,VehicleRoutingAlgorithm algorithm,Collection<VehicleRoutingProblemSolution> solutions) {
|
||||
bestResults = null;
|
||||
worstResults = null;
|
||||
avgResults = null;
|
||||
bestResultList.clear();
|
||||
worstResultList.clear();
|
||||
avgResultList.clear();
|
||||
chartBuilder = AlgorithmSearchProgressChartBuilder.newInstance("search-progress", "iterations", "results");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue