From 93dc0f69e67ad9339446ed10507fa6d3174cfbd7 Mon Sep 17 00:00:00 2001 From: oblonski <4sschroeder@gmail.com> Date: Mon, 3 Nov 2014 23:22:42 +0100 Subject: [PATCH] make JsonWriter write vehicles --- .../jsprit/core/problem/io/JsonConstants.java | 32 ++++ .../jsprit/core/problem/io/VrpJsonWriter.java | 138 ++++++++++++++---- 2 files changed, 144 insertions(+), 26 deletions(-) diff --git a/jsprit-core/src/main/java/jsprit/core/problem/io/JsonConstants.java b/jsprit-core/src/main/java/jsprit/core/problem/io/JsonConstants.java index 2cd36ccd..0b473d71 100644 --- a/jsprit-core/src/main/java/jsprit/core/problem/io/JsonConstants.java +++ b/jsprit-core/src/main/java/jsprit/core/problem/io/JsonConstants.java @@ -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 . + ******************************************************************************/ + 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"; + } } diff --git a/jsprit-core/src/main/java/jsprit/core/problem/io/VrpJsonWriter.java b/jsprit-core/src/main/java/jsprit/core/problem/io/VrpJsonWriter.java index 2be0e3c7..614c83c2 100644 --- a/jsprit-core/src/main/java/jsprit/core/problem/io/VrpJsonWriter.java +++ b/jsprit-core/src/main/java/jsprit/core/problem/io/VrpJsonWriter.java @@ -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 . + ******************************************************************************/ + 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,31 +47,14 @@ public class VrpJsonWriter { JsonGenerator jsonGenerator = new JsonFactory().createGenerator(new FileOutputStream(filename)); jsonGenerator.setPrettyPrinter(new DefaultPrettyPrinter()); jsonGenerator.writeStartObject(); - jsonGenerator.writeStringField(JsonConstants.Job.ID,service.getId()); - jsonGenerator.writeStringField(JsonConstants.Job.NAME,service.getName()); - jsonGenerator.writeObjectFieldStart(JsonConstants.Job.ADDRESS); - jsonGenerator.writeStringField(JsonConstants.Address.ID, service.getLocationId()); - jsonGenerator.writeNumberField(JsonConstants.Address.LON, service.getCoord().getX()); - jsonGenerator.writeNumberField(JsonConstants.Address.LAT,service.getCoord().getY()); - jsonGenerator.writeEndObject(); - jsonGenerator.writeNumberField(JsonConstants.Job.SERVICE_DURATION, service.getServiceDuration()); - jsonGenerator.writeObjectFieldStart(JsonConstants.Job.TIME_WINDOW); - jsonGenerator.writeNumberField(JsonConstants.TimeWindow.START,service.getTimeWindow().getStart()); - jsonGenerator.writeNumberField(JsonConstants.TimeWindow.END,service.getTimeWindow().getEnd()); - jsonGenerator.writeEndObject(); + jsonGenerator.writeStringField(JsonConstants.FLEET,vrp.getFleetSize().toString()); + writeVehicles(jsonGenerator); +// writeVehicleTypes(jsonGenerator); + writeServices(jsonGenerator); + - jsonGenerator.writeArrayFieldStart(JsonConstants.Job.SIZE); - for(int i=0;i