1
0
Fork 0
mirror of https://github.com/graphhopper/jsprit.git synced 2020-01-24 07:45:05 +01:00
This commit is contained in:
Stefan Schroeder 2013-06-04 10:25:47 +02:00
commit 3581d6e097
435 changed files with 46952 additions and 0 deletions

View file

@ -0,0 +1,149 @@
/*******************************************************************************
* Copyright (c) 2011 Stefan Schroeder.
* eMail: stefan.schroeder@kit.edu
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Public License v2.0
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*
* Contributors:
* Stefan Schroeder - initial API and implementation
******************************************************************************/
package analysis;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
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.axis.NumberTickUnit;
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;
import basics.VehicleRoutingProblem;
import basics.VehicleRoutingProblemSolution;
import basics.algo.AlgorithmEndsListener;
import basics.algo.IterationEndsListener;
/**
* VehicleRoutingAlgorithm-Listener to record the solution-search-progress.
*
* <p>Register this listener in VehicleRoutingAlgorithm.
*
* @author stefan schroeder
*
*/
public class AlgorithmSearchProgressChartListener implements IterationEndsListener, AlgorithmEndsListener {
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;
/**
* Constructs chart listener with target png-file (filename plus path).
*
* @param pngFileName
*/
public AlgorithmSearchProgressChartListener(String pngFileName) {
super();
this.filename = pngFileName;
}
@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 rangeY = new Range(minValue-0.05*minValue,maxValue + 0.05*maxValue);
yAxis.setRange(rangeY);
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);
}
@Override
public void informIterationEnds(int i, VehicleRoutingProblem problem, Collection<VehicleRoutingProblemSolution> solutions) {
double worst = 0.0;
double best = Double.MAX_VALUE;
double sum = 0.0;
for(VehicleRoutingProblemSolution sol : solutions){
if(sol.getCost() > worst) worst = sol.getCost();
if(sol.getCost() < best) best = sol.getCost();
sum += sol.getCost();
}
bestResultList.add(best);
worstResultList.add(worst);
avgResultList.add(sum/(double)solutions.size());
}
}

View file

@ -0,0 +1,204 @@
/*******************************************************************************
* Copyright (C) 2013 Stefan Schroeder
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Contributors:
* Stefan Schroeder - initial API and implementation
******************************************************************************/
package analysis;
import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
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.axis.NumberTickUnit;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.Range;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import util.Coordinate;
import util.Locations;
import basics.Job;
import basics.Service;
import basics.VehicleRoutingProblem;
import basics.VehicleRoutingProblemSolution;
import basics.route.TourActivity;
import basics.route.Vehicle;
import basics.route.VehicleRoute;
/**
* A plotter to plot vehicle-routing-solution and routes respectively.
*
* @author stefan schroeder
*
*/
public class SolutionPlotter {
private static Logger log = Logger.getLogger(SolutionPlotter.class);
/**
* Plots the solution to pngFile, with title.
*
* <p>This can only plot if vehicles and jobs have locationIds and coordinates (@see Coordinate). Otherwise a warning message is logged
* and method returns but does not plot.
*
* @param vrp
* @param solution
* @param pngFile target path with filename.
* @see VehicleRoutingProblem, VehicleRoutingProblemSolution
*/
public static void plotSolutionAsPNG(VehicleRoutingProblem vrp, VehicleRoutingProblemSolution solution, String pngFile, String plotTitle){
final Map<String,Coordinate> locs = new HashMap<String, Coordinate>();
boolean locationsRetrieved = retrieveLocations(locs,vrp);
if(!locationsRetrieved) return;
Locations locations = new Locations(){
@Override
public Coordinate getCoord(String id) {
return locs.get(id);
}
};
plotRoutesAsPNG(solution.getRoutes(), locations, pngFile, plotTitle);
}
/**
* Plots the a collection of routes to pngFile.
*
*
* @param routes
* @param locations indicating the locations for the tour-activities.
* @param pngFile target path with filename.
* @param plotTitle
* @see VehicleRoute
*/
public static void plotRoutesAsPNG(Collection<VehicleRoute> routes, Locations locations, String pngFile, String plotTitle) {
log.info("plot routes to " + pngFile);
XYSeriesCollection coll = new XYSeriesCollection();
int counter = 1;
double maxX = 0;
double minX = Double.MAX_VALUE;
double maxY = 0;
double minY = Double.MAX_VALUE;
for(VehicleRoute route : routes){
if(route.isEmpty()) continue;
XYSeries series = new XYSeries(counter, false, true);
Coordinate startCoord = locations.getCoord(route.getStart().getLocationId());
series.add(startCoord.getX(), startCoord.getY());
if(startCoord.getX() > maxX) maxX = startCoord.getX();
if(startCoord.getY() > maxY) maxY = startCoord.getY();
if(startCoord.getX() < minX) minX = startCoord.getX();
if(startCoord.getY() < minY) minY = startCoord.getY();
for(TourActivity act : route.getTourActivities().getActivities()){
Coordinate coord = locations.getCoord(act.getLocationId());
series.add(coord.getX(), coord.getY());
if(coord.getX() > maxX) maxX = coord.getX();
if(coord.getY() > maxY) maxY = coord.getY();
if(coord.getX() < minX) minX = coord.getX();
if(coord.getY() < minY) minY = coord.getY();
}
Coordinate endCoord = locations.getCoord(route.getEnd().getLocationId());
series.add(endCoord.getX(), endCoord.getY());
if(endCoord.getX() > maxX) maxX = endCoord.getX();
if(endCoord.getY() > maxY) maxY = endCoord.getY();
if(endCoord.getX() < minX) minX = endCoord.getX();
if(endCoord.getY() < minY) minY = endCoord.getY();
coll.addSeries(series);
counter++;
}
JFreeChart chart = ChartFactory.createXYLineChart(plotTitle,"x-coordinate", "y-coordinate",coll, PlotOrientation.VERTICAL,true,true,false);
XYPlot plot = chart.getXYPlot();
XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) plot.getRenderer();
renderer.setBaseShapesVisible(true);
NumberAxis xAxis = (NumberAxis) plot.getDomainAxis();
xAxis.setTickUnit(new NumberTickUnit(10));
// Range rangeX = new Range(minX - 0.05*minX, maxX+0.05*maxX);
Range rangeX = new Range(minX, maxX);
xAxis.setRange(rangeX);
NumberAxis yAxis = (NumberAxis) plot.getRangeAxis();
// yAxis.setTickUnit(new NumberTickUnit(10));
// Range rangeY = new Range(minY-0.05*minY,maxY + 0.05*maxY);
Range rangeY = new Range(minY,maxY);
yAxis.setRange(rangeY);
try {
ChartUtilities.saveChartAsPNG(new File(pngFile), chart, 1000, 600);
} catch (IOException e) {
log.error("cannot plot");
log.error(e);
e.printStackTrace();
}
}
private static boolean retrieveLocations(Map<String, Coordinate> locs, VehicleRoutingProblem vrp) {
for(Vehicle v : vrp.getVehicles()){
String locationId = v.getLocationId();
if(locationId == null){
log.warn("cannot plot solution, since vehicle " + v + " has no locationId.");
return false;
}
Coordinate coord = v.getCoord();
if(coord == null){
log.warn("cannot plot solution, since vehicle " + v + " has no location-coordinate.");
return false;
}
locs.put(locationId, coord);
}
for(Job j : vrp.getJobs().values()){
if(j instanceof Service){
String locationId = ((Service) j).getLocationId();
if(locationId == null){
log.warn("cannot plot solution, since job " + j + " has no locationId.");
return false;
}
Coordinate coord = ((Service) j).getCoord();
if(coord == null){
log.warn("cannot plot solution, since job " + j + " has no location-coordinate.");
return false;
}
locs.put(locationId, coord);
}
else{
throw new IllegalStateException("job is not a service. this is not supported yet.");
}
}
return true;
}
}

View file

@ -0,0 +1,91 @@
/*******************************************************************************
* Copyright (C) 2013 Stefan Schroeder
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Contributors:
* Stefan Schroeder - initial API and implementation
******************************************************************************/
package analysis;
import basics.VehicleRoutingProblemSolution;
import basics.route.DefaultVehicleRouteCostCalculator;
import basics.route.VehicleRoute;
/**
* Printer to print the details of a vehicle-routing-problem solution.
*
* @author stefan schroeder
*
*/
public class SolutionPrinter {
/**
* Enum to indicate verbose-level.
*
* <p> Print.CONCISE and Print.VERBOSE are available.
*
* @author stefan schroeder
*
*/
public enum Print {
CONCISE,VERBOSE
}
/**
* Prints costs and #vehicles to stdout (System.out.println).
*
* @param solution
*/
public static void print(VehicleRoutingProblemSolution solution){
System.out.println("[costs="+solution.getCost() + "]");
System.out.println("[#vehicles="+solution.getRoutes().size() + "]");
}
/**
* Prints the details of the solution according to a print-level, i.e. Print.CONCISE or PRINT.VERBOSE.
*
* <p>CONCISE prints total-costs and #vehicles.
* <p>VERBOSE prints the route-details additionally. If the DefaultVehicleRouteCostCalculator (which is the standard-calculator)
* is used in VehicleRoute, then route-costs are differentiated further between transport, activity, vehicle, driver and other-costs.
*
* @param solution
* @param level
*/
public static void print(VehicleRoutingProblemSolution solution, Print level){
if(level.equals(Print.CONCISE)){
print(solution);
}
else{
print(solution);
System.out.println("routes");
int routeCount = 1;
for(VehicleRoute route : solution.getRoutes()){
System.out.println("[route="+routeCount+"][departureTime="+route.getStart().getEndTime()+"[total=" + route.getCost() + "]");
if(route.getVehicleRouteCostCalculator() instanceof DefaultVehicleRouteCostCalculator){
DefaultVehicleRouteCostCalculator defaultCalc = (DefaultVehicleRouteCostCalculator) route.getVehicleRouteCostCalculator();
System.out.println("[transport=" + defaultCalc.getTpCosts() + "][activity=" + defaultCalc.getActCosts() +
"][vehicle=" + defaultCalc.getVehicleCosts() + "][driver=" + defaultCalc.getDriverCosts() + "][other=" + defaultCalc.getOther() + "]");
}
routeCount++;
}
}
}
}

View file

@ -0,0 +1,80 @@
/*******************************************************************************
* Copyright (C) 2013 Stefan Schroeder
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Contributors:
* Stefan Schroeder - initial API and implementation
******************************************************************************/
package analysis;
import java.util.Collection;
import org.apache.log4j.Logger;
import basics.VehicleRoutingAlgorithm;
import basics.VehicleRoutingProblem;
import basics.VehicleRoutingProblemSolution;
import basics.algo.AlgorithmEndsListener;
import basics.algo.AlgorithmStartsListener;
public class StopWatch implements AlgorithmStartsListener, AlgorithmEndsListener{
private static Logger log = Logger.getLogger(StopWatch.class);
private double ran;
private double startTime;
@Override
public void informAlgorithmStarts(VehicleRoutingProblem problem, VehicleRoutingAlgorithm algorithm, Collection<VehicleRoutingProblemSolution> solutions) {
reset();
start();
}
public double getCompTimeInSeconds(){
return (ran)/1000.0;
}
@Override
public void informAlgorithmEnds(VehicleRoutingProblem problem, Collection<VehicleRoutingProblemSolution> solutions) {
stop();
log.info("computation time [in sec]: " + getCompTimeInSeconds());
}
public void stop(){
ran += System.currentTimeMillis() - startTime;
}
public void start(){
startTime = System.currentTimeMillis();
}
public void reset(){
startTime = 0;
ran = 0;
}
@Override
public String toString() {
return "stopWatch: " + getCompTimeInSeconds() + " sec";
}
public double getCurrTimeInSeconds() {
return (System.currentTimeMillis()-startTime)/1000.0;
}
}