-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNode.java
More file actions
119 lines (99 loc) · 3.01 KB
/
Node.java
File metadata and controls
119 lines (99 loc) · 3.01 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import java.util.Random;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Node {
private int x;
private int y;
private int id;
private int numPackets;
private int minPackets;
private int maxPackets;
private static Map<Integer, Node> nodeRegistry= new HashMap<>();
private static Random rand = new Random();
private int prize;
private List<Node> network = new ArrayList<>();
private double PCR = 0.0; // Prize/Cost Ratio
// Constructor for initial depot
public Node(){
this.id = 0;
this.x = 0;
this.y = 0;
}
// Constructor used by automatedSetup, where nodes are randomly placed and assigned packets
public Node(int id, int maxWidth, int maxLength, int minPackets, int maxPackets) {
this.id = id;
this.x = rand.nextInt(maxWidth + 1); // random number from 0 to maxWidth-1
this.y = rand.nextInt(maxLength + 1); // random number from 0 to maxLength-1
this.numPackets = rand.nextInt(maxPackets - minPackets +1) + minPackets;
registerNode(this);
}
// Constructor used by inputNetwork, where placement and packets are predefined
public Node(int id, int x, int y, int packets){
this.id = id;
this.x = x;
this.y = y;
this.numPackets = packets;
registerNode(this);
}
//This method takes the network of a node (visited by robot) and drains the network of packets
public void drainNetwork(){
this.drainPackets();
for(Node node : this.network){
node.drainPackets();
}
this.prize = 0;
}
// Getter methods
public List<Node> getNetwork(){
return network;
}
public double getPCR(){
return PCR;
}
public int getX(){
return x;
}
public int getY(){
return y;
}
public int getId(){
return id;
}
public int getPackets(){
return numPackets;
}
public int getPrize(){
return prize;
}
public double getDistance(Node other) {
int dx = this.x - other.x;
int dy = this.y - other.y;
return Math.sqrt(dx*dx + dy*dy);
}
public static Node getNodeById(int id){
return nodeRegistry.get(id);
}
// Setter Methods
public void setPCR(double PCR){
this.PCR = PCR;
}
public void setPrize(int prize){
this.prize = prize;
}
public void drainPackets() {
numPackets = 0;
}
public static void registerNode(Node node){
nodeRegistry.put(node.getId(), node);
}
public void addToNetwork(Node neighbor){
this.network.add(neighbor);
}
// Cleaning up output for node variables
@Override
public String toString() {
return String.format("Node #%d (%d,%d) %d packets, %d prize, & %f PCR", this.getId(), this.getX(), this.getY(), this.numPackets, this.getPrize(), this.getPCR());
}
}