-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInstanceFile.java
More file actions
64 lines (52 loc) · 2.38 KB
/
Copy pathInstanceFile.java
File metadata and controls
64 lines (52 loc) · 2.38 KB
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
package cph.instance;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public record InstanceFile(String fileName, String name, String type) {
public Instance loadInstance() throws IOException, FormatException {
String resourcePath = "/instances/" + fileName;
InputStream inputStream = InstanceFile.class.getResourceAsStream(resourcePath);
if (inputStream == null) {
throw new IOException("Resource not found in the classpath: " + resourcePath);
}
return loadInstance(name, inputStream);
}
private Instance loadInstance(String name, InputStream inputStream) throws IOException, FormatException {
try (var br = new BufferedReader(new InputStreamReader(inputStream))) {
String line = br.readLine();
if (line == null) {
throw new FormatException("The file is empty");
}
StringTokenizer header = new StringTokenizer(line);
int numNodes = Integer.parseInt(header.nextToken());
if (header.countTokens() < 2) {
throw new FormatException("The first line must contain the number of nodes, "
+ "the number of hubs and the capacity of each hub");
}
int numHubs = Integer.parseInt(header.nextToken());
int hubCapacity = Integer.parseInt(header.nextToken());
int[][] nodeData = new int[numNodes][3];
line = br.readLine();
while (line != null && !line.isBlank()) {
StringTokenizer st = new StringTokenizer(line);
int node = Integer.parseInt(st.nextToken());
int x = Integer.parseInt(st.nextToken());
int y = Integer.parseInt(st.nextToken());
int demand = Integer.parseInt(st.nextToken());
nodeData[node][0] = x;
nodeData[node][1] = y;
nodeData[node][2] = demand;
line = br.readLine();
}
return new Instance(name, numNodes, numHubs, hubCapacity, nodeData);
} catch (NumberFormatException e) {
throw new FormatException("Invalid file format");
}
}
@Override
public String toString() {
return "Name: " + name + " FileName:" + fileName;
}
}