-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
411 lines (364 loc) · 17.6 KB
/
main.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
from NFSPTrainedPlayer import TrainedNFSPPlayer
from AggressivePlayer import AggressivePlayer
from DefensivePlayer import DefensivePlayer
from RandomPlayer import RandomPlayer
from HumanPlayer import HumanPlayer
from DurakPlayer import DurakPlayer
from NFSPPlayer import NFSPPlayer
from PPOPlayer import from_path
from Types import List, Dict, NumberType, Tuple
from NFSPTrainer import NFSPTrainer
from PPOTrainer import PPOTrainer
from DurakEnv import DurakEnv
import matplotlib.pyplot as plt
import tensorflow as tf
import logging
import random
import shutil
import sys
import os
"""
General useful parameters.
"""
colors = ["gray", "yellowgreen", "blue", "brown", "deeppink", "red", "coral", "peru", "darkorange", "gold", "darkkhaki",
"yellow", "forestgreen", "lime", "darkslategray", "cyan", "dodgerblue", "royalblue", "indigo", "purple"]
hand_size = DurakEnv.HAND_SIZE
nfsp_saved_models_dir = os.path.join(os.getcwd(), "NFSP-models")
ppo_saved_models_dir = os.path.join(os.getcwd(), "PPOParams")
def do_test_games(players: List[DurakPlayer], num_games: int, steps_lim=500) -> Dict[str, float]:
"""
Performs test games and collects loss ratios in % of each player.
:param players: list of players.
:param num_games: number of games.
:param steps_lim: limit of steps for each game.
:return: dictionary mapping the name of each player to their % of losses
"""
loss_ratios = {player.name: 0 for player in players}
env = DurakEnv(players, False)
print_every = min(num_games // 10, 50)
for i in range(1, num_games + 1):
if num_games >= 100:
if i == 1 or (i % print_every) == 0:
print("playing test game", i, "out of", num_games)
state = env.reset()
done = False
step = 1
while not done and (step < steps_lim):
act = (env.get_turn_player()).get_action(state, env.to_attack())
state, _, done = env.step(act)
step += 1
loser = env.get_loser()
if loser is not None:
loss_ratios[loser.name] += 1
if num_games > 0:
for name in loss_ratios:
loss_ratios[name] /= num_games
loss_ratios[name] *= 100
return loss_ratios
def get_sorted_filenames_and_indices():
"""
Loads every 5000'th model fro mthe directory in which the PPO models are saved.
:return: Sorted list of files of PPO models from the directory in which they are saved.
"""
files = [(int(f[5:]), f) for f in os.listdir(ppo_saved_models_dir) if f.find('model') != -1 and int(f[5:]) % 15000 == 0]
files.sort(key=lambda x: x[0])
return list(zip(*files))
def run_against_one_random(params_filename, games):
"""
Runs test games against a single random player and returns the loss ration in range [0, 1]
:param params_filename: path of the file containing the parameters of the model to test.
:param games: number of test games.
:return: loss ratio of the player in range [0, 1]
"""
with tf.compat.v1.Session() as sess:
player1 = from_path(sess, DurakEnv.HAND_SIZE, "Trained1_PPO_" + params_filename, os.path.join(ppo_saved_models_dir, params_filename))
player2 = RandomPlayer(DurakEnv.HAND_SIZE, "RandomPlayer")
loss_ratios = do_test_games([player1, player2], games)
return loss_ratios[player1.name] / 100
def run_against_3_randoms(params_filename, games):
"""
Runs test games against 3 random players and returns the loss ration in range [0, 1]
:param params_filename: path of the file containing the parameters of the model to test.
:param games: number of test games.
:return: loss ratio of the player in range [0, 1]
"""
with tf.compat.v1.Session() as sess:
player1 = from_path(sess, DurakEnv.HAND_SIZE, "Trained2_PPO_" + params_filename, os.path.join(ppo_saved_models_dir, params_filename))
player2 = RandomPlayer(DurakEnv.HAND_SIZE, "RandomPlayer1")
player3 = RandomPlayer(DurakEnv.HAND_SIZE, "RandomPlayer2")
player4 = RandomPlayer(DurakEnv.HAND_SIZE, "RandomPlayer3")
loss_ratios = do_test_games([player1, player2, player3, player4], games)
return loss_ratios[player1.name] / 100
def run_against_3_defensives(params_filename, games):
"""
Runs test games against 3 defensive players and returns the loss ration in range [0, 1]
:param params_filename: path of the file containing the parameters of the model to test.
:param games: number of test games.
:return: loss ratio of the player in range [0, 1]
"""
with tf.compat.v1.Session() as sess:
player1 = from_path(sess, DurakEnv.HAND_SIZE, "Trained3_PPO_" + params_filename, os.path.join(ppo_saved_models_dir, params_filename))
player2 = DefensivePlayer(DurakEnv.HAND_SIZE, "DefensivePlayer1")
player3 = DefensivePlayer(DurakEnv.HAND_SIZE, "DefensivePlayer2")
player4 = DefensivePlayer(DurakEnv.HAND_SIZE, "DefensivePlayer3")
loss_ratios = do_test_games([player1, player2, player3, player4], games)
return loss_ratios[player1.name] / 100
def run_against_3_aggressives(params_filename, games):
"""
Runs test games against 3 aggressive players and returns the loss ration in range [0, 1]
:param params_filename: path of the file containing the parameters of the model to test.
:param games: number of test games.
:return: loss ratio of the player in range [0, 1]
"""
with tf.compat.v1.Session() as sess:
player1 = from_path(sess, DurakEnv.HAND_SIZE, "Trained4_PPO_" + params_filename, os.path.join(ppo_saved_models_dir, params_filename))
player2 = AggressivePlayer(DurakEnv.HAND_SIZE, "AggressivePlayer1")
player3 = AggressivePlayer(DurakEnv.HAND_SIZE, "AggressivePlayer2")
player4 = AggressivePlayer(DurakEnv.HAND_SIZE, "AggressivePlayer3")
loss_ratios = do_test_games([player1, player2, player3, player4], games)
return loss_ratios[player1.name] / 100
def plot_graph(title, x_axis, y_axis):
"""
Plots a single graph on a figure.
:param title: title of the plot.
:param x_axis: x axis values.
:param y_axis: y axis values.
"""
plt.figure()
plt.title(title)
plt.xlabel("Player Model Number")
plt.ylabel("Loss Ratios")
plt.ylim(0, 1)
plt.plot(x_axis, y_axis)
plt.savefig(title.replace('\n', '') + ".jpg")
def train_ppo():
"""
Trains a PPO player.
"""
# Resets the folder of the saved parameters
if not os.path.exists(ppo_saved_models_dir):
os.mkdir(ppo_saved_models_dir)
with tf.compat.v1.Session() as sess:
trainer = PPOTrainer(sess,
games_per_batch=5,
training_steps_per_game=25,
learning_rate=0.00025,
clip_range=0.2,
save_every=100)
trainer.train(500000)
def test_and_plot_ppo():
"""
Tests the PPO player against test cases.
"""
games = 10
logs_dir = os.path.join(os.getcwd(), 'logs')
if not os.path.exists(logs_dir):
os.mkdir(logs_dir)
log_filename = os.path.join(logs_dir, 'PPOTester_log')
logging.basicConfig(filename=log_filename, level=logging.INFO)
indices, files = get_sorted_filenames_and_indices()
# run against 1 random player, save results, and plot graph
random_losses = []
for f in files:
random_losses.append(run_against_one_random(f, games))
plot_graph("PPO models loss ratio against 1 Random Agent", indices, random_losses)
# run against 3 random players, save results, and plot graph
random_3_losses = []
for f in files:
random_3_losses.append(run_against_3_randoms(f, games))
plot_graph("PPO models loss ratio against 3 Random Agents", indices, random_3_losses)
# run against 3 defensive players, save results, and plot graph
def_losses = []
for f in files:
def_losses.append(run_against_3_defensives(f, games))
plot_graph("PPO models loss ratio against 3 Defensive Agents", indices, def_losses)
# run against 3 aggressive players, save results, and plot graph
agg_losses = []
for f in files:
agg_losses.append(run_against_3_aggressives(f, games))
plot_graph("PPO models loss ratio against 3 Aggressive Agents", indices, agg_losses)
logging.shutdown()
def generate_ppo_plots():
"""
Calls the training function for the PPO player and then calls the test and plot function.
"""
train_ppo()
test_and_plot_ppo()
def plot(x_axis: List[NumberType], y_axes: Dict[str, Tuple[Tuple[float, float, float], List[NumberType]]], title: str, x_label: str, y_label: str, legend: bool):
"""
Plots multiple plots to the same figure.
:param x_axis: values of the x axis.
:param y_axes: a dictionary mapping the label of the plots to the color of the plot and the y axis values.
:param title: figure title.
:param x_label: x axis label.
:param y_label: y axis label.
:param legend: weather to display a legend (mapping each plot label to the plot)
"""
plt.figure()
plt.title(title)
plt.xlabel(x_label)
plt.ylabel(y_label)
for plot_label in y_axes:
color, values = y_axes[plot_label]
plt.plot(x_axis, values, color=color, label=plot_label)
if legend:
plt.legend()
def train_against_prev_iter(subdir, epochs=100, training_games_per_epoch=50):
"""
Trains a NFSP agent against a previous iteration of itself.
:param subdir: subdirectory in which to save the models while training.
:param epochs: number of training epochs.
:param training_games_per_epoch: number of training games per epoch.
"""
full_subdir_path = os.path.join(nfsp_saved_models_dir, subdir)
if os.path.exists(full_subdir_path):
shutil.rmtree(full_subdir_path)
os.mkdir(full_subdir_path)
learning_player = NFSPPlayer(hand_size, 'NFSP-PLAYER-1', 'cpu')
learning_players = [learning_player]
random1 = RandomPlayer(hand_size, 'random-1')
training_players = [random1, RandomPlayer(hand_size, 'random-2')]
trainer = NFSPTrainer(learning_players, training_players)
cumulative_training_games_per_epoch = list(range(training_games_per_epoch, training_games_per_epoch * epochs + 1, training_games_per_epoch))
for epoch in range(epochs):
print("------------------------- Epoch", epoch + 1, "out of", epochs, "-------------------------")
print("Training for", training_games_per_epoch, "games (total number of training games: " + str(cumulative_training_games_per_epoch[epoch]) + ")")
trainer.train(training_games_per_epoch)
learning_player.save_network(os.path.join(subdir, 'epoch-' + str(epoch + 1)))
prev_iter1 = TrainedNFSPPlayer(hand_size, 'prev-iter1')
prev_iter1.load_model(os.path.join(subdir, 'epoch-' + str(random.randint(max(1, epoch-10), epoch + 1))))
training_players = [random1, prev_iter1]
trainer = NFSPTrainer(learning_players, training_players)
def train_against_one_random(subdir, epochs=100, training_games_per_epoch=50):
"""
Trains a NFSP agent against a random player.
:param subdir: subdirectory in which to save the models while training.
:param epochs: number of training epochs.
:param training_games_per_epoch: number of training games per epoch.
"""
full_subdir_path = os.path.join(nfsp_saved_models_dir, subdir)
if os.path.exists(full_subdir_path):
shutil.rmtree(full_subdir_path)
os.mkdir(full_subdir_path)
learning_player = NFSPPlayer(hand_size, 'NFSP-PLAYER-1', 'cpu')
learning_players = [learning_player]
random1 = RandomPlayer(hand_size, 'random-1')
training_players = [random1]
trainer = NFSPTrainer(learning_players, training_players)
cumulative_training_games_per_epoch = list(range(training_games_per_epoch, training_games_per_epoch * epochs + 1, training_games_per_epoch))
for epoch in range(epochs):
print("------------------------- Epoch", epoch + 1, "out of", epochs, "-------------------------")
print("Training for", training_games_per_epoch, "games (total number of training games: " + str(cumulative_training_games_per_epoch[epoch]) + ")")
trainer.train(training_games_per_epoch)
learning_player.save_network(os.path.join(subdir, 'epoch-' + str(epoch + 1)))
def train_against_nfsp_agent(subdir, epochs=100, training_games_per_epoch=50):
"""
Trains two NFSP agents against each other.
:param subdir: subdirectory in which to save the models while training.
:param epochs: number of training epochs.
:param training_games_per_epoch: number of training games per epoch.
"""
full_subdir_path = os.path.join(nfsp_saved_models_dir, subdir)
if os.path.exists(full_subdir_path):
shutil.rmtree(full_subdir_path)
os.mkdir(full_subdir_path)
learning_player1 = NFSPPlayer(hand_size, 'NFSP-PLAYER-1', 'cpu')
learning_player2 = NFSPPlayer(hand_size, 'NFSP-PLAYER-2', 'cpu')
learning_players = [learning_player1, learning_player2]
trainer = NFSPTrainer(learning_players, [])
cumulative_training_games_per_epoch = list(range(training_games_per_epoch, training_games_per_epoch * epochs + 1, training_games_per_epoch))
for epoch in range(epochs):
print("------------------------- Epoch", epoch + 1, "out of", epochs, "-------------------------")
print("Training for", training_games_per_epoch, "games (total number of training games: " + str(cumulative_training_games_per_epoch[epoch]) + ")")
trainer.train(training_games_per_epoch)
learning_player1.save_network(os.path.join(subdir, 'epoch-' + str(epoch + 1)))
def graph(subdir, title, file_name, training_games_per_epoch=50, test_games_per_epoch_vs_test_players=500):
"""
Plots the results of the NFSP models saved in the given subdirectory vs. test cases.
:param subdir: subdirectory in which the NFSP models are saved.
:param title: figure title.
:param file_name: name of file to save the figure.
:param training_games_per_epoch: number of training games per epoch used in training of the models.
:param test_games_per_epoch_vs_test_players: number of test games for each model against each test case.
"""
filenames = os.listdir(os.path.join(nfsp_saved_models_dir, subdir))
cumulative_training_games_per_epoch = list(range(training_games_per_epoch, training_games_per_epoch * len(filenames) + 1, training_games_per_epoch))
loss_ratio_vs_1_random = []
loss_ratio_vs_3_random = []
trained = TrainedNFSPPlayer(hand_size, 'trained')
random1 = RandomPlayer(hand_size, 'random-1')
random2 = RandomPlayer(hand_size, 'random-2')
random3 = RandomPlayer(hand_size, 'random-3')
for filename in filenames:
trained.load_model(os.path.join(subdir, filename))
loss_ratios = do_test_games([trained, random1], test_games_per_epoch_vs_test_players)
loss_ratio_vs_1_random.append(loss_ratios[trained.name])
loss_ratios = do_test_games([trained, random1, random2, random3], test_games_per_epoch_vs_test_players)
loss_ratio_vs_3_random.append(loss_ratios[trained.name])
learning_player_info = {"VS. 1 random": (
colors[1], loss_ratio_vs_1_random), "VS. 3 randoms": (
colors[2], loss_ratio_vs_3_random)}
plot(cumulative_training_games_per_epoch, learning_player_info,
title, "Number of training games", "Loss Ratio", True)
plt.savefig(file_name)
def train_and_plot_nfsp():
"""
Calls the training and plotting functions for the NFSP agents.
"""
epochs = 100
training_games_per_epoch = 100
test_games_per_epoch = 100
if not os.path.exists(nfsp_saved_models_dir):
os.mkdir(nfsp_saved_models_dir)
subdir = 'train_against_prev_iter'
train_against_prev_iter(subdir, epochs, training_games_per_epoch)
graph(subdir, "NFSP trained against previous iterations", "Trained vs Previous Iterations.jpg", training_games_per_epoch, test_games_per_epoch)
subdir = 'train_against_one_random'
train_against_one_random(subdir, epochs, training_games_per_epoch)
graph(subdir, "NFSP trained against random player", "Trained vs One Random Player.jpg", training_games_per_epoch, test_games_per_epoch)
subdir = 'train_against_nfsp'
train_against_nfsp_agent(subdir, epochs, training_games_per_epoch)
graph(subdir, "NFSP trained against another NFSP", "Trained vs Another NFSP Player.jpg", training_games_per_epoch, test_games_per_epoch)
def run_example_game():
"""
Runs a complete example game.
"""
players = [HumanPlayer(hand_size, "Human"),
DefensivePlayer(hand_size, "Player")]
env = DurakEnv(players, True)
done = False
state = env.reset()
env.render()
while not done:
act = (env.get_turn_player()).get_action(state, env.to_attack())
state, _, done = env.step(act)
env.render()
env.end_gui()
def main():
"""
Runs the appropriate function according to the given arguments.
"""
example = '0'
ppo = '1'
nfsp = '2'
ppo_and_nfsp = '3'
arg = sys.argv[1] if len(sys.argv) >= 2 and sys.argv[1] in [ppo, nfsp, ppo_and_nfsp] else '0'
show = False if len(sys.argv) >= 3 and sys.argv[2] == 'f' else True
if arg == example:
print("Running example game")
run_example_game()
else:
if show:
print("Generated plots will be shown")
else:
print("Generated plots will not be shown")
if arg in [ppo, ppo_and_nfsp]:
print("Training and generating plots for the PPO player...")
generate_ppo_plots()
if arg in [nfsp, ppo_and_nfsp]:
print("Training and generating plots for the NFSP player...")
train_and_plot_nfsp()
if show:
plt.show()
if __name__ == '__main__':
main()