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

add maxTimeInVehicle feature - related to 261

This commit is contained in:
oblonski 2016-09-20 17:58:02 +02:00
parent 7590be9794
commit 42ae57d00b
14 changed files with 945 additions and 0 deletions

View file

@ -0,0 +1,69 @@
/*
* Licensed to GraphHopper GmbH under one or more contributor
* license agreements. See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*
* GraphHopper GmbH licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.graphhopper.jsprit.core.algorithm;
import com.graphhopper.jsprit.core.algorithm.box.Jsprit;
import com.graphhopper.jsprit.core.algorithm.state.StateId;
import com.graphhopper.jsprit.core.algorithm.state.StateManager;
import com.graphhopper.jsprit.core.algorithm.state.UpdateMaxTimeInVehicle;
import com.graphhopper.jsprit.core.problem.Location;
import com.graphhopper.jsprit.core.problem.VehicleRoutingProblem;
import com.graphhopper.jsprit.core.problem.constraint.ConstraintManager;
import com.graphhopper.jsprit.core.problem.constraint.MaxTimeInVehicleConstraint;
import com.graphhopper.jsprit.core.problem.job.Shipment;
import com.graphhopper.jsprit.core.problem.solution.VehicleRoutingProblemSolution;
import com.graphhopper.jsprit.core.problem.vehicle.VehicleImpl;
import com.graphhopper.jsprit.core.reporting.SolutionPrinter;
import com.graphhopper.jsprit.core.util.Solutions;
import org.junit.Assert;
import org.junit.Test;
/**
* Created by schroeder on 20/09/16.
*/
public class MaxTimeInVehicle_IT {
@Test
public void test(){
Shipment s1 = Shipment.Builder.newInstance("s1").setPickupLocation(Location.newInstance(0,0)).setDeliveryLocation(Location.newInstance(100,0)).setDeliveryServiceTime(10)
.setMaxTimeInVehicle(90d)
.build();
Shipment s2 = Shipment.Builder.newInstance("s2").setPickupLocation(Location.newInstance(0,0)).setDeliveryLocation(Location.newInstance(100,0)).setDeliveryServiceTime(10)
.setMaxTimeInVehicle(90d)
.build();
VehicleImpl v = VehicleImpl.Builder.newInstance("v").setStartLocation(Location.newInstance(0,0)).build();
VehicleRoutingProblem vrp = VehicleRoutingProblem.Builder.newInstance().addVehicle(v).addJob(s1).addJob(s2).build();
StateManager stateManager = new StateManager(vrp);
StateId id = stateManager.createStateId("max-time");
stateManager.addStateUpdater(new UpdateMaxTimeInVehicle(stateManager,id,vrp.getTransportCosts(),vrp.getActivityCosts()));
ConstraintManager constraintManager = new ConstraintManager(vrp,stateManager);
constraintManager.addConstraint(new MaxTimeInVehicleConstraint(vrp.getTransportCosts(),vrp.getActivityCosts(),id,stateManager), ConstraintManager.Priority.CRITICAL);
VehicleRoutingAlgorithm vra = Jsprit.Builder.newInstance(vrp).setStateAndConstraintManager(stateManager,constraintManager).buildAlgorithm();
VehicleRoutingProblemSolution solution = Solutions.bestOf(vra.searchSolutions());
Assert.assertEquals(400, solution.getCost(), 0.001);
// SolutionPrinter.print(vrp,solution, SolutionPrinter.Print.VERBOSE);
}
}

View file

@ -0,0 +1,203 @@
/*
* Licensed to GraphHopper GmbH under one or more contributor
* license agreements. See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*
* GraphHopper GmbH licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.graphhopper.jsprit.core.algorithm.state;
import com.graphhopper.jsprit.core.problem.Location;
import com.graphhopper.jsprit.core.problem.VehicleRoutingProblem;
import com.graphhopper.jsprit.core.problem.job.Delivery;
import com.graphhopper.jsprit.core.problem.job.Pickup;
import com.graphhopper.jsprit.core.problem.job.Shipment;
import com.graphhopper.jsprit.core.problem.solution.route.VehicleRoute;
import com.graphhopper.jsprit.core.problem.solution.route.activity.PickupActivity;
import com.graphhopper.jsprit.core.problem.solution.route.activity.TourActivity;
import com.graphhopper.jsprit.core.problem.vehicle.Vehicle;
import com.graphhopper.jsprit.core.problem.vehicle.VehicleImpl;
import com.graphhopper.jsprit.core.problem.vehicle.VehicleType;
import com.graphhopper.jsprit.core.problem.vehicle.VehicleTypeImpl;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
/**
* Created by schroeder on 15/09/16.
*/
public class UpdateMaxTimeInVehicleTest {
private VehicleRoute route;
private VehicleRoute route2;
private VehicleImpl vehicle;
private VehicleImpl v;
private VehicleImpl vehicle2;
private VehicleRoutingProblem vrp;
private com.graphhopper.jsprit.core.algorithm.state.UpdateMaxTimeInVehicle maxTimeInVehicleConstraint;
private StateManager stateManager;
private StateId latestStartId;
@Before
public void doBefore() {
VehicleType type = VehicleTypeImpl.Builder.newInstance("t").build();
v = VehicleImpl.Builder.newInstance("v0").setStartLocation(Location.newInstance(0, 0))
.setType(type).build();
vehicle = VehicleImpl.Builder.newInstance("v").setStartLocation(Location.newInstance(0,0))
.setEndLocation(Location.newInstance(0,50)).setType(type).build();
vehicle2 = VehicleImpl.Builder.newInstance("v2").setStartLocation(Location.newInstance(0,10))
.setEndLocation(Location.newInstance(0,40)).setType(type).build();
Pickup service = Pickup.Builder.newInstance("s").setLocation(Location.newInstance(0, 10)).build();
Pickup service2 = Pickup.Builder.newInstance("s2").setLocation(Location.newInstance(0, 20)).build();
Pickup service3 = Pickup.Builder.newInstance("s3").setLocation(Location.newInstance(0, 30)).build();
Pickup service4 = Pickup.Builder.newInstance("s4").setLocation(Location.newInstance(0, 40)).build();
Delivery d1 = Delivery.Builder.newInstance("d1").setLocation(Location.newInstance(10,0)).build();
Shipment shipment = Shipment.Builder.newInstance("shipment").setPickupLocation(Location.newInstance(20,0))
.setDeliveryLocation(Location.newInstance(40,0))
.setMaxTimeInVehicle(20d)
.build();
Delivery d2 = Delivery.Builder.newInstance("d2").setLocation(Location.newInstance(30,0)).setServiceTime(10).build();
vrp = VehicleRoutingProblem.Builder.newInstance().addVehicle(v).addVehicle(vehicle).addVehicle(vehicle2).addJob(service)
.addJob(service2).addJob(service3).addJob(service4)
.addJob(d1).addJob(shipment).addJob(d2)
.build();
route = VehicleRoute.Builder.newInstance(vehicle).setJobActivityFactory(vrp.getJobActivityFactory())
.addService(service).addService(service2).addService(service3).addService(service4).build();
route2 = VehicleRoute.Builder.newInstance(v).setJobActivityFactory(vrp.getJobActivityFactory())
.addDelivery(d1).addPickup(shipment).addDelivery(shipment).build();
stateManager = new StateManager(vrp);
stateManager.addStateUpdater(new UpdateActivityTimes(vrp.getTransportCosts(),vrp.getActivityCosts()));
stateManager.informInsertionStarts(Arrays.asList(route), null);
latestStartId = stateManager.createStateId("slack-time-id");
// Map<String,Double> maxTimes = new HashMap<>();
// maxTimes.put("s",40d);
// maxTimes.put("shipment",20d);
maxTimeInVehicleConstraint = new UpdateMaxTimeInVehicle(stateManager, latestStartId,vrp.getTransportCosts(), vrp.getActivityCosts());
maxTimeInVehicleConstraint.setVehiclesToUpdate(new UpdateVehicleDependentPracticalTimeWindows.VehiclesToUpdate() {
@Override
public Collection<Vehicle> get(VehicleRoute route) {
return Arrays.asList((Vehicle)vehicle,(Vehicle)vehicle2,v);
}
});
stateManager.addStateUpdater(maxTimeInVehicleConstraint);
}
// @Test
// public void testVehicle(){
// stateManager.informInsertionStarts(Arrays.asList(route), null);
// for(TourActivity act : route.getActivities()){
// String jobId = ((TourActivity.JobActivity)act).getJob().getId();
// if(jobId.equals("s4")){
// Double slackTime = stateManager.getActivityState(act,route.getVehicle(), latestStartId,Double.class);
// Assert.assertEquals(40, slackTime, 0.001);
// }
// if(jobId.equals("s3")){
// Double slackTime = stateManager.getActivityState(act,route.getVehicle(), latestStartId,Double.class);
// Assert.assertEquals(30, slackTime, 0.001);
// }
// if(jobId.equals("s2")){
// Double slackTime = stateManager.getActivityState(act,route.getVehicle(), latestStartId,Double.class);
// Assert.assertEquals(20, slackTime, 0.001);
// }
// if(jobId.equals("s")){
// Double slackTime = stateManager.getActivityState(act,route.getVehicle(), latestStartId,Double.class);
// Assert.assertEquals(Double.MAX_VALUE, slackTime, 0.001);
// }
// }
// Double slackTime = stateManager.getRouteState(route,route.getVehicle(), latestStartId,Double.class);
// Assert.assertNotNull(slackTime);
// Assert.assertEquals(50,slackTime,0.001);
// }
//
// @Test
// public void testVehicle2(){
// stateManager.informInsertionStarts(Arrays.asList(route), null);
// for(TourActivity act : route.getActivities()){
// String jobId = ((TourActivity.JobActivity)act).getJob().getId();
// if(jobId.equals("s4")){
// Double slackTime = stateManager.getActivityState(act,vehicle2, latestStartId,Double.class);
// Assert.assertEquals(40, slackTime, 0.001);
// }
// if(jobId.equals("s3")){
// Double slackTime = stateManager.getActivityState(act,vehicle2, latestStartId,Double.class);
// Assert.assertEquals(30, slackTime, 0.001);
// }
// if(jobId.equals("s2")){
// Double slackTime = stateManager.getActivityState(act,vehicle2, latestStartId,Double.class);
// Assert.assertEquals(20, slackTime, 0.001);
// }
// if(jobId.equals("s")){
// Double slackTime = stateManager.getActivityState(act,vehicle2, latestStartId,Double.class);
// Assert.assertEquals(Double.MAX_VALUE, slackTime, 0.001);
// }
// }
// Double slackTime = stateManager.getRouteState(route,vehicle2, latestStartId,Double.class);
// Assert.assertNotNull(slackTime);
// Assert.assertEquals(40,slackTime,0.001);
// }
@Test
public void testWithShipment(){
stateManager.informInsertionStarts(Arrays.asList(route2), null);
for(TourActivity act : route2.getActivities()){
String jobId = ((TourActivity.JobActivity)act).getJob().getId();
if(jobId.equals("d1")){
Double slackTime = stateManager.getActivityState(act,v, latestStartId,Double.class);
Assert.assertEquals(Double.MAX_VALUE, slackTime, 0.001);
}
if(jobId.equals("shipment")){
if(act instanceof PickupActivity){
Double slackTime = stateManager.getActivityState(act,v, latestStartId,Double.class);
Assert.assertEquals(Double.MAX_VALUE, slackTime, 0.001);
}
else{
Double slackTime = stateManager.getActivityState(act,v, latestStartId,Double.class);
Assert.assertEquals(40, slackTime, 0.001);
}
}
}
}
}

View file

@ -0,0 +1,246 @@
/*
* Licensed to GraphHopper GmbH under one or more contributor
* license agreements. See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*
* GraphHopper GmbH licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.graphhopper.jsprit.core.problem.constraint;
import com.graphhopper.jsprit.core.algorithm.state.StateId;
import com.graphhopper.jsprit.core.algorithm.state.StateManager;
import com.graphhopper.jsprit.core.algorithm.state.UpdateMaxTimeInVehicle;
import com.graphhopper.jsprit.core.problem.AbstractActivity;
import com.graphhopper.jsprit.core.problem.Location;
import com.graphhopper.jsprit.core.problem.VehicleRoutingProblem;
import com.graphhopper.jsprit.core.problem.job.Delivery;
import com.graphhopper.jsprit.core.problem.job.Job;
import com.graphhopper.jsprit.core.problem.job.Pickup;
import com.graphhopper.jsprit.core.problem.job.Shipment;
import com.graphhopper.jsprit.core.problem.misc.ActivityContext;
import com.graphhopper.jsprit.core.problem.misc.JobInsertionContext;
import com.graphhopper.jsprit.core.problem.solution.route.VehicleRoute;
import com.graphhopper.jsprit.core.problem.vehicle.Vehicle;
import com.graphhopper.jsprit.core.problem.vehicle.VehicleImpl;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import java.util.*;
/**
* Created by schroeder on 19/09/16.
*/
public class MaxTimeInVehicleConstraintTest {
Delivery d1;
Shipment shipment;
Delivery d2;
Pickup p1;
Pickup p2;
Vehicle v;
VehicleRoute route;
VehicleRoutingProblem vrp;
@Before
public void doBefore(){
}
private void ini(double maxTime){
d1 = Delivery.Builder.newInstance("d1").setLocation(Location.newInstance(10,0)).build();
shipment = Shipment.Builder.newInstance("shipment").setPickupLocation(Location.newInstance(20,0))
.setDeliveryLocation(Location.newInstance(40,0)).setMaxTimeInVehicle(maxTime).build();
d2 = Delivery.Builder.newInstance("d2").setLocation(Location.newInstance(30,0)).setServiceTime(10).build();
p1 = Pickup.Builder.newInstance("p1").setLocation(Location.newInstance(10, 0)).build();
p2 = Pickup.Builder.newInstance("p2").setLocation(Location.newInstance(20,0)).build();
v = VehicleImpl.Builder.newInstance("v").setStartLocation(Location.newInstance(0,0)).build();
vrp = VehicleRoutingProblem.Builder.newInstance().addJob(d1).addJob(shipment).addJob(d2).addJob(p1).addJob(p2)
.addVehicle(v).build();
route = VehicleRoute.Builder.newInstance(v).setJobActivityFactory(vrp.getJobActivityFactory())
.addDelivery(d1).addPickup(shipment).addDelivery(shipment).build();
}
@Test
public void insertingDeliveryAtAnyPositionShouldWork(){
ini(30d);
StateManager stateManager = new StateManager(vrp);
StateId latestStartId = stateManager.createStateId("latest-start-id");
UpdateMaxTimeInVehicle updater = new UpdateMaxTimeInVehicle(stateManager,latestStartId,vrp.getTransportCosts(), vrp.getActivityCosts());
stateManager.addStateUpdater(updater);
stateManager.informInsertionStarts(Arrays.asList(route),new ArrayList<Job>());
MaxTimeInVehicleConstraint constraint = new MaxTimeInVehicleConstraint(vrp.getTransportCosts(),vrp.getActivityCosts() , latestStartId, stateManager);
JobInsertionContext c = new JobInsertionContext(route,d2,v,route.getDriver(),0.);
List<AbstractActivity> acts = vrp.getActivities(d2);
c.getAssociatedActivities().add(acts.get(0));
Assert.assertEquals(HardActivityConstraint.ConstraintsStatus.FULFILLED, constraint.fulfilled(c, route.getStart(), acts.get(0), route.getActivities().get(0), 0));
Assert.assertEquals(HardActivityConstraint.ConstraintsStatus.FULFILLED, constraint.fulfilled(c, route.getActivities().get(0), acts.get(0), route.getActivities().get(1), 10));
Assert.assertEquals(HardActivityConstraint.ConstraintsStatus.FULFILLED, constraint.fulfilled(c, route.getActivities().get(1), acts.get(0), route.getActivities().get(2), 20));
Assert.assertEquals(HardActivityConstraint.ConstraintsStatus.FULFILLED, constraint.fulfilled(c, route.getActivities().get(2), acts.get(0), route.getEnd(), 40));
}
@Test
public void insertingDeliveryInBetweenShipmentShouldFail(){
ini(25d);
StateManager stateManager = new StateManager(vrp);
StateId latestStartId = stateManager.createStateId("latest-start-id");
UpdateMaxTimeInVehicle updater = new UpdateMaxTimeInVehicle(stateManager,latestStartId,vrp.getTransportCosts(),vrp.getActivityCosts());
stateManager.addStateUpdater(updater);
stateManager.informInsertionStarts(Arrays.asList(route),new ArrayList<Job>());
MaxTimeInVehicleConstraint constraint = new MaxTimeInVehicleConstraint(vrp.getTransportCosts(),vrp.getActivityCosts() , latestStartId, stateManager);
JobInsertionContext c = new JobInsertionContext(route,d2,v,route.getDriver(),0.);
List<AbstractActivity> acts = vrp.getActivities(d2);
c.getAssociatedActivities().add(acts.get(0));
Assert.assertEquals(HardActivityConstraint.ConstraintsStatus.FULFILLED, constraint.fulfilled(c, route.getStart(), acts.get(0), route.getActivities().get(0), 0));
Assert.assertEquals(HardActivityConstraint.ConstraintsStatus.FULFILLED, constraint.fulfilled(c, route.getActivities().get(0), acts.get(0), route.getActivities().get(1), 10));
Assert.assertEquals(HardActivityConstraint.ConstraintsStatus.NOT_FULFILLED, constraint.fulfilled(c, route.getActivities().get(1), acts.get(0), route.getActivities().get(2), 20));
Assert.assertEquals(HardActivityConstraint.ConstraintsStatus.FULFILLED, constraint.fulfilled(c, route.getActivities().get(2), acts.get(0), route.getEnd(), 40));
}
@Test
public void insertingPickupShipmentAtAnyPositionShouldWork(){
ini(25d);
VehicleRoute r = VehicleRoute.Builder.newInstance(v).setJobActivityFactory(vrp.getJobActivityFactory())
.addDelivery(d1).addDelivery(d2).build();
StateManager stateManager = new StateManager(vrp);
StateId latestStartId = stateManager.createStateId("latest-start-id");
UpdateMaxTimeInVehicle updater = new UpdateMaxTimeInVehicle(stateManager,latestStartId,vrp.getTransportCosts(), vrp.getActivityCosts());
stateManager.addStateUpdater(updater);
stateManager.informInsertionStarts(Arrays.asList(r),new ArrayList<Job>());
MaxTimeInVehicleConstraint constraint = new MaxTimeInVehicleConstraint(vrp.getTransportCosts(),vrp.getActivityCosts() , latestStartId, stateManager);
JobInsertionContext c = new JobInsertionContext(r,shipment,v,r.getDriver(),0.);
List<AbstractActivity> acts = vrp.getActivities(shipment);
c.getAssociatedActivities().add(acts.get(0));
c.getAssociatedActivities().add(acts.get(1));
Assert.assertEquals(HardActivityConstraint.ConstraintsStatus.FULFILLED, constraint.fulfilled(c, r.getStart(), acts.get(0), r.getActivities().get(0), 0));
Assert.assertEquals(HardActivityConstraint.ConstraintsStatus.FULFILLED, constraint.fulfilled(c, r.getActivities().get(0), acts.get(0), r.getActivities().get(1), 10));
Assert.assertEquals(HardActivityConstraint.ConstraintsStatus.FULFILLED, constraint.fulfilled(c, r.getActivities().get(1), acts.get(0), r.getEnd(), 40));
}
// @Test
// public void insertingPickupBeforeDeliveryShouldFail(){
// VehicleRoute r = VehicleRoute.Builder.newInstance(v).setJobActivityFactory(vrp.getJobActivityFactory())
// .addPickup(p1).addDelivery(d2).build();
//
// StateManager stateManager = new StateManager(vrp);
// StateId latestStartId = stateManager.createStateId("latest-start-id");
//
// Map<String,Double> maxTimes = new HashMap<>();
// maxTimes.put("p2",30d);
// UpdateMaxTimeInVehicle updater = new UpdateMaxTimeInVehicle(stateManager,latestStartId,vrp.getTransportCosts(),vrp.getActivityCosts());
// stateManager.addStateUpdater(updater);
// stateManager.informInsertionStarts(Arrays.asList(r),new ArrayList<Job>());
//
// MaxTimeInVehicleConstraint constraint = new MaxTimeInVehicleConstraint(vrp.getTransportCosts(), vrp.getActivityCosts(), latestStartId, stateManager);
// JobInsertionContext c = new JobInsertionContext(r,shipment,v,r.getDriver(),0.);
// List<AbstractActivity> acts = vrp.getActivities(shipment);
// c.getAssociatedActivities().add(acts.get(0));
// c.getAssociatedActivities().add(acts.get(1));
//
//
// Assert.assertEquals(HardActivityConstraint.ConstraintsStatus.NOT_FULFILLED, constraint.fulfilled(c, r.getStart(), acts.get(0), r.getActivities().get(0), 0));
// Assert.assertEquals(HardActivityConstraint.ConstraintsStatus.NOT_FULFILLED, constraint.fulfilled(c, r.getActivities().get(0), acts.get(0), r.getActivities().get(1), 10));
// Assert.assertEquals(HardActivityConstraint.ConstraintsStatus.FULFILLED, constraint.fulfilled(c, r.getActivities().get(1), acts.get(0), r.getEnd(), 30));
//// Assert.assertEquals(HardActivityConstraint.ConstraintsStatus.FULFILLED, constraint.fulfilled(c, r.getActivities().get(0), acts.get(0), r.getActivities().get(1), 10));
//// Assert.assertEquals(HardActivityConstraint.ConstraintsStatus.FULFILLED, constraint.fulfilled(c, r.getActivities().get(1), acts.get(0), r.getEnd(), 40));
// }
@Test
public void whenPickupIsInsertedAt0_insertingDeliveryShipmentShouldFailWhereConstraintIsBroken(){
ini(25d);
VehicleRoute r = VehicleRoute.Builder.newInstance(v).setJobActivityFactory(vrp.getJobActivityFactory())
.addDelivery(d1).addDelivery(d2).build();
StateManager stateManager = new StateManager(vrp);
StateId latestStartId = stateManager.createStateId("latest-start-id");
Map<String,Double> maxTimes = new HashMap<>();
maxTimes.put("shipment",25d);
UpdateMaxTimeInVehicle updater = new UpdateMaxTimeInVehicle(stateManager,latestStartId,vrp.getTransportCosts(), vrp.getActivityCosts());
stateManager.addStateUpdater(updater);
stateManager.informInsertionStarts(Arrays.asList(r),new ArrayList<Job>());
MaxTimeInVehicleConstraint constraint = new MaxTimeInVehicleConstraint(vrp.getTransportCosts(),vrp.getActivityCosts() , latestStartId, stateManager);
JobInsertionContext c = new JobInsertionContext(r,shipment,v,r.getDriver(),0.);
List<AbstractActivity> acts = vrp.getActivities(shipment);
c.getAssociatedActivities().add(acts.get(0));
c.getAssociatedActivities().add(acts.get(1));
ActivityContext ac = new ActivityContext();
ac.setArrivalTime(20);
ac.setEndTime(20);
c.setRelatedActivityContext(ac);
Assert.assertEquals(HardActivityConstraint.ConstraintsStatus.FULFILLED, constraint.fulfilled(c, acts.get(0), acts.get(1), r.getActivities().get(0), 20));
Assert.assertEquals(HardActivityConstraint.ConstraintsStatus.NOT_FULFILLED, constraint.fulfilled(c, r.getActivities().get(0), acts.get(1), r.getActivities().get(1), 30));
Assert.assertEquals(HardActivityConstraint.ConstraintsStatus.NOT_FULFILLED, constraint.fulfilled(c, r.getActivities().get(1), acts.get(1), r.getEnd(), 40));
}
@Test
public void whenPickupIsInsertedAt1_insertingDeliveryShipmentShouldFailWhereConstraintIsBroken(){
ini(25d);
VehicleRoute r = VehicleRoute.Builder.newInstance(v).setJobActivityFactory(vrp.getJobActivityFactory())
.addDelivery(d1).addDelivery(d2).build();
StateManager stateManager = new StateManager(vrp);
StateId latestStartId = stateManager.createStateId("latest-start-id");
Map<String,Double> maxTimes = new HashMap<>();
maxTimes.put("shipment",25d);
UpdateMaxTimeInVehicle updater = new UpdateMaxTimeInVehicle(stateManager,latestStartId,vrp.getTransportCosts(), vrp.getActivityCosts());
stateManager.addStateUpdater(updater);
stateManager.informInsertionStarts(Arrays.asList(r),new ArrayList<Job>());
MaxTimeInVehicleConstraint constraint = new MaxTimeInVehicleConstraint(vrp.getTransportCosts(),vrp.getActivityCosts() , latestStartId, stateManager);
JobInsertionContext c = new JobInsertionContext(r,shipment,v,r.getDriver(),0.);
List<AbstractActivity> acts = vrp.getActivities(shipment);
c.getAssociatedActivities().add(acts.get(0));
c.getAssociatedActivities().add(acts.get(1));
ActivityContext ac = new ActivityContext();
ac.setArrivalTime(20);
ac.setEndTime(20);
c.setRelatedActivityContext(ac);
Assert.assertEquals(HardActivityConstraint.ConstraintsStatus.FULFILLED, constraint.fulfilled(c, acts.get(0), acts.get(1), r.getActivities().get(1), 20));
Assert.assertEquals(HardActivityConstraint.ConstraintsStatus.NOT_FULFILLED, constraint.fulfilled(c, r.getActivities().get(1), acts.get(1), r.getEnd(), 40));
// Assert.assertEquals(HardActivityConstraint.ConstraintsStatus.NOT_FULFILLED, constraint.fulfilled(c, r.getActivities().get(1), acts.get(1), r.getEnd(), 40));
}
}

View file

@ -103,5 +103,20 @@ public class DeliveryTest {
Assert.assertEquals(2, s.getPriority());
}
@Test
public void whenAddingMaxTimeInVehicle_itShouldBeSet(){
Delivery s = Delivery.Builder.newInstance("s").setLocation(Location.newInstance("loc"))
.setMaxTimeInVehicle(10)
.build();
Assert.assertEquals(10, s.getMaxTimeInVehicle(),0.001);
}
@Test
public void whenNotAddingMaxTimeInVehicle_itShouldBeDefault(){
Delivery s = Delivery.Builder.newInstance("s").setLocation(Location.newInstance("loc"))
.build();
Assert.assertEquals(Double.MAX_VALUE, s.getMaxTimeInVehicle(),0.001);
}
}

View file

@ -105,4 +105,18 @@ public class PickupTest {
Assert.assertEquals(2, s.getPriority());
}
@Test(expected = UnsupportedOperationException.class)
public void whenAddingMaxTimeInVehicle_itShouldThrowEx(){
Pickup s = Pickup.Builder.newInstance("s").setLocation(Location.newInstance("loc"))
.setMaxTimeInVehicle(10)
.build();
}
@Test
public void whenNotAddingMaxTimeInVehicle_itShouldBeDefault(){
Pickup s = Pickup.Builder.newInstance("s").setLocation(Location.newInstance("loc"))
.build();
Assert.assertEquals(Double.MAX_VALUE, s.getMaxTimeInVehicle(),0.001);
}
}

View file

@ -271,4 +271,18 @@ public class ServiceTest {
}
@Test(expected = UnsupportedOperationException.class)
public void whenAddingMaxTimeInVehicle_itShouldThrowEx(){
Service s = Service.Builder.newInstance("s").setLocation(Location.newInstance("loc"))
.setMaxTimeInVehicle(10)
.build();
}
@Test
public void whenNotAddingMaxTimeInVehicle_itShouldBeDefault(){
Service s = Service.Builder.newInstance("s").setLocation(Location.newInstance("loc"))
.build();
Assert.assertEquals(Double.MAX_VALUE, s.getMaxTimeInVehicle(),0.001);
}
}

View file

@ -425,4 +425,19 @@ public class ShipmentTest {
}
@Test
public void whenAddingMaxTimeInVehicle_itShouldBeSet(){
Shipment s = Shipment.Builder.newInstance("s").setPickupLocation(Location.newInstance("loc")).setDeliveryLocation(Location.newInstance("loc"))
.setMaxTimeInVehicle(10)
.build();
Assert.assertEquals(10, s.getMaxTimeInVehicle(),0.001);
}
@Test
public void whenNotAddingMaxTimeInVehicle_itShouldBeDefault(){
Shipment s = Shipment.Builder.newInstance("s").setPickupLocation(Location.newInstance("loc")).setDeliveryLocation(Location.newInstance("loc"))
.build();
Assert.assertEquals(Double.MAX_VALUE, s.getMaxTimeInVehicle(),0.001);
}
}