mirror of
https://github.com/graphhopper/jsprit.git
synced 2020-01-24 07:45:05 +01:00
internals
This commit is contained in:
parent
05b883c034
commit
fb365036af
15 changed files with 177 additions and 62 deletions
|
|
@ -40,6 +40,7 @@ import org.apache.log4j.Logger;
|
|||
import basics.Job;
|
||||
import basics.VehicleRoutingProblem;
|
||||
import basics.VehicleRoutingProblemSolution;
|
||||
import basics.algo.SolutionCostCalculator;
|
||||
import basics.route.DriverImpl;
|
||||
import basics.route.TourActivities;
|
||||
import basics.route.Vehicle;
|
||||
|
|
@ -53,15 +54,18 @@ final class CreateInitialSolution implements InitialSolutionFactory {
|
|||
|
||||
private final InsertionStrategy insertion;
|
||||
|
||||
private SolutionCostCalculator solutionCostCalculator;
|
||||
|
||||
private boolean generateAsMuchAsRoutesAsVehiclesExist = false;
|
||||
|
||||
public void setGenerateAsMuchAsRoutesAsVehiclesExist(boolean generateAsMuchAsRoutesAsVehiclesExist) {
|
||||
this.generateAsMuchAsRoutesAsVehiclesExist = generateAsMuchAsRoutesAsVehiclesExist;
|
||||
}
|
||||
|
||||
public CreateInitialSolution(InsertionStrategy insertionStrategy) {
|
||||
public CreateInitialSolution(InsertionStrategy insertionStrategy, SolutionCostCalculator solutionCostCalculator) {
|
||||
super();
|
||||
this.insertion = insertionStrategy;
|
||||
this.solutionCostCalculator = solutionCostCalculator;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -74,17 +78,11 @@ final class CreateInitialSolution implements InitialSolutionFactory {
|
|||
}
|
||||
}
|
||||
insertion.insertJobs(vehicleRoutes, getUnassignedJobs(vrp));
|
||||
double totalCost = getTotalCost(vehicleRoutes);
|
||||
// double totalCost = getTotalCost(vehicleRoutes);
|
||||
logger.info("creation done");
|
||||
return new VehicleRoutingProblemSolution(vehicleRoutes, totalCost);
|
||||
}
|
||||
|
||||
private double getTotalCost(List<VehicleRoute> serviceProviders) {
|
||||
double c = 0.0;
|
||||
for(VehicleRoute a : serviceProviders){
|
||||
c += a.getCost();
|
||||
}
|
||||
return c;
|
||||
VehicleRoutingProblemSolution vehicleRoutingProblemSolution = new VehicleRoutingProblemSolution(vehicleRoutes, 0.0);
|
||||
solutionCostCalculator.calculateCosts(vehicleRoutingProblemSolution);
|
||||
return vehicleRoutingProblemSolution;
|
||||
}
|
||||
|
||||
private List<Job> getUnassignedJobs(VehicleRoutingProblem vrp) {
|
||||
|
|
|
|||
|
|
@ -49,8 +49,8 @@ class RuinAndRecreateModule implements SearchStrategyModule{
|
|||
public VehicleRoutingProblemSolution runAndGetSolution(VehicleRoutingProblemSolution vrpSolution) {
|
||||
Collection<Job> ruinedJobs = ruin.ruin(vrpSolution.getRoutes());
|
||||
insertion.insertJobs(vrpSolution.getRoutes(), ruinedJobs);
|
||||
double totalCost = RouteUtils.getTotalCost(vrpSolution.getRoutes());
|
||||
vrpSolution.setCost(totalCost);
|
||||
// double totalCost = RouteUtils.getTotalCost(vrpSolution.getRoutes());
|
||||
// vrpSolution.setCost(totalCost);
|
||||
return vrpSolution;
|
||||
}
|
||||
|
||||
|
|
|
|||
19
jsprit-core/src/main/java/algorithms/StateIdImpl.java
Normal file
19
jsprit-core/src/main/java/algorithms/StateIdImpl.java
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
package algorithms;
|
||||
|
||||
import algorithms.StateManager.StateId;
|
||||
|
||||
class StateIdImpl implements StateId {
|
||||
|
||||
private String name;
|
||||
|
||||
public StateIdImpl(String name) {
|
||||
super();
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String toString(){
|
||||
return name;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -55,6 +55,12 @@ interface StateManager {
|
|||
|
||||
}
|
||||
|
||||
interface StateId {
|
||||
|
||||
String toString();
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Map<VehicleRoute, States> getRouteStates();
|
||||
|
|
|
|||
|
|
@ -20,7 +20,12 @@
|
|||
******************************************************************************/
|
||||
package algorithms;
|
||||
|
||||
import algorithms.StateManager.StateId;
|
||||
|
||||
class StateTypes {
|
||||
|
||||
// final static StateId LOAD = new StateIdImpl("load");
|
||||
|
||||
final static String LOAD = "load";
|
||||
|
||||
final static String LOAD_AT_DEPOT = "loadAtDepot";
|
||||
|
|
@ -36,4 +41,8 @@ class StateTypes {
|
|||
final static String FUTURE_PICKS = "futurePicks";
|
||||
|
||||
final static String PAST_DELIVERIES = "pastDeliveries";
|
||||
|
||||
public static StateId createId(String stateId){
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -56,6 +56,7 @@ import basics.route.Start;
|
|||
import basics.route.TourActivity;
|
||||
import basics.route.Vehicle;
|
||||
import basics.route.VehicleRoute;
|
||||
import basics.route.VehicleType;
|
||||
|
||||
class StateUpdates {
|
||||
|
||||
|
|
@ -97,13 +98,13 @@ class StateUpdates {
|
|||
|
||||
// IterateRouteForwardInTime forwardInTime = new IterateRouteForwardInTime(tpCosts);
|
||||
// forwardInTime.addListener(new UpdateCostsAtAllLevels(actCosts, tpCosts, states));
|
||||
for(VehicleRoute route : vehicleRoutes){
|
||||
if(route.isEmpty()) continue;
|
||||
route.getVehicleRouteCostCalculator().reset();
|
||||
route.getVehicleRouteCostCalculator().addOtherCost(states.getRouteState(route, StateTypes.COSTS).toDouble());
|
||||
route.getVehicleRouteCostCalculator().price(route.getVehicle());
|
||||
// for(VehicleRoute route : vehicleRoutes){
|
||||
// if(route.isEmpty()) continue;
|
||||
// route.getVehicleRouteCostCalculator().reset();
|
||||
// route.getVehicleRouteCostCalculator().addOtherCost(states.getRouteState(route, StateTypes.COSTS).toDouble());
|
||||
// route.getVehicleRouteCostCalculator().price(route.getVehicle());
|
||||
// forwardInTime.iterate(route);
|
||||
}
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -188,8 +189,8 @@ class StateUpdates {
|
|||
double transportCost = this.transportCost.getTransportCost(prevAct.getLocationId(), act.getLocationId(), startTimeAtPrevAct, vehicleRoute.getDriver(), vehicleRoute.getVehicle());
|
||||
double actCost = activityCost.getActivityCost(act, timeTracker.getActArrTime(), vehicleRoute.getDriver(), vehicleRoute.getVehicle());
|
||||
|
||||
vehicleRoute.getVehicleRouteCostCalculator().addTransportCost(transportCost);
|
||||
vehicleRoute.getVehicleRouteCostCalculator().addActivityCost(actCost);
|
||||
// vehicleRoute.getVehicleRouteCostCalculator().addTransportCost(transportCost);
|
||||
// vehicleRoute.getVehicleRouteCostCalculator().addActivityCost(actCost);
|
||||
|
||||
totalOperationCost += transportCost;
|
||||
totalOperationCost += actCost;
|
||||
|
|
@ -206,18 +207,19 @@ class StateUpdates {
|
|||
double transportCost = this.transportCost.getTransportCost(prevAct.getLocationId(), vehicleRoute.getEnd().getLocationId(), startTimeAtPrevAct, vehicleRoute.getDriver(), vehicleRoute.getVehicle());
|
||||
double actCost = activityCost.getActivityCost(vehicleRoute.getEnd(), timeTracker.getActEndTime(), vehicleRoute.getDriver(), vehicleRoute.getVehicle());
|
||||
|
||||
vehicleRoute.getVehicleRouteCostCalculator().addTransportCost(transportCost);
|
||||
vehicleRoute.getVehicleRouteCostCalculator().addActivityCost(actCost);
|
||||
// vehicleRoute.getVehicleRouteCostCalculator().addTransportCost(transportCost);
|
||||
// vehicleRoute.getVehicleRouteCostCalculator().addActivityCost(actCost);
|
||||
|
||||
totalOperationCost += transportCost;
|
||||
totalOperationCost += actCost;
|
||||
totalOperationCost += getFixCosts();
|
||||
|
||||
states.putRouteState(vehicleRoute, StateTypes.COSTS, new StateImpl(totalOperationCost));
|
||||
|
||||
//this is rather strange and likely to change
|
||||
vehicleRoute.getVehicleRouteCostCalculator().price(vehicleRoute.getDriver());
|
||||
vehicleRoute.getVehicleRouteCostCalculator().price(vehicleRoute.getVehicle());
|
||||
vehicleRoute.getVehicleRouteCostCalculator().finish();
|
||||
// vehicleRoute.getVehicleRouteCostCalculator().price(vehicleRoute.getDriver());
|
||||
// vehicleRoute.getVehicleRouteCostCalculator().price(vehicleRoute.getVehicle());
|
||||
// vehicleRoute.getVehicleRouteCostCalculator().finish();
|
||||
|
||||
startTimeAtPrevAct = 0.0;
|
||||
prevAct = null;
|
||||
|
|
@ -225,6 +227,14 @@ class StateUpdates {
|
|||
totalOperationCost = 0.0;
|
||||
}
|
||||
|
||||
private double getFixCosts() {
|
||||
Vehicle vehicle = vehicleRoute.getVehicle();
|
||||
if(vehicle == null) return 0.0;
|
||||
VehicleType type = vehicle.getType();
|
||||
if(type == null) return 0.0;
|
||||
return type.getVehicleCostParams().fix;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static class UpdateEarliestStartTimeWindowAtActLocations implements ActivityVisitor{
|
||||
|
|
|
|||
|
|
@ -36,12 +36,6 @@ import org.apache.commons.configuration.XMLConfiguration;
|
|||
import org.apache.log4j.Logger;
|
||||
|
||||
import algorithms.HardConstraints.ConstraintManager;
|
||||
import algorithms.HardConstraints.HardTimeWindowActivityLevelConstraint;
|
||||
import algorithms.StateUpdates.UpdateActivityTimes;
|
||||
import algorithms.StateUpdates.UpdateCostsAtAllLevels;
|
||||
import algorithms.StateUpdates.UpdateEarliestStartTimeWindowAtActLocations;
|
||||
import algorithms.StateUpdates.UpdateLatestOperationStartTimeAtActLocations;
|
||||
import algorithms.StateUpdates.UpdateStates;
|
||||
import algorithms.VehicleRoutingAlgorithms.TypedMap.AbstractKey;
|
||||
import algorithms.VehicleRoutingAlgorithms.TypedMap.AcceptorKey;
|
||||
import algorithms.VehicleRoutingAlgorithms.TypedMap.InsertionStrategyKey;
|
||||
|
|
@ -68,12 +62,14 @@ import basics.algo.SearchStrategy;
|
|||
import basics.algo.SearchStrategy.DiscoveredSolution;
|
||||
import basics.algo.SearchStrategyManager;
|
||||
import basics.algo.SearchStrategyModule;
|
||||
import basics.algo.SolutionCostCalculator;
|
||||
import basics.algo.TimeBreaker;
|
||||
import basics.algo.VariationCoefficientBreaker;
|
||||
import basics.algo.VehicleRoutingAlgorithmListeners.PrioritizedVRAListener;
|
||||
import basics.algo.VehicleRoutingAlgorithmListeners.Priority;
|
||||
import basics.io.AlgorithmConfig;
|
||||
import basics.io.AlgorithmConfigXmlReader;
|
||||
import basics.route.VehicleRoute;
|
||||
|
||||
|
||||
|
||||
|
|
@ -474,7 +470,8 @@ public class VehicleRoutingAlgorithms {
|
|||
String name = getName(strategyConfig);
|
||||
SolutionAcceptor acceptor = getAcceptor(strategyConfig,vrp,algorithmListeners,definedClasses,solutionMemory);
|
||||
SolutionSelector selector = getSelector(strategyConfig,vrp,algorithmListeners,definedClasses);
|
||||
SearchStrategy strategy = new SearchStrategy(selector, acceptor);
|
||||
SolutionCostCalculator costCalculator = getCostCalculator(stateManager);
|
||||
SearchStrategy strategy = new SearchStrategy(selector, acceptor, costCalculator);
|
||||
strategy.setName(name);
|
||||
List<HierarchicalConfiguration> modulesConfig = strategyConfig.configurationsAt("modules.module");
|
||||
for(HierarchicalConfiguration moduleConfig : modulesConfig){
|
||||
|
|
@ -532,6 +529,21 @@ public class VehicleRoutingAlgorithms {
|
|||
return metaAlgorithm;
|
||||
}
|
||||
|
||||
private static SolutionCostCalculator getCostCalculator(final StateManagerImpl stateManager) {
|
||||
SolutionCostCalculator calc = new SolutionCostCalculator() {
|
||||
|
||||
@Override
|
||||
public void calculateCosts(VehicleRoutingProblemSolution solution) {
|
||||
double costs = 0.0;
|
||||
for(VehicleRoute route : solution.getRoutes()){
|
||||
costs += stateManager.getRouteState(route, StateTypes.COSTS).toDouble();
|
||||
}
|
||||
solution.setCost(costs);
|
||||
}
|
||||
};
|
||||
return calc;
|
||||
}
|
||||
|
||||
private static VehicleFleetManager createFleetManager(final VehicleRoutingProblem vrp) {
|
||||
if(vrp.getFleetSize().equals(FleetSize.INFINITE)){
|
||||
return new InfiniteVehicles(vrp.getVehicles());
|
||||
|
|
@ -611,7 +623,7 @@ public class VehicleRoutingAlgorithms {
|
|||
metaAlgorithm.getAlgorithmListeners().addAll(algorithmListeners);
|
||||
}
|
||||
|
||||
private static AlgorithmStartsListener createInitialSolution(XMLConfiguration config, final VehicleRoutingProblem vrp, VehicleFleetManager vehicleFleetManager, StateManagerImpl routeStates, Set<PrioritizedVRAListener> algorithmListeners, TypedMap definedClasses, ExecutorService executorService, int nuOfThreads, ConstraintManager constraintManager) {
|
||||
private static AlgorithmStartsListener createInitialSolution(XMLConfiguration config, final VehicleRoutingProblem vrp, VehicleFleetManager vehicleFleetManager, final StateManagerImpl routeStates, Set<PrioritizedVRAListener> algorithmListeners, TypedMap definedClasses, ExecutorService executorService, int nuOfThreads, ConstraintManager constraintManager) {
|
||||
List<HierarchicalConfiguration> modConfigs = config.configurationsAt("construction.insertion");
|
||||
if(modConfigs == null) return null;
|
||||
if(modConfigs.isEmpty()) return null;
|
||||
|
|
@ -636,12 +648,10 @@ public class VehicleRoutingAlgorithms {
|
|||
|
||||
@Override
|
||||
public void informAlgorithmStarts(VehicleRoutingProblem problem, VehicleRoutingAlgorithm algorithm, Collection<VehicleRoutingProblemSolution> solutions) {
|
||||
|
||||
CreateInitialSolution createInitialSolution = new CreateInitialSolution(finalInsertionStrategy);
|
||||
CreateInitialSolution createInitialSolution = new CreateInitialSolution(finalInsertionStrategy, getCostCalculator(routeStates));
|
||||
createInitialSolution.setGenerateAsMuchAsRoutesAsVehiclesExist(false);
|
||||
VehicleRoutingProblemSolution vrpSol = createInitialSolution.createInitialSolution(vrp);
|
||||
solutions.add(vrpSol);
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -68,15 +68,18 @@ public class SearchStrategy {
|
|||
private Collection<SearchStrategyModule> searchStrategyModules = new ArrayList<SearchStrategyModule>();
|
||||
|
||||
private SolutionSelector solutionSelector;
|
||||
|
||||
private SolutionCostCalculator solutionCostCalculator;
|
||||
|
||||
private SolutionAcceptor solutionAcceptor;
|
||||
|
||||
private String name;
|
||||
|
||||
public SearchStrategy(SolutionSelector solutionSelector, SolutionAcceptor solutionAcceptor) {
|
||||
public SearchStrategy(SolutionSelector solutionSelector, SolutionAcceptor solutionAcceptor, SolutionCostCalculator solutionCostCalculator) {
|
||||
super();
|
||||
this.solutionSelector = solutionSelector;
|
||||
this.solutionAcceptor = solutionAcceptor;
|
||||
this.solutionCostCalculator = solutionCostCalculator;
|
||||
logger.info("initialise " + this);
|
||||
}
|
||||
|
||||
|
|
@ -126,6 +129,7 @@ public class SearchStrategy {
|
|||
VehicleRoutingProblemSolution newSolution = module.runAndGetSolution(lastSolution);
|
||||
lastSolution = newSolution;
|
||||
}
|
||||
solutionCostCalculator.calculateCosts(lastSolution);
|
||||
boolean solutionAccepted = solutionAcceptor.acceptSolution(solutions, lastSolution);
|
||||
DiscoveredSolution discoveredSolution = new DiscoveredSolution(lastSolution, solutionAccepted, getName());
|
||||
return discoveredSolution;
|
||||
|
|
|
|||
|
|
@ -0,0 +1,14 @@
|
|||
package basics.algo;
|
||||
|
||||
import basics.VehicleRoutingProblemSolution;
|
||||
|
||||
public interface SolutionCostCalculator {
|
||||
|
||||
/**
|
||||
* This modifies the solution by setting its costs <br>
|
||||
* <code>solution.setCost(costs);</code>
|
||||
* @param solution
|
||||
*/
|
||||
public void calculateCosts(VehicleRoutingProblemSolution solution);
|
||||
|
||||
}
|
||||
|
|
@ -86,21 +86,10 @@ public class VehicleRoute {
|
|||
|
||||
private VehicleRouteCostCalculator costCalculator = new DefaultVehicleRouteCostCalculator();
|
||||
|
||||
public void setVehicleRouteCostCalculator(VehicleRouteCostCalculator costAccumulator){
|
||||
this.costCalculator = costAccumulator;
|
||||
}
|
||||
|
||||
public VehicleRouteCostCalculator getVehicleRouteCostCalculator(){
|
||||
return costCalculator;
|
||||
}
|
||||
|
||||
public double getCost() {
|
||||
if(tourActivities.isEmpty()){
|
||||
return 0.0;
|
||||
}
|
||||
return costCalculator.getCosts();
|
||||
}
|
||||
|
||||
private VehicleRoute(VehicleRoute route){
|
||||
this.start = Start.copyOf(route.getStart());
|
||||
this.end = End.copyOf(route.getEnd());
|
||||
|
|
@ -192,5 +181,18 @@ public class VehicleRoute {
|
|||
public End getEnd() {
|
||||
return end;
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public void setVehicleRouteCostCalculator(VehicleRouteCostCalculator costAccumulator){
|
||||
this.costCalculator = costAccumulator;
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public double getCost() {
|
||||
if(tourActivities.isEmpty()){
|
||||
return 0.0;
|
||||
}
|
||||
return costCalculator.getCosts();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,7 +20,7 @@
|
|||
******************************************************************************/
|
||||
package basics.route;
|
||||
|
||||
|
||||
@Deprecated
|
||||
public interface VehicleRouteCostCalculator {
|
||||
|
||||
public void addTransportCost(double cost);
|
||||
|
|
|
|||
|
|
@ -39,8 +39,10 @@ import basics.VehicleRoutingProblemSolution;
|
|||
import basics.algo.IterationStartsListener;
|
||||
import basics.algo.SearchStrategy;
|
||||
import basics.algo.SearchStrategyManager;
|
||||
import basics.algo.SolutionCostCalculator;
|
||||
import basics.io.VrpXMLReader;
|
||||
import basics.route.TourActivity;
|
||||
import basics.route.VehicleRoute;
|
||||
|
||||
public class BuildCVRPAlgoFromScratchTest {
|
||||
|
||||
|
|
@ -73,11 +75,23 @@ public class BuildCVRPAlgoFromScratchTest {
|
|||
RuinRadial radial = new RuinRadial(vrp, 0.15, new JobDistanceAvgCosts(vrp.getTransportCosts()));
|
||||
RuinRandom random = new RuinRandom(vrp, 0.25);
|
||||
|
||||
SearchStrategy randomStrategy = new SearchStrategy(new SelectBest(), new AcceptNewIfBetterThanWorst(1));
|
||||
SolutionCostCalculator solutionCostCalculator = new SolutionCostCalculator() {
|
||||
|
||||
@Override
|
||||
public void calculateCosts(VehicleRoutingProblemSolution solution) {
|
||||
double costs = 0.0;
|
||||
for(VehicleRoute route : solution.getRoutes()){
|
||||
costs += stateManager.getRouteState(route, StateTypes.COSTS).toDouble();
|
||||
}
|
||||
solution.setCost(costs);
|
||||
}
|
||||
};
|
||||
|
||||
SearchStrategy randomStrategy = new SearchStrategy(new SelectBest(), new AcceptNewIfBetterThanWorst(1), solutionCostCalculator);
|
||||
RuinAndRecreateModule randomModule = new RuinAndRecreateModule("randomRuin_bestInsertion", bestInsertion, random);
|
||||
randomStrategy.addModule(randomModule);
|
||||
|
||||
SearchStrategy radialStrategy = new SearchStrategy(new SelectBest(), new AcceptNewIfBetterThanWorst(1));
|
||||
SearchStrategy radialStrategy = new SearchStrategy(new SelectBest(), new AcceptNewIfBetterThanWorst(1), solutionCostCalculator);
|
||||
RuinAndRecreateModule radialModule = new RuinAndRecreateModule("radialRuin_bestInsertion", bestInsertion, radial);
|
||||
radialStrategy.addModule(radialModule);
|
||||
|
||||
|
|
@ -101,7 +115,7 @@ public class BuildCVRPAlgoFromScratchTest {
|
|||
vra.getSearchStrategyManager().addSearchStrategyModuleListener(new UpdateCostsAtRouteLevel(stateManager, vrp.getTransportCosts(), vrp.getActivityCosts()));
|
||||
vra.getSearchStrategyManager().addSearchStrategyModuleListener(new UpdateLoadAtRouteLevel(stateManager));
|
||||
|
||||
VehicleRoutingProblemSolution iniSolution = new CreateInitialSolution(bestInsertion).createInitialSolution(vrp);
|
||||
VehicleRoutingProblemSolution iniSolution = new CreateInitialSolution(bestInsertion, solutionCostCalculator).createInitialSolution(vrp);
|
||||
// System.out.println("ini: costs="+iniSolution.getCost()+";#routes="+iniSolution.getRoutes().size());
|
||||
vra.addInitialSolution(iniSolution);
|
||||
|
||||
|
|
|
|||
|
|
@ -48,6 +48,7 @@ import basics.algo.IterationStartsListener;
|
|||
import basics.algo.JobInsertedListener;
|
||||
import basics.algo.SearchStrategy;
|
||||
import basics.algo.SearchStrategyManager;
|
||||
import basics.algo.SolutionCostCalculator;
|
||||
import basics.io.VrpXMLReader;
|
||||
import basics.io.VrpXMLWriter;
|
||||
import basics.route.DeliveryActivity;
|
||||
|
|
@ -91,11 +92,23 @@ public class BuildPDVRPAlgoFromScratchTest {
|
|||
RuinRadial radial = new RuinRadial(vrp, 0.15, new JobDistanceAvgCosts(vrp.getTransportCosts()));
|
||||
RuinRandom random = new RuinRandom(vrp, 0.25);
|
||||
|
||||
SearchStrategy randomStrategy = new SearchStrategy(new SelectBest(), new AcceptNewIfBetterThanWorst(1));
|
||||
SolutionCostCalculator solutionCostCalculator = new SolutionCostCalculator() {
|
||||
|
||||
@Override
|
||||
public void calculateCosts(VehicleRoutingProblemSolution solution) {
|
||||
double costs = 0.0;
|
||||
for(VehicleRoute route : solution.getRoutes()){
|
||||
costs += stateManager.getRouteState(route, StateTypes.COSTS).toDouble();
|
||||
}
|
||||
solution.setCost(costs);
|
||||
}
|
||||
};
|
||||
|
||||
SearchStrategy randomStrategy = new SearchStrategy(new SelectBest(), new AcceptNewIfBetterThanWorst(1), solutionCostCalculator);
|
||||
RuinAndRecreateModule randomModule = new RuinAndRecreateModule("randomRuin_bestInsertion", bestInsertion, random);
|
||||
randomStrategy.addModule(randomModule);
|
||||
|
||||
SearchStrategy radialStrategy = new SearchStrategy(new SelectBest(), new AcceptNewIfBetterThanWorst(1));
|
||||
SearchStrategy radialStrategy = new SearchStrategy(new SelectBest(), new AcceptNewIfBetterThanWorst(1), solutionCostCalculator);
|
||||
RuinAndRecreateModule radialModule = new RuinAndRecreateModule("radialRuin_bestInsertion", bestInsertion, radial);
|
||||
radialStrategy.addModule(radialModule);
|
||||
|
||||
|
|
@ -172,7 +185,7 @@ public class BuildPDVRPAlgoFromScratchTest {
|
|||
bestInsertion.addListener(loadVehicleInDepot);
|
||||
bestInsertion.addListener(updateLoadAfterJobHasBeenInserted);
|
||||
|
||||
VehicleRoutingProblemSolution iniSolution = new CreateInitialSolution(bestInsertion).createInitialSolution(vrp);
|
||||
VehicleRoutingProblemSolution iniSolution = new CreateInitialSolution(bestInsertion, solutionCostCalculator).createInitialSolution(vrp);
|
||||
// System.out.println("ini: costs="+iniSolution.getCost()+";#routes="+iniSolution.getRoutes().size());
|
||||
vra.addInitialSolution(iniSolution);
|
||||
|
||||
|
|
|
|||
|
|
@ -187,7 +187,7 @@ public class GendreauPostOptTest {
|
|||
// routes.add(new VehicleRoute(getEmptyTour(),getDriver(),getNoVehicle()));
|
||||
// routes.add(new VehicleRoute(getEmptyTour(),getDriver(),getNoVehicle()));
|
||||
|
||||
VehicleRoutingProblemSolution sol = new VehicleRoutingProblemSolution(routes, route.getCost());
|
||||
VehicleRoutingProblemSolution sol = new VehicleRoutingProblemSolution(routes, states.getRouteState(route, StateTypes.COSTS).toDouble());
|
||||
|
||||
assertEquals(110.0, sol.getCost(), 0.5);
|
||||
|
||||
|
|
@ -202,12 +202,21 @@ public class GendreauPostOptTest {
|
|||
postOpt.setFleetManager(fleetManager);
|
||||
|
||||
VehicleRoutingProblemSolution newSolution = postOpt.runAndGetSolution(sol);
|
||||
newSolution.setCost(getCosts(newSolution,states));
|
||||
|
||||
assertEquals(2,RouteUtils.getNuOfActiveRoutes(newSolution.getRoutes()));
|
||||
assertEquals(2,newSolution.getRoutes().size());
|
||||
assertEquals(80.0,newSolution.getCost(),0.5);
|
||||
}
|
||||
|
||||
private double getCosts(VehicleRoutingProblemSolution newSolution, StateManagerImpl states) {
|
||||
double c = 0.0;
|
||||
for(VehicleRoute r : newSolution.getRoutes()){
|
||||
c += states.getRouteState(r, StateTypes.COSTS).toDouble();
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPostOpt_optsRoutesWithMoreThanTwoJobs_oneRouteBecomesTwoRoutes(){
|
||||
Collection<Job> jobs = new ArrayList<Job>();
|
||||
|
|
@ -233,6 +242,7 @@ public class GendreauPostOptTest {
|
|||
routes.add(route);
|
||||
|
||||
VehicleRoutingProblemSolution sol = new VehicleRoutingProblemSolution(routes, route.getCost());
|
||||
sol.setCost(getCosts(sol,states));
|
||||
|
||||
assertEquals(110.0, sol.getCost(), 0.5);
|
||||
|
||||
|
|
@ -246,6 +256,7 @@ public class GendreauPostOptTest {
|
|||
postOpt.setFleetManager(fleetManager);
|
||||
// postOpt.setWithFix(withFixCost);
|
||||
VehicleRoutingProblemSolution newSolution = postOpt.runAndGetSolution(sol);
|
||||
newSolution.setCost(getCosts(newSolution,states));
|
||||
|
||||
assertEquals(2,RouteUtils.getNuOfActiveRoutes(newSolution.getRoutes()));
|
||||
assertEquals(2,newSolution.getRoutes().size());
|
||||
|
|
|
|||
|
|
@ -46,8 +46,9 @@ public class SearchStrategyTest {
|
|||
public void whenANullModule_IsAdded_throwException(){
|
||||
SolutionSelector select = mock(SolutionSelector.class);
|
||||
SolutionAcceptor accept = mock(SolutionAcceptor.class);
|
||||
SolutionCostCalculator calc = mock(SolutionCostCalculator.class);
|
||||
|
||||
SearchStrategy strat = new SearchStrategy(select, accept);
|
||||
SearchStrategy strat = new SearchStrategy(select, accept, calc);
|
||||
strat.addModule(null);
|
||||
|
||||
}
|
||||
|
|
@ -56,6 +57,7 @@ public class SearchStrategyTest {
|
|||
public void whenStratRunsWithOneModule_runItOnes(){
|
||||
SolutionSelector select = mock(SolutionSelector.class);
|
||||
SolutionAcceptor accept = mock(SolutionAcceptor.class);
|
||||
SolutionCostCalculator calc = mock(SolutionCostCalculator.class);
|
||||
|
||||
final VehicleRoutingProblem vrp = mock(VehicleRoutingProblem.class);
|
||||
final VehicleRoutingProblemSolution newSol = mock(VehicleRoutingProblemSolution.class);
|
||||
|
|
@ -64,7 +66,7 @@ public class SearchStrategyTest {
|
|||
|
||||
final Collection<Integer> runs = new ArrayList<Integer>();
|
||||
|
||||
SearchStrategy strat = new SearchStrategy(select, accept);
|
||||
SearchStrategy strat = new SearchStrategy(select, accept, calc);
|
||||
SearchStrategyModule mod = new SearchStrategyModule() {
|
||||
|
||||
@Override
|
||||
|
|
@ -96,6 +98,7 @@ public class SearchStrategyTest {
|
|||
public void whenStratRunsWithTwoModule_runItTwice(){
|
||||
SolutionSelector select = mock(SolutionSelector.class);
|
||||
SolutionAcceptor accept = mock(SolutionAcceptor.class);
|
||||
SolutionCostCalculator calc = mock(SolutionCostCalculator.class);
|
||||
|
||||
final VehicleRoutingProblem vrp = mock(VehicleRoutingProblem.class);
|
||||
final VehicleRoutingProblemSolution newSol = mock(VehicleRoutingProblemSolution.class);
|
||||
|
|
@ -104,7 +107,7 @@ public class SearchStrategyTest {
|
|||
|
||||
final Collection<Integer> runs = new ArrayList<Integer>();
|
||||
|
||||
SearchStrategy strat = new SearchStrategy(select, accept);
|
||||
SearchStrategy strat = new SearchStrategy(select, accept, calc);
|
||||
|
||||
SearchStrategyModule mod = new SearchStrategyModule() {
|
||||
|
||||
|
|
@ -159,6 +162,7 @@ public class SearchStrategyTest {
|
|||
public void whenStratRunsWithNModule_runItNTimes(){
|
||||
SolutionSelector select = mock(SolutionSelector.class);
|
||||
SolutionAcceptor accept = mock(SolutionAcceptor.class);
|
||||
SolutionCostCalculator calc = mock(SolutionCostCalculator.class);
|
||||
|
||||
final VehicleRoutingProblem vrp = mock(VehicleRoutingProblem.class);
|
||||
final VehicleRoutingProblemSolution newSol = mock(VehicleRoutingProblemSolution.class);
|
||||
|
|
@ -169,7 +173,7 @@ public class SearchStrategyTest {
|
|||
|
||||
final Collection<Integer> runs = new ArrayList<Integer>();
|
||||
|
||||
SearchStrategy strat = new SearchStrategy(select, accept);
|
||||
SearchStrategy strat = new SearchStrategy(select, accept, calc);
|
||||
|
||||
for(int i=0;i<N;i++){
|
||||
SearchStrategyModule mod = new SearchStrategyModule() {
|
||||
|
|
@ -203,6 +207,7 @@ public class SearchStrategyTest {
|
|||
public void whenSelectorDeliversNullSolution_throwException(){
|
||||
SolutionSelector select = mock(SolutionSelector.class);
|
||||
SolutionAcceptor accept = mock(SolutionAcceptor.class);
|
||||
SolutionCostCalculator calc = mock(SolutionCostCalculator.class);
|
||||
|
||||
final VehicleRoutingProblem vrp = mock(VehicleRoutingProblem.class);
|
||||
|
||||
|
|
@ -212,7 +217,7 @@ public class SearchStrategyTest {
|
|||
|
||||
final Collection<Integer> runs = new ArrayList<Integer>();
|
||||
|
||||
SearchStrategy strat = new SearchStrategy(select, accept);
|
||||
SearchStrategy strat = new SearchStrategy(select, accept, calc);
|
||||
|
||||
for(int i=0;i<N;i++){
|
||||
SearchStrategyModule mod = new SearchStrategyModule() {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue