From bc6c1e23b5dae45ed5646953cd5baee198dbe9c2 Mon Sep 17 00:00:00 2001 From: Stefan Schroeder <4sschroeder@gmail.com> Date: Tue, 11 Jun 2013 09:57:48 +0200 Subject: [PATCH] fix algorithmSearchProgress...Listener - reset it when algorithm starts, deprecate setters VehicleRoutingProblem to make it immutable in the next release, add Example --- .../AlgorithmSearchProgressChartListener.java | 15 ++++- .../java/basics/VehicleRoutingAlgorithm.java | 4 +- .../java/basics/VehicleRoutingProblem.java | 67 +++++++++---------- .../examples/CompareAlgorithmExample.java | 64 ++++++++++++++++++ .../main/java/examples/SolomonExample.java | 2 +- 5 files changed, 114 insertions(+), 38 deletions(-) create mode 100644 jsprit-examples/src/main/java/examples/CompareAlgorithmExample.java diff --git a/jsprit-analysis/src/main/java/analysis/AlgorithmSearchProgressChartListener.java b/jsprit-analysis/src/main/java/analysis/AlgorithmSearchProgressChartListener.java index 980ced7a..f6699d4d 100644 --- a/jsprit-analysis/src/main/java/analysis/AlgorithmSearchProgressChartListener.java +++ b/jsprit-analysis/src/main/java/analysis/AlgorithmSearchProgressChartListener.java @@ -30,9 +30,11 @@ import org.jfree.data.Range; import org.jfree.data.xy.XYSeries; import org.jfree.data.xy.XYSeriesCollection; +import basics.VehicleRoutingAlgorithm; import basics.VehicleRoutingProblem; import basics.VehicleRoutingProblemSolution; import basics.algo.AlgorithmEndsListener; +import basics.algo.AlgorithmStartsListener; import basics.algo.IterationEndsListener; @@ -45,7 +47,7 @@ import basics.algo.IterationEndsListener; * */ -public class AlgorithmSearchProgressChartListener implements IterationEndsListener, AlgorithmEndsListener { +public class AlgorithmSearchProgressChartListener implements IterationEndsListener, AlgorithmEndsListener, AlgorithmStartsListener { private static Logger log = Logger.getLogger(AlgorithmSearchProgressChartListener.class); @@ -146,4 +148,15 @@ public class AlgorithmSearchProgressChartListener implements IterationEndsListen avgResultList.add(sum/(double)solutions.size()); } + + @Override + public void informAlgorithmStarts(VehicleRoutingProblem problem,VehicleRoutingAlgorithm algorithm,Collection solutions) { + bestResults = null; + worstResults = null; + avgResults = null; + bestResultList.clear(); + worstResultList.clear(); + avgResultList.clear(); + } + } diff --git a/jsprit-core/src/main/java/basics/VehicleRoutingAlgorithm.java b/jsprit-core/src/main/java/basics/VehicleRoutingAlgorithm.java index b8d3269f..05ad9ec6 100644 --- a/jsprit-core/src/main/java/basics/VehicleRoutingAlgorithm.java +++ b/jsprit-core/src/main/java/basics/VehicleRoutingAlgorithm.java @@ -123,6 +123,7 @@ public class VehicleRoutingAlgorithm { logger.info("algorithm starts"); double now = System.currentTimeMillis(); verify(); + int nuOfIterationsThisAlgoIsRunning = nOfIterations; counter.reset(); Collection solutions = new ArrayList(initialSolutions); algorithmStarts(problem,solutions); @@ -138,11 +139,12 @@ public class VehicleRoutingAlgorithm { else iterWithoutImprovement++; if(iterWithoutImprovement > prematureBreak){ logger.info("premature break at iteration "+ (i+1)); + nuOfIterationsThisAlgoIsRunning = (i+1); break; } iterationEnds(i+1,problem,solutions); } - logger.info("iterations end at " + nOfIterations + " iterations"); + logger.info("iterations end at " + nuOfIterationsThisAlgoIsRunning + " iterations"); algorithmEnds(problem,solutions); logger.info("total time: " + ((System.currentTimeMillis()-now)/1000.0) + "s"); logger.info("done"); diff --git a/jsprit-core/src/main/java/basics/VehicleRoutingProblem.java b/jsprit-core/src/main/java/basics/VehicleRoutingProblem.java index 1a95f8b4..ddd87412 100644 --- a/jsprit-core/src/main/java/basics/VehicleRoutingProblem.java +++ b/jsprit-core/src/main/java/basics/VehicleRoutingProblem.java @@ -142,6 +142,7 @@ public class VehicleRoutingProblem { this.transportCosts = costs; return this; } + /** * Sets the type of fleetSize. @@ -153,9 +154,6 @@ public class VehicleRoutingProblem { */ public Builder setFleetSize(FleetSize fleetSize){ this.fleetSize = fleetSize; -// if(fleetSize.equals(FleetSize.INFINITE)){ -// fleetSizeIsInfinite=true; -// } return this; } @@ -198,20 +196,10 @@ public class VehicleRoutingProblem { if(job instanceof Service) { addService((Service) job); } -// else if(job instanceof Shipment){ -// addShipment((Shipment)job); -// } else throw new IllegalStateException("job can only be a shipment or a service, but is instance of " + job.getClass()); return this; } -// private void addShipment(Shipment job) { -// coordinates.put(job.getFromId(),job.getFromCoord()); -// coordinates.put(job.getToId(), job.getToCoord()); -// if(jobs.containsKey(job.getId())){ logger.warn("service " + job + " already in job list. overrides existing job."); } -// jobs.put(job.getId(),job); -// -// } /** * Adds a vehicle. @@ -221,7 +209,6 @@ public class VehicleRoutingProblem { * @return */ public Builder addVehicle(Vehicle vehicle) { -// fleetSizeIsFinite = true; vehicles.add(vehicle); if(!vehicleTypes.contains(vehicle.getType())){ vehicleTypes.add(vehicle.getType()); @@ -262,7 +249,6 @@ public class VehicleRoutingProblem { logger.warn("set routing costs crowFlyDistance."); transportCosts = new CrowFlyCosts(getLocations()); } - return new VehicleRoutingProblem(this); } @@ -380,10 +366,6 @@ public class VehicleRoutingProblem { return fleetComposition; } - public void setFleetComposition(FleetComposition fleetComposition){ - this.fleetComposition = fleetComposition; - } - /** * Returns type of fleetSize, either INFINITE or FINITE. * @@ -395,10 +377,6 @@ public class VehicleRoutingProblem { return fleetSize; } - public void setFleetSize(FleetSize fleetSize){ - this.fleetSize = fleetSize; - } - /** * Returns the unmodifiable job map. * @@ -440,17 +418,6 @@ public class VehicleRoutingProblem { return transportCosts; } - /** - * Sets routing costs. - * - * @param costs - * @see VehicleRoutingTransportCosts - */ - public void setTransportCosts(VehicleRoutingTransportCosts costs) { - this.transportCosts = costs; - logger.info("transport costs set to " + costs.getClass()); - } - /** * Returns activityCosts. */ @@ -459,8 +426,38 @@ public class VehicleRoutingProblem { } /** - * Sets activityCosts. + * Is deprecated and is not going to be supported any longer. Use the builder instead. */ + @Deprecated + public void setFleetComposition(FleetComposition fleetComposition){ + this.fleetComposition = fleetComposition; + } + + /** + * Is deprecated and is not going to be supported any longer. Use the builder instead. + */ + @Deprecated + public void setFleetSize(FleetSize fleetSize){ + this.fleetSize = fleetSize; + } + + /** + * Sets routing costs. + * Is deprecated and is not going to be supported any longer. Use the builder instead. + * + * @param costs + * @see VehicleRoutingTransportCosts + */ + @Deprecated + public void setTransportCosts(VehicleRoutingTransportCosts costs) { + this.transportCosts = costs; + logger.info("transport costs set to " + costs.getClass()); + } + + /** + * Is deprecated and is not going to be supported any longer. Use the builder instead. + */ + @Deprecated public void setActivityCosts(VehicleRoutingActivityCosts activityCosts){ this.activityCosts = activityCosts; logger.info("activtiy costs set to " + activityCosts.getClass()); diff --git a/jsprit-examples/src/main/java/examples/CompareAlgorithmExample.java b/jsprit-examples/src/main/java/examples/CompareAlgorithmExample.java new file mode 100644 index 00000000..34875bf2 --- /dev/null +++ b/jsprit-examples/src/main/java/examples/CompareAlgorithmExample.java @@ -0,0 +1,64 @@ +package examples; + +import readers.SolomonReader; +import algorithms.GreedySchrimpfFactory; +import algorithms.SchrimpfFactory; +import analysis.AlgorithmSearchProgressChartListener; +import analysis.StopWatch; +import basics.VehicleRoutingAlgorithm; +import basics.VehicleRoutingProblem; +import basics.algo.VehicleRoutingAlgorithmListeners.Priority; + +public class CompareAlgorithmExample { + + /** + * @param args + */ + public static void main(String[] args) { + + /* + * 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.setPrematureBreak(40); + vra_greedy.searchSolutions(); + + + } + +} diff --git a/jsprit-examples/src/main/java/examples/SolomonExample.java b/jsprit-examples/src/main/java/examples/SolomonExample.java index ae435548..905a1652 100644 --- a/jsprit-examples/src/main/java/examples/SolomonExample.java +++ b/jsprit-examples/src/main/java/examples/SolomonExample.java @@ -66,7 +66,7 @@ public class SolomonExample { * The algorithm can be defined and configured in an xml-file. */ VehicleRoutingAlgorithm vra = new SchrimpfFactory().createAlgorithm(vrp); - +// vra.getAlgorithmListeners().addListener(new AlgorithmSearchProgressChartListener(pngFileName)); /* * Solve the problem. *