-
Notifications
You must be signed in to change notification settings - Fork 0
/
ai.py
306 lines (251 loc) · 10.2 KB
/
ai.py
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
from time import time
from typing import List
import matplotlib.pyplot as plt
import numpy as np
from random import randrange, random, choice
from abc import ABC, abstractmethod
from game2048 import *
from monte_carlo_tree import *
import math
from util import *
class Agent(ABC):
DIRECTIONS = [Direction.UP, Direction.LEFT, Direction.RIGHT, Direction.DOWN]
@abstractmethod
def next_move(self, game: Game2048):
pass
def _type(self):
return self.__class__.__name__
def heuristic(self, game):
heuristic = 0
row = game.game_board[0]
for i in range(0, len(row) - 1):
if row[i] == 0:
continue
heuristic += math.log2(row[i]) * (5-i)
heuristic += (game.BOARD_SIZE ** 2 - game.get_num_tiles()) * math.log2(game.game_board[0][0] + 1)
return heuristic
class RandomAgent(Agent):
def next_move(self, game):
move = None
while not move or not game.is_valid_move(move):
move = self.DIRECTIONS[randrange(4)]
return move
class SmarterAgent(Agent):
def next_move(self, game):
for direction in self.DIRECTIONS:
if game.is_valid_move(direction):
return direction
class SearchAgent(Agent):
def __init__(self, default_search_depth = 3):
super()
self.default_search_depth = default_search_depth
def next_move(self, game):
_, move = self.multi_level_heuristic(game, 2)
return move
def multi_level_heuristic(self, game: Game2048, depth):
if depth == 0:
return 0, None
new_game = deepcopy(game)
max_heuristic, best_move = 0, None
for direction in self.DIRECTIONS:
if not new_game.is_valid_move(direction):
continue
curr_game = deepcopy(new_game)
curr_game.move(direction)
prev_heuristic = self.heuristic(curr_game)
heuristic, _ = self.multi_level_heuristic(curr_game, depth - 1)
if heuristic + prev_heuristic > max_heuristic:
max_heuristic, best_move = heuristic + prev_heuristic, direction
return max_heuristic, best_move
class AveragingSearchAgent(Agent):
def __init__(self, default_search_depth = 3):
super()
self.default_search_depth = default_search_depth
def next_move(self, game):
_, move = self.multi_level_heuristic(game, self.default_search_depth)
return move
def multi_level_heuristic(self, game: Game2048, depth):
if depth == 0:
return 0, None
new_game = deepcopy(game)
num_avg = 1
max_heuristic, best_move = 0, None
for direction in self.DIRECTIONS:
if not new_game.is_valid_move(direction):
continue
heuristics = []
for _ in range(num_avg):
curr_game = deepcopy(new_game)
curr_game.move(direction)
curr_heuristic = self.heuristic(curr_game)
heuristic, _ = self.multi_level_heuristic(curr_game, depth - 1)
heuristics.append(curr_heuristic + heuristic)
avg_heuristic = sum(heuristics) / num_avg
if avg_heuristic > max_heuristic:
max_heuristic, best_move = avg_heuristic, direction
return max_heuristic, best_move
class MonteCarloAgent(Agent):
policy = SmarterAgent()
def next_move(self, game):
num_avg = 10
best_average, best_move = 0, None
for direction in self.DIRECTIONS:
if not game.is_valid_move(direction):
continue
scores = []
for _ in range(num_avg):
new_game = deepcopy(game)
new_game.move(direction)
num_steps, max_steps = 0, 7
while not new_game.lose() and num_steps < max_steps:
new_game.move(self.policy.next_move(new_game))
num_steps += 1
scores.append(self.heuristic(new_game))
avg_score = sum(scores) / num_avg
if avg_score > best_average:
best_average, best_move = avg_score, direction
return best_move
class MonteCarloTreeSearchAgent(Agent):
def __init__(self, num_iterations = 5):
self.search_agent = SearchAgent(1)
self.random_agent = RandomAgent()
self.game_tree = None
self.num_iterations = num_iterations
self.memo = {}
def next_move(self, game):
if not self.game_tree:
self.game_tree = MaxNode(game = game)
self.game_tree.score += self.simulate(self.game_tree)
self.game_tree.num_simulations += 1
else:
self.game_tree = self.game_tree.children[hash_board(game.game_board)]
for i in range(self.num_iterations):
self.run_mcts_iteration(self.game_tree)
max_num_simulations, max_direction = 0, None
last_child = None
for child in self.game_tree.children.values():
if not game.is_valid_move(child.direction):
continue
if child.num_simulations > max_num_simulations:
max_num_simulations, max_direction = child.num_simulations, child.direction
last_child = child
self.game_tree = last_child
return max_direction
def run_mcts_iteration(self, root):
path = self.select(root)
leaf = path[-1]
# add child nodes to leaf, if nonterminal
if not leaf.game.lose():
leaf.add_children()
# run simulations on all children of node
for expectation_child in leaf.children.values():
expectation_child.add_children()
path.append(expectation_child)
for child in expectation_child.children.values():
result = self.simulate(child)
path.append(child)
# backpropagate results to every node in path
self.backpropagate(path, result)
path.pop()
path.pop()
else:
raise Exception(f"Game leaf {leaf} is unexpectedly losing")
def select(self, root):
def find_best_child(root):
max_policy, max_child = 0, None
C = 0.2
for child in root.children.values():
avg_score = child.avg_score() if isinstance(child, ExpectationNode) else 0.01 * random()
policy = avg_score + C * math.sqrt(math.log2(child.parent.num_simulations) / child.num_simulations)
if policy >= max_policy:
max_policy, max_child = policy, child
return max_child
# select until we reach a leaf
path = [root]
node = root
while node.children:
node = find_best_child(node)
path.append(node)
return path
def random_path(self, root):
path = [root]
node = root
while node.children:
node = choice([child for child in node.children.values()])
path.append(node)
return path
def simulate(self, root):
game_copy = deepcopy(root.game)
max_num_moves = 2
num_moves = 0
while not game_copy.lose() and num_moves < max_num_moves:
game_copy.move(self.search_agent.next_move(game_copy))
num_moves += 1
heuristic = self.heuristic(game_copy)
scaled = heuristic / ((math.log2(game_copy.tile_sum)) * 26 + 12)
if scaled > 1:
raise Exception(f"Scaled heuristic value {heuristic} exceeds 1")
return scaled
def backpropagate(self, path, result):
for node in reversed(path):
node.num_simulations += 1
if isinstance(node, MaxNode):
node.score += result
if (avg_node_score := node.score/node.num_simulations > 1):
raise Exception(f"Average node score {avg_node_score} exceeds 1")
class TestPerformance:
NUM_TRIALS = 50
def __init__(self, agents):
self.agents = agents
def evaluate_performance(self):
agent_scores, agent_maxes = [], []
for agent in self.agents:
scores, maxes = [], []
for _ in range(self.NUM_TRIALS):
game = Game2048()
while not game.lose():
next_move = agent.next_move(game)
game.move(next_move)
score, max_num = game.score, int(math.log2(game.get_max_num()))
scores.append(score)
maxes.append(max_num)
agent_scores.append(scores)
agent_maxes.append(maxes)
self.plot_results(agent_maxes, self.agents)
return sum(scores) / self.NUM_TRIALS, sum(maxes) / self.NUM_TRIALS, self.percent_winning(maxes)
def percent_winning(self, maxes):
return len([max for max in maxes if max >= 11]) / len(maxes)
def plot_results(self, agent_results, agents):
for index, results in enumerate(agent_results):
score_range = range(min(results), max(results) + 1)
score_freqs = [results.count(score) for score in score_range]
scores = list(score_range)
plt.plot(scores, score_freqs)
plt.legend([agent._type() for agent in agents])
plt.show()
def play_ai_game(agent):
game = Game2048()
pygame.init()
window_size = 600
size = window_size, window_size
screen = pygame.display.set_mode(size)
draw_game(game, window_size, screen)
num_moves = 0
start_time = time()
while True:
move = None
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
if not game.lose():
move = agent.next_move(game)
num_moves += 1
if isinstance(move, Direction) and game.is_valid_move(move):
if game.move(move):
draw_game(game, window_size, screen, avg_move_time = (time() - start_time) / num_moves)
def main():
# agent = TestPerformance([AveragingSearchAgent(), MonteCarloAgent()])
# print(agent.evaluate_performance())
play_ai_game(MonteCarloTreeSearchAgent())
if __name__ == "__main__":
main()