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

make JsonWriter write vehicles

This commit is contained in:
oblonski 2014-11-03 23:22:42 +01:00
parent b1eb836ec3
commit 93dc0f69e6
2 changed files with 144 additions and 26 deletions

View file

@ -1,3 +1,20 @@
/*******************************************************************************
* Copyright (C) 2014 Stefan Schroeder
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
package jsprit.core.problem.io;
/**
@ -5,6 +22,12 @@ package jsprit.core.problem.io;
*/
public class JsonConstants {
public static final String SERVICES = "services";
public static final String FLEET = "fleet_size";
public static final String VEHICLES = "vehicles";
public static class Address {
public static String ID = "id";
@ -42,4 +65,13 @@ public class JsonConstants {
public static final String TIME_WINDOW = "time_window";
}
public static class Vehicle {
public static final String ID = "id";
public static final String START_ADDRESS = "start_address";
public static final String END_ADDRESS = "end_address";
}
}

View file

@ -1,10 +1,31 @@
/*******************************************************************************
* Copyright (C) 2014 Stefan Schroeder
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
package jsprit.core.problem.io;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
import jsprit.core.problem.VehicleRoutingProblem;
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.VehicleImpl;
import jsprit.core.util.Coordinate;
import java.io.FileOutputStream;
@ -15,10 +36,10 @@ import java.io.IOException;
*/
public class VrpJsonWriter {
private Service service;
private final VehicleRoutingProblem vrp;
public VrpJsonWriter(Service service) {
this.service = service;
public VrpJsonWriter(VehicleRoutingProblem vrp) {
this.vrp = vrp;
}
public void write(String filename){
@ -26,6 +47,61 @@ public class VrpJsonWriter {
JsonGenerator jsonGenerator = new JsonFactory().createGenerator(new FileOutputStream(filename));
jsonGenerator.setPrettyPrinter(new DefaultPrettyPrinter());
jsonGenerator.writeStartObject();
jsonGenerator.writeStringField(JsonConstants.FLEET,vrp.getFleetSize().toString());
writeVehicles(jsonGenerator);
// writeVehicleTypes(jsonGenerator);
writeServices(jsonGenerator);
jsonGenerator.writeEndObject();
jsonGenerator.flush();
jsonGenerator.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private void writeVehicles(JsonGenerator jsonGenerator) {
try {
jsonGenerator.writeArrayFieldStart(JsonConstants.VEHICLES);
for(Vehicle vehicle : vrp.getVehicles()){
jsonGenerator.writeStartObject();
jsonGenerator.writeStringField(JsonConstants.Vehicle.ID,vehicle.getId());
jsonGenerator.writeObjectFieldStart(JsonConstants.Vehicle.START_ADDRESS);
jsonGenerator.writeStringField(JsonConstants.Address.ID, vehicle.getStartLocationId());
jsonGenerator.writeNumberField(JsonConstants.Address.LON, vehicle.getStartLocationCoordinate().getX());
jsonGenerator.writeNumberField(JsonConstants.Address.LAT,vehicle.getStartLocationCoordinate().getY());
jsonGenerator.writeEndObject();
if(!(vehicle.getStartLocationCoordinate().equals(vehicle.getEndLocationCoordinate())
&& vehicle.getStartLocationId().equals(vehicle.getEndLocationId()))){
jsonGenerator.writeObjectFieldStart(JsonConstants.Vehicle.END_ADDRESS);
jsonGenerator.writeStringField(JsonConstants.Address.ID, vehicle.getEndLocationId());
jsonGenerator.writeNumberField(JsonConstants.Address.LON, vehicle.getEndLocationCoordinate().getX());
jsonGenerator.writeNumberField(JsonConstants.Address.LAT,vehicle.getEndLocationCoordinate().getY());
jsonGenerator.writeEndObject();
}
jsonGenerator.writeEndObject();
}
jsonGenerator.writeEndArray();
} catch (IOException e) {
e.printStackTrace();
}
}
private void writeServices(JsonGenerator jsonGenerator) {
try {
jsonGenerator.writeArrayFieldStart(JsonConstants.SERVICES);
for(Job job : vrp.getJobs().values()){
if(!(job instanceof Service)) continue;
Service service = (Service)job;
jsonGenerator.writeStartObject();
jsonGenerator.writeStringField(JsonConstants.Job.ID,service.getId());
jsonGenerator.writeStringField(JsonConstants.Job.NAME,service.getName());
jsonGenerator.writeObjectFieldStart(JsonConstants.Job.ADDRESS);
@ -51,15 +127,13 @@ public class VrpJsonWriter {
jsonGenerator.writeString(skill);
}
jsonGenerator.writeEndArray();
jsonGenerator.writeEndObject();
jsonGenerator.flush();
jsonGenerator.close();
}
jsonGenerator.writeEndArray();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
@ -69,6 +143,18 @@ public class VrpJsonWriter {
.setTimeWindow(TimeWindow.newInstance(10, 20))
.addRequiredSkill("drilling-machine")
.addRequiredSkill("screw-driver").build();
new VrpJsonWriter(service).write("output/jsonwriter.json");
Service service2 = Service.Builder.newInstance("s2").setLocationId("s2_loc").setCoord(Coordinate.newInstance(40, 10))
.addSizeDimension(0, 20).addSizeDimension(1, 40)
.setServiceTime(100.)
.setTimeWindow(TimeWindow.newInstance(10, 20))
.addRequiredSkill("screw-driver").build();
VehicleImpl v1 = VehicleImpl.Builder.newInstance("v1").setStartLocationId("startLoc").setStartLocationCoordinate(Coordinate.newInstance(0,0))
.setEndLocationId("endLoc").setEndLocationCoordinate(Coordinate.newInstance(12, 12)).build();
VehicleImpl v2 = VehicleImpl.Builder.newInstance("v2").setStartLocationId("startLoc").setStartLocationCoordinate(Coordinate.newInstance(0,0))
.build();
VehicleRoutingProblem vrp = VehicleRoutingProblem.Builder.newInstance().addJob(service).addJob(service2)
.addVehicle(v1).addVehicle(v2).build();
new VrpJsonWriter(vrp).write("output/vrp.json");
}
}