This repository has been archived by the owner on Dec 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCityDistCalc.java
113 lines (96 loc) · 3.1 KB
/
CityDistCalc.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import java.io.*;
import java.util.*;
import java.text.*;
import java.lang.Math;
import java.lang.Math;
public class CityDistCalc
{
public String name;
public String comment;
public String type;
public int dim;
public String edge_weight_type;
public double[][] cityCoords;
public double[][] cityDistArr;
public CityDistCalc(){
System.out.println("Calculating city dist");
// Reads in the input and saves it into the files
readInput("inputs/att48.tsp");
// Calculate and cache the distance between cities
double tempDbl;
for(int i = 0; i < dim; i++){
for(int j = 0; j < dim; j++){
// Euclidian btwn i and j
double x1 = cityCoords[i][0];
double y1 = cityCoords[i][1];
double x2 = cityCoords[j][0];
double y2 = cityCoords[j][1];
tempDbl = Math.sqrt(((x2-x1)*(x2-x1))+((y2-y1)*(y2-y1)));
// Caches for both i to j and j to i
cityDistArr[i][j] = tempDbl;
cityDistArr[j][i] = tempDbl;
}
}
}
// Returns cached distance between two cities
public double getCityDistance(int city1, int city2){
return cityDistArr[city1][city2];
}
public double getCityCoordsX(int city){
return cityCoords[city][0];
}
public double getCityCoordsY(int city){
return cityCoords[city][1];
}
private void readInput(String fileLoc){
try
{
File inputFile = new File(fileLoc);
Scanner inputReader = new Scanner(inputFile);
String data; //For temp saving in
String[] dataSplit; //for splitting to get only important info
// Saves name from file
data = inputReader.nextLine();
System.out.println(data);
dataSplit = data.split(":",0);
name = dataSplit[1];
// Saves Comment from file
data = inputReader.nextLine();
System.out.println(data);
dataSplit = data.split(":",0);
comment = dataSplit[1];
// Saves type from file
data = inputReader.nextLine();
System.out.println(data);
dataSplit = data.split(":",0);
type = dataSplit[1];
// Saves dimension number from file
data = inputReader.nextLine();
System.out.println(data);
dataSplit = data.split(" : ",0);
dim = Integer.parseInt(dataSplit[1]);
// Saves edge_weight_type from file
data = inputReader.nextLine();
System.out.println(data);
dataSplit = data.split(":",0);
edge_weight_type = dataSplit[1];
// Generate city array and distance array
cityCoords = new double[dim][2];
cityDistArr = new double[dim][dim];
// Reads in the coordinates
data = inputReader.nextLine(); //scans in next line, will be disregarded
data = inputReader.nextLine(); // Should be first coord
for(int i = 0; i < dim; i++){
System.out.println(data);
dataSplit = data.split(" ",0);
cityCoords[i][0] = Double.parseDouble(dataSplit[1]);
cityCoords[i][1] = Double.parseDouble(dataSplit[2]);
data = inputReader.nextLine();
}
}
catch (Exception e){
System.out.println("Error inputting file");
e.printStackTrace();
}
}
}