-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfuncs.py
210 lines (158 loc) · 7.79 KB
/
funcs.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
from timeit import default_timer as timer#timer
import numpy as np
import random
import loggers as lg
import sys
from game import Game, GameState
from agent import Agent, User
import config
from config import PLAYER_COUNT, TEAM_SIZE, MIN_MEMORY_SIZE
from collections import defaultdict
from tqdm import tnrange
def playMatches(agents, EPISODES, logger, epsilon, memory = None, random_agent = False, evaluation = False):
total_time_avg = 0
env = Game()
scores = {"drawn": 0}
for i in range(PLAYER_COUNT):
scores[agents[i].name] = 0
#sp_scores = {'sp':0, "drawn": 0, 'nsp':0}
turns = 0
epsilon_step = 1/EPISODES
games_to_win = (config.SCORING_THRESHOLD * EPISODES) / (1 + config.SCORING_THRESHOLD)
games_to_block = EPISODES - games_to_win
#sys.stdout.flush()
for e in range(EPISODES):
last_game = e + 1
logger.info('====================')
logger.info('EPISODE %d OF %d', e+1, EPISODES)
logger.info('====================')
if (e+1) % 5 == 0:
if evaluation:
print('ev' + str(e+1) + ' ', end='')
else:
print('sp' + str(e+1) + ' ', end='')
state = env.reset()
if state.isEndGame:
print("over before started")
exit(0)
#print("Starting hands: {0}".format(state.hands))
if len(state.allowedActions) == 1: # if things like bidding at the beginning only give one action go ahead and automate those w/ env.step
state, _, _, _ = env.step(state.allowedActions[0], logger)
done = 0
players = {}
points = {}
for i,player in enumerate(agents):
player.mcts = None
players[i] = {"agent": player, "name": player.name}
points[i] = []
env.gameState.render(logger)
start_game = timer()
while done == 0:
turns = turns + 1
if len(state.allowedActions) < 2:
print("funcs loop, no choices")
#### Run the MCTS algo and return an action
if random_agent and players[state.playerTurn]['name'] == 'best_player':
action = random.choice(state.allowedActions)
else:
action = players[state.playerTurn]['agent'].act(state, epsilon)
### Do the action
turn = state.playerTurn
if players[state.playerTurn]['name'] == 'user':
state, value, done, _ = env.step(action, logger, True) # this parameter tells the gameState to print out automated turns for the user's convenience
else:
state, value, done, _ = env.step(action, logger) # the value is [1,-1] if team/player 0 won or the opposite if team/player 1 won otherwise it's [0,0]
# store state and the player who created that state in short term memory
if memory != None:
memory.commit_stmemory(state, turn)
if done:
if memory:
#### If the game is finished, assign the values to the history of moves from the game
for move in memory.stmemory:
"""if TEAM_SIZE > 1:
if move['prev_player'] % TEAM_SIZE == winning_team:
move['value'] = 1
else:
move['value'] = -1
else:
if move['prev_player'] == winning_team:
move['value'] = 1
else:
move['value'] = -1"""
move['value'] = value
memory.commit_ltmemory()
if evaluation:
if value[0] == 0:
scores['drawn'] = scores['drawn'] + 1
else:
winning_team = int(np.argmax(value))
if TEAM_SIZE > 1:
winning_team = winning_team % TEAM_SIZE
scores[players[winning_team]['name']] = scores[players[winning_team]['name']] + 1
end_game = timer()
total_time_avg += end_game - start_game
# if it is a tournament and if either player has won enough games to win break the loop
if evaluation and (scores['best_player'] + .5 * scores['drawn'] >= games_to_block or scores['current_player'] >= games_to_win):
break
# reduce size of epsilon every episode
epsilon -= epsilon_step
#print("Avg game time: {0}, Avg # of turns: {1}".format(total_time_avg/last_game, int(turns/last_game)))
return scores, memory
def version_tournament(agents, EPISODES, logger):
total_time_avg = 0
env = Game()
scores = {"drawn": 0}
for i in range(PLAYER_COUNT):
scores[agents[i].name] = 0
#sp_scores = {'sp':0, "drawn": 0, 'nsp':0}
turns = 0
games_to_win = (config.SCORING_THRESHOLD * EPISODES) / (1 + config.SCORING_THRESHOLD)
games_to_block = EPISODES - games_to_win
for e in range(EPISODES):
state = env.reset()
if state.isEndGame:
print("over before started")
exit(0)
#print("Starting hands: {0}".format(state.hands))
if len(state.allowedActions) == 1: # if things like bidding at the beginning only give one action go ahead and automate those w/ env.step
state, _, _, _ = env.step(state.allowedActions[0], logger)
done = 0
players = {}
for i,player in enumerate(agents):
player.mcts = None
players[i] = {"agent": player, "name": player.name}
env.gameState.render(logger)
start_game = timer()
while done == 0:
#print("turn: {0}".format(turns))
turns = turns + 1 # turns until tao tracker
if len(state.allowedActions) < 2:
print("funcs loop, no choices")
d_t = state.decision_type
turn = state.playerTurn
#### Run the MCTS algo and return an action
if players[turn]["name"] == 'random_agent':
action = random.choice(state.allowedActions)
else:
action = players[state.playerTurn]['agent'].act(state, 0)
if action not in state.allowedActions:
print("error in funcs")
### Do the action
#print("from funcs")
if players[state.playerTurn]['name'] == 'user':
state, value, done, _ = env.step(action, logger, True) # this parameter tells the gameState to print out automated turns for the user's convenience
else:
state, value, done, _ = env.step(action, logger) # the value is [1,-1] if team/player 0 won or the opposite if team/player 1 won otherwise it's [0,0]
#env.gameState.render(logger) # moved logger to step so that skipped turns (1 or less action) still get logged
if done == 1:
winning_team = int(np.argmax(value))
scores[players[winning_team]['name']] += 1
print(scores)
print('{0}%'.format(100 * scores['trained_agent'] / (e + 1)))
game_time = timer() - start_game
total_time_avg += game_time
# if it is a tournament and if either player has won enough games to win break the loop
if players[1]['name'] == 'current_player' and (scores['best_player'] > games_to_block or scores['current_player'] > games_to_win):
break
print("Avg game time: {0}, Avg # of turns: {1}".format(total_time_avg/EPISODES, int(turns/EPISODES)))
return scores