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

json io stuff

This commit is contained in:
oblonski 2014-11-04 21:50:44 +01:00
parent 27d3a8dcb6
commit 382d2d850f
3 changed files with 97 additions and 35 deletions

View file

@ -86,6 +86,8 @@ public class JsonConstants {
public static final String TYPE_ID = "type_id"; public static final String TYPE_ID = "type_id";
public static final String RETURN_TO_DEPOT = "return_to_depot";
public static class Type { public static class Type {
public static final String ID = "id"; public static final String ID = "id";

View file

@ -22,6 +22,8 @@ import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import jsprit.core.problem.VehicleRoutingProblem; import jsprit.core.problem.VehicleRoutingProblem;
import jsprit.core.problem.job.Job; import jsprit.core.problem.job.Job;
import jsprit.core.problem.job.Service;
import jsprit.core.problem.solution.route.activity.TimeWindow;
import jsprit.core.problem.vehicle.Vehicle; import jsprit.core.problem.vehicle.Vehicle;
import jsprit.core.problem.vehicle.VehicleImpl; import jsprit.core.problem.vehicle.VehicleImpl;
import jsprit.core.problem.vehicle.VehicleType; import jsprit.core.problem.vehicle.VehicleType;
@ -48,55 +50,116 @@ public class VrpJsonReader {
} }
public void read(String jsonFile){ public void read(String jsonFile){
JsonNode root = buildTree_and_getRoot(jsonFile);
setFleetSize(root);
parse_and_map_vehicle_types(root);
parse_vehicles(root);
parse_services(root);
}
private JsonNode buildTree_and_getRoot(String jsonFile){
JsonNode node = null;
try { try {
ObjectMapper objectMapper = new ObjectMapper(); ObjectMapper objectMapper = new ObjectMapper();
JsonNode root = objectMapper.readTree(new File(jsonFile)); node = objectMapper.readTree(new File(jsonFile));
setFleetSize(root);
parse_and_map_vehicle_types(root);
parse_vehicles(root);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
System.exit(1);
}
return node;
}
private void parse_services(JsonNode root) {
JsonNode services = root.path(JsonConstants.SERVICES);
for(JsonNode serviceNode : services){
JsonNode jobIdNode = serviceNode.path(JsonConstants.Job.ID);
if(jobIdNode.isMissingNode()){
throw new IllegalStateException("service-id is missing");
}
Service.Builder serviceBuilder = Service.Builder.newInstance(jobIdNode.asText());
JsonNode addressIdNode = serviceNode.path(JsonConstants.Job.ADDRESS).path(JsonConstants.Address.ID);
boolean either_locationId_or_coord = false;
if(!addressIdNode.isMissingNode()){
serviceBuilder.setLocationId(addressIdNode.asText());
either_locationId_or_coord = true;
}
{
Double lon = serviceNode.path(JsonConstants.Job.ADDRESS).path(JsonConstants.Address.LON).asDouble();
Double lat = serviceNode.path(JsonConstants.Job.ADDRESS).path(JsonConstants.Address.LAT).asDouble();
if (lon != null && lat != null) {
serviceBuilder.setCoord(Coordinate.newInstance(lon, lat));
either_locationId_or_coord = true;
}
}
if(!either_locationId_or_coord) throw new IllegalStateException("location missing. either locationId or locationCoordinate is required");
serviceBuilder.setName(serviceNode.path(JsonConstants.Job.NAME).asText());
serviceBuilder.setServiceTime(serviceNode.path(JsonConstants.Job.SERVICE_DURATION).asDouble());
Double tw_start = serviceNode.path(JsonConstants.Job.TIME_WINDOW).path(JsonConstants.TimeWindow.START).asDouble();
Double tw_end = serviceNode.path(JsonConstants.Job.TIME_WINDOW).path(JsonConstants.TimeWindow.END).asDouble();
if(tw_start != null && tw_end != null){
serviceBuilder.setTimeWindow(TimeWindow.newInstance(tw_start,tw_end));
}
JsonNode sizeNode = serviceNode.path(JsonConstants.Job.SIZE);
int size_index = 0;
for(JsonNode sizeValNode : sizeNode){
int size_value = sizeValNode.intValue();
serviceBuilder.addSizeDimension(size_index,size_value);
size_index++;
}
JsonNode reqSkills = serviceNode.path(JsonConstants.Job.SKILLS);
for(JsonNode skillNode : reqSkills){
serviceBuilder.addRequiredSkill(skillNode.asText());
}
vrpBuilder.addJob(serviceBuilder.build());
} }
} }
private void parse_vehicles(JsonNode root) { private void parse_vehicles(JsonNode root) {
JsonNode vehicles = root.path(JsonConstants.VEHICLES); JsonNode vehicles = root.path(JsonConstants.VEHICLES);
Iterator<JsonNode> vehicle_iterator = vehicles.iterator();
while(vehicle_iterator.hasNext()){ for(JsonNode vehicleNode : vehicles){
JsonNode vehicleNode = vehicle_iterator.next(); // JsonNode vehicleNode = vehicle_iterator.next();
VehicleImpl.Builder vehicleBuilder = VehicleImpl.Builder.newInstance(vehicleNode.path(JsonConstants.Vehicle.ID).asText()); VehicleImpl.Builder vehicleBuilder = VehicleImpl.Builder.newInstance(vehicleNode.path(JsonConstants.Vehicle.ID).asText());
VehicleType type = vehicle_type_map.get(vehicleNode.path(JsonConstants.Vehicle.TYPE_ID).asText()); VehicleType type = vehicle_type_map.get(vehicleNode.path(JsonConstants.Vehicle.TYPE_ID).asText());
vehicleBuilder.setType(type); vehicleBuilder.setType(type);
vehicleBuilder.setEarliestStart(vehicleNode.path(JsonConstants.Vehicle.EARLIEST_START).asDouble());
vehicleBuilder.setLatestArrival(vehicleNode.path(JsonConstants.Vehicle.LATEST_END).asDouble()); JsonNode earliestStartNode = vehicleNode.path(JsonConstants.Vehicle.EARLIEST_START);
if(!earliestStartNode.isMissingNode()) vehicleBuilder.setEarliestStart(earliestStartNode.asDouble());
JsonNode latestEndNode = vehicleNode.path(JsonConstants.Vehicle.LATEST_END);
if(!latestEndNode.isMissingNode()) vehicleBuilder.setLatestArrival(latestEndNode.asDouble());
String startLocationId = vehicleNode.path(JsonConstants.Vehicle.START_ADDRESS).path(JsonConstants.Address.ID).asText(); String startLocationId = vehicleNode.path(JsonConstants.Vehicle.START_ADDRESS).path(JsonConstants.Address.ID).asText();
vehicleBuilder.setStartLocationId(startLocationId); vehicleBuilder.setStartLocationId(startLocationId);
String endLocationId = vehicleNode.path(JsonConstants.Vehicle.END_ADDRESS).path(JsonConstants.Address.ID).asText(); JsonNode endAddressId = vehicleNode.path(JsonConstants.Vehicle.END_ADDRESS).path(JsonConstants.Address.ID);
if(!startLocationId.equals(endLocationId)){ if(!endAddressId.isMissingNode()){
vehicleBuilder.setEndLocationId(endLocationId); if(!startLocationId.equals(endAddressId.asText())){
} vehicleBuilder.setEndLocationId(endAddressId.asText());
{
Double lon = vehicleNode.path(JsonConstants.Vehicle.START_ADDRESS).path(JsonConstants.Address.LON).asDouble();
Double lat = vehicleNode.path(JsonConstants.Vehicle.START_ADDRESS).path(JsonConstants.Address.LAT).asDouble();
if (lon != null && lat != null) {
vehicleBuilder.setStartLocationCoordinate(Coordinate.newInstance(lon, lat));
} }
} }
{ {
Double lon = vehicleNode.path(JsonConstants.Vehicle.END_ADDRESS).path(JsonConstants.Address.LON).asDouble(); JsonNode lonNode = vehicleNode.path(JsonConstants.Vehicle.START_ADDRESS).path(JsonConstants.Address.LON);
Double lat = vehicleNode.path(JsonConstants.Vehicle.END_ADDRESS).path(JsonConstants.Address.LAT).asDouble(); JsonNode latNode = vehicleNode.path(JsonConstants.Vehicle.START_ADDRESS).path(JsonConstants.Address.LAT);
if (lon != null && lat != null) { if (!lonNode.isMissingNode() && !latNode.isMissingNode()) {
vehicleBuilder.setEndLocationCoordinate(Coordinate.newInstance(lon, lat)); vehicleBuilder.setStartLocationCoordinate(Coordinate.newInstance(lonNode.asDouble(), latNode.asDouble()));
}
}
{
JsonNode lonNode = vehicleNode.path(JsonConstants.Vehicle.END_ADDRESS).path(JsonConstants.Address.LON);
JsonNode latNode = vehicleNode.path(JsonConstants.Vehicle.END_ADDRESS).path(JsonConstants.Address.LAT);
if (!lonNode.isMissingNode() && !latNode.isMissingNode()) {
vehicleBuilder.setEndLocationCoordinate(Coordinate.newInstance(lonNode.asDouble(), latNode.asDouble()));
} }
} }
JsonNode skillsNode = vehicleNode.path(JsonConstants.Vehicle.SKILLS); JsonNode skillsNode = vehicleNode.path(JsonConstants.Vehicle.SKILLS);
Iterator<JsonNode> skill_iterator = skillsNode.iterator(); for(JsonNode skillNode : skillsNode){
while(skill_iterator.hasNext()){
JsonNode skillNode = skill_iterator.next();
String skill = skillNode.asText(); String skill = skillNode.asText();
vehicleBuilder.addSkill(skill); vehicleBuilder.addSkill(skill);
} }
@ -109,9 +172,7 @@ public class VrpJsonReader {
private void parse_and_map_vehicle_types(JsonNode root) { private void parse_and_map_vehicle_types(JsonNode root) {
JsonNode types = root.path(JsonConstants.VEHICLE_TYPES); JsonNode types = root.path(JsonConstants.VEHICLE_TYPES);
Iterator<JsonNode> typeIterator = types.iterator(); for(JsonNode typeNode : types){
while(typeIterator.hasNext()){
JsonNode typeNode = typeIterator.next();
VehicleTypeImpl.Builder typeBuilder = VehicleTypeImpl.Builder.newInstance(typeNode.path(JsonConstants.Vehicle.Type.ID).asText()); VehicleTypeImpl.Builder typeBuilder = VehicleTypeImpl.Builder.newInstance(typeNode.path(JsonConstants.Vehicle.Type.ID).asText());
typeBuilder.setFixedCost(typeNode.path(JsonConstants.Vehicle.Type.FIXED_COSTS).asDouble()); typeBuilder.setFixedCost(typeNode.path(JsonConstants.Vehicle.Type.FIXED_COSTS).asDouble());
typeBuilder.setCostPerDistance(typeNode.path(JsonConstants.Vehicle.Type.DISTANCE).asDouble()); typeBuilder.setCostPerDistance(typeNode.path(JsonConstants.Vehicle.Type.DISTANCE).asDouble());

View file

@ -57,10 +57,6 @@ public class VrpJsonWriter {
writeVehicles(jsonGenerator); writeVehicles(jsonGenerator);
writeVehicleTypes(jsonGenerator); writeVehicleTypes(jsonGenerator);
writeServices(jsonGenerator); writeServices(jsonGenerator);
jsonGenerator.writeEndObject(); jsonGenerator.writeEndObject();
jsonGenerator.flush(); jsonGenerator.flush();
@ -115,6 +111,7 @@ public class VrpJsonWriter {
jsonGenerator.writeNumberField(JsonConstants.Address.LON, vehicle.getStartLocationCoordinate().getX()); jsonGenerator.writeNumberField(JsonConstants.Address.LON, vehicle.getStartLocationCoordinate().getX());
jsonGenerator.writeNumberField(JsonConstants.Address.LAT,vehicle.getStartLocationCoordinate().getY()); jsonGenerator.writeNumberField(JsonConstants.Address.LAT,vehicle.getStartLocationCoordinate().getY());
jsonGenerator.writeEndObject(); jsonGenerator.writeEndObject();
jsonGenerator.writeBooleanField(JsonConstants.Vehicle.RETURN_TO_DEPOT,vehicle.isReturnToDepot());
if(!(vehicle.getStartLocationCoordinate().equals(vehicle.getEndLocationCoordinate()) if(!(vehicle.getStartLocationCoordinate().equals(vehicle.getEndLocationCoordinate())
&& vehicle.getStartLocationId().equals(vehicle.getEndLocationId()))){ && vehicle.getStartLocationId().equals(vehicle.getEndLocationId()))){
jsonGenerator.writeObjectFieldStart(JsonConstants.Vehicle.END_ADDRESS); jsonGenerator.writeObjectFieldStart(JsonConstants.Vehicle.END_ADDRESS);
@ -206,8 +203,10 @@ public class VrpJsonWriter {
.setType(type) .setType(type)
.build(); .build();
VehicleImpl v2 = VehicleImpl.Builder.newInstance("v2").setStartLocationId("startLoc").setStartLocationCoordinate(Coordinate.newInstance(0,0)) VehicleImpl v2 = VehicleImpl.Builder.newInstance("v2").setStartLocationId("startLoc").setStartLocationCoordinate(Coordinate.newInstance(0, 0))
.setType(type2).build(); .setType(type2)
.setReturnToDepot(false)
.build();
VehicleRoutingProblem vrp = VehicleRoutingProblem.Builder.newInstance().addJob(service).addJob(service2) VehicleRoutingProblem vrp = VehicleRoutingProblem.Builder.newInstance().addJob(service).addJob(service2)
.addVehicle(v1).addVehicle(v2).build(); .addVehicle(v1).addVehicle(v2).build();
new VrpJsonWriter(vrp).write("output/vrp.json"); new VrpJsonWriter(vrp).write("output/vrp.json");