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

add new insertion strat

This commit is contained in:
oblonski 2014-11-21 22:44:14 +01:00
parent 5d7d662124
commit 54f1d8819e
2 changed files with 17 additions and 26 deletions

View file

@ -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 * This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public * 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. * 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, * This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details. * Lesser General Public License for more details.
* *
* You should have received a copy of the GNU Lesser General Public * 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/>. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/ ******************************************************************************/
package jsprit.core.algorithm.io; package jsprit.core.algorithm.io;
import jsprit.core.algorithm.listener.VehicleRoutingAlgorithmListeners.PrioritizedVRAListener; 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.InsertionStrategy;
import jsprit.core.algorithm.recreate.listener.InsertionListener;
import jsprit.core.algorithm.state.StateManager; import jsprit.core.algorithm.state.StateManager;
import jsprit.core.problem.VehicleRoutingProblem; import jsprit.core.problem.VehicleRoutingProblem;
import jsprit.core.problem.constraint.ConstraintManager; 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.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
@ -39,19 +37,17 @@ class InsertionFactory {
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public static InsertionStrategy createInsertion(VehicleRoutingProblem vrp, HierarchicalConfiguration config, public static InsertionStrategy createInsertion(VehicleRoutingProblem vrp, HierarchicalConfiguration config,
VehicleFleetManager vehicleFleetManager, StateManager routeStates, List<PrioritizedVRAListener> algorithmListeners, ExecutorService executorService, int nuOfThreads, ConstraintManager constraintManager, boolean addDefaultCostCalculators){ VehicleFleetManager vehicleFleetManager, StateManager stateManager, List<PrioritizedVRAListener> algorithmListeners, ExecutorService executorService, int nuOfThreads, ConstraintManager constraintManager, boolean addDefaultCostCalculators){
if(config.containsKey("[@name]")){ if(config.containsKey("[@name]")){
String insertionName = config.getString("[@name]"); String insertionName = config.getString("[@name]");
if(!insertionName.equals("bestInsertion") && !insertionName.equals("regretInsertion")){ 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<InsertionListener> insertionListeners = new ArrayList<InsertionListener>();
List<PrioritizedVRAListener> algoListeners = new ArrayList<PrioritizedVRAListener>(); InsertionBuilder iBuilder = new InsertionBuilder(vrp, vehicleFleetManager, stateManager, constraintManager);
BestInsertionBuilder iBuilder = new BestInsertionBuilder(vrp, vehicleFleetManager, routeStates, constraintManager);
if(executorService != null){ if(executorService != null){
iBuilder.setConcurrentMode(executorService, nuOfThreads); iBuilder.setConcurrentMode(executorService, nuOfThreads);
} }
@ -105,16 +101,11 @@ class InsertionFactory {
if(allowVehicleSwitch != null){ if(allowVehicleSwitch != null){
iBuilder.setAllowVehicleSwitch(Boolean.parseBoolean(allowVehicleSwitch)); 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); if(insertionName.equals("regretInsertion")){
iBuilder.setInsertionStrategy(InsertionBuilder.Strategy.REGRET);
return insertionStrategy; }
return iBuilder.build();
} }
else throw new IllegalStateException("cannot create insertionStrategy, since it has no name."); else throw new IllegalStateException("cannot create insertionStrategy, since it has no name.");
} }

View file

@ -589,7 +589,7 @@ public class VehicleRoutingAlgorithms {
SolutionAcceptor acceptor = getAcceptor(strategyConfig,vrp,algorithmListeners,definedClasses,solutionMemory); SolutionAcceptor acceptor = getAcceptor(strategyConfig,vrp,algorithmListeners,definedClasses,solutionMemory);
SolutionSelector selector = getSelector(strategyConfig,vrp,algorithmListeners,definedClasses); 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); strategy.setName(name);
List<HierarchicalConfiguration> modulesConfig = strategyConfig.configurationsAt("modules.module"); List<HierarchicalConfiguration> modulesConfig = strategyConfig.configurationsAt("modules.module");
for(HierarchicalConfiguration moduleConfig : modulesConfig){ for(HierarchicalConfiguration moduleConfig : modulesConfig){