-
Notifications
You must be signed in to change notification settings - Fork 0
/
Node.java
128 lines (115 loc) · 3.22 KB
/
Node.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package grah8384;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;import java.util.UUID;
import spacesettlers.objects.Asteroid;
import spacesettlers.objects.Base;
import spacesettlers.objects.Beacon;
import spacesettlers.objects.Ship;
public class Node implements Comparable<Node> {
StateRepresentation state;
GoToAction action;
HashSet<Node> possibleActions;
Node parent;
Ship ship;
double pathCost;
int resourcesCollected;
public Node(StateRepresentation state, Ship ship) {
this.state = state;
action = null;
possibleActions = null;
parent = null;
pathCost = 0;
resourcesCollected = 0;
this.ship = ship;
}
public HashSet<Node> explore(HashSet<UUID> shipToObjectMap) {
GoToAction action;
possibleActions = new HashSet<Node>();
for (Beacon b : state.beacons) {
if (shipToObjectMap.contains(b.getId())) continue;
action = new GoToBeaconAction(ship, b);
if (action.isApplicable(state)) {
possibleActions.add(createChild(action));
}
}
for (Base b : state.bases) {
action = new GoToBaseAction(ship, b);
if (action.isApplicable(state)) {
possibleActions.add(createChild(action));
}
}
for (Asteroid a : state.asteroids) {
if (shipToObjectMap.contains(a.getId())) continue;
action = new GoToAsteroidAction(ship, a);
if (action.isApplicable(state)) {
possibleActions.add(createChild(action));
}
}
return possibleActions;
}
public HashSet<Node> explore() {
GoToAction action;
possibleActions = new HashSet<Node>();
for (Beacon b : state.beacons) {
action = new GoToBeaconAction(ship, b);
if (action.isApplicable(state)) {
possibleActions.add(createChild(action));
}
}
for (Base b : state.bases) {
action = new GoToBaseAction(ship, b);
if (action.isApplicable(state)) {
possibleActions.add(createChild(action));
}
}
for (Asteroid a : state.asteroids) {
action = new GoToAsteroidAction(ship, a);
if (action.isApplicable(state)) {
possibleActions.add(createChild(action));
}
}
return possibleActions;
}
public Node createChild(GoToAction action) {
Node result = new Node(state, ship);
result.action = action;
result.parent = this;
result.state = action.effects(state);
result.pathCost = this.pathCost + action.getPathCost(state);
result.resourcesCollected = this.resourcesCollected + action.getResources().getTotal();
return result;
}
public double evaluate() {
return pathCost;
}
@Override
public int compareTo(Node o) {
return Double.compare(this.evaluate(), o.evaluate());
}
public LinkedList<Node> getPath() {
Node current = this;
LinkedList<Node> result = new LinkedList<Node>();
while (current.action != null) {
result.addFirst(current);
current = current.parent;
}
if (current != null) {
result.addFirst(current);
}
return result;
}
public String toString() {
String result = "";
if (action instanceof GoToAsteroidAction) {
result += "Go to asteroid. ";
} else if (action instanceof GoToBeaconAction) {
result += "Go to beacon. ";
} else if (action instanceof GoToBaseAction) {
result += "Go to base. ";
}
result += "Path cost: " + pathCost;
result += ". Resources collected: " + resourcesCollected;
return result;
}
}