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

View file

@ -7,9 +7,11 @@ import jsprit.core.algorithm.box.Jsprit;
import jsprit.core.algorithm.state.StateManager;
import jsprit.core.algorithm.state.UpdateDepartureTime;
import jsprit.core.algorithm.state.UpdateTimeSlack;
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.job.Service;
import jsprit.core.problem.solution.SolutionCostCalculator;
import jsprit.core.problem.solution.VehicleRoutingProblemSolution;
@ -35,19 +37,20 @@ public class VariableStartAndWaitingTimeExample {
public static void main(String[] args) {
VehicleTypeImpl type = VehicleTypeImpl.Builder.newInstance("type").setCostPerDistance(2.5).setCostPerWaitingTime(2.0).build();
// VehicleTypeImpl type1 = VehicleTypeImpl.Builder.newInstance("type1").setCostPerDistance(1.5).setCostPerWaitingTime(.0).build();
VehicleTypeImpl type = VehicleTypeImpl.Builder.newInstance("type").setCostPerDistance(4.).setCostPerWaitingTime(2.0).build();
VehicleTypeImpl type1 = VehicleTypeImpl.Builder.newInstance("type1").setCostPerDistance(4.).setCostPerWaitingTime(2.0).build();
VehicleTypeImpl type2 = VehicleTypeImpl.Builder.newInstance("type2").setCostPerDistance(4.).setCostPerWaitingTime(2.0).build();
VehicleImpl v2 = VehicleImpl.Builder.newInstance("v2").setType(type).setReturnToDepot(true)
.setHasVariableDepartureTime(true).setStartLocation(Location.newInstance(0, 0)).build();
VehicleImpl v3 = VehicleImpl.Builder.newInstance("v3").setType(type).setReturnToDepot(true)
VehicleImpl v3 = VehicleImpl.Builder.newInstance("v3").setType(type1).setReturnToDepot(true)
.setHasVariableDepartureTime(true).setStartLocation(Location.newInstance(0, 0)).build();
VehicleImpl v4 = VehicleImpl.Builder.newInstance("v4").setType(type2).setReturnToDepot(true)
.setHasVariableDepartureTime(true).setStartLocation(Location.newInstance(0, 0)).build();
VehicleImpl v4 = VehicleImpl.Builder.newInstance("v4").setType(type).setReturnToDepot(true)
.setHasVariableDepartureTime(false).setStartLocation(Location.newInstance(0, 0)).build();
VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
Random r = RandomNumberGeneration.newInstance();
for(int i=0;i<40;i++){
Service s = Service.Builder.newInstance("s_"+i).setServiceTime(0)
Service s = Service.Builder.newInstance("s_"+i).setServiceTime(1)
// .setTimeWindow(TimeWindow.newInstance(r.nextInt(50),100+r.nextInt(150)))
.setLocation(Location.newInstance(1 - r.nextInt(5), 10 + r.nextInt(10))).build();
vrpBuilder.addJob(s);
@ -64,14 +67,14 @@ public class VariableStartAndWaitingTimeExample {
@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));
stateManager.addStateUpdater(new UpdateTimeSlack(stateManager,vrp.getTransportCosts()));
ConstraintManager constraintManager = new ConstraintManager(vrp,stateManager);
return Jsprit.Builder.newInstance(vrp)
.addCoreStateAndConstraintStuff(true)
.setStateAndConstraintManager(stateManager, constraintManager)
// .setProperty(Jsprit.Parameter.THRESHOLD_INI, "0.1")
.setProperty(Jsprit.Parameter.THRESHOLD_INI, "0.1")
// .setProperty(Jsprit.Parameter.THRESHOLD_ALPHA, "0.3")
// .setProperty(Parameter.)
// .setProperty(Jsprit.Parameter.CONSTRUCTION, Jsprit.Construction.BEST_INSERTION.toString())
@ -97,12 +100,22 @@ public class VariableStartAndWaitingTimeExample {
}
};
VehicleRoutingAlgorithm vra = algorithmFactory.createAlgorithm(vrp);
vra.setMaxIterations(500);
vra.setMaxIterations(2000);
vra.addListener(new AlgorithmSearchProgressChartListener("output/search"));
VehicleRoutingProblemSolution solution = Solutions.bestOf(vra.searchSolutions());
System.out.println("c: " + solution.getCost());
SolutionPrinter.print(vrp, solution, SolutionPrinter.Print.VERBOSE);
SolutionAnalyser sa = new SolutionAnalyser(vrp, solution, new TransportDistance() {
@Override
public double getDistance(Location from, Location to) {
return vrp.getTransportCosts().getTransportTime(from,to,0.,null,null);
}
});
System.out.println("totalWaiting: " + sa.getWaitingTime());
System.out.println("brokenTWs: " + sa.getTimeWindowViolation());
new Plotter(vrp,solution).setLabel(Plotter.Label.ID).plot("output/plot","plot");
}
}

View file

@ -0,0 +1,98 @@
package jsprit.examples;
import jsprit.analysis.toolbox.AlgorithmSearchProgressChartListener;
import jsprit.analysis.toolbox.Plotter;
import jsprit.core.algorithm.VehicleRoutingAlgorithm;
import jsprit.core.algorithm.box.Jsprit;
import jsprit.core.algorithm.state.StateManager;
import jsprit.core.algorithm.state.UpdateDepartureTime;
import jsprit.core.algorithm.state.UpdateTimeSlack;
import jsprit.core.problem.Location;
import jsprit.core.problem.VehicleRoutingProblem;
import jsprit.core.problem.constraint.ConstraintManager;
import jsprit.core.problem.job.Service;
import jsprit.core.problem.solution.SolutionCostCalculator;
import jsprit.core.problem.solution.VehicleRoutingProblemSolution;
import jsprit.core.problem.solution.route.VehicleRoute;
import jsprit.core.problem.solution.route.activity.TimeWindow;
import jsprit.core.problem.solution.route.activity.TourActivity;
import jsprit.core.problem.vehicle.VehicleImpl;
import jsprit.core.problem.vehicle.VehicleTypeImpl;
import jsprit.core.reporting.SolutionPrinter;
import jsprit.core.util.Solutions;
/**
* Created by schroeder on 23/07/15.
*/
public class VariableStartAndWaitingTimeExample2 {
static interface AlgorithmFactory {
VehicleRoutingAlgorithm createAlgorithm(VehicleRoutingProblem vrp);
}
public static void main(String[] args) {
VehicleTypeImpl type = VehicleTypeImpl.Builder.newInstance("type").setCostPerDistance(1.5).setCostPerWaitingTime(1.0).build();
// VehicleTypeImpl type1 = VehicleTypeImpl.Builder.newInstance("type1").setCostPerDistance(1.5).setCostPerWaitingTime(.0).build();
VehicleImpl v2 = VehicleImpl.Builder.newInstance("v2").setType(type).setReturnToDepot(true)
.setHasVariableDepartureTime(false).setStartLocation(Location.newInstance(0, 0)).build();
VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
Service s1 = Service.Builder.newInstance("s12").setLocation(Location.newInstance(-1, 5)).setTimeWindow(TimeWindow.newInstance(100, 110)).build();
Service s4 = Service.Builder.newInstance("s13").setLocation(Location.newInstance(0, 10)).build();
Service s2 = Service.Builder.newInstance("s10").setLocation(Location.newInstance(1, 12)).build();
Service s3 = Service.Builder.newInstance("s11").setLocation(Location.newInstance(4, 10)).build();
Service s5 = Service.Builder.newInstance("s14").setLocation(Location.newInstance(6, 5)).setTimeWindow(TimeWindow.newInstance(100,220)).build();
vrpBuilder.addJob(s1).addJob(s2).addJob(s3).addJob(s4).addJob(s5).addVehicle(v2);
vrpBuilder.setFleetSize(VehicleRoutingProblem.FleetSize.FINITE);
final VehicleRoutingProblem vrp = vrpBuilder.build();
AlgorithmFactory algorithmFactory = new AlgorithmFactory() {
@Override
public VehicleRoutingAlgorithm createAlgorithm(final VehicleRoutingProblem vrp) {
StateManager stateManager = new StateManager(vrp);
stateManager.addStateUpdater(new UpdateDepartureTime(vrp.getTransportCosts(), stateManager));
stateManager.addStateUpdater(new UpdateTimeSlack(stateManager,vrp.getTransportCosts()));
ConstraintManager constraintManager = new ConstraintManager(vrp,stateManager);
return Jsprit.Builder.newInstance(vrp)
.addCoreStateAndConstraintStuff(true)
.setStateAndConstraintManager(stateManager, constraintManager)
// .setProperty(Jsprit.Parameter.THRESHOLD_INI, "0.1")
// .setProperty(Jsprit.Parameter.THRESHOLD_ALPHA, "0.3")
// .setProperty(Parameter.)
// .setProperty(Jsprit.Parameter.CONSTRUCTION, Jsprit.Construction.BEST_INSERTION.toString())
.setObjectiveFunction(new SolutionCostCalculator() {
@Override
public double getCosts(VehicleRoutingProblemSolution solution) {
double costs = 0.;
for (VehicleRoute route : solution.getRoutes()) {
costs += route.getVehicle().getType().getVehicleCostParams().fix;
TourActivity prevAct = route.getStart();
for (TourActivity act : route.getActivities()) {
costs += vrp.getTransportCosts().getTransportCost(prevAct.getLocation(), act.getLocation(), prevAct.getEndTime(), route.getDriver(), route.getVehicle());
costs += vrp.getActivityCosts().getActivityCost(act, act.getArrTime(), route.getDriver(), route.getVehicle());
prevAct = act;
}
costs += vrp.getTransportCosts().getTransportCost(prevAct.getLocation(), route.getEnd().getLocation(), prevAct.getEndTime(), route.getDriver(), route.getVehicle());
}
costs += solution.getUnassignedJobs().size() * 200;
return costs;
}
})
.buildAlgorithm();
}
};
VehicleRoutingAlgorithm vra = algorithmFactory.createAlgorithm(vrp);
vra.setMaxIterations(1000);
vra.addListener(new AlgorithmSearchProgressChartListener("output/search"));
VehicleRoutingProblemSolution solution = Solutions.bestOf(vra.searchSolutions());
System.out.println("c: " + solution.getCost());
SolutionPrinter.print(vrp, solution, SolutionPrinter.Print.VERBOSE);
new Plotter(vrp,solution).setLabel(Plotter.Label.ID).plot("output/plot","plot");
}
}