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

add and test FastTransportCostMatrix based array

This commit is contained in:
oblonski 2014-12-17 22:20:01 +01:00
parent 0a196f0abd
commit 56f170e593
6 changed files with 231 additions and 21 deletions

View file

@ -114,4 +114,9 @@ public final class Location implements HasIndex, HasId{
result = 31 * result + (id != null ? id.hashCode() : 0);
return result;
}
@Override
public String toString() {
return "[id="+id+"][index="+index+"][coordinate="+coordinate+"]";
}
}

View file

@ -299,7 +299,7 @@ public class VehicleRoute {
}
if(!vehicle.isReturnToDepot()){
if(!tourActivities.isEmpty()){
end.setLocationId(tourActivities.getActivities().get(tourActivities.getActivities().size()-1).getLocationId());
end.setLocation(tourActivities.getActivities().get(tourActivities.getActivities().size() - 1).getLocation());
}
}
return new VehicleRoute(this);
@ -407,8 +407,8 @@ public class VehicleRoute {
start.setEndTime(Math.max(vehicleDepTime, vehicle.getEarliestDeparture()));
start.setTheoreticalEarliestOperationStartTime(vehicle.getEarliestDeparture());
start.setTheoreticalLatestOperationStartTime(vehicle.getLatestArrival());
start.setLocationId(vehicle.getStartLocationId());
end.setLocationId(vehicle.getEndLocationId());
start.setLocation(vehicle.getStartLocation());
end.setLocation(vehicle.getEndLocation());
end.setTheoreticalEarliestOperationStartTime(vehicle.getEarliestDeparture());
end.setTheoreticalLatestOperationStartTime(vehicle.getLatestArrival());
}

View file

@ -114,6 +114,7 @@ public class FastVehicleRoutingTransportCostsMatrix extends AbstractForwardVehic
@Override
public double getTransportTime(Location from, Location to, double departureTime, Driver driver, Vehicle vehicle) {
if(from.getIndex() < 0 || to.getIndex() < 0) throw new IllegalArgumentException("index of from " + from + " to " + to + " < 0 ");
return get(from.getIndex(),to.getIndex(),1);
}
@ -142,7 +143,8 @@ public class FastVehicleRoutingTransportCostsMatrix extends AbstractForwardVehic
@Override
public double getTransportCost(Location from, Location to, double departureTime, Driver driver, Vehicle vehicle) {
if(vehicle == null) return getDistance(from.getIndex(), to.getIndex());
if(from.getIndex() < 0 || to.getIndex() < 0) throw new IllegalArgumentException("index of from " + from + " to " + to + " < 0 ");
if(vehicle == null) return getDistance(from.getIndex(), to.getIndex());
VehicleCostParams costParams = vehicle.getType().getVehicleCostParams();
return costParams.perDistanceUnit * getDistance(from.getIndex(), to.getIndex()) + costParams.perTimeUnit * getTransportTime(from, to, departureTime, driver, vehicle);
}

View file

@ -20,6 +20,7 @@ import jsprit.core.algorithm.recreate.listener.InsertionListeners;
import jsprit.core.algorithm.state.UpdateEndLocationIfRouteIsOpen;
import jsprit.core.problem.AbstractActivity;
import jsprit.core.problem.Capacity;
import jsprit.core.problem.Location;
import jsprit.core.problem.VehicleRoutingProblem;
import jsprit.core.problem.driver.Driver;
import jsprit.core.problem.job.Service;
@ -48,15 +49,15 @@ public class TestInserter {
public void whenInsertingServiceAndRouteIsClosed_itInsertsCorrectly(){
Service service = mock(Service.class);
Vehicle vehicle = mock(Vehicle.class);
when(vehicle.getStartLocationId()).thenReturn("vehLoc");
when(vehicle.getEndLocationId()).thenReturn("vehLoc");
when(vehicle.getStartLocation()).thenReturn(loc("vehLoc"));
when(vehicle.getEndLocation()).thenReturn(loc("vehLoc"));
when(vehicle.isReturnToDepot()).thenReturn(true);
when(vehicle.getId()).thenReturn("vehId");
VehicleRoute route = VehicleRoute.Builder.newInstance(vehicle, mock(Driver.class)).addService(service).build();
//start - pick(shipment) - del(shipment) - end
Service serviceToInsert = mock(Service.class);
when(serviceToInsert.getLocationId()).thenReturn("delLoc");
when(serviceToInsert.getLocation()).thenReturn(loc("delLoc"));
InsertionData iData = mock(InsertionData.class);
when(iData.getDeliveryInsertionIndex()).thenReturn(1);
@ -71,11 +72,15 @@ public class TestInserter {
inserter.insertJob(serviceToInsert, iData, route);
assertEquals(2,route.getTourActivities().getActivities().size());
assertEquals(route.getTourActivities().getActivities().get(1).getLocationId(),serviceToInsert.getLocationId());
assertEquals(route.getEnd().getLocationId(),vehicle.getEndLocationId());
assertEquals(route.getTourActivities().getActivities().get(1).getLocation().getId(),serviceToInsert.getLocation().getId());
assertEquals(route.getEnd().getLocation().getId(),vehicle.getEndLocation().getId());
}
@Test
private Location loc(String vehLoc) {
return Location.Builder.newInstance().setId(vehLoc).build();
}
@Test
public void whenInsertingServiceAndRouteIsOpen_itInsertsCorrectlyAndSwitchesEndLocation(){
Service service = mock(Service.class);
Vehicle vehicle = mock(Vehicle.class);
@ -116,8 +121,8 @@ public class TestInserter {
Capacity capacity = Capacity.Builder.newInstance().build();
when(shipment.getSize()).thenReturn(capacity);
Vehicle vehicle = mock(Vehicle.class);
when(vehicle.getStartLocationId()).thenReturn("vehLoc");
when(vehicle.getEndLocationId()).thenReturn("vehLoc");
when(vehicle.getStartLocation()).thenReturn(loc("vehLoc"));
when(vehicle.getEndLocation()).thenReturn(loc("vehLoc"));
when(vehicle.isReturnToDepot()).thenReturn(true);
when(vehicle.getId()).thenReturn("vehId");
@ -136,9 +141,9 @@ public class TestInserter {
inserter.insertJob(shipmentToInsert, iData, route);
assertEquals(4,route.getTourActivities().getActivities().size());
assertEquals(route.getTourActivities().getActivities().get(2).getLocationId(),shipmentToInsert.getPickupLocationId());
assertEquals(route.getTourActivities().getActivities().get(3).getLocationId(),shipmentToInsert.getDeliveryLocationId());
assertEquals(route.getEnd().getLocationId(),vehicle.getEndLocationId());
assertEquals(route.getTourActivities().getActivities().get(2).getLocation().getId(),shipmentToInsert.getPickupLocation().getId());
assertEquals(route.getTourActivities().getActivities().get(3).getLocation().getId(),shipmentToInsert.getDeliveryLocation().getId());
assertEquals(route.getEnd().getLocation().getId(),vehicle.getEndLocation().getId());
}
private List<AbstractActivity> getTourActivities(Shipment shipmentToInsert) {

View file

@ -17,6 +17,7 @@
package jsprit.core.problem.solution.route;
import jsprit.core.problem.Capacity;
import jsprit.core.problem.Location;
import jsprit.core.problem.driver.Driver;
import jsprit.core.problem.job.Shipment;
import jsprit.core.problem.vehicle.Vehicle;
@ -113,20 +114,24 @@ public class VehicleRouteBuilderTest {
Capacity capacity = Capacity.Builder.newInstance().build();
when(s.getSize()).thenReturn(capacity);
when(s2.getSize()).thenReturn(capacity);
when(s2.getDeliveryLocationId()).thenReturn("delLoc");
when(s2.getDeliveryLocation()).thenReturn(loc("delLoc"));
Vehicle vehicle = mock(Vehicle.class);
when(vehicle.isReturnToDepot()).thenReturn(false);
when(vehicle.getStartLocationId()).thenReturn("vehLoc");
when(vehicle.getStartLocation()).thenReturn(loc("vehLoc"));
VehicleRoute.Builder builder = VehicleRoute.Builder.newInstance(vehicle, mock(Driver.class));
builder.addPickup(s);
builder.addPickup(s2);
builder.addDelivery(s);
builder.addDelivery(s2);
VehicleRoute route = builder.build();
assertEquals(route.getEnd().getLocationId(), s2.getDeliveryLocationId());
assertEquals(route.getEnd().getLocationId(), s2.getDeliveryLocation().getId());
}
@Test
private Location loc(String delLoc) {
return Location.Builder.newInstance().setId(delLoc).build();
}
@Test
public void whenSettingDepartureTime(){
Shipment s = mock(Shipment.class);
Shipment s2 = mock(Shipment.class);