From 552729d7ee9f87fea6e5a932aac1e7cea2ef03af Mon Sep 17 00:00:00 2001 From: oblonski Date: Wed, 22 Jul 2015 22:53:45 +0200 Subject: [PATCH] variable departure times and waiting time opt --- ...igureLocalActivityInsertionCalculator.java | 40 +++++ .../JobInsertionCostsCalculatorBuilder.java | 8 +- ...LocalActivityInsertionCostsCalculator.java | 29 +++- .../algorithm/state/UpdateDepartureTime.java | 54 +++++++ .../jsprit/core/problem/vehicle/Vehicle.java | 2 + .../core/problem/vehicle/VehicleImpl.java | 26 +++- .../VariableDepartureAndWaitingTime_IT.java | 143 ++++++++++++++++++ .../state/UpdateDepartureTimeTest.java | 97 ++++++++++++ .../core/problem/vehicle/VehicleImplTest.java | 12 +- .../jsprit/examples/CostMatrixExample.java | 6 +- 10 files changed, 403 insertions(+), 14 deletions(-) create mode 100644 jsprit-core/src/main/java/jsprit/core/algorithm/recreate/ConfigureLocalActivityInsertionCalculator.java create mode 100644 jsprit-core/src/main/java/jsprit/core/algorithm/state/UpdateDepartureTime.java create mode 100644 jsprit-core/src/test/java/jsprit/core/algorithm/VariableDepartureAndWaitingTime_IT.java create mode 100644 jsprit-core/src/test/java/jsprit/core/algorithm/state/UpdateDepartureTimeTest.java diff --git a/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/ConfigureLocalActivityInsertionCalculator.java b/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/ConfigureLocalActivityInsertionCalculator.java new file mode 100644 index 00000000..5f72aeb2 --- /dev/null +++ b/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/ConfigureLocalActivityInsertionCalculator.java @@ -0,0 +1,40 @@ +package jsprit.core.algorithm.recreate; + +import jsprit.core.algorithm.recreate.listener.InsertionStartsListener; +import jsprit.core.algorithm.recreate.listener.JobInsertedListener; +import jsprit.core.problem.VehicleRoutingProblem; +import jsprit.core.problem.job.Job; +import jsprit.core.problem.solution.route.VehicleRoute; + +import java.util.Collection; + +/** + * Created by schroeder on 22/07/15. + */ +public class ConfigureLocalActivityInsertionCalculator implements InsertionStartsListener, JobInsertedListener { + + private VehicleRoutingProblem vrp; + + private LocalActivityInsertionCostsCalculator localActivityInsertionCostsCalculator; + + private int nuOfJobsToRecreate; + + public ConfigureLocalActivityInsertionCalculator(VehicleRoutingProblem vrp, LocalActivityInsertionCostsCalculator localActivityInsertionCostsCalculator) { + this.vrp = vrp; + this.localActivityInsertionCostsCalculator = localActivityInsertionCostsCalculator; + } + + @Override + public void informInsertionStarts(Collection vehicleRoutes, Collection unassignedJobs) { + this.nuOfJobsToRecreate = unassignedJobs.size(); + double completenessRatio = (1-((double)nuOfJobsToRecreate/(double)vrp.getJobs().values().size())); + localActivityInsertionCostsCalculator.setSolutionCompletenessRatio(completenessRatio); + } + + @Override + public void informJobInserted(Job job2insert, VehicleRoute inRoute, double additionalCosts, double additionalTime) { + nuOfJobsToRecreate--; + double completenessRatio = (1-((double)nuOfJobsToRecreate/(double)vrp.getJobs().values().size())); + localActivityInsertionCostsCalculator.setSolutionCompletenessRatio(completenessRatio); + } +} diff --git a/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/JobInsertionCostsCalculatorBuilder.java b/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/JobInsertionCostsCalculatorBuilder.java index 32ba952b..a7d79ae9 100644 --- a/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/JobInsertionCostsCalculatorBuilder.java +++ b/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/JobInsertionCostsCalculatorBuilder.java @@ -259,8 +259,10 @@ public class JobInsertionCostsCalculatorBuilder { if(constraintManager == null) throw new IllegalStateException("constraint-manager is null"); ActivityInsertionCostsCalculator actInsertionCalc; + ConfigureLocalActivityInsertionCalculator configLocal = null; if(activityInsertionCostCalculator == null && addDefaultCostCalc){ actInsertionCalc = new LocalActivityInsertionCostsCalculator(vrp.getTransportCosts(), vrp.getActivityCosts()); + configLocal = new ConfigureLocalActivityInsertionCalculator(vrp, (LocalActivityInsertionCostsCalculator) actInsertionCalc); } else if(activityInsertionCostCalculator == null && !addDefaultCostCalc){ actInsertionCalc = new ActivityInsertionCostsCalculator(){ @@ -296,7 +298,11 @@ public class JobInsertionCostsCalculatorBuilder { switcher.put(Pickup.class, serviceInsertion); switcher.put(Delivery.class, serviceInsertion); - return new CalculatorPlusListeners(switcher); + CalculatorPlusListeners calculatorPlusListeners = new CalculatorPlusListeners(switcher); + if(configLocal != null){ + calculatorPlusListeners.insertionListener.add(configLocal); + } + return calculatorPlusListeners; } private CalculatorPlusListeners createCalculatorConsideringFixedCosts(VehicleRoutingProblem vrp, JobInsertionCostsCalculator baseCalculator, RouteAndActivityStateGetter activityStates2, double weightOfFixedCosts){ diff --git a/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/LocalActivityInsertionCostsCalculator.java b/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/LocalActivityInsertionCostsCalculator.java index 0a8e61c0..d58ecd86 100644 --- a/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/LocalActivityInsertionCostsCalculator.java +++ b/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/LocalActivityInsertionCostsCalculator.java @@ -22,6 +22,7 @@ import jsprit.core.problem.cost.VehicleRoutingActivityCosts; import jsprit.core.problem.cost.VehicleRoutingTransportCosts; import jsprit.core.problem.misc.JobInsertionContext; import jsprit.core.problem.solution.route.activity.End; +import jsprit.core.problem.solution.route.activity.Start; import jsprit.core.problem.solution.route.activity.TourActivity; import jsprit.core.util.CalculationUtils; @@ -40,8 +41,11 @@ class LocalActivityInsertionCostsCalculator implements ActivityInsertionCostsCal private VehicleRoutingTransportCosts routingCosts; private VehicleRoutingActivityCosts activityCosts; - - + + private double activityCostsWeight = 1.; + + private double solutionCompletenessRatio = 1.; + public LocalActivityInsertionCostsCalculator(VehicleRoutingTransportCosts routingCosts, VehicleRoutingActivityCosts actCosts) { super(); this.routingCosts = routingCosts; @@ -56,7 +60,12 @@ class LocalActivityInsertionCostsCalculator implements ActivityInsertionCostsCal double newAct_arrTime = depTimeAtPrevAct + tp_time_prevAct_newAct; double newAct_endTime = CalculationUtils.getActivityEndTime(newAct_arrTime, newAct); double act_costs_newAct = activityCosts.getActivityCost(newAct, newAct_arrTime, iFacts.getNewDriver(), iFacts.getNewVehicle()); - + if(prevAct instanceof Start) { + if(iFacts.getNewVehicle().hasVariableDepartureTime()){ + act_costs_newAct = 0; + } + } + //open routes if(nextAct instanceof End){ if(!iFacts.getNewVehicle().isReturnToDepot()){ @@ -68,22 +77,30 @@ class LocalActivityInsertionCostsCalculator implements ActivityInsertionCostsCal double tp_time_newAct_nextAct = routingCosts.getTransportTime(newAct.getLocation(), nextAct.getLocation(), newAct_endTime, iFacts.getNewDriver(), iFacts.getNewVehicle()); double nextAct_arrTime = newAct_endTime + tp_time_newAct_nextAct; double act_costs_nextAct = activityCosts.getActivityCost(nextAct, nextAct_arrTime, iFacts.getNewDriver(), iFacts.getNewVehicle()); - double totalCosts = tp_costs_prevAct_newAct + tp_costs_newAct_nextAct + act_costs_newAct + act_costs_nextAct; + + double totalCosts = tp_costs_prevAct_newAct + tp_costs_newAct_nextAct + solutionCompletenessRatio * activityCostsWeight * (act_costs_newAct + act_costs_nextAct); double oldCosts; if(iFacts.getRoute().isEmpty()){ double tp_costs_prevAct_nextAct = routingCosts.getTransportCost(prevAct.getLocation(), nextAct.getLocation(), depTimeAtPrevAct, iFacts.getNewDriver(), iFacts.getNewVehicle()); double arrTime_nextAct = routingCosts.getTransportTime(prevAct.getLocation(), nextAct.getLocation(), depTimeAtPrevAct, iFacts.getNewDriver(), iFacts.getNewVehicle()); double actCost_nextAct = activityCosts.getActivityCost(nextAct, arrTime_nextAct, iFacts.getNewDriver(), iFacts.getNewVehicle()); - oldCosts = tp_costs_prevAct_nextAct + actCost_nextAct; + oldCosts = tp_costs_prevAct_nextAct + solutionCompletenessRatio * activityCostsWeight * actCost_nextAct; } else{ double tp_costs_prevAct_nextAct = routingCosts.getTransportCost(prevAct.getLocation(), nextAct.getLocation(), prevAct.getEndTime(), iFacts.getRoute().getDriver(), iFacts.getRoute().getVehicle()); double arrTime_nextAct = routingCosts.getTransportTime(prevAct.getLocation(), nextAct.getLocation(), prevAct.getEndTime(), iFacts.getRoute().getDriver(), iFacts.getRoute().getVehicle()); double actCost_nextAct = activityCosts.getActivityCost(nextAct, arrTime_nextAct, iFacts.getRoute().getDriver(), iFacts.getRoute().getVehicle()); - oldCosts = tp_costs_prevAct_nextAct + actCost_nextAct; + oldCosts = tp_costs_prevAct_nextAct + solutionCompletenessRatio * activityCostsWeight * actCost_nextAct; } return totalCosts - oldCosts; } + public void setActivityCostWeight(double weight){ + this.activityCostsWeight = weight; + } + + public void setSolutionCompletenessRatio(double solutionCompletenessRatio) { + this.solutionCompletenessRatio = solutionCompletenessRatio; + } } diff --git a/jsprit-core/src/main/java/jsprit/core/algorithm/state/UpdateDepartureTime.java b/jsprit-core/src/main/java/jsprit/core/algorithm/state/UpdateDepartureTime.java new file mode 100644 index 00000000..11319341 --- /dev/null +++ b/jsprit-core/src/main/java/jsprit/core/algorithm/state/UpdateDepartureTime.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.state; + +import jsprit.core.problem.cost.VehicleRoutingTransportCosts; +import jsprit.core.problem.solution.route.RouteVisitor; +import jsprit.core.problem.solution.route.VehicleRoute; +import jsprit.core.problem.solution.route.activity.TourActivity; + +public class UpdateDepartureTime implements StateUpdater, RouteVisitor{ + + private VehicleRoutingTransportCosts routingCosts; + + public UpdateDepartureTime(VehicleRoutingTransportCosts routingCosts) { + this.routingCosts = routingCosts; + } + + @Override + public void visit(VehicleRoute route) { + if(route.getVehicle() != null){ + if(route.getVehicle().hasVariableDepartureTime()) { + if (!route.isEmpty()) { + TourActivity first = route.getActivities().get(0); + double earliestStart = first.getTheoreticalEarliestOperationStartTime(); + double backwardTravelTime = routingCosts.getBackwardTransportTime(first.getLocation(), route.getStart().getLocation(), + earliestStart, route.getDriver(), route.getVehicle()); + double newDepartureTime = Math.max(route.getStart().getEndTime(), earliestStart - backwardTravelTime); + route.setVehicleAndDepartureTime(route.getVehicle(), newDepartureTime); + } + } + } + } + + + + + + + +} diff --git a/jsprit-core/src/main/java/jsprit/core/problem/vehicle/Vehicle.java b/jsprit-core/src/main/java/jsprit/core/problem/vehicle/Vehicle.java index b29391d1..59b025ff 100644 --- a/jsprit-core/src/main/java/jsprit/core/problem/vehicle/Vehicle.java +++ b/jsprit-core/src/main/java/jsprit/core/problem/vehicle/Vehicle.java @@ -72,4 +72,6 @@ public interface Vehicle extends HasId, HasIndex { public abstract VehicleTypeKey getVehicleTypeIdentifier(); public abstract Skills getSkills(); + + public boolean hasVariableDepartureTime(); } diff --git a/jsprit-core/src/main/java/jsprit/core/problem/vehicle/VehicleImpl.java b/jsprit-core/src/main/java/jsprit/core/problem/vehicle/VehicleImpl.java index bfcf00a1..dcd3ee0c 100644 --- a/jsprit-core/src/main/java/jsprit/core/problem/vehicle/VehicleImpl.java +++ b/jsprit-core/src/main/java/jsprit/core/problem/vehicle/VehicleImpl.java @@ -91,7 +91,12 @@ public class VehicleImpl extends AbstractVehicle{ public Skills getSkills() { return null; } - } + + @Override + public boolean hasVariableDepartureTime() { + return false; + } + } /** * Builder that builds the vehicle. @@ -125,7 +130,9 @@ public class VehicleImpl extends AbstractVehicle{ private Location endLocation; - private Builder(String id) { + private boolean hasVariableDepartureTime = false; + + private Builder(String id) { super(); this.id = id; } @@ -142,6 +149,11 @@ public class VehicleImpl extends AbstractVehicle{ this.type = type; return this; } + + public Builder setHasVariableDepartureTime(boolean hasVariableDepartureTime){ + this.hasVariableDepartureTime = hasVariableDepartureTime; + return this; + } /** * Sets the flag whether the vehicle must return to depot or not. @@ -274,6 +286,8 @@ public class VehicleImpl extends AbstractVehicle{ private final Location startLocation; + private final boolean hasVariableDepartureTime; + private VehicleImpl(Builder builder){ id = builder.id; type = builder.type; @@ -283,6 +297,7 @@ public class VehicleImpl extends AbstractVehicle{ skills = builder.skills; endLocation = builder.endLocation; startLocation = builder.startLocation; + hasVariableDepartureTime = builder.hasVariableDepartureTime; setVehicleIdentifier(new VehicleTypeKey(type.getTypeId(),startLocation.getId(),endLocation.getId(),earliestDeparture,latestArrival,skills)); } @@ -341,7 +356,12 @@ public class VehicleImpl extends AbstractVehicle{ return skills; } - /* (non-Javadoc) + @Override + public boolean hasVariableDepartureTime() { + return hasVariableDepartureTime; + } + + /* (non-Javadoc) * @see java.lang.Object#hashCode() */ @Override diff --git a/jsprit-core/src/test/java/jsprit/core/algorithm/VariableDepartureAndWaitingTime_IT.java b/jsprit-core/src/test/java/jsprit/core/algorithm/VariableDepartureAndWaitingTime_IT.java new file mode 100644 index 00000000..a9432fde --- /dev/null +++ b/jsprit-core/src/test/java/jsprit/core/algorithm/VariableDepartureAndWaitingTime_IT.java @@ -0,0 +1,143 @@ +package jsprit.core.algorithm; + +import jsprit.core.algorithm.box.Jsprit; +import jsprit.core.algorithm.state.StateManager; +import jsprit.core.algorithm.state.UpdateDepartureTime; +import jsprit.core.analysis.SolutionAnalyser; +import jsprit.core.problem.Location; +import jsprit.core.problem.VehicleRoutingProblem; +import jsprit.core.problem.constraint.ConstraintManager; +import jsprit.core.problem.cost.TransportDistance; +import jsprit.core.problem.cost.VehicleRoutingActivityCosts; +import jsprit.core.problem.driver.Driver; +import jsprit.core.problem.job.Service; +import jsprit.core.problem.solution.SolutionCostCalculator; +import jsprit.core.problem.solution.VehicleRoutingProblemSolution; +import jsprit.core.problem.solution.route.activity.TimeWindow; +import jsprit.core.problem.solution.route.activity.TourActivity; +import jsprit.core.problem.vehicle.Vehicle; +import jsprit.core.problem.vehicle.VehicleImpl; +import jsprit.core.util.CostFactory; +import jsprit.core.util.Solutions; +import junit.framework.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * Created by schroeder on 22/07/15. + */ +public class VariableDepartureAndWaitingTime_IT { + + static interface AlgorithmFactory { + VehicleRoutingAlgorithm createAlgorithm(VehicleRoutingProblem vrp); + } + + VehicleRoutingActivityCosts activityCosts; + + AlgorithmFactory algorithmFactory; + + @Before + public void doBefore(){ + activityCosts = new VehicleRoutingActivityCosts() { + + @Override + public double getActivityCost(TourActivity tourAct, double arrivalTime, Driver driver, Vehicle vehicle) { + return Math.max(0,tourAct.getTheoreticalEarliestOperationStartTime() - arrivalTime); + } + + }; + algorithmFactory = new AlgorithmFactory() { + @Override + public VehicleRoutingAlgorithm createAlgorithm(final VehicleRoutingProblem vrp) { + StateManager stateManager = new StateManager(vrp); + stateManager.addStateUpdater(new UpdateDepartureTime(vrp.getTransportCosts())); + ConstraintManager constraintManager = new ConstraintManager(vrp,stateManager); + + return Jsprit.Builder.newInstance(vrp) + .addCoreStateAndConstraintStuff(true) + .setStateAndConstraintManager(stateManager,constraintManager) + .setObjectiveFunction(new SolutionCostCalculator() { + @Override + public double getCosts(VehicleRoutingProblemSolution solution) { + SolutionAnalyser sa = new SolutionAnalyser(vrp, solution, new TransportDistance() { + @Override + public double getDistance(Location from, Location to) { + return vrp.getTransportCosts().getTransportCost(from, to, 0., null, null); + } + }); + return sa.getWaitingTime() + sa.getDistance(); + } + }) + .buildAlgorithm(); + } + }; + } + + @Test + public void plainSetupShouldWork(){ + VehicleImpl v = VehicleImpl.Builder.newInstance("v").setStartLocation(Location.newInstance(0,0)).build(); + Service s1 = Service.Builder.newInstance("s1").setLocation(Location.newInstance(10,0)).build(); + Service s2 = Service.Builder.newInstance("s2").setLocation(Location.newInstance(20,0)).build(); + VehicleRoutingProblem vrp = VehicleRoutingProblem.Builder.newInstance() + .addJob(s1).addJob(s2).addVehicle(v) + .setFleetSize(VehicleRoutingProblem.FleetSize.FINITE) + .setRoutingCost(CostFactory.createManhattanCosts()) + .setActivityCosts(activityCosts) + .build(); + VehicleRoutingAlgorithm vra = algorithmFactory.createAlgorithm(vrp); + VehicleRoutingProblemSolution solution = Solutions.bestOf(vra.searchSolutions()); + Assert.assertEquals(40.,solution.getCost()); + } + + @Test + public void withTimeWindowsShouldWork(){ + VehicleImpl v = VehicleImpl.Builder.newInstance("v").setStartLocation(Location.newInstance(0,0)).build(); + Service s1 = Service.Builder.newInstance("s1").setTimeWindow(TimeWindow.newInstance(1010,1100)).setLocation(Location.newInstance(10,0)).build(); + Service s2 = Service.Builder.newInstance("s2").setTimeWindow(TimeWindow.newInstance(1020,1100)).setLocation(Location.newInstance(20,0)).build(); + final VehicleRoutingProblem vrp = VehicleRoutingProblem.Builder.newInstance() + .addJob(s1).addJob(s2).addVehicle(v) + .setFleetSize(VehicleRoutingProblem.FleetSize.FINITE) + .setRoutingCost(CostFactory.createManhattanCosts()) + .setActivityCosts(activityCosts) + .build(); + VehicleRoutingAlgorithm vra = algorithmFactory.createAlgorithm(vrp); + VehicleRoutingProblemSolution solution = Solutions.bestOf(vra.searchSolutions()); + Assert.assertEquals(40.+1000.,solution.getCost()); + } + + @Test + public void variableDepartureShouldWork(){ + VehicleImpl v = VehicleImpl.Builder.newInstance("v").setHasVariableDepartureTime(true).setStartLocation(Location.newInstance(0, 0)).build(); + Service s1 = Service.Builder.newInstance("s1").setTimeWindow(TimeWindow.newInstance(1010,1100)).setLocation(Location.newInstance(10,0)).build(); + Service s2 = Service.Builder.newInstance("s2").setTimeWindow(TimeWindow.newInstance(1020,1100)).setLocation(Location.newInstance(20,0)).build(); + final VehicleRoutingProblem vrp = VehicleRoutingProblem.Builder.newInstance() + .addJob(s1).addJob(s2).addVehicle(v) + .setFleetSize(VehicleRoutingProblem.FleetSize.FINITE) + .setRoutingCost(CostFactory.createManhattanCosts()) + .setActivityCosts(activityCosts) + .build(); + VehicleRoutingAlgorithm vra = algorithmFactory.createAlgorithm(vrp); + VehicleRoutingProblemSolution solution = Solutions.bestOf(vra.searchSolutions()); + Assert.assertEquals(40.,solution.getCost()); + } + + @Test + public void variableDepartureShouldWork2(){ + VehicleImpl v = VehicleImpl.Builder.newInstance("v").setHasVariableDepartureTime(true).setStartLocation(Location.newInstance(0, 0)).build(); + Service s1 = Service.Builder.newInstance("s1").setTimeWindow(TimeWindow.newInstance(1010,1100)).setLocation(Location.newInstance(10,0)).build(); + Service s2 = Service.Builder.newInstance("s2").setTimeWindow(TimeWindow.newInstance(1020,1100)).setLocation(Location.newInstance(20,0)).build(); + Service s3 = Service.Builder.newInstance("s3").setTimeWindow(TimeWindow.newInstance(1020,1100)).setLocation(Location.newInstance(15,0)).build(); + final VehicleRoutingProblem vrp = VehicleRoutingProblem.Builder.newInstance() + .addJob(s1).addJob(s2).addVehicle(v) + .setFleetSize(VehicleRoutingProblem.FleetSize.FINITE) + .setRoutingCost(CostFactory.createManhattanCosts()) + .setActivityCosts(activityCosts) + .build(); + VehicleRoutingAlgorithm vra = algorithmFactory.createAlgorithm(vrp); + VehicleRoutingProblemSolution solution = Solutions.bestOf(vra.searchSolutions()); + Assert.assertEquals(40.,solution.getCost()); + } + + + +} diff --git a/jsprit-core/src/test/java/jsprit/core/algorithm/state/UpdateDepartureTimeTest.java b/jsprit-core/src/test/java/jsprit/core/algorithm/state/UpdateDepartureTimeTest.java new file mode 100644 index 00000000..7f1c9e9d --- /dev/null +++ b/jsprit-core/src/test/java/jsprit/core/algorithm/state/UpdateDepartureTimeTest.java @@ -0,0 +1,97 @@ +package jsprit.core.algorithm.state; + +import jsprit.core.problem.Location; +import jsprit.core.problem.cost.VehicleRoutingTransportCosts; +import jsprit.core.problem.job.Service; +import jsprit.core.problem.solution.route.VehicleRoute; +import jsprit.core.problem.solution.route.activity.TimeWindow; +import jsprit.core.problem.vehicle.VehicleImpl; +import jsprit.core.util.CostFactory; +import junit.framework.Assert; +import org.junit.Test; + +/** + * Created by schroeder on 22/07/15. + */ +public class UpdateDepartureTimeTest { + + @Test + public void whenNoTimeWindow_departureTimeShouldBeEarliestStartTime(){ + VehicleRoutingTransportCosts routingCosts = CostFactory.createManhattanCosts(); + VehicleImpl v = VehicleImpl.Builder.newInstance("v").setStartLocation(Location.newInstance(0,0)).build(); + Service s = Service.Builder.newInstance("s").setLocation(Location.newInstance(10,0)).build(); + VehicleRoute route = VehicleRoute.Builder.newInstance(v).addService(s).build(); + + UpdateDepartureTime udt = new UpdateDepartureTime(routingCosts); + udt.visit(route); + + Assert.assertEquals(0.,route.getDepartureTime()); + } + + @Test + public void whenNoTimeWindow_departureTimeShouldBeEarliestStartTime2(){ + VehicleRoutingTransportCosts routingCosts = CostFactory.createManhattanCosts(); + VehicleImpl v = VehicleImpl.Builder.newInstance("v").setEarliestStart(10).setStartLocation(Location.newInstance(0, 0)).build(); + Service s = Service.Builder.newInstance("s").setLocation(Location.newInstance(10,0)).build(); + VehicleRoute route = VehicleRoute.Builder.newInstance(v).addService(s).build(); + + UpdateDepartureTime udt = new UpdateDepartureTime(routingCosts); + udt.visit(route); + + Assert.assertEquals(10.,route.getDepartureTime()); + } + + @Test + public void whenTimeWindowAndNoWaitingTime_departureTimeShouldBeEarliestStartTime1(){ + VehicleRoutingTransportCosts routingCosts = CostFactory.createManhattanCosts(); + VehicleImpl v = VehicleImpl.Builder.newInstance("v").setEarliestStart(0).setStartLocation(Location.newInstance(0,0)).build(); + Service s = Service.Builder.newInstance("s").setTimeWindow(TimeWindow.newInstance(5,20)).setLocation(Location.newInstance(10, 0)).build(); + VehicleRoute route = VehicleRoute.Builder.newInstance(v).addService(s).build(); + + UpdateDepartureTime udt = new UpdateDepartureTime(routingCosts); + udt.visit(route); + + Assert.assertEquals(0.,route.getDepartureTime()); + } + + @Test + public void whenTimeWindowAndNoWaitingTime_departureTimeShouldBeEarliestStartTime2(){ + VehicleRoutingTransportCosts routingCosts = CostFactory.createManhattanCosts(); + VehicleImpl v = VehicleImpl.Builder.newInstance("v").setEarliestStart(10).setStartLocation(Location.newInstance(0,0)).build(); + Service s = Service.Builder.newInstance("s").setTimeWindow(TimeWindow.newInstance(5,20)).setLocation(Location.newInstance(10, 0)).build(); + VehicleRoute route = VehicleRoute.Builder.newInstance(v).addService(s).build(); + + UpdateDepartureTime udt = new UpdateDepartureTime(routingCosts); + udt.visit(route); + + Assert.assertEquals(10.,route.getDepartureTime()); + } + + @Test + public void whenTimeWindowAndWaitingTime_departureTimeShouldChange(){ + VehicleRoutingTransportCosts routingCosts = CostFactory.createManhattanCosts(); + VehicleImpl v = VehicleImpl.Builder.newInstance("v").setEarliestStart(0) + .setHasVariableDepartureTime(true) + .setStartLocation(Location.newInstance(0,0)).build(); + Service s = Service.Builder.newInstance("s").setTimeWindow(TimeWindow.newInstance(15,20)).setLocation(Location.newInstance(10, 0)).build(); + VehicleRoute route = VehicleRoute.Builder.newInstance(v).addService(s).build(); + + UpdateDepartureTime udt = new UpdateDepartureTime(routingCosts); + udt.visit(route); + + Assert.assertEquals(5.,route.getDepartureTime()); + } + + @Test + public void whenTimeWindowAndWaitingTime_departureTimeShouldChange2(){ + VehicleRoutingTransportCosts routingCosts = CostFactory.createManhattanCosts(); + VehicleImpl v = VehicleImpl.Builder.newInstance("v").setHasVariableDepartureTime(true).setEarliestStart(5).setStartLocation(Location.newInstance(0,0)).build(); + Service s = Service.Builder.newInstance("s").setTimeWindow(TimeWindow.newInstance(18,20)).setLocation(Location.newInstance(10, 0)).build(); + VehicleRoute route = VehicleRoute.Builder.newInstance(v).addService(s).build(); + + UpdateDepartureTime udt = new UpdateDepartureTime(routingCosts); + udt.visit(route); + + Assert.assertEquals(8.,route.getDepartureTime()); + } +} diff --git a/jsprit-core/src/test/java/jsprit/core/problem/vehicle/VehicleImplTest.java b/jsprit-core/src/test/java/jsprit/core/problem/vehicle/VehicleImplTest.java index 20a2c349..229c46d9 100644 --- a/jsprit-core/src/test/java/jsprit/core/problem/vehicle/VehicleImplTest.java +++ b/jsprit-core/src/test/java/jsprit/core/problem/vehicle/VehicleImplTest.java @@ -221,5 +221,15 @@ public class VehicleImplTest { assertFalse(v.getSkills().containsSkill("ScrewDriver")); } - + @Test + public void whenSettingVariableDeparture_itShouldBeSet(){ + VehicleImpl v = VehicleImpl.Builder.newInstance("v").setStartLocation(Location.newInstance("loc")).setHasVariableDepartureTime(true).build(); + assertTrue(v.hasVariableDepartureTime()); + } + + @Test + public void defaultOfHasVariableDepartureShouldBeFalse(){ + VehicleImpl v = VehicleImpl.Builder.newInstance("v").setStartLocation(Location.newInstance("loc")).build(); + assertFalse(v.hasVariableDepartureTime()); + } } diff --git a/jsprit-examples/src/main/java/jsprit/examples/CostMatrixExample.java b/jsprit-examples/src/main/java/jsprit/examples/CostMatrixExample.java index 17bc0d74..d7bea962 100644 --- a/jsprit-examples/src/main/java/jsprit/examples/CostMatrixExample.java +++ b/jsprit-examples/src/main/java/jsprit/examples/CostMatrixExample.java @@ -18,7 +18,7 @@ package jsprit.examples; import jsprit.analysis.toolbox.Plotter; import jsprit.core.algorithm.VehicleRoutingAlgorithm; -import jsprit.core.algorithm.io.VehicleRoutingAlgorithms; +import jsprit.core.algorithm.box.Jsprit; import jsprit.core.problem.Location; import jsprit.core.problem.VehicleRoutingProblem; import jsprit.core.problem.VehicleRoutingProblem.FleetSize; @@ -58,7 +58,7 @@ public class CostMatrixExample { Service s2 = Service.Builder.newInstance("2").addSizeDimension(0, 1).setLocation(Location.newInstance("2")).build(); Service s3 = Service.Builder.newInstance("3").addSizeDimension(0, 1).setLocation(Location.newInstance("3")).build(); - + /* * Assume the following symmetric distance-matrix * from,to,distance @@ -98,7 +98,7 @@ public class CostMatrixExample { VehicleRoutingProblem vrp = VehicleRoutingProblem.Builder.newInstance().setFleetSize(FleetSize.INFINITE).setRoutingCost(costMatrix) .addVehicle(vehicle).addJob(s1).addJob(s2).addJob(s3).build(); - VehicleRoutingAlgorithm vra = VehicleRoutingAlgorithms.readAndCreateAlgorithm(vrp, "input/fastAlgo.xml"); + VehicleRoutingAlgorithm vra = Jsprit.createAlgorithm(vrp); Collection solutions = vra.searchSolutions();