forked from udacity/AIND-Isolation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxtest.py
192 lines (165 loc) · 6.57 KB
/
xtest.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
from isolation import Board
from sample_players import RandomPlayer, GreedyPlayer, HumanPlayer
from visual_isolation import VisualBoard
from queue import Queue
from threading import Thread
from time import sleep
#import pygame
from xlogger import xlogger
from isolation import Board
from sample_players import RandomPlayer
from sample_players import null_score
from sample_players import open_move_score
from sample_players import improved_score
from game_agent import CustomPlayer
from game_agent import custom_score
# from game_agent import custom_score_x
from collections import namedtuple
import timeit
logger = xlogger()
Agent = namedtuple("Agent", ["player", "name"])
TIME_LIMIT = 150
HEURISTICS = [("Null", null_score),
("Open", open_move_score),
("Improved", improved_score)]
AB_ARGS = {"search_depth": 5, "method": 'alphabeta', "iterative": False}
MM_ARGS = {"search_depth": 3, "method": 'minimax', "iterative": False}
CUSTOM_ARGS = {"method": 'alphabeta', 'iterative': True}
# Create a collection of CPU agents using fixed-depth minimax or alpha beta
# search, or random selection. The agent names encode the search method
# (MM=minimax, AB=alpha-beta) and the heuristic function (Null=null_score,
# Open=open_move_score, Improved=improved_score). For example, MM_Open is
# an agent using minimax search with the open moves heuristic.
mm_agents = [Agent(CustomPlayer(score_fn=h, **MM_ARGS),
"MM_" + name) for name, h in HEURISTICS]
ab_agents = [Agent(CustomPlayer(score_fn=h, **AB_ARGS),
"AB_" + name) for name, h in HEURISTICS]
random_agents = [Agent(RandomPlayer(), "Random")]
# ID_Improved agent is used for comparison to the performance of the
# submitted agent for calibration on the performance across different
# systems; i.e., the performance of the student agent is considered
# relative to the performance of the ID_Improved agent to account for
# faster or slower computers.
# test_agents = [Agent(CustomPlayer(score_fn=improved_score, **CUSTOM_ARGS), "ID_Improved"),
# Agent(CustomPlayer(score_fn=custom_score, **CUSTOM_ARGS), "Student")]
#test_agents = [ Agent(CustomPlayer(score_fn=custom_score, **CUSTOM_ARGS), "Student"),
# Agent(CustomPlayer(score_fn=improved_score, **CUSTOM_ARGS), "ID_Improved")]
my_agent = Agent(CustomPlayer(score_fn=custom_score, **CUSTOM_ARGS), "Student")
adversary = Agent(RandomPlayer(), "Random")
# game = Board(my_agent.player, adversary.player,7,7)
"""
print(' ', end='')
for x in range(7):
print("%4d" % x, end='')
print()
for x in range(7):
print(x,' ', end='')
for y in range(7):
current_game = game.forecast_move((x,y))
# print(x,y, my_agent.player.score(current_game, my_agent.player), end=' ')
print(my_agent.player.score(current_game, my_agent.player), end=' ')
print()
"""
"""
game.__board_state__= [[4,0,0,0,4,0,0],
[0,0,4,0,4,4,4],
[0,4,4,4,4,1,0],
[0,4,4,4,4,4,0],
[4,4,4,4,0,0,4],
[4,0,4,4,4,0,0],
[4,0,4,2,0,0,0]]
game.__last_player_move__ = {game.__player_1__: (2,5), game.__player_2__: (6,3)}
"""
"""
game.__board_state__= [[4,4,4,1,4,0,4],
[4,4,4,4,4,0,0],
[4,4,4,4,0,4,0],
[4,4,4,4,0,0,0],
[0,4,4,0,4,4,4],
[4,0,4,4,0,0,4],
[4,0,0,0,4,2,0]]
game.__last_player_move__ = {game.__player_1__: (0,3), game.__player_2__: (6,5)}
"""
"""
game.__board_state__= [[4,4,4,4,4,4,0],
[4,4,4,4,0,4,4],
[4,4,4,4,4,0,4],
[0,4,2,0,4,4,4],
[4,4,4,4,4,4,4],
[4,0,0,4,4,0,0],
[0,4,4,4,0,4,4]]
game.__last_player_move__ = {game.__player_1__: (1,1), game.__player_2__: (3,2)}
"""
"""
game.__board_state__= [[4,0,0,4,4,0,0],
[0,4,4,4,0,4,4],
[0,0,0,1,4,0,0],
[4,0,4,0,4,0,0],
[0,0,0,0,0,0,4],
[0,2,0,0,0,0,0],
[0,0,0,0,0,4,0]]
game.__last_player_move__ = {game.__player_1__: (2,3), game.__player_2__: (5,1)}
"""
"""
game.__board_state__= [[4,0,0,4,4,0,0],
[0,4,4,4,0,4,4],
[0,0,0,4,4,0,0],
[4,0,4,0,4,0,0],
[0,0,4,4,0,0,4],
[0,4,0,0,1,0,0],
[0,0,0,0,2,4,0]]
game.__last_player_move__ = {game.__player_1__: (5,4), game.__player_2__: (6,4)}
"""
"""
game = Board(my_agent.player, adversary.player, 7, 7)
game.__board_state__= [[0,0,0,0,0,0,0],
[0,0,0,0,1,0,0],
[0,0,4,0,0,0,0],
[0,2,0,0,0,0,0],
[0,0,0,0,0,0,0],
[4,0,0,0,0,0,0],
[0,0,0,0,0,0,0]]
"""
"""
game = Board(my_agent.player, adversary.player, 7, 7)
game.__board_state__= [[0,0,0,0,4,4,0],
[0,0,4,4,2,0,0],
[0,4,4,4,4,1,4],
[0,4,4,4,4,0,0],
[0,4,4,4,4,4,0],
[0,0,0,4,4,0,0],
[4,0,0,0,0,0,4]]
game.__last_player_move__ = {game.__player_1__: (2,5), game.__player_2__: (1,4)}
print(game.to_string())
TIME_LIMIT = 20
curr_time_millis = lambda: 1000 * timeit.default_timer()
move_start = curr_time_millis()
time_left = lambda: TIME_LIMIT - (curr_time_millis() - move_start)
move = my_agent.player.get_move(game, game.get_legal_moves(my_agent.player),time_left)
# winner, history, outcome = game.play()
# print("\nWinner: {}\nOutcome: {}".format(winner, outcome))
# print(game.to_string())
# print(history)
print(move)
"""
again = True
while again:
game = Board(my_agent.player, adversary.player,7,7)
print(game.to_string())
curr_time_millis = lambda: 1000 * timeit.default_timer()
move_start = curr_time_millis()
time_left = lambda : TIME_LIMIT - (curr_time_millis() - move_start)
# move = my_agent.player.get_move(game, game.get_legal_moves(my_agent.player),time_left)
winner, history, outcome = game.play()
print("\nWinner: {}\nOutcome: {}".format(winner, outcome))
print(game.to_string())
print(history)
again = (winner == my_agent.player)
# """
# print(move)
# logger.set_context("context1")
# logger.debug("message 1")
# logger.push_context(" context_2")
# logger.debug("message_2")
# logger.pop_context()
# logger.debug("message 3")