From 5720a0eae2c755f067807993bfed5a93d77cd79f Mon Sep 17 00:00:00 2001 From: oblonski <4sschroeder@gmail.com> Date: Tue, 15 Apr 2014 13:22:47 +0200 Subject: [PATCH] added examples to illustrate analysis.toolbox.ComputationalLaborator.java --- ...utationalExperiments_randomVariations.java | 136 ++++++++++++++ ...s_randomVariations_and_nuOfIterations.java | 173 ++++++++++++++++++ ...ationalExperiments_schrimpf_vs_greedy.java | 140 ++++++++++++++ 3 files changed, 449 insertions(+) create mode 100644 jsprit-examples/src/main/java/jsprit/examples/ComputationalExperiments_randomVariations.java create mode 100644 jsprit-examples/src/main/java/jsprit/examples/ComputationalExperiments_randomVariations_and_nuOfIterations.java create mode 100644 jsprit-examples/src/main/java/jsprit/examples/ComputationalExperiments_schrimpf_vs_greedy.java diff --git a/jsprit-examples/src/main/java/jsprit/examples/ComputationalExperiments_randomVariations.java b/jsprit-examples/src/main/java/jsprit/examples/ComputationalExperiments_randomVariations.java new file mode 100644 index 00000000..e57d1138 --- /dev/null +++ b/jsprit-examples/src/main/java/jsprit/examples/ComputationalExperiments_randomVariations.java @@ -0,0 +1,136 @@ +/******************************************************************************* + * Copyright (C) 2013 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 . + ******************************************************************************/ +package jsprit.examples; + +import java.util.Collection; + +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; + +/** + * 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.setNuOfIterations(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 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 solutions) {} + + }); + + computationalLab.setThreads(2); + /* + * run the experiments + */ + computationalLab.run(); + + /* + * plot the lineChart + */ + XYLineChartBuilder.saveChartAsPNG(chartBuilder.build(), "output/computationalStudies_randomVariations.png"); + + } + +} diff --git a/jsprit-examples/src/main/java/jsprit/examples/ComputationalExperiments_randomVariations_and_nuOfIterations.java b/jsprit-examples/src/main/java/jsprit/examples/ComputationalExperiments_randomVariations_and_nuOfIterations.java new file mode 100644 index 00000000..e5169db6 --- /dev/null +++ b/jsprit-examples/src/main/java/jsprit/examples/ComputationalExperiments_randomVariations_and_nuOfIterations.java @@ -0,0 +1,173 @@ +/******************************************************************************* + * Copyright (C) 2013 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 . + ******************************************************************************/ +package jsprit.examples; + +import java.util.Collection; + +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; + +/** + * 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 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 doubles){ + double min = Double.MAX_VALUE; + for(Double d : doubles){ + if(d doubles){ + double max = 0.; + for(Double d : doubles){ + if(d>max) max=d; + } + return max; + } + + public static double avg(Collection 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.setNuOfIterations(iterations); + return vra; + } + }; + } + +} diff --git a/jsprit-examples/src/main/java/jsprit/examples/ComputationalExperiments_schrimpf_vs_greedy.java b/jsprit-examples/src/main/java/jsprit/examples/ComputationalExperiments_schrimpf_vs_greedy.java new file mode 100644 index 00000000..b5844d7d --- /dev/null +++ b/jsprit-examples/src/main/java/jsprit/examples/ComputationalExperiments_schrimpf_vs_greedy.java @@ -0,0 +1,140 @@ +/******************************************************************************* + * Copyright (C) 2013 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 . + ******************************************************************************/ +package jsprit.examples; + +import java.util.Collection; + +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; + +/** + * 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 3 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.setNuOfIterations(4000); + return vra; + } + }); + computationalLab.addAlgorithmFactory("greedyAcceptance", new VehicleRoutingAlgorithmFactory() { + + @Override + public VehicleRoutingAlgorithm createAlgorithm(VehicleRoutingProblem vrp) { + VehicleRoutingAlgorithm vra = VehicleRoutingAlgorithms.readAndCreateAlgorithm(vrp, "input/algorithmConfig_solomon.xml"); + vra.setNuOfIterations(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 solutions) { + chartBuilder.addData(algorithmName, i, Solutions.bestOf(solutions).getCost()); + } + + }); + + } + + @Override + public void calculationEnds(BenchmarkInstance p, String algorithmName,VehicleRoutingAlgorithm algorithm, int run,Collection solutions) {} + + }); + + computationalLab.setThreads(2); + /* + * run the experiments + */ + computationalLab.run(); + + /* + * plot the lineChart + */ + XYLineChartBuilder.saveChartAsPNG(chartBuilder.build(), "output/computationalStudies_schrimpf_vs_greedy.png"); + + } + +}