-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_rl.py
60 lines (55 loc) · 1.74 KB
/
train_rl.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
from quinto.quinto import Quarto
from players import *
import matplotlib.pyplot as plt
from contextlib import redirect_stdout
from tqdm import trange
import argparse
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
'-o',
choices=[
'naive',
'minmax',
'random',
'risky',
'montecarlo'],
help='selects opponent player',
required=True)
parser.add_argument(
'-of',
action='store_true',
help='set if opponent is thr first player',
required=False)
args = parser.parse_args()
game = Quarto()
player_rl = RLPlayer(game, train=True)
player_opponent = PLAYERS[args.o](game)
players = (
player_opponent,
player_rl) if args.of else (
player_rl,
player_opponent)
index_rl_player = 0 if isinstance(players[0], RLPlayer) else 1
game.set_players(players)
rewards_per_episode = []
iterations = 2000
with redirect_stdout(None):
with trange(iterations) as t:
for i in t:
winner = game.run()
players[index_rl_player].learn()
rewards_per_episode.append(
players[index_rl_player].episode_reward)
game.reset_all()
players[index_rl_player].reset_player()
game.set_players(players)
players[index_rl_player].save_model()
average = []
for i in range(len(rewards_per_episode)):
episode_rew = rewards_per_episode[:i + 1]
average.append(sum(episode_rew) / (i + 1))
plt.figure()
plt.title("Mean episode return")
plt.plot(average)
plt.savefig('./images/new_reward_per_episode.png')