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

multiple tws

This commit is contained in:
oblonski 2015-07-13 19:58:24 +02:00
parent ebeae6f693
commit 0b3b07a7de
9 changed files with 219 additions and 58 deletions

View file

@ -6,6 +6,7 @@ import org.junit.Test;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
/**
* Created by schroeder on 08/07/15.
@ -65,6 +66,30 @@ public class GetLatestArrivalTimeTest {
Assert.assertEquals(1.,getLatestArrivalTime(Arrays.asList(tw,tw2),1));
}
@Test
public void whenMultiple3TW1_itShouldReturnCorrectTime(){
TimeWindow tw = TimeWindow.newInstance(2,10);
TimeWindow tw2 = TimeWindow.newInstance(20,30);
TimeWindow tw3 = TimeWindow.newInstance(40,50);
Assert.assertEquals(30.,getLatestArrivalTime(Arrays.asList(tw,tw2,tw3),35));
}
@Test
public void whenMultiple3TW2_itShouldReturnCorrectTime(){
TimeWindow tw = TimeWindow.newInstance(2,10);
TimeWindow tw2 = TimeWindow.newInstance(20,30);
TimeWindow tw3 = TimeWindow.newInstance(40,50);
Assert.assertEquals(50.,getLatestArrivalTime(Arrays.asList(tw,tw2,tw3),55));
}
@Test
public void whenMultiple3TW3_itShouldReturnCorrectTime(){
TimeWindow tw = TimeWindow.newInstance(2,10);
TimeWindow tw2 = TimeWindow.newInstance(20,30);
TimeWindow tw3 = TimeWindow.newInstance(40,50);
Assert.assertEquals(45.,getLatestArrivalTime(Arrays.asList(tw,tw2,tw3),45));
}
@Test
public void whenSingleTW_ActivityStartTime_shouldReturnCorrectTime(){
TimeWindow tw = TimeWindow.newInstance(2,10);
@ -118,6 +143,31 @@ public class GetLatestArrivalTimeTest {
Assert.assertEquals(31.,getActivityStartTime(Arrays.asList(tw,tw2),31));
}
@Test
public void whenMultiple3TW1_ActivityStartTime_shouldReturnCorrectTime(){
TimeWindow tw = TimeWindow.newInstance(2,10);
TimeWindow tw2 = TimeWindow.newInstance(20,30);
TimeWindow tw3 = TimeWindow.newInstance(40,80);
Assert.assertEquals(40.,getActivityStartTime(Arrays.asList(tw,tw2,tw3),31));
}
@Test
public void whenMultiple3TW2_ActivityStartTime_shouldReturnCorrectTime(){
TimeWindow tw = TimeWindow.newInstance(2,10);
TimeWindow tw2 = TimeWindow.newInstance(20,30);
TimeWindow tw3 = TimeWindow.newInstance(40,80);
Assert.assertEquals(90.,getActivityStartTime(Arrays.asList(tw,tw2,tw3),90));
}
@Test
public void whenMultiple4TW1_ActivityStartTime_shouldReturnCorrectTime(){
TimeWindow tw = TimeWindow.newInstance(2,10);
TimeWindow tw2 = TimeWindow.newInstance(20,30);
TimeWindow tw3 = TimeWindow.newInstance(40,80);
TimeWindow tw4 = TimeWindow.newInstance(140,180);
Assert.assertEquals(140.,getActivityStartTime(Arrays.asList(tw,tw2,tw3,tw4),130));
}
private double getLatestArrivalTime(Collection<TimeWindow> timeWindows, double potentialLatestArrivalTimeAtCurrAct) {
TimeWindow last = null;
for(TimeWindow tw : timeWindows){
@ -135,23 +185,51 @@ public class GetLatestArrivalTimeTest {
return last.getEnd();
}
private double getActivityStartTime(Collection<TimeWindow> timeWindows, double arrivalTime) {
boolean next = false;
for(TimeWindow tw : timeWindows){
if(next){
return Math.max(tw.getStart(),arrivalTime);
}
private double getActivityStartTime(List<TimeWindow> timeWindows, double arrivalTime) {
TimeWindow last = null;
for(int i=timeWindows.size()-1; i >= 0; i--){
TimeWindow tw = timeWindows.get(i);
if(tw.getStart() <= arrivalTime && tw.getEnd() >= arrivalTime){
return arrivalTime;
}
else if(tw.getEnd() < arrivalTime){
next = true;
else if(arrivalTime > tw.getEnd()){
if(last != null) return last.getStart();
else return arrivalTime;
}
else if(tw.getStart() > arrivalTime){
return tw.getStart();
last = tw;
}
return Math.max(arrivalTime,last.getStart());
}
@Test
public void singleTWshouldWork(){
TimeWindow tw = TimeWindow.newInstance(2,10);
Assert.assertEquals(tw,getNextTimeWindow(11, Arrays.asList(tw)));
}
@Test
public void multipleTWshouldWork(){
TimeWindow tw = TimeWindow.newInstance(2,10);
TimeWindow tw1 = TimeWindow.newInstance(20,30);
Assert.assertEquals(tw1,getNextTimeWindow(19,Arrays.asList(tw,tw1)));
}
@Test
public void multipleTW2shouldWork(){
TimeWindow tw = TimeWindow.newInstance(2,10);
TimeWindow tw1 = TimeWindow.newInstance(20,30);
TimeWindow tw2 = TimeWindow.newInstance(40,50);
Assert.assertEquals(tw2,getNextTimeWindow(31,Arrays.asList(tw,tw1,tw2)));
}
private TimeWindow getNextTimeWindow(double actArrTime, Collection<TimeWindow> timeWindows) {
for(TimeWindow tw : timeWindows){
if(actArrTime >= tw.getStart() && actArrTime <= tw.getEnd()) return tw;
else if(actArrTime < tw.getStart()){
return tw;
}
}
return arrivalTime;
return null;
}
}

View file

@ -179,7 +179,40 @@ public class UpdateVehicleDependentTimeWindowTest {
}
@Test
public void twUpdateShouldWorkWithMultipleTWs(){
//
VehicleImpl vehicle = VehicleImpl.Builder.newInstance("v").setStartLocation(Location.newInstance("0,0")).setEarliestStart(0.).setLatestArrival(100.).build();
Service service = Service.Builder.newInstance("s1").setLocation(Location.newInstance("10,0"))
.addTimeWindow(10,20).addTimeWindow(30,40).build();
Service service2 = Service.Builder.newInstance("s2")
.addTimeWindow(20,30).addTimeWindow(40,60).addTimeWindow(70,80).setLocation(Location.newInstance("20,0")).build();
VehicleRoutingProblem vrp = VehicleRoutingProblem.Builder.newInstance().addJob(service).addJob(service2).addVehicle(vehicle)
.setRoutingCost(routingCosts).build();
VehicleRoute route = VehicleRoute.Builder.newInstance(vehicle).setJobActivityFactory(vrp.getJobActivityFactory())
.addService(service).addService(service2).build();
StateManager stateManager = new StateManager(vrp);
UpdateVehicleDependentPracticalTimeWindows updater = new UpdateVehicleDependentPracticalTimeWindows(stateManager,routingCosts);
updater.setVehiclesToUpdate(new UpdateVehicleDependentPracticalTimeWindows.VehiclesToUpdate() {
@Override
public Collection<Vehicle> get(VehicleRoute route) {
Collection<Vehicle> vehicles = new ArrayList<Vehicle>();
vehicles.add(route.getVehicle());
// vehicles.addAll(fleetManager.getAvailableVehicles(route.getVehicle()));
return vehicles;
}
});
stateManager.addStateUpdater(updater);
stateManager.informInsertionStarts(Arrays.asList(route), Collections.<Job>emptyList());
assertEquals(80.,stateManager.getActivityState(route.getActivities().get(1),vehicle,
InternalStates.LATEST_OPERATION_START_TIME, Double.class),0.01);
}
}

View file

@ -2,24 +2,9 @@
<problem xmlns="http://www.w3schools.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3schools.com vrp_xml_schema.xsd">
<problemType>
<fleetSize>FINITE</fleetSize>
<fleetSize>INFINITE</fleetSize>
</problemType>
<vehicles>
<vehicle>
<id>v2</id>
<typeId>vehType2</typeId>
<startLocation>
<id>loc</id>
</startLocation>
<endLocation>
<id>loc</id>
</endLocation>
<timeSchedule>
<start>0.0</start>
<end>1.7976931348623157E308</end>
</timeSchedule>
<returnToDepot>true</returnToDepot>
</vehicle>
<vehicle>
<id>v1</id>
<typeId>vehType</typeId>
@ -48,16 +33,58 @@
<time>0.0</time>
</costs>
</type>
<type>
<id>vehType2</id>
<capacity-dimensions>
<dimension index="0">200</dimension>
</capacity-dimensions>
<costs>
<fixed>0.0</fixed>
<distance>1.0</distance>
<time>0.0</time>
</costs>
</type>
</vehicleTypes>
<services>
<service id="1" type="service">
<location>
<id>loc</id>
</location>
<capacity-dimensions>
<dimension index="0">1</dimension>
</capacity-dimensions>
<duration>2.0</duration>
<timeWindows>
<timeWindow>
<start>0.0</start>
<end>1.7976931348623157E308</end>
</timeWindow>
</timeWindows>
</service>
<service id="2" type="service">
<location>
<id>loc2</id>
</location>
<capacity-dimensions>
<dimension index="0">1</dimension>
</capacity-dimensions>
<duration>4.0</duration>
<timeWindows>
<timeWindow>
<start>0.0</start>
<end>1.7976931348623157E308</end>
</timeWindow>
</timeWindows>
</service>
</services>
<solutions>
<solution>
<cost>10.0</cost>
<routes>
<route>
<driverId>noDriver</driverId>
<vehicleId>v1</vehicleId>
<start>0.0</start>
<act type="service">
<serviceId>1</serviceId>
<arrTime>0.0</arrTime>
<endTime>0.0</endTime>
</act>
<end>0.0</end>
</route>
</routes>
<unassignedJobs>
<job id="2"/>
</unassignedJobs>
</solution>
</solutions>
</problem>