1
0
Fork 0
mirror of https://github.com/graphhopper/jsprit.git synced 2020-01-24 07:45:05 +01:00

verschlankify jsprit

This commit is contained in:
oblonski 2015-02-12 21:02:26 +01:00
parent 2acfad31ba
commit 1907a66d2a
7 changed files with 0 additions and 1032 deletions

View file

@ -1,82 +0,0 @@
/*******************************************************************************
* Copyright (C) 2014 Stefan Schroeder
*
* This library 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.0 of the License, or (at your option) any later version.
*
* This library 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 this library. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
package jsprit.examples;
import jsprit.analysis.toolbox.AlgorithmSearchProgressChartListener;
import jsprit.analysis.toolbox.StopWatch;
import jsprit.core.algorithm.VehicleRoutingAlgorithm;
import jsprit.core.algorithm.box.GreedySchrimpfFactory;
import jsprit.core.algorithm.box.SchrimpfFactory;
import jsprit.core.algorithm.listener.VehicleRoutingAlgorithmListeners.Priority;
import jsprit.core.algorithm.termination.IterationWithoutImprovementTermination;
import jsprit.core.problem.VehicleRoutingProblem;
import jsprit.instance.reader.SolomonReader;
import jsprit.util.Examples;
public class CompareAlgorithmExample {
public static void main(String[] args) {
/*
* some preparation - create output folder
*/
Examples.createOutputFolder();
/*
* Build the problem.
*
* But define a problem-builder first.
*/
VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
/*
* A solomonReader reads solomon-instance files, and stores the required information in the builder.
*/
new SolomonReader(vrpBuilder).read("input/C101_solomon.txt");
/*
* Finally, the problem can be built. By default, transportCosts are crowFlyDistances (as usually used for vrp-instances).
*/
VehicleRoutingProblem vrp = vrpBuilder.build();
/*
* Get schrimpf with threshold accepting
* Note that Priority.LOW is a way to priorize AlgorithmListeners
*/
VehicleRoutingAlgorithm vra_withThreshold = new SchrimpfFactory().createAlgorithm(vrp);
vra_withThreshold.getAlgorithmListeners().addListener(new AlgorithmSearchProgressChartListener("output/schrimpfThreshold_progress.png"), Priority.LOW);
vra_withThreshold.getAlgorithmListeners().addListener(new StopWatch(), Priority.HIGH);
/*
* Get greedy schrimpf
*/
VehicleRoutingAlgorithm vra_greedy = new GreedySchrimpfFactory().createAlgorithm(vrp);
vra_greedy.getAlgorithmListeners().addListener(new AlgorithmSearchProgressChartListener("output/schrimpfGreedy_progress.png"), Priority.LOW);
vra_greedy.getAlgorithmListeners().addListener(new StopWatch(), Priority.HIGH);
/*
* run both
*/
vra_withThreshold.searchSolutions();
vra_greedy.searchSolutions();
vra_greedy.setPrematureAlgorithmTermination(new IterationWithoutImprovementTermination(40));
vra_greedy.searchSolutions();
}
}

View file

@ -1,201 +0,0 @@
/*******************************************************************************
* Copyright (C) 2014 Stefan Schroeder
*
* This library 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.0 of the License, or (at your option) any later version.
*
* This library 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 this library. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
package jsprit.examples;
import jsprit.analysis.toolbox.ComputationalLaboratory;
import jsprit.analysis.toolbox.ComputationalLaboratory.CalculationListener;
import jsprit.analysis.toolbox.ComputationalLaboratory.DataCollector;
import jsprit.analysis.toolbox.Plotter;
import jsprit.analysis.toolbox.XYLineChartBuilder;
import jsprit.core.algorithm.VehicleRoutingAlgorithm;
import jsprit.core.algorithm.VehicleRoutingAlgorithmFactory;
import jsprit.core.algorithm.io.AlgorithmConfig;
import jsprit.core.algorithm.io.VehicleRoutingAlgorithms;
import jsprit.core.algorithm.listener.IterationStartsListener;
import jsprit.core.problem.VehicleRoutingProblem;
import jsprit.core.problem.solution.VehicleRoutingProblemSolution;
import jsprit.core.reporting.SolutionPrinter;
import jsprit.core.reporting.SolutionPrinter.Print;
import jsprit.core.util.BenchmarkInstance;
import jsprit.core.util.Solutions;
import jsprit.instance.reader.SolomonReader;
import jsprit.util.Examples;
import org.apache.commons.configuration.XMLConfiguration;
import java.util.Collection;
/**
* Based on Solomon's R101 instance
*
* @author schroeder
*
*/
public class ComputationalExperiments_alphaSenstivity {
public static void main(String[] args) {
/*
* some preparation - create output folder
*/
Examples.createOutputFolder();
/*
* Build the problem.
*
* But define a problem-builder first.
*/
VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
/*
* A solomonReader reads solomon-instance files, and stores the required information in the builder.
*/
new SolomonReader(vrpBuilder,100).read("input/R101.txt");
/*
* Finally, the problem can be built. By default, transportCosts are crowFlyDistances (as usually used for vrp-instances).
*/
VehicleRoutingProblem vrp = vrpBuilder.build();
/*
* Create ComputationalLaboratory
*/
ComputationalLaboratory computationalLab = new ComputationalLaboratory();
/*
* add benchmarking instance
*/
computationalLab.addInstance("SolomonR101", vrp);
/*
* add algorithms through factories
*
*
*
*/
for(double alphaVal=0.;alphaVal<.4;alphaVal+=.1){
final String alpha = String.valueOf(alphaVal).substring(0, 3);
computationalLab.addAlgorithmFactory("alpha_"+alpha, new VehicleRoutingAlgorithmFactory() {
@Override
public VehicleRoutingAlgorithm createAlgorithm(VehicleRoutingProblem vrp) {
return VehicleRoutingAlgorithms.createAlgorithm(vrp, getAlgorithmConfig(alpha));
}
});
}
/*
* plot search progress of different algorithms
*/
final XYLineChartBuilder chartBuilder = XYLineChartBuilder.newInstance("alpha-sensitivity", "iterations", "costs");
computationalLab.addListener(new CalculationListener() {
@Override
public void calculationStarts(BenchmarkInstance p, final String algorithmName,VehicleRoutingAlgorithm algorithm, int run) {
algorithm.addListener(new IterationStartsListener() {
@Override
public void informIterationStarts(int i, VehicleRoutingProblem problem,Collection<VehicleRoutingProblemSolution> solutions) {
/*
* plot only distance-costs, i.e. without fixed costs
*/
VehicleRoutingProblemSolution bestOf = Solutions.bestOf(solutions);
chartBuilder.addData(algorithmName, i, bestOf.getCost()-bestOf.getRoutes().size()*100.);
}
});
}
@Override
public void calculationEnds(BenchmarkInstance p, String algorithmName,VehicleRoutingAlgorithm algorithm, int run,Collection<VehicleRoutingProblemSolution> solutions) {}
});
/*
* define dataCollector to collect an arbitrary number of indicators as well as solutions
*/
final DataCollector dataCollector = new DataCollector();
computationalLab.addListener(new CalculationListener() {
@Override
public void calculationStarts(BenchmarkInstance p, String algorithmName,VehicleRoutingAlgorithm algorithm, int run) {}
@Override
public void calculationEnds(BenchmarkInstance p, String algorithmName,VehicleRoutingAlgorithm algorithm, int run,Collection<VehicleRoutingProblemSolution> solutions) {
//memorize solution
dataCollector.addSolution(p.name, algorithmName, run, Solutions.bestOf(solutions));
}
});
/*
* determine number of threads to be used
*/
computationalLab.setThreads(2);
/*
* run the experiments
*/
computationalLab.run();
/*
* plot the lineChart
*/
XYLineChartBuilder.saveChartAsPNG(chartBuilder.build(), "output/computationalStudies_alphaSensitivity.png");
/*
* print best solution
*/
SolutionPrinter.print(vrp, Solutions.bestOf(dataCollector.getSolutions()), Print.VERBOSE);
/*
* plot best
*/
Plotter plotter = new Plotter(vrp,Solutions.bestOf(dataCollector.getSolutions()));
plotter.plot("output/bestOf.png", "bestOfR101");
}
private static AlgorithmConfig getAlgorithmConfig(String alpha) {
AlgorithmConfig config = new AlgorithmConfig();
XMLConfiguration xmlConfig = config.getXMLConfiguration();
xmlConfig.setProperty("iterations",10000);
xmlConfig.setProperty("construction.insertion[@name]","bestInsertion");
xmlConfig.setProperty("strategy.memory", 1);
String searchStrategy = "strategy.searchStrategies.searchStrategy";
xmlConfig.setProperty(searchStrategy + "(0).selector[@name]","selectBest");
xmlConfig.setProperty(searchStrategy + "(0).acceptor[@name]","schrimpfAcceptance");
xmlConfig.setProperty(searchStrategy + "(0).acceptor.alpha",alpha);
xmlConfig.setProperty(searchStrategy + "(0).acceptor.warmup","50");
xmlConfig.setProperty(searchStrategy + "(0).modules.module(0)[@name]","ruin_and_recreate");
xmlConfig.setProperty(searchStrategy + "(0).modules.module(0).ruin[@name]","randomRuin");
xmlConfig.setProperty(searchStrategy + "(0).modules.module(0).ruin.share","0.3");
xmlConfig.setProperty(searchStrategy + "(0).modules.module(0).insertion[@name]","bestInsertion");
xmlConfig.setProperty(searchStrategy + "(0).probability",".5");
xmlConfig.setProperty(searchStrategy + "(1).selector[@name]","selectBest");
xmlConfig.setProperty(searchStrategy + "(1).acceptor[@name]","schrimpfAcceptance");
xmlConfig.setProperty(searchStrategy + "(1).modules.module(0)[@name]","ruin_and_recreate");
xmlConfig.setProperty(searchStrategy + "(1).modules.module(0).ruin[@name]","radialRuin");
xmlConfig.setProperty(searchStrategy + "(1).modules.module(0).ruin.share","0.1");
xmlConfig.setProperty(searchStrategy + "(1).modules.module(0).insertion[@name]","bestInsertion");
xmlConfig.setProperty(searchStrategy + "(1).probability","0.5");
return config;
}
}

View file

@ -1,136 +0,0 @@
/*******************************************************************************
* Copyright (C) 2014 Stefan Schroeder
*
* This library 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.0 of the License, or (at your option) any later version.
*
* This library 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 this library. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
package jsprit.examples;
import jsprit.analysis.toolbox.ComputationalLaboratory;
import jsprit.analysis.toolbox.ComputationalLaboratory.CalculationListener;
import jsprit.analysis.toolbox.XYLineChartBuilder;
import jsprit.core.algorithm.VehicleRoutingAlgorithm;
import jsprit.core.algorithm.VehicleRoutingAlgorithmFactory;
import jsprit.core.algorithm.io.VehicleRoutingAlgorithms;
import jsprit.core.algorithm.listener.IterationStartsListener;
import jsprit.core.problem.VehicleRoutingProblem;
import jsprit.core.problem.solution.VehicleRoutingProblemSolution;
import jsprit.core.util.BenchmarkInstance;
import jsprit.core.util.Solutions;
import jsprit.instance.reader.SolomonReader;
import jsprit.util.Examples;
import java.util.Collection;
/**
* Based on Solomon's R101 instance
*
* @author schroeder
*
*/
public class ComputationalExperiments_randomVariations {
public static void main(String[] args) {
/*
* some preparation - create output folder
*/
Examples.createOutputFolder();
/*
* Build the problem.
*
* But define a problem-builder first.
*/
VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
/*
* A solomonReader reads solomon-instance files, and stores the required information in the builder.
*/
new SolomonReader(vrpBuilder).read("input/R101.txt");
/*
* Finally, the problem can be built. By default, transportCosts are crowFlyDistances (as usually used for vrp-instances).
*/
VehicleRoutingProblem vrp = vrpBuilder.build();
/*
* Create ComputationalLaboratory
*/
ComputationalLaboratory computationalLab = new ComputationalLaboratory();
/*
* add benchmarking instance
*/
computationalLab.addInstance("SolomonR101", vrp);
/*
* add algorithms through factories
*
*
*
*/
computationalLab.addAlgorithmFactory("schrimpfAcceptance", new VehicleRoutingAlgorithmFactory() {
@Override
public VehicleRoutingAlgorithm createAlgorithm(VehicleRoutingProblem vrp) {
VehicleRoutingAlgorithm vra = VehicleRoutingAlgorithms.readAndCreateAlgorithm(vrp, "input/algorithmConfigWithSchrimpfAcceptance.xml");
vra.setMaxIterations(4000);
return vra;
}
});
/*
* run schrimpfAcceptance 5 times (and thus each with another seed of random number generator)
*/
computationalLab.setNuOfRuns(5);
/*
* plot search progress of different algorithms
*/
final XYLineChartBuilder chartBuilder = XYLineChartBuilder.newInstance("random variations", "iterations", "costs");
computationalLab.addListener(new CalculationListener() {
@Override
public void calculationStarts(BenchmarkInstance p, final String algorithmName,VehicleRoutingAlgorithm algorithm, final int run) {
algorithm.addListener(new IterationStartsListener() {
@Override
public void informIterationStarts(int i, VehicleRoutingProblem problem,Collection<VehicleRoutingProblemSolution> solutions) {
/*
* since there will be more than 1 run and we want to plot each run, we need to specify an apropriate
* XYSeries-name. Thus we add run to algorithmName.
*/
chartBuilder.addData(algorithmName+"_"+run, i, Solutions.bestOf(solutions).getCost());
}
});
}
@Override
public void calculationEnds(BenchmarkInstance p, String algorithmName,VehicleRoutingAlgorithm algorithm, int run,Collection<VehicleRoutingProblemSolution> solutions) {}
});
computationalLab.setThreads(2);
/*
* run the experiments
*/
computationalLab.run();
/*
* plot the lineChart
*/
XYLineChartBuilder.saveChartAsPNG(chartBuilder.build(), "output/computationalStudies_randomVariations.png");
}
}

View file

@ -1,173 +0,0 @@
/*******************************************************************************
* Copyright (C) 2014 Stefan Schroeder
*
* This library 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.0 of the License, or (at your option) any later version.
*
* This library 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 this library. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
package jsprit.examples;
import jsprit.analysis.toolbox.ComputationalLaboratory;
import jsprit.analysis.toolbox.ComputationalLaboratory.CalculationListener;
import jsprit.analysis.toolbox.ComputationalLaboratory.DataCollector;
import jsprit.analysis.toolbox.XYLineChartBuilder;
import jsprit.core.algorithm.VehicleRoutingAlgorithm;
import jsprit.core.algorithm.VehicleRoutingAlgorithmFactory;
import jsprit.core.algorithm.io.VehicleRoutingAlgorithms;
import jsprit.core.problem.VehicleRoutingProblem;
import jsprit.core.problem.solution.VehicleRoutingProblemSolution;
import jsprit.core.util.BenchmarkInstance;
import jsprit.core.util.Solutions;
import jsprit.instance.reader.SolomonReader;
import jsprit.util.Examples;
import java.util.Collection;
/**
* Based on Solomon's R101 instance
*
* @author schroeder
*
*/
public class ComputationalExperiments_randomVariations_and_nuOfIterations {
public static void main(String[] args) {
/*
* some preparation - create output folder
*/
Examples.createOutputFolder();
/*
* Build the problem.
*
* But define a problem-builder first.
*/
VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
/*
* A solomonReader reads solomon-instance files, and stores the required information in the builder.
*/
new SolomonReader(vrpBuilder).read("input/R101.txt");
/*
* Finally, the problem can be built. By default, transportCosts are crowFlyDistances (as usually used for vrp-instances).
*/
VehicleRoutingProblem vrp = vrpBuilder.build();
/*
* Create ComputationalLaboratory
*/
ComputationalLaboratory computationalLab = new ComputationalLaboratory();
/*
* add benchmarking instance
*/
computationalLab.addInstance("SolomonR101", vrp);
/*
* add algorithms through factories
*
*
*
*/
computationalLab.addAlgorithmFactory("schrimpfAcceptance_1", getAlgorithmFactory(1));
computationalLab.addAlgorithmFactory("schrimpfAcceptance_100", getAlgorithmFactory(100));
computationalLab.addAlgorithmFactory("schrimpfAcceptance_500", getAlgorithmFactory(500));
computationalLab.addAlgorithmFactory("schrimpfAcceptance_1000", getAlgorithmFactory(1000));
computationalLab.addAlgorithmFactory("schrimpfAcceptance_2000", getAlgorithmFactory(2000));
computationalLab.addAlgorithmFactory("schrimpfAcceptance_4000", getAlgorithmFactory(4000));
computationalLab.addAlgorithmFactory("schrimpfAcceptance_8000", getAlgorithmFactory(8000));
computationalLab.addAlgorithmFactory("schrimpfAcceptance_12000", getAlgorithmFactory(12000));
/*
* run schrimpfAcceptance 5 times (and thus each with another seed of random number generator)
*/
computationalLab.setNuOfRuns(5);
/*
* plot search progress of different algorithms
*/
final DataCollector dataCollector = new DataCollector();
computationalLab.addListener(new CalculationListener() {
@Override
public void calculationStarts(BenchmarkInstance p, final String algorithmName,VehicleRoutingAlgorithm algorithm, final int run) {}
@Override
public void calculationEnds(BenchmarkInstance p, String algorithmName,VehicleRoutingAlgorithm algorithm, int run,Collection<VehicleRoutingProblemSolution> solutions) {
dataCollector.addDate("R101", algorithmName, run, "costs", Solutions.bestOf(solutions).getCost());
}
});
computationalLab.setThreads(2);
/*
* run the experiments
*/
computationalLab.run();
/*
* plot min, avg and max
*/
XYLineChartBuilder chartBuilder = XYLineChartBuilder.newInstance("variations with iterations", "iterations", "costs");
for(String algorithmName : computationalLab.getAlgorithmNames()){
String[] nameTokens = algorithmName.split("_");
int iteration = Integer.parseInt(nameTokens[1]);
chartBuilder.addData("min", iteration, min(dataCollector.getData("R101", algorithmName, "costs")));
chartBuilder.addData("max", iteration, max(dataCollector.getData("R101", algorithmName, "costs")));
chartBuilder.addData("avg", iteration, avg(dataCollector.getData("R101", algorithmName, "costs")));
}
XYLineChartBuilder.saveChartAsPNG(chartBuilder.build(), "output/computationalStudies_min_max_avg.png");
}
public static double min(Collection<Double> doubles){
double min = Double.MAX_VALUE;
for(Double d : doubles){
if(d<min) min=d;
}
return min;
}
public static double max(Collection<Double> doubles){
double max = 0.;
for(Double d : doubles){
if(d>max) max=d;
}
return max;
}
public static double avg(Collection<Double> doubles){
if(doubles.isEmpty()) return 0.;
double sum = 0.;
for(Double d : doubles){
sum+=d;
}
return sum/(double)doubles.size();
}
private static VehicleRoutingAlgorithmFactory getAlgorithmFactory(final int iterations) {
return new VehicleRoutingAlgorithmFactory() {
@Override
public VehicleRoutingAlgorithm createAlgorithm(VehicleRoutingProblem vrp) {
VehicleRoutingAlgorithm vra = VehicleRoutingAlgorithms.readAndCreateAlgorithm(vrp, "input/algorithmConfigWithSchrimpfAcceptance.xml");
vra.setMaxIterations(iterations);
return vra;
}
};
}
}

View file

@ -1,140 +0,0 @@
/*******************************************************************************
* Copyright (C) 2014 Stefan Schroeder
*
* This library 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.0 of the License, or (at your option) any later version.
*
* This library 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 this library. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
package jsprit.examples;
import jsprit.analysis.toolbox.ComputationalLaboratory;
import jsprit.analysis.toolbox.ComputationalLaboratory.CalculationListener;
import jsprit.analysis.toolbox.XYLineChartBuilder;
import jsprit.core.algorithm.VehicleRoutingAlgorithm;
import jsprit.core.algorithm.VehicleRoutingAlgorithmFactory;
import jsprit.core.algorithm.io.VehicleRoutingAlgorithms;
import jsprit.core.algorithm.listener.IterationStartsListener;
import jsprit.core.problem.VehicleRoutingProblem;
import jsprit.core.problem.solution.VehicleRoutingProblemSolution;
import jsprit.core.util.BenchmarkInstance;
import jsprit.core.util.Solutions;
import jsprit.instance.reader.SolomonReader;
import jsprit.util.Examples;
import java.util.Collection;
/**
* Based on Solomon's R101 instance
*
* @author schroeder
*
*/
public class ComputationalExperiments_schrimpf_vs_greedy {
public static void main(String[] args) {
/*
* some preparation - create output folder
*/
Examples.createOutputFolder();
/*
* Build the problem.
*
* But define a problem-builder first.
*/
VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
/*
* A solomonReader reads solomon-instance files, and stores the required information in the builder.
*/
new SolomonReader(vrpBuilder).read("input/R101.txt");
/*
* Finally, the problem can be built. By default, transportCosts are crowFlyDistances (as usually used for vrp-instances).
*/
VehicleRoutingProblem vrp = vrpBuilder.build();
/*
* Create ComputationalLaboratory
*/
ComputationalLaboratory computationalLab = new ComputationalLaboratory();
/*
* add benchmarking instance
*/
computationalLab.addInstance("SolomonR101", vrp);
/*
* add algorithms through factories
*
*
* Define 2 algorithms
* - algorithmsConfigWithSchrimpfAcceptance (4000 iterations)
* - algorithmsConfigWithGreedyAcceptance (4000 iterations)
*
*/
computationalLab.addAlgorithmFactory("schrimpfAcceptance", new VehicleRoutingAlgorithmFactory() {
@Override
public VehicleRoutingAlgorithm createAlgorithm(VehicleRoutingProblem vrp) {
VehicleRoutingAlgorithm vra = VehicleRoutingAlgorithms.readAndCreateAlgorithm(vrp, "input/algorithmConfigWithSchrimpfAcceptance.xml");
vra.setMaxIterations(4000);
return vra;
}
});
computationalLab.addAlgorithmFactory("greedyAcceptance", new VehicleRoutingAlgorithmFactory() {
@Override
public VehicleRoutingAlgorithm createAlgorithm(VehicleRoutingProblem vrp) {
VehicleRoutingAlgorithm vra = VehicleRoutingAlgorithms.readAndCreateAlgorithm(vrp, "input/algorithmConfig_solomon.xml");
vra.setMaxIterations(4000);
return vra;
}
});
/*
* plot search progress of different algorithms
*/
final XYLineChartBuilder chartBuilder = XYLineChartBuilder.newInstance("schrimpf vs greedy", "iterations", "costs");
computationalLab.addListener(new CalculationListener() {
@Override
public void calculationStarts(BenchmarkInstance p, final String algorithmName,VehicleRoutingAlgorithm algorithm, int run) {
algorithm.addListener(new IterationStartsListener() {
@Override
public void informIterationStarts(int i, VehicleRoutingProblem problem,Collection<VehicleRoutingProblemSolution> solutions) {
chartBuilder.addData(algorithmName, i, Solutions.bestOf(solutions).getCost());
}
});
}
@Override
public void calculationEnds(BenchmarkInstance p, String algorithmName,VehicleRoutingAlgorithm algorithm, int run,Collection<VehicleRoutingProblemSolution> solutions) {}
});
computationalLab.setThreads(2);
/*
* run the experiments
*/
computationalLab.run();
/*
* plot the lineChart
*/
XYLineChartBuilder.saveChartAsPNG(chartBuilder.build(), "output/computationalStudies_schrimpf_vs_greedy.png");
}
}

View file

@ -1,220 +0,0 @@
/*******************************************************************************
* Copyright (C) 2014 Stefan Schroeder
*
* This library 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.0 of the License, or (at your option) any later version.
*
* This library 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 this library. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
package jsprit.examples;
import jsprit.analysis.toolbox.GraphStreamViewer;
import jsprit.analysis.toolbox.GraphStreamViewer.Label;
import jsprit.core.algorithm.VehicleRoutingAlgorithm;
import jsprit.core.algorithm.VehicleRoutingAlgorithmBuilder;
import jsprit.core.algorithm.state.StateManager;
import jsprit.core.algorithm.termination.IterationWithoutImprovementTermination;
import jsprit.core.problem.Location;
import jsprit.core.problem.VehicleRoutingProblem;
import jsprit.core.problem.VehicleRoutingProblem.FleetSize;
import jsprit.core.problem.constraint.ConstraintManager;
import jsprit.core.problem.constraint.HardRouteConstraint;
import jsprit.core.problem.job.Shipment;
import jsprit.core.problem.misc.JobInsertionContext;
import jsprit.core.problem.solution.VehicleRoutingProblemSolution;
import jsprit.core.problem.vehicle.VehicleImpl;
import jsprit.core.problem.vehicle.VehicleImpl.Builder;
import jsprit.core.problem.vehicle.VehicleType;
import jsprit.core.problem.vehicle.VehicleTypeImpl;
import jsprit.core.reporting.SolutionPrinter;
import jsprit.core.util.Coordinate;
import jsprit.core.util.Solutions;
import jsprit.util.Examples;
import java.util.Collection;
public class EnRoutePickupAndDeliveryWithMultipleDepotsAndVehicleAccessConstraintAndSpecifiedVehicleEndLocationsExample {
public static void main(String[] args) {
/*
* some preparation - create output folder
*/
Examples.createOutputFolder();
/*
* get a vehicle type-builder and build a type with the typeId "vehicleType" and a capacity of 2
*/
VehicleTypeImpl.Builder vehicleTypeBuilder = VehicleTypeImpl.Builder.newInstance("vehicleType").addCapacityDimension(0, 2);
vehicleTypeBuilder.setCostPerDistance(1.0);
VehicleType vehicleType = vehicleTypeBuilder.build();
/*
* define two vehicles and their locations.
*
* this example employs two vehicles. one that has to return to its start-location (vehicle1) and one that has a different
* end-location.
*
* play with these location to see which impact they have on customer-sequences.
*/
Builder vehicleBuilder1 = VehicleImpl.Builder.newInstance("v1");
vehicleBuilder1.setStartLocation(loc(Coordinate.newInstance(10, 10)));
vehicleBuilder1.setType(vehicleType);
VehicleImpl vehicle1 = vehicleBuilder1.build();
Builder vehicleBuilder2 = VehicleImpl.Builder.newInstance("v2");
vehicleBuilder2.setStartLocation(loc(Coordinate.newInstance(30, 30))).setEndLocation(loc(Coordinate.newInstance(30, 19)));
vehicleBuilder2.setType(vehicleType);
VehicleImpl vehicle2 = vehicleBuilder2.build();
/*
* build shipments at the required locations, each with a capacity-demand of 1.
*
*/
Shipment shipment1 = Shipment.Builder.newInstance("1").addSizeDimension(0, 1).setPickupLocation(loc(Coordinate.newInstance(5, 7))).setDeliveryLocation(loc(Coordinate.newInstance(6, 9))).build();
Shipment shipment2 = Shipment.Builder.newInstance("2").addSizeDimension(0, 1).setPickupLocation(loc(Coordinate.newInstance(5, 13))).setDeliveryLocation(loc(Coordinate.newInstance(6, 11))).build();
Shipment shipment3 = Shipment.Builder.newInstance("3").addSizeDimension(0, 1).setPickupLocation(loc(Coordinate.newInstance(15, 7))).setDeliveryLocation(loc(Coordinate.newInstance(14, 9))).build();
Shipment shipment4 = Shipment.Builder.newInstance("4").addSizeDimension(0, 1).setPickupLocation(loc(Coordinate.newInstance(15, 13))).setDeliveryLocation(loc(Coordinate.newInstance(14, 11))).build();
Shipment shipment5 = Shipment.Builder.newInstance("5").addSizeDimension(0, 1).setPickupLocation(loc(Coordinate.newInstance(25, 27))).setDeliveryLocation(loc(Coordinate.newInstance(26, 29))).build();
Shipment shipment6 = Shipment.Builder.newInstance("6").addSizeDimension(0, 1).setPickupLocation(loc(Coordinate.newInstance(25, 33))).setDeliveryLocation(loc(Coordinate.newInstance(26, 31))).build();
Shipment shipment7 = Shipment.Builder.newInstance("7").addSizeDimension(0, 1).setPickupLocation(loc(Coordinate.newInstance(35, 27))).setDeliveryLocation(loc(Coordinate.newInstance(34, 29))).build();
Shipment shipment8 = Shipment.Builder.newInstance("8").addSizeDimension(0, 1).setPickupLocation(loc(Coordinate.newInstance(35, 33))).setDeliveryLocation(loc(Coordinate.newInstance(34, 31))).build();
Shipment shipment9 = Shipment.Builder.newInstance("9").addSizeDimension(0, 1).setPickupLocation(loc(Coordinate.newInstance(5, 27))).setDeliveryLocation(loc(Coordinate.newInstance(6, 29))).build();
Shipment shipment10 = Shipment.Builder.newInstance("10").addSizeDimension(0, 1).setPickupLocation(loc(Coordinate.newInstance(5, 33))).setDeliveryLocation(loc(Coordinate.newInstance(6, 31))).build();
Shipment shipment11 = Shipment.Builder.newInstance("11").addSizeDimension(0, 1).setPickupLocation(loc(Coordinate.newInstance(15, 27))).setDeliveryLocation(loc(Coordinate.newInstance(14, 29))).build();
Shipment shipment12 = Shipment.Builder.newInstance("12").addSizeDimension(0, 1).setPickupLocation(loc(Coordinate.newInstance(15, 33))).setDeliveryLocation(loc(Coordinate.newInstance(14, 31))).build();
Shipment shipment13 = Shipment.Builder.newInstance("13").addSizeDimension(0, 1).setPickupLocation(loc(Coordinate.newInstance(25, 7))).setDeliveryLocation(loc(Coordinate.newInstance(26, 9))).build();
Shipment shipment14 = Shipment.Builder.newInstance("14").addSizeDimension(0, 1).setPickupLocation(loc(Coordinate.newInstance(25, 13))).setDeliveryLocation(loc(Coordinate.newInstance(26, 11))).build();
Shipment shipment15 = Shipment.Builder.newInstance("15").addSizeDimension(0, 1).setPickupLocation(loc(Coordinate.newInstance(35, 7))).setDeliveryLocation(loc(Coordinate.newInstance(34, 9))).build();
Shipment shipment16 = Shipment.Builder.newInstance("16").addSizeDimension(0, 1).setPickupLocation(loc(Coordinate.newInstance(35, 13))).setDeliveryLocation(loc(Coordinate.newInstance(34, 11))).build();
Shipment shipment17 = Shipment.Builder.newInstance("17").addSizeDimension(0, 1).setPickupLocation(loc(Coordinate.newInstance(5, 14))).setDeliveryLocation(loc(Coordinate.newInstance(6, 16))).build();
Shipment shipment18 = Shipment.Builder.newInstance("18").addSizeDimension(0, 1).setPickupLocation(loc(Coordinate.newInstance(5, 20))).setDeliveryLocation(loc(Coordinate.newInstance(6, 18))).build();
Shipment shipment19 = Shipment.Builder.newInstance("19").addSizeDimension(0, 1).setPickupLocation(loc(Coordinate.newInstance(15, 14))).setDeliveryLocation(loc(Coordinate.newInstance(14, 16))).build();
Shipment shipment20 = Shipment.Builder.newInstance("20").addSizeDimension(0, 1).setPickupLocation(loc(Coordinate.newInstance(15, 20))).setDeliveryLocation(loc(Coordinate.newInstance(14, 18))).build();
VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
vrpBuilder.addVehicle(vehicle1).addVehicle(vehicle2);
vrpBuilder.addJob(shipment1).addJob(shipment2).addJob(shipment3).addJob(shipment4);
vrpBuilder.addJob(shipment5).addJob(shipment6).addJob(shipment7).addJob(shipment8);
vrpBuilder.addJob(shipment9).addJob(shipment10).addJob(shipment11).addJob(shipment12);
vrpBuilder.addJob(shipment13).addJob(shipment14).addJob(shipment15).addJob(shipment16);
vrpBuilder.addJob(shipment17).addJob(shipment18).addJob(shipment19).addJob(shipment20);
//you only have two vehicles
vrpBuilder.setFleetSize(FleetSize.FINITE);
//build the problem
VehicleRoutingProblem problem = vrpBuilder.build();
/*
* add a geographic constraint determining that vehicle1 cannot go to x>15 and vehicle2 cannot go to x<15
*
* switch off the geoConstraints to see the impact of this constraint on routes, or just exchange v1 and v2 to reverse the geo-constraint.
*/
HardRouteConstraint geoClusterConstraint = new HardRouteConstraint() {
@Override
public boolean fulfilled(JobInsertionContext insertionContext) {
Shipment shipment2insert = ((Shipment)insertionContext.getJob());
if(insertionContext.getNewVehicle().getId().equals("v1")){
if(shipment2insert.getPickupLocation().getCoordinate().getX() > 15. || shipment2insert.getDeliveryLocation().getCoordinate().getX() > 15.){
return false;
}
}
if(insertionContext.getNewVehicle().getId().equals("v2")){
if(shipment2insert.getPickupLocation().getCoordinate().getX() < 15. || shipment2insert.getDeliveryLocation().getCoordinate().getX() < 15.){
return false;
}
}
return true;
}
};
/*
* get a sample algorithm.
*
* Note that you need to make sure to prohibit vehicle-switching by adding the insertion-tag <vehicleSwitchAllowed>false</vehicleSwitchAllowed>.
* This way you make sure that no vehicle can take over a route that is employed by another. Allowing this might make sense when dealing with
* a heterogeneous fleet and you want to employ a bigger vehicle on a still existing route. However, allowing it makes constraint-checking
* bit more complicated and you cannot just add the above hard-constraint. Latter will be covered in another example.
*
*/
VehicleRoutingAlgorithmBuilder vraBuilder = new VehicleRoutingAlgorithmBuilder(problem,"input/algorithmConfig_noVehicleSwitch.xml");
vraBuilder.addCoreConstraints();
vraBuilder.addDefaultCostCalculators();
StateManager stateManager = new StateManager(problem);
ConstraintManager constraintManager = new ConstraintManager(problem,stateManager);
constraintManager.addConstraint(geoClusterConstraint);
vraBuilder.setStateAndConstraintManager(stateManager,constraintManager);
VehicleRoutingAlgorithm algorithm = vraBuilder.build();
algorithm.setPrematureAlgorithmTermination(new IterationWithoutImprovementTermination(100));
// algorithm.setMaxIterations(30000);
/*
* and search a solution
*/
Collection<VehicleRoutingProblemSolution> solutions = algorithm.searchSolutions();
/*
* get the best
*/
VehicleRoutingProblemSolution bestSolution = Solutions.bestOf(solutions);
/*
* write out problem and solution to xml-file
*/
// new VrpXMLWriter(problem, solutions).write("output/shipment-problem-with-solution.xml");
/*
* print nRoutes and totalCosts of bestSolution
*/
SolutionPrinter.print(bestSolution);
/*
* plot problem without solution
*/
// Plotter problemPlotter = new Plotter(problem);
// problemPlotter.plotShipments(true);
// problemPlotter.plot("output/enRoutePickupAndDeliveryWithMultipleLocationsExample_problem.png", "en-route pickup and delivery");
//
// /*
// * plot problem with solution
// */
// Plotter solutionPlotter = new Plotter(problem,Arrays.asList(Solutions.bestOf(solutions).getRoutes().iterator().next()));
// solutionPlotter.plotShipments(true);
// solutionPlotter.plot("output/enRoutePickupAndDeliveryWithMultipleLocationsExample_solution.png", "en-route pickup and delivery");
new GraphStreamViewer(problem).labelWith(Label.ID).setRenderDelay(100).setRenderShipments(true).display();
new GraphStreamViewer(problem,Solutions.bestOf(solutions)).labelWith(Label.ACTIVITY).setRenderDelay(100).setRenderShipments(true).display();
}
private static Location loc(Coordinate coordinate) {
return Location.Builder.newInstance().setCoordinate(coordinate).build();
}
}

View file

@ -1,80 +0,0 @@
/*******************************************************************************
* Copyright (C) 2014 Stefan Schroeder
*
* This library 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.0 of the License, or (at your option) any later version.
*
* This library 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 this library. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
package jsprit.examples;
import jsprit.analysis.toolbox.AlgorithmSearchProgressChartListener;
import jsprit.analysis.toolbox.GraphStreamViewer;
import jsprit.analysis.toolbox.Plotter;
import jsprit.core.algorithm.VehicleRoutingAlgorithm;
import jsprit.core.algorithm.io.VehicleRoutingAlgorithms;
import jsprit.core.problem.VehicleRoutingProblem;
import jsprit.core.problem.solution.VehicleRoutingProblemSolution;
import jsprit.core.reporting.SolutionPrinter;
import jsprit.core.util.Solutions;
import jsprit.instance.reader.VrphGoldenReader;
import jsprit.instance.reader.VrphGoldenReader.VrphType;
import jsprit.util.Examples;
import java.util.Collection;
/**
* Shows how to benchmark the algorithm on different classical HVRP and FSM instances.
*
* <p>These instances are from Golden and Taillard and copied from
* <a href=http://mistic.heig-vd.ch/taillard/problemes.dir/vrp.dir/vrp.html>.
*
* <p>You can find best results of different problems, instances and authors here:
* <br><a href="http://link.springer.com/article/10.1007%2Fs10732-011-9186-y">http://link.springer.com/article/10.1007%2Fs10732-011-9186-y</a>
* <br><a href="http://www2.ic.uff.br/~satoru/conteudo/artigos/PAPER%20PUCA-JHeuristics-2011.pdf">http://www2.ic.uff.br/~satoru/conteudo/artigos/PAPER%20PUCA-JHeuristics-2011.pdf</a>
* <br><a href="http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.100.2331&rep=rep1&type=pdf">http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.100.2331&rep=rep1&type=pdf</a>
*
* @author schroeder
*
*/
public class HVRPBenchmarkExample {
public static void main(String[] args) {
Examples.createOutputFolder();
VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
//read modified Golden-instance, you can find all relevant instances in jsprit-instances/instances/vrph
//you can build various problems, see VrphType doc for more details
new VrphGoldenReader(vrpBuilder, VrphType.HVRPFD).read("input/cn_14mix.txt");
// vrpBuilder.addPenaltyVehicles(10.0);
VehicleRoutingProblem vrp = vrpBuilder.build();
//try also input//jsprit-examples/input/algorithmConfig_considerFixedCosts_routeLevel.xml
//results might even be a bit better, but it is slower, since it checks insertion on routeLevel
//rather than on local level
VehicleRoutingAlgorithm vra = VehicleRoutingAlgorithms.readAndCreateAlgorithm(vrp, "input/algorithmConfig_considerFixedCosts.xml");
vra.setMaxIterations(10000);
// vra.setPrematureAlgorithmTermination(new IterationWithoutImprovementTermination(500));
vra.addListener(new AlgorithmSearchProgressChartListener("output/progress.png"));
Collection<VehicleRoutingProblemSolution> solutions = vra.searchSolutions();
VehicleRoutingProblemSolution best = Solutions.bestOf(solutions);
SolutionPrinter.print(vrp, best, SolutionPrinter.Print.VERBOSE);
Plotter plotter = new Plotter(vrp,best);
plotter.plot("output/cn14.png", "cn14");
new GraphStreamViewer(vrp, best).setRenderDelay(100).display();
}
}