mirror of
https://github.com/graphhopper/jsprit.git
synced 2020-01-24 07:45:05 +01:00
comply with .editorconfig
This commit is contained in:
parent
58afc3590d
commit
33075b479f
416 changed files with 29653 additions and 29979 deletions
|
|
@ -49,7 +49,7 @@ public class TSPLIB95Reader {
|
|||
this.vrpBuilder = vrpBuilder;
|
||||
}
|
||||
|
||||
public void read(String filename){
|
||||
public void read(String filename) {
|
||||
BufferedReader reader = getBufferedReader(filename);
|
||||
String line_;
|
||||
Coordinate[] coords = null;
|
||||
|
|
@ -65,13 +65,13 @@ public class TSPLIB95Reader {
|
|||
List<Double> edgeWeights = new ArrayList<Double>();
|
||||
int dimensions = 0;
|
||||
int coordIndex = 0;
|
||||
Map<Integer,Integer> indexMap = new HashMap<Integer, Integer>();
|
||||
while( ( line_ = getLine(reader) ) != null ){
|
||||
Map<Integer, Integer> indexMap = new HashMap<Integer, Integer>();
|
||||
while ((line_ = getLine(reader)) != null) {
|
||||
String line = line_.trim();
|
||||
if(line.startsWith("EOF") || line.contains("EOF")){
|
||||
if (line.startsWith("EOF") || line.contains("EOF")) {
|
||||
break;
|
||||
}
|
||||
if(line.startsWith("DIMENSION")){
|
||||
if (line.startsWith("DIMENSION")) {
|
||||
String[] tokens = line.split(":");
|
||||
String dim = tokens[1].trim();
|
||||
dimensions = Integer.parseInt(dim);
|
||||
|
|
@ -79,86 +79,85 @@ public class TSPLIB95Reader {
|
|||
demands = new int[dimensions];
|
||||
continue;
|
||||
}
|
||||
if(line.startsWith("CAPACITY")){
|
||||
if (line.startsWith("CAPACITY")) {
|
||||
String[] tokens = line.trim().split(":");
|
||||
capacity = Integer.parseInt(tokens[1].trim());
|
||||
continue;
|
||||
}
|
||||
if(line.startsWith("EDGE_WEIGHT_TYPE")){
|
||||
if (line.startsWith("EDGE_WEIGHT_TYPE")) {
|
||||
String[] tokens = line.trim().split(":");
|
||||
edgeType = tokens[1].trim();
|
||||
continue;
|
||||
}
|
||||
if(line.startsWith("EDGE_WEIGHT_FORMAT")){
|
||||
if (line.startsWith("EDGE_WEIGHT_FORMAT")) {
|
||||
String[] tokens = line.trim().split(":");
|
||||
edgeWeightFormat = tokens[1].trim();
|
||||
continue;
|
||||
}
|
||||
if(line.startsWith("NODE_COORD_SECTION")){
|
||||
if (line.startsWith("NODE_COORD_SECTION")) {
|
||||
isCoordSection = true;
|
||||
isDemandSection = false;
|
||||
isDepotSection = false;
|
||||
isEdgeWeightSection = false;
|
||||
continue;
|
||||
}
|
||||
if(line.startsWith("DEMAND_SECTION")){
|
||||
if (line.startsWith("DEMAND_SECTION")) {
|
||||
isDemandSection = true;
|
||||
isCoordSection = false;
|
||||
isDepotSection = false;
|
||||
isEdgeWeightSection = false;
|
||||
continue;
|
||||
}
|
||||
if(line.startsWith("DEPOT_SECTION")){
|
||||
if (line.startsWith("DEPOT_SECTION")) {
|
||||
isDepotSection = true;
|
||||
isDemandSection = false;
|
||||
isCoordSection = false;
|
||||
isEdgeWeightSection = false;
|
||||
continue;
|
||||
}
|
||||
if(line.startsWith("EDGE_WEIGHT_SECTION")){
|
||||
if (line.startsWith("EDGE_WEIGHT_SECTION")) {
|
||||
isDepotSection = false;
|
||||
isCoordSection = false;
|
||||
isDemandSection = false;
|
||||
isEdgeWeightSection = true;
|
||||
continue;
|
||||
}
|
||||
if(line.startsWith("DISPLAY_DATA_SECTION")){
|
||||
if (line.startsWith("DISPLAY_DATA_SECTION")) {
|
||||
isDepotSection = false;
|
||||
isCoordSection = true;
|
||||
isDemandSection = false;
|
||||
isEdgeWeightSection = false;
|
||||
continue;
|
||||
}
|
||||
if(isCoordSection){
|
||||
if(coords == null) throw new IllegalStateException("DIMENSION tag missing");
|
||||
if (isCoordSection) {
|
||||
if (coords == null) throw new IllegalStateException("DIMENSION tag missing");
|
||||
String[] tokens = line.trim().split("\\s+");
|
||||
Integer id = Integer.parseInt(tokens[0]);
|
||||
if(switchCoordinates){
|
||||
if (switchCoordinates) {
|
||||
coords[coordIndex] = Coordinate.newInstance(Double.parseDouble(tokens[2]), Double.parseDouble(tokens[1]));
|
||||
}
|
||||
else coords[coordIndex] = Coordinate.newInstance(Double.parseDouble(tokens[1]), Double.parseDouble(tokens[2]));
|
||||
indexMap.put(id,coordIndex);
|
||||
} else
|
||||
coords[coordIndex] = Coordinate.newInstance(Double.parseDouble(tokens[1]), Double.parseDouble(tokens[2]));
|
||||
indexMap.put(id, coordIndex);
|
||||
coordIndex++;
|
||||
continue;
|
||||
}
|
||||
if(isDemandSection){
|
||||
if(demands == null) throw new IllegalStateException("DIMENSION tag missing");
|
||||
if (isDemandSection) {
|
||||
if (demands == null) throw new IllegalStateException("DIMENSION tag missing");
|
||||
String[] tokens = line.trim().split("\\s+");
|
||||
Integer id = Integer.parseInt(tokens[0]);
|
||||
int index = indexMap.get(id);
|
||||
demands[index] = Integer.parseInt(tokens[1]);
|
||||
continue;
|
||||
}
|
||||
if(isDepotSection){
|
||||
if(line.equals("-1")){
|
||||
if (isDepotSection) {
|
||||
if (line.equals("-1")) {
|
||||
isDepotSection = false;
|
||||
}
|
||||
else{
|
||||
} else {
|
||||
depotIds.add(Integer.parseInt(line));
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if(isEdgeWeightSection){
|
||||
if (isEdgeWeightSection) {
|
||||
String[] tokens = line.trim().split("\\s+");
|
||||
for (String s : tokens) edgeWeights.add(Double.parseDouble(s));
|
||||
continue;
|
||||
|
|
@ -166,113 +165,108 @@ public class TSPLIB95Reader {
|
|||
}
|
||||
close(reader);
|
||||
vrpBuilder.setFleetSize(VehicleRoutingProblem.FleetSize.FINITE);
|
||||
for(Integer depotId : depotIds){
|
||||
VehicleTypeImpl type = VehicleTypeImpl.Builder.newInstance("type").addCapacityDimension(0,capacity).build();
|
||||
for (Integer depotId : depotIds) {
|
||||
VehicleTypeImpl type = VehicleTypeImpl.Builder.newInstance("type").addCapacityDimension(0, capacity).build();
|
||||
VehicleImpl vehicle = VehicleImpl.Builder.newInstance("vehicle")
|
||||
.setStartLocation(Location.Builder.newInstance().setId(depotId.toString()).setCoordinate(coords[depotId - 1]).build())
|
||||
.setType(type).build();
|
||||
.setStartLocation(Location.Builder.newInstance().setId(depotId.toString()).setCoordinate(coords[depotId - 1]).build())
|
||||
.setType(type).build();
|
||||
vrpBuilder.addVehicle(vehicle);
|
||||
}
|
||||
|
||||
for (Integer id_ : indexMap.keySet()) {
|
||||
String id = id_.toString();
|
||||
int index = indexMap.get(id_);
|
||||
if(depotIds.isEmpty()){
|
||||
if(index == 0) {
|
||||
if (depotIds.isEmpty()) {
|
||||
if (index == 0) {
|
||||
VehicleImpl vehicle = VehicleImpl.Builder.newInstance("traveling_salesman")
|
||||
.setStartLocation(Location.Builder.newInstance().setId(id)
|
||||
.setCoordinate(coords[index]).setIndex(index).build())
|
||||
.build();
|
||||
.setStartLocation(Location.Builder.newInstance().setId(id)
|
||||
.setCoordinate(coords[index]).setIndex(index).build())
|
||||
.build();
|
||||
vrpBuilder.addVehicle(vehicle);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
Service service = Service.Builder.newInstance(id)
|
||||
.setLocation(Location.Builder.newInstance().setId(id)
|
||||
.setCoordinate(coords[index]).setIndex(index).build())
|
||||
.addSizeDimension(0, demands[index]).build();
|
||||
.setLocation(Location.Builder.newInstance().setId(id)
|
||||
.setCoordinate(coords[index]).setIndex(index).build())
|
||||
.addSizeDimension(0, demands[index]).build();
|
||||
vrpBuilder.addJob(service);
|
||||
}
|
||||
if(edgeType.equals("GEO")){
|
||||
if (edgeType.equals("GEO")) {
|
||||
List<Location> locations = new ArrayList<Location>();
|
||||
for(Vehicle v : vrpBuilder.getAddedVehicles()) locations.add(v.getStartLocation());
|
||||
for(Job j : vrpBuilder.getAddedJobs()) locations.add(((Service)j).getLocation());
|
||||
for (Vehicle v : vrpBuilder.getAddedVehicles()) locations.add(v.getStartLocation());
|
||||
for (Job j : vrpBuilder.getAddedJobs()) locations.add(((Service) j).getLocation());
|
||||
vrpBuilder.setRoutingCost(getGEOMatrix(locations));
|
||||
}
|
||||
else if(edgeType.equals("EXPLICIT")){
|
||||
if(edgeWeightFormat.equals("UPPER_ROW")){
|
||||
FastVehicleRoutingTransportCostsMatrix.Builder matrixBuilder = FastVehicleRoutingTransportCostsMatrix.Builder.newInstance(dimensions,true);
|
||||
} else if (edgeType.equals("EXPLICIT")) {
|
||||
if (edgeWeightFormat.equals("UPPER_ROW")) {
|
||||
FastVehicleRoutingTransportCostsMatrix.Builder matrixBuilder = FastVehicleRoutingTransportCostsMatrix.Builder.newInstance(dimensions, true);
|
||||
int fromIndex = 0;
|
||||
int toIndex = 1;
|
||||
for(int i=0;i<edgeWeights.size();i++){
|
||||
if(toIndex == dimensions){
|
||||
for (int i = 0; i < edgeWeights.size(); i++) {
|
||||
if (toIndex == dimensions) {
|
||||
fromIndex++;
|
||||
toIndex = fromIndex + 1;
|
||||
}
|
||||
matrixBuilder.addTransportDistance(fromIndex, toIndex, edgeWeights.get(i));
|
||||
matrixBuilder.addTransportTime(fromIndex,toIndex,edgeWeights.get(i));
|
||||
matrixBuilder.addTransportTime(fromIndex, toIndex, edgeWeights.get(i));
|
||||
toIndex++;
|
||||
}
|
||||
vrpBuilder.setRoutingCost(matrixBuilder.build());
|
||||
}
|
||||
else if(edgeWeightFormat.equals("UPPER_DIAG_ROW")){
|
||||
FastVehicleRoutingTransportCostsMatrix.Builder matrixBuilder = FastVehicleRoutingTransportCostsMatrix.Builder.newInstance(dimensions,true);
|
||||
} else if (edgeWeightFormat.equals("UPPER_DIAG_ROW")) {
|
||||
FastVehicleRoutingTransportCostsMatrix.Builder matrixBuilder = FastVehicleRoutingTransportCostsMatrix.Builder.newInstance(dimensions, true);
|
||||
int fromIndex = 0;
|
||||
int toIndex = 0;
|
||||
for(int i=0;i<edgeWeights.size();i++){
|
||||
if(toIndex == dimensions){
|
||||
for (int i = 0; i < edgeWeights.size(); i++) {
|
||||
if (toIndex == dimensions) {
|
||||
fromIndex++;
|
||||
toIndex = fromIndex;
|
||||
}
|
||||
matrixBuilder.addTransportDistance(fromIndex,toIndex,edgeWeights.get(i));
|
||||
matrixBuilder.addTransportTime(fromIndex,toIndex,edgeWeights.get(i));
|
||||
matrixBuilder.addTransportDistance(fromIndex, toIndex, edgeWeights.get(i));
|
||||
matrixBuilder.addTransportTime(fromIndex, toIndex, edgeWeights.get(i));
|
||||
toIndex++;
|
||||
}
|
||||
vrpBuilder.setRoutingCost(matrixBuilder.build());
|
||||
}
|
||||
else if(edgeWeightFormat.equals("LOWER_DIAG_ROW")){
|
||||
FastVehicleRoutingTransportCostsMatrix.Builder matrixBuilder = FastVehicleRoutingTransportCostsMatrix.Builder.newInstance(dimensions,true);
|
||||
} else if (edgeWeightFormat.equals("LOWER_DIAG_ROW")) {
|
||||
FastVehicleRoutingTransportCostsMatrix.Builder matrixBuilder = FastVehicleRoutingTransportCostsMatrix.Builder.newInstance(dimensions, true);
|
||||
int fromIndex = 0;
|
||||
int toIndex = 0;
|
||||
for(int i=0;i<edgeWeights.size();i++){
|
||||
if(toIndex > fromIndex){
|
||||
for (int i = 0; i < edgeWeights.size(); i++) {
|
||||
if (toIndex > fromIndex) {
|
||||
fromIndex++;
|
||||
toIndex = 0;
|
||||
}
|
||||
matrixBuilder.addTransportDistance(fromIndex,toIndex,edgeWeights.get(i));
|
||||
matrixBuilder.addTransportTime(fromIndex,toIndex,edgeWeights.get(i));
|
||||
matrixBuilder.addTransportDistance(fromIndex, toIndex, edgeWeights.get(i));
|
||||
matrixBuilder.addTransportTime(fromIndex, toIndex, edgeWeights.get(i));
|
||||
toIndex++;
|
||||
}
|
||||
vrpBuilder.setRoutingCost(matrixBuilder.build());
|
||||
}
|
||||
else if(edgeWeightFormat.equals("FULL_MATRIX")){
|
||||
FastVehicleRoutingTransportCostsMatrix.Builder matrixBuilder = FastVehicleRoutingTransportCostsMatrix.Builder.newInstance(dimensions,false);
|
||||
} else if (edgeWeightFormat.equals("FULL_MATRIX")) {
|
||||
FastVehicleRoutingTransportCostsMatrix.Builder matrixBuilder = FastVehicleRoutingTransportCostsMatrix.Builder.newInstance(dimensions, false);
|
||||
int fromIndex = 0;
|
||||
int toIndex = 0;
|
||||
for(int i=0;i<edgeWeights.size();i++){
|
||||
if(toIndex == dimensions){
|
||||
for (int i = 0; i < edgeWeights.size(); i++) {
|
||||
if (toIndex == dimensions) {
|
||||
fromIndex++;
|
||||
toIndex = 0;
|
||||
}
|
||||
matrixBuilder.addTransportDistance(fromIndex,toIndex,edgeWeights.get(i));
|
||||
matrixBuilder.addTransportTime(fromIndex,toIndex,edgeWeights.get(i));
|
||||
matrixBuilder.addTransportDistance(fromIndex, toIndex, edgeWeights.get(i));
|
||||
matrixBuilder.addTransportTime(fromIndex, toIndex, edgeWeights.get(i));
|
||||
toIndex++;
|
||||
}
|
||||
vrpBuilder.setRoutingCost(matrixBuilder.build());
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
private VehicleRoutingTransportCosts getGEOMatrix(List<Location> noLocations) {
|
||||
FastVehicleRoutingTransportCostsMatrix.Builder matrixBuilder = FastVehicleRoutingTransportCostsMatrix.Builder.newInstance(noLocations.size(),true);
|
||||
for(Location i : noLocations){
|
||||
for(Location j : noLocations){
|
||||
matrixBuilder.addTransportDistance(i.getIndex(),j.getIndex(),getDistance(i,j));
|
||||
FastVehicleRoutingTransportCostsMatrix.Builder matrixBuilder = FastVehicleRoutingTransportCostsMatrix.Builder.newInstance(noLocations.size(), true);
|
||||
for (Location i : noLocations) {
|
||||
for (Location j : noLocations) {
|
||||
matrixBuilder.addTransportDistance(i.getIndex(), j.getIndex(), getDistance(i, j));
|
||||
matrixBuilder.addTransportTime(i.getIndex(), j.getIndex(), getDistance(i, j));
|
||||
}
|
||||
}
|
||||
|
|
@ -284,10 +278,10 @@ public class TSPLIB95Reader {
|
|||
double longitude_to = getLongitude(to);
|
||||
double latitude_from = getLatitude(from);
|
||||
double latitude_to = getLatitude(to);
|
||||
double q1 = Math.cos( longitude_from - longitude_to);
|
||||
double q2 = Math.cos( latitude_from - latitude_to);
|
||||
double q3 = Math.cos( latitude_from + latitude_to);
|
||||
return 6378.388 * Math.acos( .5 * ( ( 1. + q1 ) * q2 - ( 1. - q1 ) * q3 ) ) + 1.;
|
||||
double q1 = Math.cos(longitude_from - longitude_to);
|
||||
double q2 = Math.cos(latitude_from - latitude_to);
|
||||
double q3 = Math.cos(latitude_from + latitude_to);
|
||||
return 6378.388 * Math.acos(.5 * ((1. + q1) * q2 - (1. - q1) * q3)) + 1.;
|
||||
}
|
||||
|
||||
private double getLatitude(Location loc) {
|
||||
|
|
@ -303,7 +297,6 @@ public class TSPLIB95Reader {
|
|||
}
|
||||
|
||||
|
||||
|
||||
private void close(BufferedReader reader) {
|
||||
try {
|
||||
reader.close();
|
||||
|
|
@ -316,7 +309,7 @@ public class TSPLIB95Reader {
|
|||
private String getLine(BufferedReader reader) {
|
||||
String s = null;
|
||||
try {
|
||||
s = reader.readLine();
|
||||
s = reader.readLine();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue