From 0179a00e0b448d6e47862b674ebce16641b3d9b6 Mon Sep 17 00:00:00 2001
From: oblonski <4sschroeder@gmail.com>
Date: Wed, 3 Sep 2014 16:09:07 +0200
Subject: [PATCH] add TSPLIB95Reader and Geoffrey's roaddistances matrix reader
---
.../reader/TSPLIB95CostMatrixReader.java | 80 +++++++++++
.../instance/reader/TSPLIB95Reader.java | 136 ++++++++++++++++++
2 files changed, 216 insertions(+)
create mode 100644 jsprit-instances/src/main/java/jsprit/instance/reader/TSPLIB95CostMatrixReader.java
create mode 100644 jsprit-instances/src/main/java/jsprit/instance/reader/TSPLIB95Reader.java
diff --git a/jsprit-instances/src/main/java/jsprit/instance/reader/TSPLIB95CostMatrixReader.java b/jsprit-instances/src/main/java/jsprit/instance/reader/TSPLIB95CostMatrixReader.java
new file mode 100644
index 00000000..d0d60b45
--- /dev/null
+++ b/jsprit-instances/src/main/java/jsprit/instance/reader/TSPLIB95CostMatrixReader.java
@@ -0,0 +1,80 @@
+/*******************************************************************************
+ * 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.instance.reader;
+
+
+import jsprit.core.util.VehicleRoutingTransportCostsMatrix;
+
+import java.io.*;
+
+public class TSPLIB95CostMatrixReader {
+
+ private VehicleRoutingTransportCostsMatrix.Builder costMatrixBuilder;
+
+ public TSPLIB95CostMatrixReader(VehicleRoutingTransportCostsMatrix.Builder costMatrixBuilder) {
+ this.costMatrixBuilder = costMatrixBuilder;
+ }
+
+ public void read(String matrixFile){
+ BufferedReader reader = getBufferedReader(matrixFile);
+ String line;
+ boolean isEdgeWeights = false;
+ int fromIndex = 0;
+ while( ( line = getLine(reader) ) != null ){
+ if(line.startsWith("EDGE_WEIGHT_SECTION")){
+ isEdgeWeights = true;
+ continue;
+ }
+ if(line.startsWith("DEMAND_SECTION")){
+ isEdgeWeights = false;
+ continue;
+ }
+ if(isEdgeWeights){
+ String[] tokens = line.split("\\s+");
+ String fromId = "" + (fromIndex + 1);
+ for(int i=0;i.
+ ******************************************************************************/
+
+package jsprit.instance.reader;
+
+import jsprit.core.problem.VehicleRoutingProblem;
+import jsprit.core.problem.job.Service;
+import jsprit.core.problem.vehicle.VehicleImpl;
+import jsprit.core.problem.vehicle.VehicleTypeImpl;
+import jsprit.core.util.Coordinate;
+
+import java.io.*;
+import java.util.ArrayList;
+import java.util.List;
+
+
+public class TSPLIB95Reader {
+
+ private VehicleRoutingProblem.Builder vrpBuilder;
+
+ public TSPLIB95Reader(VehicleRoutingProblem.Builder vrpBuilder) {
+ this.vrpBuilder = vrpBuilder;
+ }
+
+ public void read(String filename){
+ BufferedReader reader = getBufferedReader(filename);
+ String line;
+ Coordinate[] coords = null;
+ Integer[] demands = null;
+ Integer capacity = null;
+ List depotIds = new ArrayList();
+ boolean isCoordSection = false;
+ boolean isDemandSection = false;
+ boolean isDepotSection = false;
+ while( ( line = getLine(reader) ) != null ){
+ if(line.startsWith("DIMENSION")){
+ String[] tokens = line.split(":");
+ String dim = tokens[1].trim();
+ coords = new Coordinate[Integer.parseInt(dim)];
+ demands = new Integer[Integer.parseInt(dim)];
+ continue;
+ }
+ if(line.startsWith("CAPACITY")){
+ String[] tokens = line.split(":");
+ capacity = Integer.parseInt(tokens[1].trim());
+ continue;
+ }
+ if(line.startsWith("NODE_COORD_SECTION")){
+ isCoordSection = true;
+ isDemandSection = false;
+ isDepotSection = false;
+ continue;
+ }
+ if(line.startsWith("DEMAND_SECTION")){
+ isDemandSection = true;
+ isCoordSection = false;
+ isDepotSection = false;
+ continue;
+ }
+ if(line.startsWith("DEPOT_SECTION")){
+ isDepotSection = true;
+ isDemandSection = false;
+ isCoordSection = false;
+ continue;
+ }
+ if(isCoordSection){
+ if(coords == null) throw new IllegalStateException("DIMENSION tag missing");
+ String[] tokens = line.split("\\s+");
+ coords[Integer.parseInt(tokens[0]) - 1] = Coordinate.newInstance(Double.parseDouble(tokens[1]), Double.parseDouble(tokens[2]));
+ continue;
+ }
+ if(isDemandSection){
+ if(demands == null) throw new IllegalStateException("DIMENSION tag missing");
+ String[] tokens = line.split("\\s+");
+ demands[Integer.parseInt(tokens[0]) - 1] = Integer.parseInt(tokens[1]);
+ continue;
+ }
+ if(isDepotSection){
+ if(line.equals("-1")){
+ isDepotSection = false;
+ }
+ else{
+ depotIds.add(Integer.parseInt(line));
+ }
+ }
+ }
+ vrpBuilder.setFleetSize(VehicleRoutingProblem.FleetSize.INFINITE);
+ for(Integer depotId : depotIds){
+ VehicleTypeImpl type = VehicleTypeImpl.Builder.newInstance("typeId").addCapacityDimension(0,capacity).build();
+ VehicleImpl vehicle = VehicleImpl.Builder.newInstance("vehicle").setStartLocationId(depotId.toString())
+ .setStartLocationCoordinate(coords[depotId - 1]).setType(type).build();
+ vrpBuilder.addVehicle(vehicle);
+ }
+ for(int i=0;i