Repository navigation
Expand file tree
/
Copy pathCPHInstance.java
More file actions
99 lines (80 loc) · 3.07 KB
/
Copy pathCPHInstance.java
File metadata and controls
99 lines (80 loc) · 3.07 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
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
package cph.instance;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import jmh.instance.FormatException;
import jmh.instance.Instance;
/**
* A Capacitated p-hub instance: nodes with (x, y) coordinates and a demand, a
* number of hubs to open and the capacity of each hub.
*
* <p>Extends the generic {@link Instance} with the problem-specific data,
* accessors, description and file parser. Node indices are 0-based, matching
* the framework's default index mapping.
*/
public class CPHInstance extends Instance<CPHNode> {
private final int numHubs;
private final int hubCapacity;
private final int[][] nodeData;
public CPHInstance(String name, int numNodes, int numHubs, int hubCapacity, int[][] nodeData) {
super(name);
this.numHubs = numHubs;
this.hubCapacity = hubCapacity;
this.nodeData = nodeData;
List<CPHNode> nodes = new ArrayList<>();
for (int i = 0; i < numNodes; i++) {
nodes.add(new CPHNode(i, this));
}
setNodes(nodes);
}
public int getX(int index) {
return nodeData[index][0];
}
public int getY(int index) {
return nodeData[index][1];
}
public int getDemand(int index) {
return nodeData[index][2];
}
public int getNumHubs() {
return numHubs;
}
public int getHubCapacity() {
return hubCapacity;
}
@Override
public String getDescription() {
return getName() + " (n=" + getNumNodes() + ", p=" + numHubs + ", c=" + hubCapacity + ")";
}
/** Reads the CPH file format: header {@code numNodes numHubs hubCapacity}, then {@code node x y demand} lines. */
public static CPHInstance parse(String name, BufferedReader br) throws IOException, FormatException {
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 CPHInstance(name, numNodes, numHubs, hubCapacity, nodeData);
}
}