From 54f1d8819e62aa7b0e4470d1e40b2508aa188cc1 Mon Sep 17 00:00:00 2001 From: oblonski <4sschroeder@gmail.com> Date: Fri, 21 Nov 2014 22:44:14 +0100 Subject: [PATCH] add new insertion strat --- .../core/algorithm/io/InsertionFactory.java | 41 ++++++++----------- .../io/VehicleRoutingAlgorithms.java | 2 +- 2 files changed, 17 insertions(+), 26 deletions(-) diff --git a/jsprit-core/src/main/java/jsprit/core/algorithm/io/InsertionFactory.java b/jsprit-core/src/main/java/jsprit/core/algorithm/io/InsertionFactory.java index c7d19ec6..8b44bcb2 100644 --- a/jsprit-core/src/main/java/jsprit/core/algorithm/io/InsertionFactory.java +++ b/jsprit-core/src/main/java/jsprit/core/algorithm/io/InsertionFactory.java @@ -1,25 +1,24 @@ /******************************************************************************* - * Copyright (C) 2013 Stefan Schroeder - * + * 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 + * 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.io; import jsprit.core.algorithm.listener.VehicleRoutingAlgorithmListeners.PrioritizedVRAListener; -import jsprit.core.algorithm.recreate.BestInsertionBuilder; +import jsprit.core.algorithm.recreate.InsertionBuilder; import jsprit.core.algorithm.recreate.InsertionStrategy; -import jsprit.core.algorithm.recreate.listener.InsertionListener; import jsprit.core.algorithm.state.StateManager; import jsprit.core.problem.VehicleRoutingProblem; import jsprit.core.problem.constraint.ConstraintManager; @@ -28,7 +27,6 @@ import org.apache.commons.configuration.HierarchicalConfiguration; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import java.util.ArrayList; import java.util.List; import java.util.concurrent.ExecutorService; @@ -39,19 +37,17 @@ class InsertionFactory { @SuppressWarnings("deprecation") public static InsertionStrategy createInsertion(VehicleRoutingProblem vrp, HierarchicalConfiguration config, - VehicleFleetManager vehicleFleetManager, StateManager routeStates, List algorithmListeners, ExecutorService executorService, int nuOfThreads, ConstraintManager constraintManager, boolean addDefaultCostCalculators){ + VehicleFleetManager vehicleFleetManager, StateManager stateManager, List algorithmListeners, ExecutorService executorService, int nuOfThreads, ConstraintManager constraintManager, boolean addDefaultCostCalculators){ if(config.containsKey("[@name]")){ String insertionName = config.getString("[@name]"); if(!insertionName.equals("bestInsertion") && !insertionName.equals("regretInsertion")){ - new IllegalStateException(insertionName + " is not supported. use either \"bestInsertion\" or \"regretInsertion\""); + throw new IllegalStateException(insertionName + " is not supported. use either \"bestInsertion\" or \"regretInsertion\""); } - InsertionStrategy insertionStrategy = null; - List insertionListeners = new ArrayList(); - List algoListeners = new ArrayList(); - - BestInsertionBuilder iBuilder = new BestInsertionBuilder(vrp, vehicleFleetManager, routeStates, constraintManager); - + + + InsertionBuilder iBuilder = new InsertionBuilder(vrp, vehicleFleetManager, stateManager, constraintManager); + if(executorService != null){ iBuilder.setConcurrentMode(executorService, nuOfThreads); } @@ -105,16 +101,11 @@ class InsertionFactory { if(allowVehicleSwitch != null){ iBuilder.setAllowVehicleSwitch(Boolean.parseBoolean(allowVehicleSwitch)); } - if(insertionName.equals("bestInsertion")){ - insertionStrategy = iBuilder.build(); - } - else throw new IllegalStateException("currently only 'bestInsertion' is supported"); - - for(InsertionListener l : insertionListeners) insertionStrategy.addListener(l); - algorithmListeners.addAll(algoListeners); - - return insertionStrategy; + if(insertionName.equals("regretInsertion")){ + iBuilder.setInsertionStrategy(InsertionBuilder.Strategy.REGRET); + } + return iBuilder.build(); } else throw new IllegalStateException("cannot create insertionStrategy, since it has no name."); } 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 7bc1a1c3..d9886b73 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 @@ -589,7 +589,7 @@ public class VehicleRoutingAlgorithms { SolutionAcceptor acceptor = getAcceptor(strategyConfig,vrp,algorithmListeners,definedClasses,solutionMemory); SolutionSelector selector = getSelector(strategyConfig,vrp,algorithmListeners,definedClasses); - SearchStrategy strategy = new SearchStrategy(selector, acceptor, costCalculator); + SearchStrategy strategy = new SearchStrategy(name, selector, acceptor, costCalculator); strategy.setName(name); List modulesConfig = strategyConfig.configurationsAt("modules.module"); for(HierarchicalConfiguration moduleConfig : modulesConfig){