From f6d06c0b4820a16c85f8a2cc71e30ba4186dc0d4 Mon Sep 17 00:00:00 2001 From: oblonski <4sschroeder@gmail.com> Date: Tue, 16 Sep 2014 10:28:05 +0200 Subject: [PATCH] add check to prevent SolomonReader to throw IndexOutOfBoundsException when reading file ending with empty lines --- .../jsprit/instance/reader/SolomonReader.java | 23 ++++++++----------- .../src/test/resources/C101_solomon.txt | 4 +++- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/jsprit-instances/src/main/java/jsprit/instance/reader/SolomonReader.java b/jsprit-instances/src/main/java/jsprit/instance/reader/SolomonReader.java index 11bdb5c6..2a8cff57 100644 --- a/jsprit-instances/src/main/java/jsprit/instance/reader/SolomonReader.java +++ b/jsprit-instances/src/main/java/jsprit/instance/reader/SolomonReader.java @@ -1,16 +1,16 @@ /******************************************************************************* - * Copyright (C) 2013 Stefan Schroeder - * + * 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 + * 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 . ******************************************************************************/ @@ -21,7 +21,6 @@ import jsprit.core.problem.VehicleRoutingProblem; import jsprit.core.problem.VehicleRoutingProblem.FleetSize; 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.problem.vehicle.VehicleTypeImpl; import jsprit.core.util.Coordinate; @@ -81,7 +80,7 @@ public class SolomonReader { int vehicleCapacity = 0; int counter = 0; - String line = null; + String line; while((line = readLine(reader)) != null){ line = line.replace("\r", ""); line = line.trim(); @@ -92,7 +91,8 @@ public class SolomonReader { continue; } if(counter > 9){ - Coordinate coord = makeCoord(tokens[1],tokens[2]); + if(tokens.length < 7) continue; + Coordinate coord = makeCoord(tokens[1],tokens[2]); String customerId = tokens[0]; int demand = Integer.parseInt(tokens[3]); double start = Double.parseDouble(tokens[4])*timeProjectionFactor; @@ -101,13 +101,10 @@ public class SolomonReader { if(counter == 10){ VehicleTypeImpl.Builder typeBuilder = VehicleTypeImpl.Builder.newInstance("solomonType").addCapacityDimension(0, vehicleCapacity); typeBuilder.setCostPerDistance(1.0*variableCostProjectionFactor).setFixedCost(fixedCostPerVehicle); - VehicleTypeImpl vehicleType = typeBuilder.build(); - - Vehicle vehicle = VehicleImpl.Builder.newInstance("solomonVehicle").setEarliestStart(start).setLatestArrival(end) + + VehicleImpl vehicle = VehicleImpl.Builder.newInstance("solomonVehicle").setEarliestStart(start).setLatestArrival(end) .setStartLocationId(customerId).setStartLocationCoordinate(coord).setType(vehicleType).build(); - -// vrpBuilder.addVehicleType(vehicleType); vrpBuilder.addVehicle(vehicle); } diff --git a/jsprit-instances/src/test/resources/C101_solomon.txt b/jsprit-instances/src/test/resources/C101_solomon.txt index ed6b1d7f..d7e6819e 100644 --- a/jsprit-instances/src/test/resources/C101_solomon.txt +++ b/jsprit-instances/src/test/resources/C101_solomon.txt @@ -107,4 +107,6 @@ CUST NO. XCOORD. YCOORD. DEMAND READY TIME DUE DATE SERVICE TIME 97 60 85 30 561 622 90 98 58 75 20 30 84 90 99 55 80 10 743 820 90 - 100 55 85 20 647 726 90 + 100 55 85 20 647 726 90 + +