-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInputNetwork.java
More file actions
67 lines (57 loc) · 2.25 KB
/
InputNetwork.java
File metadata and controls
67 lines (57 loc) · 2.25 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
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class InputNetwork {
private List<Node> nodeList;
private ListGraph graph;
private int transmissionRange;
// This constructor takes in a transmission range & a network file and runs the file through a fileReader
public InputNetwork(String filename, int transmissionRange) throws FileNotFoundException {
this.transmissionRange = transmissionRange;
this.nodeList = new ArrayList<>();
readNetworkFromFile(filename);
createGraph();
}
// This is where the file gets read; splitting each line into four parts, nodeID, x, y, and packets
private void readNetworkFromFile(String filename) throws FileNotFoundException {
Scanner fileScanner = new Scanner(new File(filename));
while (fileScanner.hasNextLine()) {
String[] parts = fileScanner.nextLine().trim().split("\\s+");
if (parts.length == 4) {
int id = Integer.parseInt(parts[0]);
int x = Integer.parseInt(parts[1]);
int y = Integer.parseInt(parts[2]);
int packets = Integer.parseInt(parts[3]);
// Create a custom node with predefined coordinates and packets
Node node = new Node(id, x, y, packets);
nodeList.add(node);
}
}
fileScanner.close();
}
private void createGraph() {
graph = new ListGraph(nodeList.size());
// Add edges based on transmission range
for (int i = 0; i < nodeList.size(); i++) {
for (int j = i + 1; j < nodeList.size(); j++) {
if (nodeList.get(i).getDistance(nodeList.get(j)) <= transmissionRange) {
graph.addEdge(nodeList.get(i), nodeList.get(j));
}
}
}
// Calculate prizes for nodes
for (Node node : nodeList) {
int prize = graph.calculatePrize(node.getId(), true);
node.setPrize(prize);
}
}
// Getter Methods
public ListGraph getGraph() {
return graph;
}
public List<Node> getNodeList() {
return nodeList;
}
}