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

made core.problem.io.VrpXMLReader.java read initialRoutes

This commit is contained in:
oblonski 2014-04-22 16:29:54 +02:00
parent a2ae693544
commit 26ad6addf6
3 changed files with 92 additions and 15 deletions

View file

@ -214,9 +214,61 @@ public class VrpXMLReader{
readShipments(xmlConfig); readShipments(xmlConfig);
readServices(xmlConfig); readServices(xmlConfig);
readInitialRoutes(xmlConfig);
readSolutions(xmlConfig); readSolutions(xmlConfig);
} }
private void readInitialRoutes(XMLConfiguration xmlConfig) {
List<HierarchicalConfiguration> initialRouteConfigs = xmlConfig.configurationsAt("initialRoutes.route");
for(HierarchicalConfiguration routeConfig : initialRouteConfigs){
Driver driver = DriverImpl.noDriver();
String vehicleId = routeConfig.getString("vehicleId");
Vehicle vehicle = getVehicle(vehicleId);
if(vehicle == null) throw new IllegalStateException("vehicle is missing.");
String start = routeConfig.getString("start");
if(start == null) throw new IllegalStateException("route start-time is missing.");
double departureTime = Double.parseDouble(start);
VehicleRoute.Builder routeBuilder = VehicleRoute.Builder.newInstance(vehicle, driver);
routeBuilder.setDepartureTime(departureTime);
List<HierarchicalConfiguration> actConfigs = routeConfig.configurationsAt("act");
for(HierarchicalConfiguration actConfig : actConfigs){
String type = actConfig.getString("[@type]");
if(type == null) throw new IllegalStateException("act[@type] is missing.");
double arrTime = 0.;
double endTime = 0.;
String arrTimeS = actConfig.getString("arrTime");
if(arrTimeS!=null) arrTime=Double.parseDouble(arrTimeS);
String endTimeS = actConfig.getString("endTime");
if(endTimeS!=null) endTime=Double.parseDouble(endTimeS);
String serviceId = actConfig.getString("serviceId");
if(serviceId != null) {
Service service = getService(serviceId);
//!!!
routeBuilder.addService(service, arrTime, endTime);
}
else{
String shipmentId = actConfig.getString("shipmentId");
if(shipmentId == null) throw new IllegalStateException("either serviceId or shipmentId is missing");
Shipment shipment = getShipment(shipmentId);
if(shipment == null) throw new IllegalStateException("shipment with id " + shipmentId + " does not exist.");
if(type.equals("pickupShipment")){
routeBuilder.addPickup(shipment, arrTime, endTime);
}
else if(type.equals("deliverShipment")){
routeBuilder.addDelivery(shipment, arrTime, endTime);
}
else throw new IllegalStateException("type " + type + " is not supported. Use 'pickupShipment' or 'deliverShipment' here");
}
}
VehicleRoute route = routeBuilder.build();
vrpBuilder.addInitialVehicleRoute(route);
}
}
private void readSolutions(XMLConfiguration vrpProblem) { private void readSolutions(XMLConfiguration vrpProblem) {
if(solutions == null) return; if(solutions == null) return;
List<HierarchicalConfiguration> solutionConfigs = vrpProblem.configurationsAt("solutions.solution"); List<HierarchicalConfiguration> solutionConfigs = vrpProblem.configurationsAt("solutions.solution");
@ -246,13 +298,13 @@ public class VrpXMLReader{
for(HierarchicalConfiguration actConfig : actConfigs){ for(HierarchicalConfiguration actConfig : actConfigs){
String type = actConfig.getString("[@type]"); String type = actConfig.getString("[@type]");
if(type == null) throw new IllegalStateException("act[@type] is missing."); if(type == null) throw new IllegalStateException("act[@type] is missing.");
double arrTime = 0.;
double endTime = 0.;
String arrTimeS = actConfig.getString("arrTime"); String arrTimeS = actConfig.getString("arrTime");
if(arrTimeS == null) throw new IllegalStateException("act.arrTime is missing."); if(arrTimeS!=null) arrTime=Double.parseDouble(arrTimeS);
String endTimeS = actConfig.getString("endTime"); String endTimeS = actConfig.getString("endTime");
if(endTimeS == null) throw new IllegalStateException("act.endTime is missing."); if(endTimeS!=null) endTime=Double.parseDouble(endTimeS);
double arrTime = Double.parseDouble(arrTimeS);
double endTime = Double.parseDouble(endTimeS);
String serviceId = actConfig.getString("serviceId"); String serviceId = actConfig.getString("serviceId");
if(serviceId != null) { if(serviceId != null) {
Service service = getService(serviceId); Service service = getService(serviceId);
@ -270,8 +322,7 @@ public class VrpXMLReader{
routeBuilder.addDelivery(shipment, arrTime, endTime); routeBuilder.addDelivery(shipment, arrTime, endTime);
} }
else throw new IllegalStateException("type " + type + " is not supported. Use 'pickupShipment' or 'deliverShipment' here"); else throw new IllegalStateException("type " + type + " is not supported. Use 'pickupShipment' or 'deliverShipment' here");
} }
} }
routes.add(routeBuilder.build()); routes.add(routeBuilder.build());
} }
@ -399,7 +450,7 @@ public class VrpXMLReader{
Shipment shipment = builder.build(); Shipment shipment = builder.build();
vrpBuilder.addJob(shipment); vrpBuilder.addJob(shipment);
shipmentMap .put(shipment.getId(),shipment); shipmentMap.put(shipment.getId(),shipment);
} }
} }

View file

@ -219,6 +219,32 @@
</xs:complexType> </xs:complexType>
</xs:element> </xs:element>
<xs:element name="initialRoutes" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="route" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="driverId" type="xs:string" minOccurs="1" maxOccurs="1"/>
<xs:element name="vehicleId" type="xs:string" minOccurs="1" maxOccurs="1"/>
<xs:element name="start" type="xs:double" minOccurs="1" maxOccurs="1"/>
<xs:element name="act" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:choice>
<xs:group ref="serviceActGroup"/>
<xs:group ref="shipmentActGroup"/>
</xs:choice>
<xs:attribute name="type" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
<xs:element name="end" type="xs:anySimpleType" minOccurs="1" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="solutions" minOccurs="0" maxOccurs="1"> <xs:element name="solutions" minOccurs="0" maxOccurs="1">
<xs:complexType> <xs:complexType>
<xs:sequence> <xs:sequence>
@ -272,16 +298,16 @@
<xs:group name="serviceActGroup"> <xs:group name="serviceActGroup">
<xs:sequence> <xs:sequence>
<xs:element name="serviceId" type="xs:string" minOccurs="1" maxOccurs="1"/> <xs:element name="serviceId" type="xs:string" minOccurs="1" maxOccurs="1"/>
<xs:element name="arrTime" type="xs:double" minOccurs="1" maxOccurs="1"/> <xs:element name="arrTime" type="xs:double" minOccurs="0" maxOccurs="1"/>
<xs:element name="endTime" type="xs:double" minOccurs="1" maxOccurs="1"/> <xs:element name="endTime" type="xs:double" minOccurs="0" maxOccurs="1"/>
</xs:sequence> </xs:sequence>
</xs:group> </xs:group>
<xs:group name="shipmentActGroup"> <xs:group name="shipmentActGroup">
<xs:sequence> <xs:sequence>
<xs:element name="shipmentId" type="xs:string" minOccurs="1" maxOccurs="1"/> <xs:element name="shipmentId" type="xs:string" minOccurs="1" maxOccurs="1"/>
<xs:element name="arrTime" type="xs:double" minOccurs="1" maxOccurs="1"/> <xs:element name="arrTime" type="xs:double" minOccurs="0" maxOccurs="1"/>
<xs:element name="endTime" type="xs:double" minOccurs="1" maxOccurs="1"/> <xs:element name="endTime" type="xs:double" minOccurs="0" maxOccurs="1"/>
</xs:sequence> </xs:sequence>
</xs:group> </xs:group>

View file

@ -449,7 +449,7 @@ public class VrpXMLReaderTest {
} }
@Test @Test
public void whenReadingInitialRouteWithDepTime10_departureTimeOfRouteShouldBeReadCorrectly(){ //i.e. these jobs should not be part of the problem public void whenReadingInitialRouteWithDepTime10_departureTimeOfRouteShouldBeReadCorrectly(){
VehicleRoutingProblem.Builder builder = VehicleRoutingProblem.Builder.newInstance(); VehicleRoutingProblem.Builder builder = VehicleRoutingProblem.Builder.newInstance();
new VrpXMLReader(builder).read("src/test/resources/finiteVrpWithInitialSolutionForReaderTest.xml"); new VrpXMLReader(builder).read("src/test/resources/finiteVrpWithInitialSolutionForReaderTest.xml");
VehicleRoutingProblem vrp = builder.build(); VehicleRoutingProblem vrp = builder.build();
@ -457,7 +457,7 @@ public class VrpXMLReaderTest {
} }
@Test @Test
public void whenReadingInitialRoute_nuInitialRoutesShouldBeCorrect(){ //i.e. these jobs should not be part of the problem public void whenReadingInitialRoute_nuInitialRoutesShouldBeCorrect(){
VehicleRoutingProblem.Builder builder = VehicleRoutingProblem.Builder.newInstance(); VehicleRoutingProblem.Builder builder = VehicleRoutingProblem.Builder.newInstance();
new VrpXMLReader(builder, null).read("src/test/resources/finiteVrpWithInitialSolutionForReaderTest.xml"); new VrpXMLReader(builder, null).read("src/test/resources/finiteVrpWithInitialSolutionForReaderTest.xml");
VehicleRoutingProblem vrp = builder.build(); VehicleRoutingProblem vrp = builder.build();
@ -465,7 +465,7 @@ public class VrpXMLReaderTest {
} }
@Test @Test
public void whenReadingInitialRoute_nuActivitiesShouldBeCorrect(){ //i.e. these jobs should not be part of the problem public void whenReadingInitialRoute_nuActivitiesShouldBeCorrect(){
VehicleRoutingProblem.Builder builder = VehicleRoutingProblem.Builder.newInstance(); VehicleRoutingProblem.Builder builder = VehicleRoutingProblem.Builder.newInstance();
new VrpXMLReader(builder, null).read("src/test/resources/finiteVrpWithInitialSolutionForReaderTest.xml"); new VrpXMLReader(builder, null).read("src/test/resources/finiteVrpWithInitialSolutionForReaderTest.xml");
VehicleRoutingProblem vrp = builder.build(); VehicleRoutingProblem vrp = builder.build();