mirror of
https://github.com/graphhopper/jsprit.git
synced 2020-01-24 07:45:05 +01:00
tested core.problem.io.VrpXMLWriter.java to write initialRoutes
correctly
This commit is contained in:
parent
601f485b32
commit
b1c3231991
2 changed files with 113 additions and 7 deletions
|
|
@ -19,7 +19,9 @@ package jsprit.core.problem.io;
|
||||||
import java.io.FileWriter;
|
import java.io.FileWriter;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.Writer;
|
import java.io.Writer;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import jsprit.core.problem.VehicleRoutingProblem;
|
import jsprit.core.problem.VehicleRoutingProblem;
|
||||||
import jsprit.core.problem.job.Job;
|
import jsprit.core.problem.job.Job;
|
||||||
|
|
@ -85,13 +87,23 @@ public class VrpXMLWriter {
|
||||||
xmlConfig.setAttributeSplittingDisabled(true);
|
xmlConfig.setAttributeSplittingDisabled(true);
|
||||||
xmlConfig.setDelimiterParsingDisabled(true);
|
xmlConfig.setDelimiterParsingDisabled(true);
|
||||||
|
|
||||||
|
|
||||||
writeProblemType(xmlConfig);
|
writeProblemType(xmlConfig);
|
||||||
writeVehiclesAndTheirTypes(xmlConfig);
|
writeVehiclesAndTheirTypes(xmlConfig);
|
||||||
writeServices(xmlConfig);
|
|
||||||
writeShipments(xmlConfig);
|
//might be sorted?
|
||||||
|
List<Job> jobs = new ArrayList<Job>();
|
||||||
|
jobs.addAll(vrp.getJobs().values());
|
||||||
|
for(VehicleRoute r : vrp.getInitialVehicleRoutes()){
|
||||||
|
jobs.addAll(r.getTourActivities().getJobs());
|
||||||
|
}
|
||||||
|
|
||||||
|
writeServices(xmlConfig,jobs);
|
||||||
|
writeShipments(xmlConfig,jobs);
|
||||||
|
|
||||||
|
writeInitialRoutes(xmlConfig);
|
||||||
writeSolutions(xmlConfig);
|
writeSolutions(xmlConfig);
|
||||||
|
|
||||||
|
|
||||||
OutputFormat format = new OutputFormat();
|
OutputFormat format = new OutputFormat();
|
||||||
format.setIndenting(true);
|
format.setIndenting(true);
|
||||||
format.setIndent(5);
|
format.setIndent(5);
|
||||||
|
|
@ -123,6 +135,39 @@ public class VrpXMLWriter {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void writeInitialRoutes(XMLConf xmlConfig) {
|
||||||
|
if(vrp.getInitialVehicleRoutes().isEmpty()) return;
|
||||||
|
String path = "initialRoutes.route";
|
||||||
|
int routeCounter = 0;
|
||||||
|
for(VehicleRoute route : vrp.getInitialVehicleRoutes()){
|
||||||
|
xmlConfig.setProperty(path + "(" + routeCounter + ").driverId", route.getDriver().getId());
|
||||||
|
xmlConfig.setProperty(path + "(" + routeCounter + ").vehicleId", route.getVehicle().getId());
|
||||||
|
xmlConfig.setProperty(path + "(" + routeCounter + ").start", route.getStart().getEndTime());
|
||||||
|
int actCounter = 0;
|
||||||
|
for(TourActivity act : route.getTourActivities().getActivities()){
|
||||||
|
xmlConfig.setProperty(path + "(" + routeCounter + ").act("+actCounter+")[@type]", act.getName());
|
||||||
|
if(act instanceof JobActivity){
|
||||||
|
Job job = ((JobActivity) act).getJob();
|
||||||
|
if(job instanceof Service){
|
||||||
|
xmlConfig.setProperty(path + "(" + routeCounter + ").act("+actCounter+").serviceId", job.getId());
|
||||||
|
}
|
||||||
|
else if(job instanceof Shipment){
|
||||||
|
xmlConfig.setProperty(path + "(" + routeCounter + ").act("+actCounter+").shipmentId", job.getId());
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
throw new IllegalStateException("cannot write solution correctly since job-type is not know. make sure you use either service or shipment, or another writer");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
xmlConfig.setProperty(path + "(" + routeCounter + ").act("+actCounter+").arrTime", act.getArrTime());
|
||||||
|
xmlConfig.setProperty(path + "(" + routeCounter + ").act("+actCounter+").endTime", act.getEndTime());
|
||||||
|
actCounter++;
|
||||||
|
}
|
||||||
|
xmlConfig.setProperty(path + "(" + routeCounter + ").end", route.getEnd().getArrTime());
|
||||||
|
routeCounter++;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
private void writeSolutions(XMLConf xmlConfig) {
|
private void writeSolutions(XMLConf xmlConfig) {
|
||||||
if(solutions == null) return;
|
if(solutions == null) return;
|
||||||
String solutionPath = "solutions.solution";
|
String solutionPath = "solutions.solution";
|
||||||
|
|
@ -161,10 +206,10 @@ public class VrpXMLWriter {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void writeServices(XMLConf xmlConfig) {
|
private void writeServices(XMLConf xmlConfig, List<Job> jobs) {
|
||||||
String shipmentPathString = "services.service";
|
String shipmentPathString = "services.service";
|
||||||
int counter = 0;
|
int counter = 0;
|
||||||
for(Job j : vrp.getJobs().values()){
|
for(Job j : jobs){
|
||||||
if(!(j instanceof Service)) continue;
|
if(!(j instanceof Service)) continue;
|
||||||
Service service = (Service) j;
|
Service service = (Service) j;
|
||||||
xmlConfig.setProperty(shipmentPathString + "("+counter+")[@id]", service.getId());
|
xmlConfig.setProperty(shipmentPathString + "("+counter+")[@id]", service.getId());
|
||||||
|
|
@ -186,10 +231,10 @@ public class VrpXMLWriter {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void writeShipments(XMLConf xmlConfig) {
|
private void writeShipments(XMLConf xmlConfig, List<Job> jobs) {
|
||||||
String shipmentPathString = "shipments.shipment";
|
String shipmentPathString = "shipments.shipment";
|
||||||
int counter = 0;
|
int counter = 0;
|
||||||
for(Job j : vrp.getJobs().values()){
|
for(Job j : jobs){
|
||||||
if(!(j instanceof Shipment)) continue;
|
if(!(j instanceof Shipment)) continue;
|
||||||
Shipment shipment = (Shipment) j;
|
Shipment shipment = (Shipment) j;
|
||||||
xmlConfig.setProperty(shipmentPathString + "("+counter+")[@id]", shipment.getId());
|
xmlConfig.setProperty(shipmentPathString + "("+counter+")[@id]", shipment.getId());
|
||||||
|
|
|
||||||
|
|
@ -589,5 +589,66 @@ public class VrpXMLWriterTest {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenWritingAndReadingInitialRouteWithShipment4_thisShipmentShouldNotAppearInJobMap(){ //since it is not part of the problem anymore
|
||||||
|
VehicleRoutingProblem.Builder builder = VehicleRoutingProblem.Builder.newInstance();
|
||||||
|
new VrpXMLReader(builder).read("src/test/resources/finiteVrpWithInitialSolutionForReaderTest.xml");
|
||||||
|
VehicleRoutingProblem vrp = builder.build();
|
||||||
|
|
||||||
|
new VrpXMLWriter(vrp).write("src/test/resources/finiteVrpWithInitialSolutionForWriterTest.xml");
|
||||||
|
|
||||||
|
VehicleRoutingProblem.Builder newBuilder = VehicleRoutingProblem.Builder.newInstance();
|
||||||
|
new VrpXMLReader(newBuilder).read("src/test/resources/finiteVrpWithInitialSolutionForWriterTest.xml");
|
||||||
|
VehicleRoutingProblem newVrp = newBuilder.build();
|
||||||
|
|
||||||
|
assertFalse(newVrp.getJobs().containsKey("4"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenReadingInitialRouteWithDepTime10_departureTimeOfRouteShouldBeReadCorrectly(){
|
||||||
|
VehicleRoutingProblem.Builder builder = VehicleRoutingProblem.Builder.newInstance();
|
||||||
|
new VrpXMLReader(builder).read("src/test/resources/finiteVrpWithInitialSolutionForReaderTest.xml");
|
||||||
|
VehicleRoutingProblem vrp = builder.build();
|
||||||
|
|
||||||
|
new VrpXMLWriter(vrp).write("src/test/resources/finiteVrpWithInitialSolutionForWriterTest.xml");
|
||||||
|
|
||||||
|
VehicleRoutingProblem.Builder newBuilder = VehicleRoutingProblem.Builder.newInstance();
|
||||||
|
new VrpXMLReader(newBuilder).read("src/test/resources/finiteVrpWithInitialSolutionForWriterTest.xml");
|
||||||
|
VehicleRoutingProblem newVrp = newBuilder.build();
|
||||||
|
|
||||||
|
assertEquals(10.,newVrp.getInitialVehicleRoutes().iterator().next().getDepartureTime(),0.01);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenReadingInitialRoute_nuInitialRoutesShouldBeCorrect(){
|
||||||
|
VehicleRoutingProblem.Builder builder = VehicleRoutingProblem.Builder.newInstance();
|
||||||
|
new VrpXMLReader(builder, null).read("src/test/resources/finiteVrpWithInitialSolutionForReaderTest.xml");
|
||||||
|
VehicleRoutingProblem vrp = builder.build();
|
||||||
|
|
||||||
|
new VrpXMLWriter(vrp).write("src/test/resources/finiteVrpWithInitialSolutionForWriterTest.xml");
|
||||||
|
|
||||||
|
VehicleRoutingProblem.Builder newBuilder = VehicleRoutingProblem.Builder.newInstance();
|
||||||
|
new VrpXMLReader(newBuilder).read("src/test/resources/finiteVrpWithInitialSolutionForWriterTest.xml");
|
||||||
|
VehicleRoutingProblem newVrp = newBuilder.build();
|
||||||
|
|
||||||
|
|
||||||
|
assertEquals(1,newVrp.getInitialVehicleRoutes().size());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenReadingInitialRoute_nuActivitiesShouldBeCorrect(){
|
||||||
|
VehicleRoutingProblem.Builder builder = VehicleRoutingProblem.Builder.newInstance();
|
||||||
|
new VrpXMLReader(builder, null).read("src/test/resources/finiteVrpWithInitialSolutionForReaderTest.xml");
|
||||||
|
VehicleRoutingProblem vrp = builder.build();
|
||||||
|
|
||||||
|
new VrpXMLWriter(vrp).write("src/test/resources/finiteVrpWithInitialSolutionForWriterTest.xml");
|
||||||
|
|
||||||
|
VehicleRoutingProblem.Builder newBuilder = VehicleRoutingProblem.Builder.newInstance();
|
||||||
|
new VrpXMLReader(newBuilder).read("src/test/resources/finiteVrpWithInitialSolutionForWriterTest.xml");
|
||||||
|
VehicleRoutingProblem newVrp = newBuilder.build();
|
||||||
|
|
||||||
|
|
||||||
|
assertEquals(2,newVrp.getInitialVehicleRoutes().iterator().next().getActivities().size());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue