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

make it even easier to implement new InsertionStrategies

This commit is contained in:
oblonski 2014-12-11 12:06:49 +01:00
parent 51c14afd73
commit 8ed138dddc
6 changed files with 102 additions and 41 deletions

View file

@ -126,7 +126,7 @@ public class BestInsertionBuilder {
public InsertionStrategy build() {
List<InsertionListener> iListeners = new ArrayList<InsertionListener>();
List<PrioritizedVRAListener> algorithmListeners = new ArrayList<PrioritizedVRAListener>();
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);

View file

@ -137,7 +137,7 @@ public class InsertionBuilder {
public InsertionStrategy build() {
List<InsertionListener> iListeners = new ArrayList<InsertionListener>();
List<PrioritizedVRAListener> algorithmListeners = new ArrayList<PrioritizedVRAListener>();
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);

View file

@ -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 <http://www.gnu.org/licenses/>.
******************************************************************************/
@ -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);

View file

@ -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 <http://www.gnu.org/licenses/>.
******************************************************************************/
@ -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<InsertionListener> insertionListeners, List<PrioritizedVRAListener> algorithmListeners) {
public JobInsertionCostsCalculatorBuilder(List<InsertionListener> insertionListeners, List<PrioritizedVRAListener> 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 {
* <p>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;
}
}

View file

@ -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 <http://www.gnu.org/licenses/>.
******************************************************************************/
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<VehicleRoutingAlgorithmListeners.PrioritizedVRAListener> al = new ArrayList<VehicleRoutingAlgorithmListeners.PrioritizedVRAListener>();
List<InsertionListener> il = new ArrayList<InsertionListener>();
JobInsertionCostsCalculatorBuilder builder = new JobInsertionCostsCalculatorBuilder(il,al);
builder.setVehicleRoutingProblem(vrp).setConstraintManager(constraintManager).setStateManager(stateManager).setVehicleFleetManager(fleetManager);
JobInsertionCostsCalculator calculator = builder.build();
return calculator;
}
}

View file

@ -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 <http://www.gnu.org/licenses/>.
******************************************************************************/
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){