mirror of
https://github.com/graphhopper/jsprit.git
synced 2020-01-24 07:45:05 +01:00
modify problem reader/writer to deal vehicles that can start AND end at
specified locations
This commit is contained in:
parent
6575b6d48e
commit
63060f3b35
10 changed files with 310 additions and 38 deletions
|
|
@ -17,6 +17,7 @@
|
|||
package jsprit.core.problem.io;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
|
|
@ -58,7 +59,7 @@ public class VrpReaderV2Test {
|
|||
VehicleRoutingProblem.Builder builder = VehicleRoutingProblem.Builder.newInstance();
|
||||
new VrpXMLReader(builder, null).read(inFileName);
|
||||
VehicleRoutingProblem vrp = builder.build();
|
||||
assertEquals(2,vrp.getVehicles().size());
|
||||
assertEquals(4,vrp.getVehicles().size());
|
||||
assertTrue(idsInCollection(Arrays.asList("v1","v2"),vrp.getVehicles()));
|
||||
}
|
||||
|
||||
|
|
@ -143,4 +144,127 @@ public class VrpReaderV2Test {
|
|||
assertEquals(0.0,s1.getServiceDuration(),0.01);
|
||||
assertEquals(3, vrp.getJobs().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReadingFile_v2MustNotReturnToDepot(){
|
||||
VehicleRoutingProblem.Builder builder = VehicleRoutingProblem.Builder.newInstance();
|
||||
new VrpXMLReader(builder, null).read(inFileName);
|
||||
VehicleRoutingProblem vrp = builder.build();
|
||||
Vehicle v = getVehicle("v2",vrp.getVehicles());
|
||||
assertFalse(v.isReturnToDepot());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReadingFile_v3HasTheCorrectStartLocation(){
|
||||
VehicleRoutingProblem.Builder builder = VehicleRoutingProblem.Builder.newInstance();
|
||||
new VrpXMLReader(builder, null).read(inFileName);
|
||||
VehicleRoutingProblem vrp = builder.build();
|
||||
Vehicle v3 = getVehicle("v3",vrp.getVehicles());
|
||||
assertEquals("startLoc",v3.getStartLocationId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReadingFile_v3HasTheCorrectEndLocation(){
|
||||
VehicleRoutingProblem.Builder builder = VehicleRoutingProblem.Builder.newInstance();
|
||||
new VrpXMLReader(builder, null).read(inFileName);
|
||||
VehicleRoutingProblem vrp = builder.build();
|
||||
Vehicle v3 = getVehicle("v3",vrp.getVehicles());
|
||||
assertEquals("endLoc",v3.getEndLocationId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReadingFile_v3HasTheCorrectEndLocationCoordinate(){
|
||||
VehicleRoutingProblem.Builder builder = VehicleRoutingProblem.Builder.newInstance();
|
||||
new VrpXMLReader(builder, null).read(inFileName);
|
||||
VehicleRoutingProblem vrp = builder.build();
|
||||
Vehicle v3 = getVehicle("v3",vrp.getVehicles());
|
||||
assertEquals(1000.0,v3.getEndLocationCoordinate().getX(),0.01);
|
||||
assertEquals(2000.0,v3.getEndLocationCoordinate().getY(),0.01);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReadingFile_v3HasTheCorrectStartLocationCoordinate(){
|
||||
VehicleRoutingProblem.Builder builder = VehicleRoutingProblem.Builder.newInstance();
|
||||
new VrpXMLReader(builder, null).read(inFileName);
|
||||
VehicleRoutingProblem vrp = builder.build();
|
||||
Vehicle v3 = getVehicle("v3",vrp.getVehicles());
|
||||
assertEquals(10.0,v3.getStartLocationCoordinate().getX(),0.01);
|
||||
assertEquals(100.0,v3.getStartLocationCoordinate().getY(),0.01);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReadingFile_v3HasTheCorrectLocationCoordinate(){
|
||||
VehicleRoutingProblem.Builder builder = VehicleRoutingProblem.Builder.newInstance();
|
||||
new VrpXMLReader(builder, null).read(inFileName);
|
||||
VehicleRoutingProblem vrp = builder.build();
|
||||
Vehicle v3 = getVehicle("v3",vrp.getVehicles());
|
||||
assertEquals(10.0,v3.getCoord().getX(),0.01);
|
||||
assertEquals(100.0,v3.getCoord().getY(),0.01);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReadingFile_v3HasTheCorrectLocationId(){
|
||||
VehicleRoutingProblem.Builder builder = VehicleRoutingProblem.Builder.newInstance();
|
||||
new VrpXMLReader(builder, null).read(inFileName);
|
||||
VehicleRoutingProblem vrp = builder.build();
|
||||
Vehicle v3 = getVehicle("v3",vrp.getVehicles());
|
||||
assertEquals("startLoc",v3.getLocationId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReadingFile_v4HasTheCorrectStartLocation(){
|
||||
VehicleRoutingProblem.Builder builder = VehicleRoutingProblem.Builder.newInstance();
|
||||
new VrpXMLReader(builder, null).read(inFileName);
|
||||
VehicleRoutingProblem vrp = builder.build();
|
||||
Vehicle v = getVehicle("v4",vrp.getVehicles());
|
||||
assertEquals("startLoc",v.getStartLocationId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReadingFile_v4HasTheCorrectEndLocation(){
|
||||
VehicleRoutingProblem.Builder builder = VehicleRoutingProblem.Builder.newInstance();
|
||||
new VrpXMLReader(builder, null).read(inFileName);
|
||||
VehicleRoutingProblem vrp = builder.build();
|
||||
Vehicle v = getVehicle("v4",vrp.getVehicles());
|
||||
assertEquals("endLoc",v.getEndLocationId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReadingFile_v4HasTheCorrectEndLocationCoordinate(){
|
||||
VehicleRoutingProblem.Builder builder = VehicleRoutingProblem.Builder.newInstance();
|
||||
new VrpXMLReader(builder, null).read(inFileName);
|
||||
VehicleRoutingProblem vrp = builder.build();
|
||||
Vehicle v = getVehicle("v4",vrp.getVehicles());
|
||||
assertEquals(1000.0,v.getEndLocationCoordinate().getX(),0.01);
|
||||
assertEquals(2000.0,v.getEndLocationCoordinate().getY(),0.01);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReadingFile_v4HasTheCorrectStartLocationCoordinate(){
|
||||
VehicleRoutingProblem.Builder builder = VehicleRoutingProblem.Builder.newInstance();
|
||||
new VrpXMLReader(builder, null).read(inFileName);
|
||||
VehicleRoutingProblem vrp = builder.build();
|
||||
Vehicle v = getVehicle("v4",vrp.getVehicles());
|
||||
assertEquals(10.0,v.getStartLocationCoordinate().getX(),0.01);
|
||||
assertEquals(100.0,v.getStartLocationCoordinate().getY(),0.01);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReadingFile_v4HasTheCorrectLocationCoordinate(){
|
||||
VehicleRoutingProblem.Builder builder = VehicleRoutingProblem.Builder.newInstance();
|
||||
new VrpXMLReader(builder, null).read(inFileName);
|
||||
VehicleRoutingProblem vrp = builder.build();
|
||||
Vehicle v = getVehicle("v4",vrp.getVehicles());
|
||||
assertEquals(10.0,v.getCoord().getX(),0.01);
|
||||
assertEquals(100.0,v.getCoord().getY(),0.01);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReadingFile_v4HasTheCorrectLocationId(){
|
||||
VehicleRoutingProblem.Builder builder = VehicleRoutingProblem.Builder.newInstance();
|
||||
new VrpXMLReader(builder, null).read(inFileName);
|
||||
VehicleRoutingProblem vrp = builder.build();
|
||||
Vehicle v = getVehicle("v4",vrp.getVehicles());
|
||||
assertEquals("startLoc",v.getStartLocationId());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,6 +17,10 @@
|
|||
package jsprit.core.problem.io;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import jsprit.core.problem.VehicleRoutingProblem;
|
||||
import jsprit.core.problem.VehicleRoutingProblem.Builder;
|
||||
import jsprit.core.problem.VehicleRoutingProblem.FleetSize;
|
||||
|
|
@ -43,11 +47,6 @@ public class VrpWriterV2Test {
|
|||
public void whenWritingInfiniteVrp_itWritesCorrectly(){
|
||||
VehicleRoutingProblem.Builder builder = VehicleRoutingProblem.Builder.newInstance();
|
||||
builder.setFleetSize(FleetSize.INFINITE);
|
||||
// Depot depot = new Depot("depotLoc",Coordinate.newInstance(0, 0));
|
||||
// Depot depot2 = new Depot("depotLoc2",Coordinate.newInstance(100, 100));
|
||||
// builder.addDepot(depot2);
|
||||
// builder.assignVehicleType(depot, VehicleType.Builder.newInstance("vehType", 20).build());
|
||||
// builder.assignVehicleType(depot, VehicleType.Builder.newInstance("vehType2", 200).build());
|
||||
VehicleTypeImpl type = VehicleTypeImpl.Builder.newInstance("vehType", 20).build();
|
||||
Vehicle vehicle = VehicleImpl.Builder.newInstance("myVehicle").setLocationId("loc").setType(type).build();
|
||||
builder.addVehicle(vehicle);
|
||||
|
|
@ -59,9 +58,6 @@ public class VrpWriterV2Test {
|
|||
public void whenWritingFiniteVrp_itWritesCorrectly(){
|
||||
VehicleRoutingProblem.Builder builder = VehicleRoutingProblem.Builder.newInstance();
|
||||
builder.setFleetSize(FleetSize.FINITE);
|
||||
// Depot depot = new Depot("depotLoc",Coordinate.newInstance(0, 0));
|
||||
// Depot depot2 = new Depot("depotLoc2",Coordinate.newInstance(100, 100));
|
||||
// builder.addDepot(depot2);
|
||||
VehicleTypeImpl type1 = VehicleTypeImpl.Builder.newInstance("vehType", 20).build();
|
||||
VehicleTypeImpl type2 = VehicleTypeImpl.Builder.newInstance("vehType2", 200).build();
|
||||
Vehicle v1 = VehicleImpl.Builder.newInstance("v1").setLocationId("loc").setType(type1).build();
|
||||
|
|
@ -76,9 +72,6 @@ public class VrpWriterV2Test {
|
|||
public void t(){
|
||||
VehicleRoutingProblem.Builder builder = VehicleRoutingProblem.Builder.newInstance();
|
||||
builder.setFleetSize(FleetSize.FINITE);
|
||||
// Depot depot = new Depot("depotLoc",Coordinate.newInstance(0, 0));
|
||||
// Depot depot2 = new Depot("depotLoc2",Coordinate.newInstance(100, 100));
|
||||
// builder.addDepot(depot2);
|
||||
VehicleTypeImpl type1 = VehicleTypeImpl.Builder.newInstance("vehType", 20).build();
|
||||
VehicleTypeImpl type2 = VehicleTypeImpl.Builder.newInstance("vehType2", 200).build();
|
||||
Vehicle v1 = VehicleImpl.Builder.newInstance("v1").setLocationId("loc").setType(type1).build();
|
||||
|
|
@ -122,6 +115,64 @@ public class VrpWriterV2Test {
|
|||
assertEquals(2.0,s1_read.getServiceDuration(),0.01);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenWritingVehicleV1_itsStartLocationMustBeWrittenCorrectly(){
|
||||
Builder builder = VehicleRoutingProblem.Builder.newInstance();
|
||||
|
||||
VehicleTypeImpl type1 = VehicleTypeImpl.Builder.newInstance("vehType", 20).build();
|
||||
VehicleTypeImpl type2 = VehicleTypeImpl.Builder.newInstance("vehType2", 200).build();
|
||||
Vehicle v1 = VehicleImpl.Builder.newInstance("v1").setLocationId("loc").setType(type1).build();
|
||||
Vehicle v2 = VehicleImpl.Builder.newInstance("v2").setLocationId("loc").setType(type2).build();
|
||||
|
||||
builder.addVehicle(v1);
|
||||
builder.addVehicle(v2);
|
||||
|
||||
Service s1 = Service.Builder.newInstance("1", 1).setLocationId("loc").setServiceTime(2.0).build();
|
||||
Service s2 = Service.Builder.newInstance("2", 1).setLocationId("loc2").setServiceTime(4.0).build();
|
||||
|
||||
VehicleRoutingProblem vrp = builder.addJob(s1).addJob(s2).build();
|
||||
new VrpXMLWriter(vrp, null).write(infileName);
|
||||
|
||||
VehicleRoutingProblem.Builder vrpToReadBuilder = VehicleRoutingProblem.Builder.newInstance();
|
||||
new VrpXMLReader(vrpToReadBuilder, null).read(infileName);
|
||||
VehicleRoutingProblem readVrp = vrpToReadBuilder.build();
|
||||
|
||||
Vehicle v = getVehicle("v1",readVrp.getVehicles());
|
||||
assertEquals("loc",v.getStartLocationId());
|
||||
assertEquals("loc",v.getEndLocationId());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenWritingVehicleV1_itDoesNotReturnToDepotMustBeWrittenCorrectly(){
|
||||
Builder builder = VehicleRoutingProblem.Builder.newInstance();
|
||||
|
||||
VehicleTypeImpl type1 = VehicleTypeImpl.Builder.newInstance("vehType", 20).build();
|
||||
VehicleTypeImpl type2 = VehicleTypeImpl.Builder.newInstance("vehType2", 200).build();
|
||||
Vehicle v1 = VehicleImpl.Builder.newInstance("v1").setReturnToDepot(false).setLocationId("loc").setType(type1).build();
|
||||
Vehicle v2 = VehicleImpl.Builder.newInstance("v2").setLocationId("loc").setType(type2).build();
|
||||
|
||||
builder.addVehicle(v1);
|
||||
builder.addVehicle(v2);
|
||||
|
||||
Service s1 = Service.Builder.newInstance("1", 1).setLocationId("loc").setServiceTime(2.0).build();
|
||||
Service s2 = Service.Builder.newInstance("2", 1).setLocationId("loc2").setServiceTime(4.0).build();
|
||||
|
||||
VehicleRoutingProblem vrp = builder.addJob(s1).addJob(s2).build();
|
||||
new VrpXMLWriter(vrp, null).write(infileName);
|
||||
|
||||
VehicleRoutingProblem.Builder vrpToReadBuilder = VehicleRoutingProblem.Builder.newInstance();
|
||||
new VrpXMLReader(vrpToReadBuilder, null).read(infileName);
|
||||
VehicleRoutingProblem readVrp = vrpToReadBuilder.build();
|
||||
|
||||
Vehicle v = getVehicle("v1",readVrp.getVehicles());
|
||||
assertFalse(v.isReturnToDepot());
|
||||
}
|
||||
|
||||
private Vehicle getVehicle(String string, Collection<Vehicle> vehicles) {
|
||||
for(Vehicle v : vehicles) if(string.equals(v.getId())) return v;
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -200,5 +200,18 @@ public class VehicleImplTest {
|
|||
Vehicle v = VehicleImpl.Builder.newInstance("v").setStartLocationCoordinate(Coordinate.newInstance(1.0, 2.0)).setReturnToDepot(false).build();
|
||||
assertEquals(v.getStartLocationCoordinate().toString(),v.getEndLocationId());
|
||||
}
|
||||
|
||||
@Test(expected=IllegalStateException.class)
|
||||
public void whenStartAndEndAreUnequalANDReturnToDepotIsFalse_itShouldThrowException(){
|
||||
@SuppressWarnings("unused")
|
||||
Vehicle v = VehicleImpl.Builder.newInstance("v").setStartLocationId("start").setEndLocationId("end").setReturnToDepot(false).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenStartAndEndAreEqualANDReturnToDepotIsFalse_itShouldThrowException(){
|
||||
@SuppressWarnings("unused")
|
||||
Vehicle v = VehicleImpl.Builder.newInstance("v").setStartLocationId("start").setEndLocationId("start").setReturnToDepot(false).build();
|
||||
assertTrue(true);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -46,6 +46,39 @@
|
|||
<id>depotLoc</id>
|
||||
<coord x="10.0" y="100.0"/>
|
||||
</location>
|
||||
<returnToDepot>false</returnToDepot>
|
||||
<typeId>vehType2</typeId>
|
||||
<timeSchedule>
|
||||
<start>0.0</start>
|
||||
<end>1000.0</end>
|
||||
</timeSchedule>
|
||||
</vehicle>
|
||||
<vehicle>
|
||||
<id>v3</id>
|
||||
<startLocation>
|
||||
<id>startLoc</id>
|
||||
<coord x="10.0" y="100.0"/>
|
||||
</startLocation>
|
||||
<endLocation>
|
||||
<id>endLoc</id>
|
||||
<coord x="1000.0" y="2000.0"/>
|
||||
</endLocation>
|
||||
<typeId>vehType2</typeId>
|
||||
<timeSchedule>
|
||||
<start>0.0</start>
|
||||
<end>1000.0</end>
|
||||
</timeSchedule>
|
||||
</vehicle>
|
||||
<vehicle>
|
||||
<id>v4</id>
|
||||
<location>
|
||||
<id>startLoc</id>
|
||||
<coord x="10.0" y="100.0"/>
|
||||
</location>
|
||||
<endLocation>
|
||||
<id>endLoc</id>
|
||||
<coord x="1000.0" y="2000.0"/>
|
||||
</endLocation>
|
||||
<typeId>vehType2</typeId>
|
||||
<timeSchedule>
|
||||
<start>0.0</start>
|
||||
|
|
|
|||
|
|
@ -9,21 +9,27 @@
|
|||
<vehicle>
|
||||
<id>v1</id>
|
||||
<typeId>vehType</typeId>
|
||||
<location>
|
||||
<startLocation>
|
||||
<id>loc</id>
|
||||
</location>
|
||||
</startLocation>
|
||||
<endLocation>
|
||||
<id>loc</id>
|
||||
</endLocation>
|
||||
<timeSchedule>
|
||||
<start>0.0</start>
|
||||
<end>1.7976931348623157E308</end>
|
||||
</timeSchedule>
|
||||
<returnToDepot>true</returnToDepot>
|
||||
<returnToDepot>false</returnToDepot>
|
||||
</vehicle>
|
||||
<vehicle>
|
||||
<id>v2</id>
|
||||
<typeId>vehType2</typeId>
|
||||
<location>
|
||||
<startLocation>
|
||||
<id>loc</id>
|
||||
</location>
|
||||
</startLocation>
|
||||
<endLocation>
|
||||
<id>loc</id>
|
||||
</endLocation>
|
||||
<timeSchedule>
|
||||
<start>0.0</start>
|
||||
<end>1.7976931348623157E308</end>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue