-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgame_of_agents.py
212 lines (170 loc) · 6.89 KB
/
game_of_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
from envs import *
from agents import *
from itertools import product
from time import time
POWER = 5
# Heuristic function for "4 in row" game
def inrow_heuristic(env, player, weight=0):
"""
Score system:
win = inf
loss = -inf
array of some symbols without space to extend = 0
array with 1 symbol = +/-1 = +/-1^5
array with 2 symbols = +/-32 = +/-2^5
array with 3 symbols = +/-243 = +/-3^5
This scores growth if the array have multiply space to extend to!!!
"""
# Check terminal state
if env.is_terminal_state():
if env.player_status(player) > 0:
return float("inf")
if env.player_status(player) < 0:
return -float("inf")
return 0
if weight == 0:
return 0
score = 0
directions = {"hor": (0, 1),
"ver": (1, 0),
"diag": (1, -1),
"re-diag": (1, 1),
}
# Run on every empty cell
for i in range(env.boardH):
for j in range(env.boardW):
if env.board[i][j] == 0:
# Run in every direction
for direc in directions:
# Run in positive and negative diraction of direct
counter_p = counter(
i, j, env, directions[direc], 4, 1, player)
counter_n = counter(
i, j, env, directions[direc], 4, -1, player)
# calculate the score of both directions
if counter_p * counter_n > 0:
score += score_func(counter_p + counter_n, weight)
else:
score += score_func(counter_p, weight) + \
score_func(counter_n, weight)
return score
def score_func(count, weight):
score = count**POWER
# return score of the env by weight.
# If weight > 0: the agent is passive!
# If weight < 0: the agent is aggressive!
# If weight = 0: the agent is neutral!
if weight > 0:
return score*weight if score > 0 else score
if weight < 0:
return score*-weight if score < 0 else score
return score
# Helper function to count len of symbols array that we have in some direction
def counter(i, j, env, direc, depth, np, player):
counter = 0
for k in range(1, depth):
i_k = i + np * direc[0] * k
j_k = j + np * direc[1] * k
if 0 <= i_k < env.boardH and \
0 <= j_k < env.boardW and \
env.board[i_k][j_k] != 0:
symbol = env.board[i_k][j_k]
count = 1 if env.player2symbol[player] == symbol else -1
if k == 1 or \
(counter * count > 0): # Check if the counter and count both positive or negative
counter += count
else:
break
else:
break
return counter
# Wrapper functions to define different heuristic strategics
def aggressive_heuristic(env, player): # Aggressive strategy
return inrow_heuristic(env, player, WEIGHT)
def passive_heuristic(env, player): # Passive strategy
return inrow_heuristic(env, player, -WEIGHT)
def neutral_heuristic(env, player): # Neutral strategy
return inrow_heuristic(env, player, 1)
def zero_heuristic(env, player): # Zero strategy
return inrow_heuristic(env, player, 0)
heuristics = {"aggressive": aggressive_heuristic,
"passive": passive_heuristic,
"neutral": neutral_heuristic,
"zero": zero_heuristic}
# Function to play a game for each pair of players with different timers
def tournament(players, timers):
# Remember the pair of agents that got timeout
timeout_detector = None, None
# Run over all combination of players and timers
for player1, player2, timeout in product(players, players, timers):
# Check if the player1 == player2 and if this pair of agents got timeout in previous match
if player1 == player2 or timeout_detector == (player1, player2):
continue
# Play the game and return the result
result = game(player1, player2, timeout)
if result["total-time"] == "Player1 Timeout" or\
result["total-time"] == "Player2 Timeout":
timeout_detector = player1, player2
yield {"players": (player1, player2),
"timeout": timeout,
"result": result["result"],
"game-time": result["total-time"],
"actions": result["actions"],
}
# Play a single game
def game(player1, player2, timeout):
# Define timeout and create board
player1.timeout = timeout
player2.timeout = timeout
board = create_env('4-in-row', player1, player2, (6, 7))
# Try to play the game
actions = 0
try:
start_time = time()
while not board.is_terminal_state():
if board.apply_action(player1, player1.choose_action(board)):
actions += 1
if board.apply_action(player2, player2.choose_action(board)):
actions += 1
total_time = time() - start_time
return {"result": (board.player_status(player1), board.player_status(player2)),
"total-time": total_time,
"actions": actions}
# Except timeout
except PlayerTimeout as ex:
if ex.player == player1:
return {"result": (-1, 1), "total-time": "Player1 Timeout", "actions": actions}
return {"result": (1, -1), "total-time": "Player2 Timeout", "actions": actions}
# Print the result of the match
def render(num, match):
print(f"Match number: # {num + 1}")
print("Palyer 1: {}".format(match["players"][0].name))
print("Player 2: {}".format(match["players"][1].name))
print("Timeout: {} (sec)".format(match["timeout"]))
print("Total time of the match: {} (sec)".format(match["game-time"]))
print("Total actions of the players: {}".format(match["actions"]))
if match["result"][0] > 0:
print("Player 1 won!")
print("Player 2 loss...")
elif match["result"][1] > 0:
print("Player 1 loss...")
print("Player 2 won!")
else:
print("draw")
print("--------------------------------------------")
# Initial parameters
WEIGHT = 1.5
depths = [1, 3, 5, 7]
timers = [3, 2, 1, 0.1]
if __name__ == "__main__":
# Create list of all players with different initial parameters
players = [RandomAgent("random")]
for depth, (heuristic_name, heuristic_func) in product(depths, heuristics.items()):
players.append(MinimaxAgent(
f"minimax agent, depth = {depth}, heuristic = {heuristic_name}", depth, heuristic_func, 0))
print("List of players was created Successfly")
print("Results of the tournament:")
print("--------------------------------------------")
for num, match in enumerate(tournament(players, timers)):
render(num, match)
print("Done")