mirror of
https://github.com/graphhopper/jsprit.git
synced 2020-01-24 07:45:05 +01:00
update tsplibreader to actually read tsp
This commit is contained in:
parent
07ac961048
commit
16085ab97a
1 changed files with 29 additions and 9 deletions
|
|
@ -17,6 +17,7 @@
|
||||||
|
|
||||||
package jsprit.instance.reader;
|
package jsprit.instance.reader;
|
||||||
|
|
||||||
|
import jsprit.core.problem.Location;
|
||||||
import jsprit.core.problem.VehicleRoutingProblem;
|
import jsprit.core.problem.VehicleRoutingProblem;
|
||||||
import jsprit.core.problem.job.Service;
|
import jsprit.core.problem.job.Service;
|
||||||
import jsprit.core.problem.vehicle.VehicleImpl;
|
import jsprit.core.problem.vehicle.VehicleImpl;
|
||||||
|
|
@ -40,18 +41,23 @@ public class TSPLIB95Reader {
|
||||||
BufferedReader reader = getBufferedReader(filename);
|
BufferedReader reader = getBufferedReader(filename);
|
||||||
String line;
|
String line;
|
||||||
Coordinate[] coords = null;
|
Coordinate[] coords = null;
|
||||||
Integer[] demands = null;
|
int[] demands = null;
|
||||||
Integer capacity = null;
|
Integer capacity = null;
|
||||||
List<Integer> depotIds = new ArrayList<Integer>();
|
List<Integer> depotIds = new ArrayList<Integer>();
|
||||||
boolean isCoordSection = false;
|
boolean isCoordSection = false;
|
||||||
boolean isDemandSection = false;
|
boolean isDemandSection = false;
|
||||||
boolean isDepotSection = false;
|
boolean isDepotSection = false;
|
||||||
|
int dimensions = 0;
|
||||||
while( ( line = getLine(reader) ) != null ){
|
while( ( line = getLine(reader) ) != null ){
|
||||||
|
if(line.startsWith("EOF")){
|
||||||
|
break;
|
||||||
|
}
|
||||||
if(line.startsWith("DIMENSION")){
|
if(line.startsWith("DIMENSION")){
|
||||||
String[] tokens = line.split(":");
|
String[] tokens = line.split(":");
|
||||||
String dim = tokens[1].trim();
|
String dim = tokens[1].trim();
|
||||||
coords = new Coordinate[Integer.parseInt(dim)];
|
dimensions = Integer.parseInt(dim);
|
||||||
demands = new Integer[Integer.parseInt(dim)];
|
coords = new Coordinate[dimensions];
|
||||||
|
demands = new int[dimensions];
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if(line.startsWith("CAPACITY")){
|
if(line.startsWith("CAPACITY")){
|
||||||
|
|
@ -79,13 +85,13 @@ public class TSPLIB95Reader {
|
||||||
}
|
}
|
||||||
if(isCoordSection){
|
if(isCoordSection){
|
||||||
if(coords == null) throw new IllegalStateException("DIMENSION tag missing");
|
if(coords == null) throw new IllegalStateException("DIMENSION tag missing");
|
||||||
String[] tokens = line.split("\\s+");
|
String[] tokens = line.trim().split("\\s+");
|
||||||
coords[Integer.parseInt(tokens[0]) - 1] = Coordinate.newInstance(Double.parseDouble(tokens[1]), Double.parseDouble(tokens[2]));
|
coords[Integer.parseInt(tokens[0]) - 1] = Coordinate.newInstance(Double.parseDouble(tokens[1]), Double.parseDouble(tokens[2]));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if(isDemandSection){
|
if(isDemandSection){
|
||||||
if(demands == null) throw new IllegalStateException("DIMENSION tag missing");
|
if(demands == null) throw new IllegalStateException("DIMENSION tag missing");
|
||||||
String[] tokens = line.split("\\s+");
|
String[] tokens = line.trim().split("\\s+");
|
||||||
demands[Integer.parseInt(tokens[0]) - 1] = Integer.parseInt(tokens[1]);
|
demands[Integer.parseInt(tokens[0]) - 1] = Integer.parseInt(tokens[1]);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
@ -106,13 +112,27 @@ public class TSPLIB95Reader {
|
||||||
.setStartLocationCoordinate(coords[depotId - 1]).setType(type).build();
|
.setStartLocationCoordinate(coords[depotId - 1]).setType(type).build();
|
||||||
vrpBuilder.addVehicle(vehicle);
|
vrpBuilder.addVehicle(vehicle);
|
||||||
}
|
}
|
||||||
for(int i=0;i<demands.length;i++){
|
|
||||||
if(demands[i] == 0) continue;
|
for (int i = 0; i < coords.length; i++) {
|
||||||
String id = "" + (i+1);
|
String id = "" + (i + 1);
|
||||||
Service service = Service.Builder.newInstance(id).setLocationId(id).setCoord(coords[i]).addSizeDimension(0,demands[i]).build();
|
if(depotIds.isEmpty()){
|
||||||
|
if(i==0) {
|
||||||
|
VehicleImpl vehicle = VehicleImpl.Builder.newInstance("start")
|
||||||
|
.setStartLocation(Location.Builder.newInstance().setId(id)
|
||||||
|
.setCoordinate(coords[i]).build())
|
||||||
|
.build();
|
||||||
|
vrpBuilder.addVehicle(vehicle);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Service service = Service.Builder.newInstance(id)
|
||||||
|
.setLocation(Location.Builder.newInstance().setId(id).setCoordinate(coords[i]).build())
|
||||||
|
.addSizeDimension(0, demands[i]).build();
|
||||||
vrpBuilder.addJob(service);
|
vrpBuilder.addJob(service);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void close(BufferedReader reader) {
|
private void close(BufferedReader reader) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue