-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent.py
282 lines (234 loc) · 9.55 KB
/
agent.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
# Author: Bishal Sarang
"""
This file contains implementation of AI agent that uses minimax algorithm to find the best possible move
"""
from collections import namedtuple
from utilities import x_not_equals_y_operations, x_equals_y_operations, is_inside_board, is_game_over
import random
from datetime import datetime
from copy import deepcopy
Move = namedtuple("Move", "type frm to inter")
INF = float("inf")
class Agent(object):
def __init__(self, board, turn,goats_in_hand, dead_goats, depth=5):
self.board = board
self.depth = depth
self.best_move = None
self.winner = None
self.turn = turn
self.goats_in_hand = goats_in_hand
self.dead_goats = dead_goats
def is_vacant(self, i, j):
return self.board[i][j] == '_'
def number_of_movable_tigers(self):
cnt = 0
for i in range(5):
for j in range(5):
operations = x_not_equals_y_operations
if i % 2 == j % 2:
operations = x_equals_y_operations
if self.board[i][j] == 'T':
for offset_x, offset_y in operations:
next_i, next_j = i + offset_x, j + offset_y
if is_inside_board(next_i, next_j) and self.is_vacant(next_i, next_j):
cnt += 1
break
assert(cnt <= 4)
return cnt
def number_of_closed_spaces(self):
cnt = 0
for i in range(5):
for j in range(5):
if self.board[i][j] == '_':
cnt += 1
return cnt
def evaluate(self, depth=0):
winner = is_game_over(self.board, self.dead_goats)
if not winner[0]:
random.seed(datetime.now())
return 5 * self.number_of_movable_tigers() + 50 * self.dead_goats + 15 * self.number_of_goats_that_can_be_captured() - random.random()
# return depth
if winner[1] == "Goat":
return -INF
elif winner[1] == "Tiger":
return INF
def place_goats(self):
moves = []
for i in range(5):
for j in range(5):
if self.is_vacant(i, j):
moves.append(Move("p", None, (i, j), None))
return moves
def number_of_goats_that_can_be_captured(self):
cnt = 0
board = deepcopy(self.board)
for i in range(5):
for j in range(5):
if board[i][j] == 'T':
operations = x_not_equals_y_operations
if i % 2 == j % 2:
operations = x_equals_y_operations
for offset_x, offset_y in operations:
next_i, next_j = i + offset_x, j + offset_y
if is_inside_board(next_i, next_j) and board[next_i][next_j] == 'G':
next_next_i, next_next_j = i + 2 * offset_x, j + 2 * offset_y
if is_inside_board(next_next_i, next_next_j) and \
board[next_next_i][next_next_j] == '_':
board[next_i][next_j] = '_'
cnt += 1
return cnt
def move_goats(self):
moves = []
for i in range(5):
for j in range(5):
if self.board[i][j] == 'G':
operations = x_not_equals_y_operations
if i % 2 == j % 2:
operations = x_equals_y_operations
for offset_x, offset_y in operations:
next_i, next_j = i + offset_x, j + offset_y
if is_inside_board(next_i, next_j) and self.is_vacant(next_i, next_j):
moves.append(Move("m", (i, j), (next_i, next_j), None))
return moves
def move_tigers(self):
moves = []
for i in range(5):
for j in range(5):
if self.board[i][j] == 'T':
operations = x_not_equals_y_operations
if i % 2 == j % 2:
operations = x_equals_y_operations
for offset_x, offset_y in operations:
next_i, next_j = i + offset_x, j + offset_y
if is_inside_board(next_i, next_j) and self.is_vacant(next_i, next_j):
moves.append(Move("m", (i, j), (next_i, next_j), None))
return moves
def eat_goats(self):
moves = []
for i in range(5):
for j in range(5):
if self.board[i][j] == 'T':
operations = x_not_equals_y_operations
if i % 2 == j % 2:
operations = x_equals_y_operations
for offset_x, offset_y in operations:
next_i, next_j = i + offset_x, j + offset_y
if is_inside_board(next_i, next_j) and self.board[next_i][next_j] == 'G':
next_next_i, next_next_j = i + 2 * offset_x, j + 2 * offset_y
if is_inside_board(next_next_i, next_next_j) and self.board[next_next_i][next_next_j] == '_':
moves.append(Move('e', (i, j), (next_next_i, next_next_j), (next_i, next_j)))
return moves
def generate_move_list(self, is_max):
move_list = []
# Goat is minimizing
if not is_max:
if self.goats_in_hand > 0:
move_list.extend(self.place_goats())
else:
move_list.extend(self.move_goats())
# Tiger is maximizing
else:
move_list.extend(self.eat_goats())
move_list.extend(self.move_tigers())
return move_list
def make_move(self, move, is_max):
object_to_be_placed = 'T' if is_max else 'G'
_, frm, to, inter = move
if move.type == "p":
row, col = to
self.board[row][col] = object_to_be_placed
self.goats_in_hand -= 1
elif move.type == "m":
row1, col1 = frm
row2, col2 = to
self.board[row1][col1] = '_'
self.board[row2][col2] = object_to_be_placed
elif move.type == "e":
row1, col1 = frm
goat_x, goat_y = inter
row2, col2 = to
self.dead_goats += 1
self.board[row1][col1] = '_'
self.board[goat_x][goat_y] = '_'
self.board[row2][col2] = object_to_be_placed
def revert_move(self, move, is_max):
object_to_be_placed = 'T' if is_max else 'G'
_, frm, to, inter = move
if move.type == "p":
row, col = to
self.board[row][col] = '_'
self.goats_in_hand += 1
elif move.type == "m":
row1, col1 = frm
row2, col2 = to
self.board[row1][col1] = object_to_be_placed
self.board[row2][col2] = '_'
elif move.type == "e":
row1, col1 = frm
goat_x, goat_y = inter
row2, col2 = to
self.dead_goats -= 1
self.board[row1][col1] = object_to_be_placed
self.board[goat_x][goat_y] = 'G'
self.board[row2][col2] = '_'
def minimax(self, is_max=True, depth=0, alpha=-INF, beta=INF):
score = self.evaluate(depth)
if depth == self.depth or abs(score) == INF:
return score
if not is_max:
value = INF
for move in self.generate_move_list(is_max):
self.make_move(move, is_max)
current_value = self.minimax(True, depth + 1, alpha, beta)
beta = min(beta, current_value)
if current_value == value and depth == 0:
self.best_move = move
if current_value < value:
value = current_value
beta = min(beta, value)
if depth == 0:
self.best_move = move
self.revert_move(move, is_max)
if alpha >= beta:
break
return value
else:
value = -INF
for move in self.generate_move_list(is_max):
self.make_move(move, is_max)
current_value = self.minimax(False, depth + 1, alpha, beta)
alpha = max(alpha, value)
if current_value == value and depth == 0:
self.best_move = move
if current_value > value:
value = current_value
alpha = max(alpha, value)
if depth == 0:
self.best_move = move
self.revert_move(move, is_max)
if alpha >= beta:
break
return value
def best_tiger_move(self):
self.minimax()
return self.best_move
def best_goat_move(self):
self.minimax(is_max=False)
return self.best_move
def make_best_move(self):
if self.turn == "Goat":
move = self.best_goat_move()
else:
move = self.best_tiger_move()
if self.turn == "Goat":
is_max = False
else:
is_max = True
self.make_move(move, is_max)
def get_best_move(self):
if self.turn == "Goat":
move = self.best_goat_move()
else:
move = self.best_tiger_move()
return move
# The heuristic value for the tiger piece was calculated based on the number of goats in the board, mobility of the piece and number of possible captures.