-
Notifications
You must be signed in to change notification settings - Fork 0
/
agents.py
212 lines (170 loc) · 6.84 KB
/
agents.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
import random
from heuristic import Heuristic
class SearchTimeout(Exception):
"""Subclass base exception for code clarity."""
pass
class MinimaxPlayer:
def __init__(self, search_depth=3, score_cls=Heuristic(), timeout=10.):
'''
Game-playing agent that chooses a move using minimax search.
You must finish and test this player to make sure it properly uses
minimax to return a good move before the search time limit expires.
Params
----------
search_depth : int (optional)
A strictly positive integer (i.e., 1, 2, 3,...) for the number of
layers in the game tree to explore for fixed-depth search. (i.e., a
depth of one (1) would only explore the immediate sucessors of the
current state.)
score_fn : callable (optional)
A function to use for heuristic evaluation of game states.
timeout : float (optional)
Time remaining (in milliseconds) when search is aborted. Should be a
positive value large enough to allow the function to return before the
timer expires.
'''
self.search_depth = search_depth
self.score = score_cls.get_score
self.TIMER_THRESHOLD = timeout
def search(self, game, time_left):
'''
Search for the best move from the available legal moves and return a
result before the time limit expires.
Parameters
----------
game : `connect4.Connect4`
An instance of `connect4.Connect4` encoding the current state of the game.
time_left : callable
A function that returns the number of milliseconds left in the
current turn. Returning with any less than 0 ms remaining forfeits
the game.
Returns
-------
int
Board row corresponding to a legal move; may return
-1 if there are no available legal moves.
'''
self.time_left = time_left
best_move = -1
try:
return self.minimax(game, self.search_depth)
except SearchTimeout:
print('timeout')
pass
return best_move
def minimax(self, game, depth):
'''
Implement minimax search algorithm as described in the workshop.
This should be a modified version of MINIMAX-DECISION in the AIMA text.
https://github.com/aimacode/aima-pseudocode/blob/master/md/Minimax-Decision.md
Parameters
----------
game : `connect4.Connect4`
An instance of `connect4.Connect4` encoding the current state of the game.
depth : int
Depth is an integer representing the maximum number of plies to
search in the game tree before aborting
Returns
-------
int
The board row of the best move found in the current search;
-1 if there no legal move is found
Notes
-----
(1) You MUST use the `self.score()` method for board evaluation
to participate in the tournament; you cannot call any other evaluation
function directly.
(2) If you use any helper functions (e.g., as shown in the AIMA pseudocode)
then you must copy the timer check into the top of each helper function
or else your agent will timeout while playing.
'''
if self.time_left() < self.TIMER_THRESHOLD:
raise SearchTimeout()
'''
TODO
----
Return the optimal move from the given game state.
'''
raise NotImplementedError
def terminal_state(self, game):
'''
Checks if the game has ended
Parameters
----------
game : `connect4.Connect4`
An instance of `connect4.Connect4` encoding the current state of the game.
'''
return not game.available_moves
def min_value(self, game, depth):
'''
Finds the lowest utility among all the posible actions from the given board
Parameters
----------
game : `connect4.Connect4`
An instance of `connect4.Connect4` encoding the current state of the game.
depth : int
Depth is an integer representing the maximum number of plies to
search in the game tree before aborting
Returns
-------
float
The lowest utility value found among all the actions for the given board state.
'''
'''
TODO
----
check if there is time left.
if depth == 0 return the utility of the current board.
if a terminal state is reached return the score.
Otherwise for each available action calculate the maximun utility value.
then return the lowest utility value found.
'''
raise NotImplementedError
def max_value(self, game, depth):
'''
Finds the highest utility among all the posible actions from the given board
Parameters
----------
game : `connect4.Connect4`
An instance of `connect4.Connect4` encoding the current state of the game.
depth : int
Depth is an integer representing the maximum number of plies to
search in the game tree before aborting
Returns
-------
float
The highest utility value found among all the actions for the given board state.
'''
'''
TODO
----
check if there is time left.
if depth == 0 return the utility of the current board.
if a terminal state is reached return the score.
Otherwise for each available action calculate the minimum utility value.
then return the highest utility value found.
'''
raise NotImplementedError
def minimax_search(self, game, depth):
'''
Finds the best action among all the posible actions from the given board
Parameters
----------
game : `connect4.Connect4`
An instance of `connect4.Connect4` encoding the current state of the game.
depth : int
Depth is an integer representing the maximum number of plies to
search in the game tree before aborting
Returns
-------
int
Board row corresponding to a legal move.
'''
'''
TODO
----
check if there is time left.
for each available action calculate the minimum utility value.
then return the highest utility value found.
'''
return -1