forked from thaleaschlender/GAME_AI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeNode.py
More file actions
245 lines (183 loc) · 7.73 KB
/
TreeNode.py
File metadata and controls
245 lines (183 loc) · 7.73 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import random
import math
from Player import Player
from Gamestate import Gamestate
import time
# Defines how deep the tree is searched
ROLLOUT_DEPTH = 70
# UCT formula parameter
K = math.sqrt(2)
REPEAT_ACTIONS = 1
HUGE_NEGATIVE = -100000
HUGE_POSITIVE = 100000
EPSILON = 1e-6
GREEDYEPSILON = 0.05
class TreeNode():
def __init__(self, parent, childIndex, actions, time):
self.actions = actions
self.index = childIndex
self.parent = parent
self.time = time
self.childs = [None] * len(actions)
self.totValue = 0.0
self.childIdx = childIndex
self.depth = 0 if parent == None else parent.depth + 1
self.visits = 0
self.bounds = [HUGE_POSITIVE, HUGE_NEGATIVE]
def setRootState(self, gameState):
self.rootState = gameState
# Perform MCTS
# If possible change to time limitation instead of fixed number of iterations.
def mcts(self):
startTime = time.time() * 1000
while (startTime + self.time > time.time() * 1000):
state = self.rootState.copy()
# Select next node
selected = self.treePolicy(state)
# Rollout from the selected node
delta = selected.rollOut(state)
# Back up the score
self.backUp(selected, delta)
# Find the child that was most visited
def mostVisited(self):
mostVisited = 0
mostVisitedChildIndex = 0
for childIndex ,child in enumerate(self.childs):
if (child == None) :
continue
if (child.visits > mostVisited):
mostVisited = child.visits
mostVisitedChildIndex = childIndex
# Advance the root state with the taken action
self.rootState.advance(self.actions[mostVisitedChildIndex])
# If the action kills don't move neither left or right
if (not self.rootState.gameover):
return self.actions[mostVisitedChildIndex]
return 0
# Find the child that has highest average score
def bestAction(self):
bestAction = HUGE_NEGATIVE * 10
bestActionChildIndex = 0
for childIndex ,child in enumerate(self.childs):
if (child == None) :
continue
childValue = child.totValue / (child.visits + EPSILON)
if (childValue > bestAction):
bestAction = childValue
bestActionChildIndex = childIndex
# Advance the root state with the taken action
self.rootState.advance(self.actions[bestActionChildIndex])
# If the action kills don't move neither left or right
if (not self.rootState.gameover):
return self.actions[bestActionChildIndex]
return 0
# Expand next node in the tree
def treePolicy(self, state):
cur = self
while (not state.gameover and cur.depth < ROLLOUT_DEPTH):
if (cur.notFullyExpanded()):
return cur.expand(state)
else:
nextNode = cur.uct(state)
cur = nextNode
return cur
# Rollout by choosing random actions until maximum depth has been reached or game is over
def rollOut(self, state):
currentDepth = self.depth
while (not self.finishRollout(state, currentDepth)):
actionIndex = random.randint(0, len(self.actions) - 1)
for i in range(REPEAT_ACTIONS):
if (state.gameover):
break
state.advance(self.actions[actionIndex])
currentDepth+=1
delta = self.value(state)
if (delta < self.bounds[0]):
self.bounds[0] = delta
if (delta > self.bounds[1]):
self.bounds[1] = delta
return delta
# Save the score back up the tree
def backUp(self, selectedNode, score):
node = selectedNode
while (node != None):
node.visits += 1
node.totValue += score
if (score < node.bounds[0]):
node.bounds[0] = score
if (score > node.bounds[1]):
node.bounds[1] = score
node = node.parent
# Find next node to explore
def uct(self, state):
selectedNode = None
bestValue = HUGE_NEGATIVE * 10
for child in self.childs:
childValue = child.totValue
averageChildValue = childValue / (child.visits + EPSILON)
normalisedValue = self.normalise(averageChildValue)
uctValue = normalisedValue + K * math.sqrt(math.log(self.visits + 1) / (child.visits + EPSILON))
uctValue += EPSILON * random.random()
if (uctValue > bestValue):
selectedNode = child
bestValue = uctValue
state.advance(self.actions[selectedNode.childIdx])
return selectedNode
# Choose the next action to perform from unvisited nodes.
def expand(self, state):
bestAction = 1
bestValue = -1
for i in range(len(self.actions)):
x = random.random()
if (x > bestValue and self.childs[i] == None):
bestAction = i
bestValue = x
state.advance(self.actions[bestAction])
# At root state branch into two childs, one which goes left the other goes right
if (self.parent == None):
tn = TreeNode(self, bestAction, [0,self.actions[bestAction]], self.time)
else:
tn = TreeNode(self, bestAction, self.actions, self.time)
self.childs[bestAction] = tn
return tn
# Check if current node has been fully expanded
def notFullyExpanded(self):
for child in self.childs:
if (child == None):
return True
return False
# Check if tree has reached maximum depth
def finishRollout(self, state, depth):
if (depth >= ROLLOUT_DEPTH):
return True
if (state.gameover):
return True
return False
# Get the value of the current state
def value(self, state):
if state.gameover:
return HUGE_NEGATIVE + state.get_score()
# Award bonus for going towards middle if the wide obstacle is far away or just passed
if (state.get_wide_obstacle()[0] < 0.33 * state.DISPLAYSURF.get_height() or
state.get_player()[1] > state.get_wide_obstacle()[0]):
return state.get_score() + self.awardCentralizedLocation(state)
return state.get_score()
# Award bonus for going towards middle of the game space
def awardCentralizedLocation(self, state):
# Get player position
playerX = state.get_player()[0]
# Get center position
centerX = state.DISPLAYSURF.get_width() / 2
# The maximum difference possible to center
maxDif = state.DISPLAYSURF.get_width() - centerX
# Calculate difference to center
centerDif = abs(centerX - playerX)
# Remove the current difference form max difference to award bonus for being closer to the center
addedWeight = 1 / 100 - centerDif / maxDif / 100
return addedWeight
# Normalise the given value
def normalise(self, score):
if (self.bounds[1] > self.bounds[0]):
return (score - self.bounds[0]) / (self.bounds[1] - self.bounds[0])
else:
return score