From 8ed138dddc009d5fd1aa4e20d262cde646a471d0 Mon Sep 17 00:00:00 2001 From: oblonski <4sschroeder@gmail.com> Date: Thu, 11 Dec 2014 12:06:49 +0100 Subject: [PATCH] make it even easier to implement new InsertionStrategies --- .../recreate/BestInsertionBuilder.java | 4 +- .../algorithm/recreate/InsertionBuilder.java | 4 +- .../algorithm/recreate/InsertionData.java | 12 ++--- ...> JobInsertionCostsCalculatorBuilder.java} | 51 ++++++++++-------- .../JobInsertionCostsCalculatorFactory.java | 54 +++++++++++++++++++ .../recreate/listener/InsertionListeners.java | 18 +++---- 6 files changed, 102 insertions(+), 41 deletions(-) rename jsprit-core/src/main/java/jsprit/core/algorithm/recreate/{CalculatorBuilder.java => JobInsertionCostsCalculatorBuilder.java} (88%) create mode 100644 jsprit-core/src/main/java/jsprit/core/algorithm/recreate/JobInsertionCostsCalculatorFactory.java diff --git a/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/BestInsertionBuilder.java b/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/BestInsertionBuilder.java index c7c7d3b1..ac4eb1a5 100644 --- a/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/BestInsertionBuilder.java +++ b/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/BestInsertionBuilder.java @@ -126,7 +126,7 @@ public class BestInsertionBuilder { public InsertionStrategy build() { List iListeners = new ArrayList(); List algorithmListeners = new ArrayList(); - CalculatorBuilder calcBuilder = new CalculatorBuilder(iListeners, algorithmListeners); + JobInsertionCostsCalculatorBuilder calcBuilder = new JobInsertionCostsCalculatorBuilder(iListeners, algorithmListeners); if(local){ calcBuilder.setLocalLevel(addDefaultCostCalc); } @@ -134,7 +134,7 @@ public class BestInsertionBuilder { calcBuilder.setRouteLevel(forwaredLooking, memory, addDefaultCostCalc); } calcBuilder.setConstraintManager(constraintManager); - calcBuilder.setStates(stateManager); + calcBuilder.setStateManager(stateManager); calcBuilder.setVehicleRoutingProblem(vrp); calcBuilder.setVehicleFleetManager(fleetManager); calcBuilder.setActivityInsertionCostsCalculator(actInsertionCostsCalculator); diff --git a/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/InsertionBuilder.java b/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/InsertionBuilder.java index f83f3357..437fe79d 100644 --- a/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/InsertionBuilder.java +++ b/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/InsertionBuilder.java @@ -137,7 +137,7 @@ public class InsertionBuilder { public InsertionStrategy build() { List iListeners = new ArrayList(); List algorithmListeners = new ArrayList(); - CalculatorBuilder calcBuilder = new CalculatorBuilder(iListeners, algorithmListeners); + JobInsertionCostsCalculatorBuilder calcBuilder = new JobInsertionCostsCalculatorBuilder(iListeners, algorithmListeners); if(local){ calcBuilder.setLocalLevel(addDefaultCostCalc); } @@ -145,7 +145,7 @@ public class InsertionBuilder { calcBuilder.setRouteLevel(forwaredLooking, memory, addDefaultCostCalc); } calcBuilder.setConstraintManager(constraintManager); - calcBuilder.setStates(stateManager); + calcBuilder.setStateManager(stateManager); calcBuilder.setVehicleRoutingProblem(vrp); calcBuilder.setVehicleFleetManager(fleetManager); calcBuilder.setActivityInsertionCostsCalculator(actInsertionCostsCalculator); diff --git a/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/InsertionData.java b/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/InsertionData.java index 147eb80c..1fe8c273 100644 --- a/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/InsertionData.java +++ b/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/InsertionData.java @@ -1,16 +1,16 @@ /******************************************************************************* - * 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 . ******************************************************************************/ @@ -28,7 +28,7 @@ import jsprit.core.problem.vehicle.Vehicle; */ public class InsertionData { - static class NoInsertionFound extends InsertionData{ + public static class NoInsertionFound extends InsertionData{ public NoInsertionFound() { super(Double.MAX_VALUE, NO_INDEX, NO_INDEX, null, null); diff --git a/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/CalculatorBuilder.java b/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/JobInsertionCostsCalculatorBuilder.java similarity index 88% rename from jsprit-core/src/main/java/jsprit/core/algorithm/recreate/CalculatorBuilder.java rename to jsprit-core/src/main/java/jsprit/core/algorithm/recreate/JobInsertionCostsCalculatorBuilder.java index ac4807be..33238111 100644 --- a/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/CalculatorBuilder.java +++ b/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/JobInsertionCostsCalculatorBuilder.java @@ -1,16 +1,16 @@ /******************************************************************************* - * 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 . ******************************************************************************/ @@ -34,7 +34,7 @@ import java.util.List; -class CalculatorBuilder { +public class JobInsertionCostsCalculatorBuilder { private static class CalculatorPlusListeners { @@ -104,7 +104,7 @@ class CalculatorBuilder { * @param insertionListeners * @param algorithmListeners */ - public CalculatorBuilder(List insertionListeners, List algorithmListeners) { + public JobInsertionCostsCalculatorBuilder(List insertionListeners, List algorithmListeners) { super(); this.insertionListeners = insertionListeners; this.algorithmListeners = algorithmListeners; @@ -112,12 +112,12 @@ class CalculatorBuilder { /** * Sets activityStates. MUST be set. - * @param states TODO + * @param stateManager TODO * * @return */ - public CalculatorBuilder setStates(RouteAndActivityStateGetter states){ - this.states = states; + public JobInsertionCostsCalculatorBuilder setStateManager(RouteAndActivityStateGetter stateManager){ + this.states = stateManager; return this; } @@ -127,7 +127,7 @@ class CalculatorBuilder { * @param vehicleRoutingProblem * @return */ - public CalculatorBuilder setVehicleRoutingProblem(VehicleRoutingProblem vehicleRoutingProblem){ + public JobInsertionCostsCalculatorBuilder setVehicleRoutingProblem(VehicleRoutingProblem vehicleRoutingProblem){ this.vrp = vehicleRoutingProblem; return this; } @@ -138,7 +138,7 @@ class CalculatorBuilder { * @param fleetManager * @return */ - public CalculatorBuilder setVehicleFleetManager(VehicleFleetManager fleetManager){ + public JobInsertionCostsCalculatorBuilder setVehicleFleetManager(VehicleFleetManager fleetManager){ this.fleetManager = fleetManager; return this; } @@ -149,13 +149,15 @@ class CalculatorBuilder { *

Insertion of a job and job-activity is evaluated based on the previous and next activity. * @param addDefaultCostCalc TODO */ - public void setLocalLevel(boolean addDefaultCostCalc){ + public JobInsertionCostsCalculatorBuilder setLocalLevel(boolean addDefaultCostCalc){ local = true; this.addDefaultCostCalc = addDefaultCostCalc; + return this; } - public void setActivityInsertionCostsCalculator(ActivityInsertionCostsCalculator activityInsertionCostsCalculator){ + public JobInsertionCostsCalculatorBuilder setActivityInsertionCostsCalculator(ActivityInsertionCostsCalculator activityInsertionCostsCalculator){ this.activityInsertionCostCalculator = activityInsertionCostsCalculator; + return this; } /** @@ -165,10 +167,11 @@ class CalculatorBuilder { * @param memory * @param addDefaultMarginalCostCalc TODO */ - public void setRouteLevel(int forwardLooking, int memory, boolean addDefaultMarginalCostCalc){ + public JobInsertionCostsCalculatorBuilder setRouteLevel(int forwardLooking, int memory, boolean addDefaultMarginalCostCalc){ local = false; this.forwardLooking = forwardLooking; this.memory = memory; + return this; } /** @@ -177,15 +180,17 @@ class CalculatorBuilder { * * @param weightOfFixedCosts */ - public void considerFixedCosts(double weightOfFixedCosts){ + public JobInsertionCostsCalculatorBuilder considerFixedCosts(double weightOfFixedCosts){ considerFixedCost = true; - this.weightOfFixedCost = weightOfFixedCosts; + this.weightOfFixedCost = weightOfFixedCosts; + return this; } - public void experimentalTimeScheduler(double timeSlice, int neighbors){ + public JobInsertionCostsCalculatorBuilder experimentalTimeScheduler(double timeSlice, int neighbors){ timeScheduling = true; this.timeSlice = timeSlice; this.neighbors = neighbors; + return this; } /** @@ -196,7 +201,7 @@ class CalculatorBuilder { */ public JobInsertionCostsCalculator build(){ if(vrp == null) throw new IllegalStateException("vehicle-routing-problem is null, but it must be set (this.setVehicleRoutingProblem(vrp))"); - if(states == null) throw new IllegalStateException("states is null, but is must be set (this.setStates(states))"); + if(states == null) throw new IllegalStateException("states is null, but is must be set (this.setStateManager(states))"); if(fleetManager == null) throw new IllegalStateException("fleetManager is null, but it must be set (this.setVehicleFleetManager(fleetManager))"); JobInsertionCostsCalculator baseCalculator = null; CalculatorPlusListeners standardLocal = null; @@ -352,12 +357,14 @@ class CalculatorBuilder { return vehicleTypeDependentJobInsertionCalculator; } - public void setConstraintManager(ConstraintManager constraintManager) { + public JobInsertionCostsCalculatorBuilder setConstraintManager(ConstraintManager constraintManager) { this.constraintManager = constraintManager; + return this; } - public void setAllowVehicleSwitch(boolean allowVehicleSwitch) { - this.allowVehicleSwitch = allowVehicleSwitch; + public JobInsertionCostsCalculatorBuilder setAllowVehicleSwitch(boolean allowVehicleSwitch) { + this.allowVehicleSwitch = allowVehicleSwitch; + return this; } } diff --git a/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/JobInsertionCostsCalculatorFactory.java b/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/JobInsertionCostsCalculatorFactory.java new file mode 100644 index 00000000..a4140595 --- /dev/null +++ b/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/JobInsertionCostsCalculatorFactory.java @@ -0,0 +1,54 @@ +/******************************************************************************* + * 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.recreate; + +import jsprit.core.algorithm.listener.VehicleRoutingAlgorithmListeners; +import jsprit.core.algorithm.recreate.listener.InsertionListener; +import jsprit.core.algorithm.state.StateManager; +import jsprit.core.problem.VehicleRoutingProblem; +import jsprit.core.problem.constraint.ConstraintManager; +import jsprit.core.problem.vehicle.VehicleFleetManager; + +import java.util.ArrayList; +import java.util.List; + +/** + * Created by schroeder on 11.12.14. + */ +public class JobInsertionCostsCalculatorFactory { + + /** + * Returns standard insertion calculator, i.e. the calculator that identifies best insertion positions for the + * jobs to be inserted. The position basically consists of the route and the according indices. + * + * @param vrp vehicle routing problem + * @param fleetManager fleet manager + * @param stateManager state manager + * @param constraintManager constraint manager + * @return insertion calculator + */ + public static JobInsertionCostsCalculator createStandardCalculator(VehicleRoutingProblem vrp, VehicleFleetManager fleetManager, StateManager stateManager, ConstraintManager constraintManager){ + List al = new ArrayList(); + List il = new ArrayList(); + JobInsertionCostsCalculatorBuilder builder = new JobInsertionCostsCalculatorBuilder(il,al); + builder.setVehicleRoutingProblem(vrp).setConstraintManager(constraintManager).setStateManager(stateManager).setVehicleFleetManager(fleetManager); + JobInsertionCostsCalculator calculator = builder.build(); + return calculator; + } + +} diff --git a/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/listener/InsertionListeners.java b/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/listener/InsertionListeners.java index 67443c46..795f9d4c 100644 --- a/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/listener/InsertionListeners.java +++ b/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/listener/InsertionListeners.java @@ -1,29 +1,29 @@ /******************************************************************************* - * 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.recreate.listener; -import java.util.ArrayList; -import java.util.Collection; - import jsprit.core.algorithm.recreate.InsertionData; import jsprit.core.problem.job.Job; import jsprit.core.problem.solution.route.VehicleRoute; import jsprit.core.problem.vehicle.Vehicle; +import java.util.ArrayList; +import java.util.Collection; + public class InsertionListeners { @@ -74,7 +74,7 @@ public class InsertionListeners { } public void addListener(InsertionListener insertionListener){ - listeners.add(insertionListener); + listeners.add(insertionListener); } public void removeListener(InsertionListener insertionListener){