From 7ce6a8d68a0545af50b9bcb6b7a23da6556d5f0e Mon Sep 17 00:00:00 2001 From: oblonski <4sschroeder@gmail.com> Date: Wed, 10 Dec 2014 13:12:15 +0100 Subject: [PATCH] enable search with different insertion strategies --- .../io/VehicleRoutingAlgorithms.java | 62 +++++++++-------- ...iesAndDifferentInsertionStrategies_IT.java | 62 +++++++++++++++++ .../algorithmConfig_greedyWithRegret.xml | 66 +++++++++++++++++++ .../algorithmConfig_greedyWithRegret_v2.xml | 66 +++++++++++++++++++ .../algorithmConfig_greedyWithRegret.xml | 4 +- .../SolomonWithRegretInsertionExample.java | 4 +- 6 files changed, 233 insertions(+), 31 deletions(-) create mode 100644 jsprit-core/src/test/java/jsprit/core/algorithm/CVRPwithDeliveriesAndDifferentInsertionStrategies_IT.java create mode 100755 jsprit-core/src/test/resources/algorithmConfig_greedyWithRegret.xml create mode 100755 jsprit-core/src/test/resources/algorithmConfig_greedyWithRegret_v2.xml diff --git a/jsprit-core/src/main/java/jsprit/core/algorithm/io/VehicleRoutingAlgorithms.java b/jsprit-core/src/main/java/jsprit/core/algorithm/io/VehicleRoutingAlgorithms.java index 36df5d2f..b5a30750 100644 --- a/jsprit-core/src/main/java/jsprit/core/algorithm/io/VehicleRoutingAlgorithms.java +++ b/jsprit-core/src/main/java/jsprit/core/algorithm/io/VehicleRoutingAlgorithms.java @@ -494,7 +494,7 @@ public class VehicleRoutingAlgorithms { } private static VehicleRoutingAlgorithm readAndCreateAlgorithm(final VehicleRoutingProblem vrp, XMLConfiguration config, - int nuOfThreads, SolutionCostCalculator solutionCostCalculator, final StateManager stateManager, ConstraintManager constraintManager, boolean addDefaultCostCalculators) { + int nuOfThreads, final SolutionCostCalculator solutionCostCalculator, final StateManager stateManager, ConstraintManager constraintManager, boolean addDefaultCostCalculators) { // map to store constructed modules TypedMap definedClasses = new TypedMap(); @@ -572,13 +572,22 @@ public class VehicleRoutingAlgorithms { stateManager.addStateUpdater(new UpdateActivityTimes(vrp.getTransportCosts(),activityPolicy)); stateManager.addStateUpdater(new UpdateVariableCosts(vrp.getActivityCosts(), vrp.getTransportCosts(), stateManager, activityPolicy)); - SolutionCostCalculator costCalculator; + final SolutionCostCalculator costCalculator; if(solutionCostCalculator==null) costCalculator = getDefaultCostCalculator(stateManager); else costCalculator = solutionCostCalculator; //construct initial solution creator - AlgorithmStartsListener createInitialSolution = createInitialSolution(config,vrp,vehicleFleetManager,stateManager,algorithmListeners,definedClasses,executorService,nuOfThreads,costCalculator, constraintManager, addDefaultCostCalculators); - if(createInitialSolution != null) algorithmListeners.add(new PrioritizedVRAListener(Priority.MEDIUM, createInitialSolution)); + final InsertionStrategy initialInsertionStrategy = createInitialSolution(config,vrp,vehicleFleetManager,stateManager,algorithmListeners,definedClasses,executorService,nuOfThreads,costCalculator, constraintManager, addDefaultCostCalculators); + if(initialInsertionStrategy != null) algorithmListeners.add(new PrioritizedVRAListener(Priority.MEDIUM, new AlgorithmStartsListener() { + + @Override + public void informAlgorithmStarts(VehicleRoutingProblem problem, VehicleRoutingAlgorithm algorithm, Collection solutions) { + InsertionInitialSolutionFactory insertionInitialSolutionFactory = new InsertionInitialSolutionFactory(initialInsertionStrategy, costCalculator); + VehicleRoutingProblemSolution vrpSol = insertionInitialSolutionFactory.createSolution(vrp); + solutions.add(vrpSol); + } + + })); //construct algorithm, i.e. search-strategies and its modules int solutionMemory = config.getInt("strategy.memory"); @@ -605,13 +614,6 @@ public class VehicleRoutingAlgorithms { if(maxIterationsString == null) maxIterationsString = config.getString("maxIterations"); if(maxIterationsString != null) metaAlgorithm.setMaxIterations(Integer.parseInt(maxIterationsString)); - metaAlgorithm.getSearchStrategyManager().addSearchStrategyModuleListener(stateManager); - metaAlgorithm.getAlgorithmListeners().addListener(stateManager); - - metaAlgorithm.getSearchStrategyManager().addSearchStrategyModuleListener(new RemoveEmptyVehicles(vehicleFleetManager)); - metaAlgorithm.getSearchStrategyManager().addSearchStrategyModuleListener(new ResetAndIniFleetManager(vehicleFleetManager)); - metaAlgorithm.getSearchStrategyManager().addSearchStrategyModuleListener(new VehicleSwitched(vehicleFleetManager)); - //define prematureBreak PrematureAlgorithmTermination prematureAlgorithmTermination = getPrematureTermination(config, algorithmListeners); if(prematureAlgorithmTermination != null) metaAlgorithm.setPrematureAlgorithmTermination(prematureAlgorithmTermination); @@ -623,12 +625,26 @@ public class VehicleRoutingAlgorithms { } } - - //misc -// algorithmListeners.add(new PrioritizedVRAListener(Priority.LOW, new SolutionVerifier())); - - //register listeners - registerListeners(metaAlgorithm,algorithmListeners); + RemoveEmptyVehicles removeEmptyVehicles = new RemoveEmptyVehicles(vehicleFleetManager); + ResetAndIniFleetManager resetAndIniFleetManager = new ResetAndIniFleetManager(vehicleFleetManager); + VehicleSwitched vehicleSwitched = new VehicleSwitched(vehicleFleetManager); + + metaAlgorithm.addListener(stateManager); + metaAlgorithm.addListener(removeEmptyVehicles); + metaAlgorithm.addListener(resetAndIniFleetManager); + metaAlgorithm.addListener(vehicleSwitched); + + if(initialInsertionStrategy != null) { + if(initialInsertionStrategy.getListeners() != null){ + if(!initialInsertionStrategy.getListeners().contains(stateManager)) initialInsertionStrategy.addListener(stateManager); + if(!initialInsertionStrategy.getListeners().contains(removeEmptyVehicles)) initialInsertionStrategy.addListener(removeEmptyVehicles); + if(!initialInsertionStrategy.getListeners().contains(resetAndIniFleetManager)) initialInsertionStrategy.addListener(resetAndIniFleetManager); + if(!initialInsertionStrategy.getListeners().contains(vehicleSwitched)) initialInsertionStrategy.addListener(vehicleSwitched); + } + } + + //register listeners + registerListeners(metaAlgorithm, algorithmListeners); registerInsertionListeners(definedClasses,insertionListeners); return metaAlgorithm; } @@ -747,7 +763,7 @@ public class VehicleRoutingAlgorithms { metaAlgorithm.getAlgorithmListeners().addAll(algorithmListeners); } - private static AlgorithmStartsListener createInitialSolution(XMLConfiguration config, final VehicleRoutingProblem vrp, VehicleFleetManager vehicleFleetManager, final StateManager routeStates, Set algorithmListeners, TypedMap definedClasses, ExecutorService executorService, int nuOfThreads, final SolutionCostCalculator solutionCostCalculator, ConstraintManager constraintManager, boolean addDefaultCostCalculators) { + private static InsertionStrategy createInitialSolution(XMLConfiguration config, final VehicleRoutingProblem vrp, VehicleFleetManager vehicleFleetManager, final StateManager routeStates, Set algorithmListeners, TypedMap definedClasses, ExecutorService executorService, int nuOfThreads, final SolutionCostCalculator solutionCostCalculator, ConstraintManager constraintManager, boolean addDefaultCostCalculators) { List modConfigs = config.configurationsAt("construction.insertion"); if(modConfigs == null) return null; if(modConfigs.isEmpty()) return null; @@ -768,15 +784,7 @@ public class VehicleRoutingAlgorithms { } final InsertionStrategy finalInsertionStrategy = insertionStrategy; - return new AlgorithmStartsListener() { - - @Override - public void informAlgorithmStarts(VehicleRoutingProblem problem, VehicleRoutingAlgorithm algorithm, Collection solutions) { - InsertionInitialSolutionFactory insertionInitialSolutionFactory = new InsertionInitialSolutionFactory(finalInsertionStrategy, solutionCostCalculator); - VehicleRoutingProblemSolution vrpSol = insertionInitialSolutionFactory.createSolution(vrp); - solutions.add(vrpSol); - } - }; + return finalInsertionStrategy; } diff --git a/jsprit-core/src/test/java/jsprit/core/algorithm/CVRPwithDeliveriesAndDifferentInsertionStrategies_IT.java b/jsprit-core/src/test/java/jsprit/core/algorithm/CVRPwithDeliveriesAndDifferentInsertionStrategies_IT.java new file mode 100644 index 00000000..9c92d6c3 --- /dev/null +++ b/jsprit-core/src/test/java/jsprit/core/algorithm/CVRPwithDeliveriesAndDifferentInsertionStrategies_IT.java @@ -0,0 +1,62 @@ +/******************************************************************************* + * 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 . + ******************************************************************************/ +package jsprit.core.algorithm; + +import jsprit.core.algorithm.io.VehicleRoutingAlgorithms; +import jsprit.core.problem.VehicleRoutingProblem; +import jsprit.core.problem.io.VrpXMLReader; +import jsprit.core.problem.solution.VehicleRoutingProblemSolution; +import junit.framework.Assert; +import org.junit.Test; + +import java.util.Collection; + +public class CVRPwithDeliveriesAndDifferentInsertionStrategies_IT { + + @Test + public void whenWithTwoInsertionStrategiesWhereOnleOneIsInAlgo_itShouldWork(){ + VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance(); + new VrpXMLReader(vrpBuilder).read("src/test/resources/vrpnc1-jsprit-with-deliveries.xml"); + VehicleRoutingProblem vrp = vrpBuilder.build(); + VehicleRoutingAlgorithm vra = VehicleRoutingAlgorithms.readAndCreateAlgorithm(vrp, "src/test/resources/algorithmConfig_greedyWithRegret.xml"); + vra.setMaxIterations(10); + try { + Collection solutions = vra.searchSolutions(); + Assert.assertTrue(true); + } + catch (Exception e){ + Assert.assertTrue(false); + } + } + + @Test + public void whenWithTwoInsertionStrategiesWhereBothAreInAlgo_itShouldWork(){ + VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance(); + new VrpXMLReader(vrpBuilder).read("src/test/resources/vrpnc1-jsprit-with-deliveries.xml"); + VehicleRoutingProblem vrp = vrpBuilder.build(); + VehicleRoutingAlgorithm vra = VehicleRoutingAlgorithms.readAndCreateAlgorithm(vrp, "src/test/resources/algorithmConfig_greedyWithRegret_v2.xml"); + vra.setMaxIterations(10); + try { + Collection solutions = vra.searchSolutions(); + Assert.assertTrue(true); + } + catch (Exception e){ + Assert.assertTrue(false); + } + } + +} diff --git a/jsprit-core/src/test/resources/algorithmConfig_greedyWithRegret.xml b/jsprit-core/src/test/resources/algorithmConfig_greedyWithRegret.xml new file mode 100755 index 00000000..05b7ac50 --- /dev/null +++ b/jsprit-core/src/test/resources/algorithmConfig_greedyWithRegret.xml @@ -0,0 +1,66 @@ + + + + + + + 2000 + + + + + + + + + 1 + + + + + + + + + 0.5 + + + + + 0.5 + + + + + + + + + 0.3 + + + + + 0.5 + + + + + + diff --git a/jsprit-core/src/test/resources/algorithmConfig_greedyWithRegret_v2.xml b/jsprit-core/src/test/resources/algorithmConfig_greedyWithRegret_v2.xml new file mode 100755 index 00000000..7caab620 --- /dev/null +++ b/jsprit-core/src/test/resources/algorithmConfig_greedyWithRegret_v2.xml @@ -0,0 +1,66 @@ + + + + + + + 2000 + + + + + + + + + 1 + + + + + + + + + 0.5 + + + + + 0.5 + + + + + + + + + 0.3 + + + + + 0.5 + + + + + + diff --git a/jsprit-examples/input/algorithmConfig_greedyWithRegret.xml b/jsprit-examples/input/algorithmConfig_greedyWithRegret.xml index 42abefcc..05b7ac50 100755 --- a/jsprit-examples/input/algorithmConfig_greedyWithRegret.xml +++ b/jsprit-examples/input/algorithmConfig_greedyWithRegret.xml @@ -40,7 +40,7 @@ 0.5 - + 0.5 @@ -54,7 +54,7 @@ 0.3 - + 0.5 diff --git a/jsprit-examples/src/main/java/jsprit/examples/SolomonWithRegretInsertionExample.java b/jsprit-examples/src/main/java/jsprit/examples/SolomonWithRegretInsertionExample.java index 11890a68..29e384df 100644 --- a/jsprit-examples/src/main/java/jsprit/examples/SolomonWithRegretInsertionExample.java +++ b/jsprit-examples/src/main/java/jsprit/examples/SolomonWithRegretInsertionExample.java @@ -67,8 +67,8 @@ public class SolomonWithRegretInsertionExample { * * The algorithm can be defined and configured in an xml-file. */ - VehicleRoutingAlgorithm vra = VehicleRoutingAlgorithms.readAndCreateAlgorithm(vrp, 2, "input/algorithmConfig_greedyWithRegret.xml"); - vra.setMaxIterations(50); + VehicleRoutingAlgorithm vra = VehicleRoutingAlgorithms.readAndCreateAlgorithm(vrp, "input/algorithmConfig_greedyWithRegret.xml"); + vra.setMaxIterations(2); AlgorithmEventsRecorder eventsRecorder = new AlgorithmEventsRecorder(vrp,new File("output/events.dgs.gz")); eventsRecorder.setRecordingRange(0,50);