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

further refine wainting time min

This commit is contained in:
oblonski 2015-08-03 09:15:52 +02:00
parent 9717926b8e
commit 70d6ae1797
8 changed files with 251 additions and 38 deletions

View file

@ -45,7 +45,7 @@ class LocalActivityInsertionCostsCalculator implements ActivityInsertionCostsCal
private VehicleRoutingActivityCosts activityCosts;
private double activityCostsWeight = 1.;
private double activityCostsWeight = 0.75;
private double solutionCompletenessRatio = 1.;
@ -66,17 +66,12 @@ class LocalActivityInsertionCostsCalculator implements ActivityInsertionCostsCal
@Override
public double getCosts(JobInsertionContext iFacts, TourActivity prevAct, TourActivity nextAct, TourActivity newAct, double depTimeAtPrevAct) {
// double waiting = 0;
// if(!isEnd(nextAct)){
// waiting = stateManager.getActivityState(nextAct, InternalStates.WAITING,Double.class);
// }
double tp_costs_prevAct_newAct = routingCosts.getTransportCost(prevAct.getLocation(), newAct.getLocation(), depTimeAtPrevAct, iFacts.getNewDriver(), iFacts.getNewVehicle());
double tp_time_prevAct_newAct = routingCosts.getTransportTime(prevAct.getLocation(), newAct.getLocation(), depTimeAtPrevAct, iFacts.getNewDriver(), iFacts.getNewVehicle());
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());
double slack_time_new = 0;
@ -104,10 +99,10 @@ class LocalActivityInsertionCostsCalculator implements ActivityInsertionCostsCal
double totalCosts = tp_costs_prevAct_newAct + tp_costs_newAct_nextAct + solutionCompletenessRatio * activityCostsWeight * (act_costs_newAct + act_costs_nextAct);
double oldCosts;
double oldCosts = 0.;
if(iFacts.getRoute().isEmpty()){
double tp_costs_prevAct_nextAct = routingCosts.getTransportCost(prevAct.getLocation(), nextAct.getLocation(), depTimeAtPrevAct, iFacts.getNewDriver(), iFacts.getNewVehicle());
oldCosts = tp_costs_prevAct_nextAct;
oldCosts += tp_costs_prevAct_nextAct;
}
else{
double tp_costs_prevAct_nextAct = routingCosts.getTransportCost(prevAct.getLocation(), nextAct.getLocation(), prevAct.getEndTime(), iFacts.getRoute().getDriver(), iFacts.getRoute().getVehicle());
@ -119,13 +114,24 @@ class LocalActivityInsertionCostsCalculator implements ActivityInsertionCostsCal
}
else if(hasVariableDeparture(iFacts.getRoute().getVehicle())){
actCost_nextAct = activityCosts.getActivityCost(nextAct, arrTime_nextAct + slack_time_prev, iFacts.getRoute().getDriver(), iFacts.getRoute().getVehicle());
Double earliestWithoutWaiting = stateManager.getActivityState(nextAct,iFacts.getRoute().getVehicle(),InternalStates.EARLIEST_WITHOUT_WAITING,Double.class);
if(earliestWithoutWaiting != null){
double potentialArrivalTimeAtNewAct = earliestWithoutWaiting - routingCosts.getBackwardTransportTime(nextAct.getLocation(), newAct.getLocation(), earliestWithoutWaiting, iFacts.getRoute().getDriver(),iFacts.getRoute().getVehicle()) - newAct.getOperationTime();
if(potentialArrivalTimeAtNewAct > newAct.getTheoreticalLatestOperationStartTime()){
double delta = potentialArrivalTimeAtNewAct - newAct.getTheoreticalLatestOperationStartTime();
totalCosts += solutionCompletenessRatio * activityCostsWeight * delta * iFacts.getNewVehicle().getType().getVehicleCostParams().perWaitingTimeUnit;
}
}
}
double endTimeDelay_nextAct = Math.max(0,endTime_nextAct_new - endTime_nextAct_old);
Double futureWaiting = stateManager.getActivityState(nextAct,iFacts.getRoute().getVehicle(),InternalStates.FUTURE_WAITING,Double.class);
if(futureWaiting == null) futureWaiting = 0.;
double waitingTime_savings_timeUnit = Math.min(futureWaiting,endTimeDelay_nextAct);
double waitingTime_savings = waitingTime_savings_timeUnit * iFacts.getRoute().getVehicle().getType().getVehicleCostParams().perWaitingTimeUnit;
oldCosts = tp_costs_prevAct_nextAct + solutionCompletenessRatio * activityCostsWeight * ( actCost_nextAct + waitingTime_savings);
else {
double endTimeDelay_nextAct = Math.max(0, endTime_nextAct_new - endTime_nextAct_old);
Double futureWaiting = stateManager.getActivityState(nextAct, iFacts.getRoute().getVehicle(), InternalStates.FUTURE_WAITING, Double.class);
if (futureWaiting == null) futureWaiting = 0.;
double waitingTime_savings_timeUnit = Math.min(futureWaiting, endTimeDelay_nextAct);
double waitingTime_savings = waitingTime_savings_timeUnit * iFacts.getRoute().getVehicle().getType().getVehicleCostParams().perWaitingTimeUnit;
oldCosts += solutionCompletenessRatio * activityCostsWeight * waitingTime_savings;
}
oldCosts += tp_costs_prevAct_nextAct + solutionCompletenessRatio * activityCostsWeight * actCost_nextAct;
}
return totalCosts - oldCosts;
}
@ -151,6 +157,7 @@ class LocalActivityInsertionCostsCalculator implements ActivityInsertionCostsCal
}
public void setSolutionCompletenessRatio(double solutionCompletenessRatio) {
// this.solutionCompletenessRatio = 1.;
this.solutionCompletenessRatio = solutionCompletenessRatio;
}
}

View file

@ -48,4 +48,6 @@ public class InternalStates {
public static final StateId TIME_SLACK = new StateFactory.StateIdImpl("time_slack",12);
public static final StateId FUTURE_WAITING = new StateFactory.StateIdImpl("future_waiting",13);
public static final StateId EARLIEST_WITHOUT_WAITING = new StateFactory.StateIdImpl("earliest_without_waiting",14);
}

View file

@ -45,13 +45,14 @@ public class UpdateDepartureTime implements RouteVisitor, StateUpdater{
private boolean hasVariableDepartureTime;
public UpdateDepartureTime(VehicleRoutingTransportCosts tpCosts) {
private StateManager stateManager;
public UpdateDepartureTime(VehicleRoutingTransportCosts tpCosts, StateManager stateManager) {
super();
this.transportCosts = tpCosts;
this.stateManager = stateManager;
}
public void begin(VehicleRoute route) {
this.route = route;
hasEarliest = false;
@ -73,11 +74,13 @@ public class UpdateDepartureTime implements RouteVisitor, StateUpdater{
else {
arrTimeAtPrevActWithoutWaiting = activity.getTheoreticalLatestOperationStartTime();
}
stateManager.putInternalTypedActivityState(activity,route.getVehicle(),InternalStates.EARLIEST_WITHOUT_WAITING,arrTimeAtPrevActWithoutWaiting);
}
else{
if(activity.getTheoreticalEarliestOperationStartTime() > 0){
hasEarliest = true;
arrTimeAtPrevActWithoutWaiting = activity.getTheoreticalEarliestOperationStartTime();
stateManager.putInternalTypedActivityState(activity,route.getVehicle(),InternalStates.EARLIEST_WITHOUT_WAITING,arrTimeAtPrevActWithoutWaiting);
}
}
prevAct = activity;

View file

@ -56,7 +56,7 @@ public class VariableDepartureAndWaitingTime_IT {
@Override
public VehicleRoutingAlgorithm createAlgorithm(final VehicleRoutingProblem vrp) {
StateManager stateManager = new StateManager(vrp);
stateManager.addStateUpdater(new UpdateDepartureTime(vrp.getTransportCosts()));
stateManager.addStateUpdater(new UpdateDepartureTime(vrp.getTransportCosts(),stateManager ));
ConstraintManager constraintManager = new ConstraintManager(vrp,stateManager);
return Jsprit.Builder.newInstance(vrp)
@ -160,7 +160,7 @@ public class VariableDepartureAndWaitingTime_IT {
@Override
public VehicleRoutingAlgorithm createAlgorithm(final VehicleRoutingProblem vrp) {
StateManager stateManager = new StateManager(vrp);
stateManager.addStateUpdater(new UpdateDepartureTime(vrp.getTransportCosts()));
stateManager.addStateUpdater(new UpdateDepartureTime(vrp.getTransportCosts(), stateManager));
ConstraintManager constraintManager = new ConstraintManager(vrp,stateManager);
return Jsprit.Builder.newInstance(vrp)
@ -206,7 +206,7 @@ public class VariableDepartureAndWaitingTime_IT {
@Override
public VehicleRoutingAlgorithm createAlgorithm(final VehicleRoutingProblem vrp) {
StateManager stateManager = new StateManager(vrp);
stateManager.addStateUpdater(new UpdateDepartureTime(vrp.getTransportCosts()));
stateManager.addStateUpdater(new UpdateDepartureTime(vrp.getTransportCosts(), stateManager));
ConstraintManager constraintManager = new ConstraintManager(vrp,stateManager);
return Jsprit.Builder.newInstance(vrp)
@ -260,7 +260,7 @@ public class VariableDepartureAndWaitingTime_IT {
@Override
public VehicleRoutingAlgorithm createAlgorithm(final VehicleRoutingProblem vrp) {
StateManager stateManager = new StateManager(vrp);
stateManager.addStateUpdater(new UpdateDepartureTime(vrp.getTransportCosts()));
stateManager.addStateUpdater(new UpdateDepartureTime(vrp.getTransportCosts(), stateManager));
ConstraintManager constraintManager = new ConstraintManager(vrp,stateManager);
return Jsprit.Builder.newInstance(vrp)
@ -323,7 +323,7 @@ public class VariableDepartureAndWaitingTime_IT {
@Override
public VehicleRoutingAlgorithm createAlgorithm(final VehicleRoutingProblem vrp) {
StateManager stateManager = new StateManager(vrp);
stateManager.addStateUpdater(new UpdateDepartureTime(vrp.getTransportCosts()));
stateManager.addStateUpdater(new UpdateDepartureTime(vrp.getTransportCosts(), stateManager));
ConstraintManager constraintManager = new ConstraintManager(vrp,stateManager);
return Jsprit.Builder.newInstance(vrp)

View file

@ -446,12 +446,12 @@ public class TestLocalActivityInsertionCostsCalculator {
LocalActivityInsertionCostsCalculator calc = new LocalActivityInsertionCostsCalculator(CostFactory.createEuclideanCosts(),new WaitingTimeCosts(),stateManager);
calc.setSolutionCompletenessRatio(1.);
double c = calc.getCosts(context,prevAct,nextAct,newAct,10);
assertEquals(-10.,c,0.01);
assertEquals(0.,c,0.01);
/*
activity start time delay at next act = start-time-old - start-time-new is always bigger than subsequent waiting time savings
*/
/*
old = s:40,act1:50,act2:60,act3:70,act4:80 --> w=20
old = s:40,act1:50,act2:60,act3:70,act4:90 --> w=10
new = s:30,act1:40,act2:50,act3:70,act4:80,act5:90 --> w=10 => -10
*/
}
@ -485,13 +485,99 @@ public class TestLocalActivityInsertionCostsCalculator {
LocalActivityInsertionCostsCalculator calc = new LocalActivityInsertionCostsCalculator(CostFactory.createEuclideanCosts(),new WaitingTimeCosts(),stateManager);
calc.setSolutionCompletenessRatio(1.);
double c = calc.getCosts(context,prevAct,nextAct,newAct,10);
assertEquals(20.,c,0.01);
assertEquals(30.,c,0.01);
/*
activity start time delay at next act = start-time-old - start-time-new is always bigger than subsequent waiting time savings
*/
/*
old = s:40,act1:50,act2:60,act3:70,act4:80 --> w=20
new = s:0,act1:10,act2:20,act3:40,act4:50,act5:60 --> w=40 => 20
old = s:40,act1:50,act2:60,70,act3:(70,80),act4:90 --> w=10
new = s:0,act1:10,act2:20,act3:(40,50),act4:50,act5:60 --> w=40 => 20
*/
}
@Test
public void whenAddingNewWithTWBetweenTwoActs1WithVarStart_itShouldCalcInsertionCostsCorrectly(){
VehicleTypeImpl type = VehicleTypeImpl.Builder.newInstance("t").setCostPerWaitingTime(1.).build();
VehicleImpl v = VehicleImpl.Builder.newInstance("v").setHasVariableDepartureTime(true).setType(type).setStartLocation(Location.newInstance(0,0)).build();
// VehicleImpl v2 = VehicleImpl.Builder.newInstance("v2").setHasVariableDepartureTime(true).setType(type).setStartLocation(Location.newInstance(0,0)).build();
Service prevS = Service.Builder.newInstance("prev").setLocation(Location.newInstance(10,0)).build();
Service newS = Service.Builder.newInstance("new").setServiceTime(10).setTimeWindow(TimeWindow.newInstance(10,20)).setLocation(Location.newInstance(20, 0)).build();
Service nextS = Service.Builder.newInstance("next").setLocation(Location.newInstance(30,0)).setTimeWindow(TimeWindow.newInstance(40,70)).build();
Service afterNextS = Service.Builder.newInstance("afterNext").setLocation(Location.newInstance(40,0)).setTimeWindow(TimeWindow.newInstance(50,100)).build();
Service afterAfterNextS = Service.Builder.newInstance("afterAfterNext").setLocation(Location.newInstance(50,0)).setTimeWindow(TimeWindow.newInstance(100,500)).build();
VehicleRoutingProblem vrp = VehicleRoutingProblem.Builder.newInstance().addVehicle(v).addJob(prevS).addJob(newS).addJob(nextS)
.addJob(afterNextS).addJob(afterAfterNextS).build();
TourActivity prevAct = vrp.getActivities(prevS).get(0);
TourActivity newAct = vrp.getActivities(newS).get(0);
TourActivity nextAct = vrp.getActivities(nextS).get(0);
VehicleRoute route = VehicleRoute.Builder.newInstance(v).setJobActivityFactory(vrp.getJobActivityFactory()).addService(prevS).addService(nextS).addService(afterNextS).addService(afterAfterNextS).build();
JobInsertionContext context = new JobInsertionContext(route,newS,v,null,0.);
StateManager stateManager = getStateManager(vrp, route);
LocalActivityInsertionCostsCalculator calc = new LocalActivityInsertionCostsCalculator(CostFactory.createEuclideanCosts(),new WaitingTimeCosts(),stateManager);
calc.setSolutionCompletenessRatio(1.);
double c = calc.getCosts(context,prevAct,nextAct,newAct,10);
assertEquals(30.,c,0.01);
/*
activity start time delay at next act = start-time-old - start-time-new is always bigger than subsequent waiting time savings
*/
/*
old = s:40,act1:50,act2:60,70,act3:(70,80),act4:90 --> w=10
new = s:0,act1:10,act2:20,act3:(40,50),act4:50,act5:60 --> w=40 => 20
*/
}
@Test
public void whenAddingNewWithTWBetweenTwoActs5WithVarStart_itShouldCalcInsertionCostsCorrectly(){
VehicleTypeImpl type = VehicleTypeImpl.Builder.newInstance("t").setCostPerWaitingTime(1.).build();
VehicleImpl v = VehicleImpl.Builder.newInstance("v").setHasVariableDepartureTime(true).setType(type).setStartLocation(Location.newInstance(0,0)).build();
// VehicleImpl v2 = VehicleImpl.Builder.newInstance("v2").setHasVariableDepartureTime(true).setType(type).setStartLocation(Location.newInstance(0,0)).build();
Service prevS = Service.Builder.newInstance("prev").setLocation(Location.newInstance(10,0)).build();
Service newS = Service.Builder.newInstance("new").setServiceTime(10)
.setTimeWindow(TimeWindow.newInstance(0,20))
.setLocation(Location.newInstance(20, 0)).build();
Service nextS = Service.Builder.newInstance("next").setLocation(Location.newInstance(30,0))
// .setTimeWindow(TimeWindow.newInstance(40,80))
.build();
Service afterNextS = Service.Builder.newInstance("afterNext").setLocation(Location.newInstance(40,0))
// .setTimeWindow(TimeWindow.newInstance(50,100))
.build();
Service afterAfterNextS = Service.Builder.newInstance("afterAfterNext").setLocation(Location.newInstance(50,0))
.setTimeWindow(TimeWindow.newInstance(100,500))
.build();
VehicleRoutingProblem vrp = VehicleRoutingProblem.Builder.newInstance().addVehicle(v).addJob(prevS).addJob(newS).addJob(nextS)
.addJob(afterNextS).addJob(afterAfterNextS).build();
TourActivity prevAct = vrp.getActivities(prevS).get(0);
TourActivity newAct = vrp.getActivities(newS).get(0);
TourActivity nextAct = vrp.getActivities(nextS).get(0);
VehicleRoute route = VehicleRoute.Builder.newInstance(v).setJobActivityFactory(vrp.getJobActivityFactory()).addService(prevS).addService(nextS).addService(afterNextS).addService(afterAfterNextS).build();
JobInsertionContext context = new JobInsertionContext(route,newS,v,null,0.);
StateManager stateManager = getStateManager(vrp, route);
LocalActivityInsertionCostsCalculator calc = new LocalActivityInsertionCostsCalculator(CostFactory.createEuclideanCosts(),new WaitingTimeCosts(),stateManager);
calc.setSolutionCompletenessRatio(1.);
double c = calc.getCosts(context,prevAct,nextAct,newAct,10);
assertEquals(40.,c,0.01);
/*
activity start time delay at next act = start-time-old - start-time-new is always bigger than subsequent waiting time savings
*/
/*
old = s:40,act1:50,act2:60,70,act3:(70,80),act4:90 --> w=10
new = s:0,act1:10,act2:20,act3:(40,50),act4:50,act5:60 --> w=40 => 20
*/
}
@ -501,7 +587,7 @@ public class TestLocalActivityInsertionCostsCalculator {
stateManager.addStateUpdater(new UpdateActivityTimes(vrp.getTransportCosts()));
stateManager.addStateUpdater(new UpdateVehicleDependentPracticalTimeWindows(stateManager,vrp.getTransportCosts()));
stateManager.addStateUpdater(new UpdateFutureWaitingTimes(stateManager,vrp.getTransportCosts()));
stateManager.addStateUpdater(new UpdateDepartureTime(vrp.getTransportCosts()));
stateManager.addStateUpdater(new UpdateDepartureTime(vrp.getTransportCosts(),stateManager ));
stateManager.informInsertionStarts(Arrays.asList(route),new ArrayList<Job>());
return stateManager;
}

View file

@ -1,6 +1,7 @@
package jsprit.core.algorithm.state;
import jsprit.core.problem.Location;
import jsprit.core.problem.VehicleRoutingProblem;
import jsprit.core.problem.job.Service;
import jsprit.core.problem.solution.route.RouteVisitor;
import jsprit.core.problem.solution.route.VehicleRoute;
@ -11,6 +12,8 @@ import junit.framework.Assert;
import org.junit.Before;
import org.junit.Test;
import static org.mockito.Mockito.mock;
/**
* Created by schroeder on 22/07/15.
*/
@ -20,7 +23,8 @@ public class UpdateDepartureTimeTest {
@Before
public void doBefore(){
revVisitors = new UpdateDepartureTime(CostFactory.createManhattanCosts());
StateManager stateManager = new StateManager(mock(VehicleRoutingProblem.class));
revVisitors = new UpdateDepartureTime(CostFactory.createManhattanCosts(), stateManager);
}
@Test