-
Notifications
You must be signed in to change notification settings - Fork 7
/
evaluate.py
86 lines (52 loc) · 2.56 KB
/
evaluate.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
import tensorflow as tf
import argparse
import numpy as np
from statistics import mean
from agents.random_agent import RandomAgent
from agents.ai_agent import AIAgent
from agents.q_agent import QAgent
from graphic_visualizations import stats_plotter
import environment as brisc
from utils import BriscolaLogger
from utils import NetworkTypes
def evaluate(game, agents, num_evaluations):
total_wins = [0] * len(agents)
points_history = [ [] for i in range(len(agents))]
for _ in range(num_evaluations):
game_winner_id, winner_points = brisc.play_episode(game, agents, train=False)
for player in game.players:
points_history[player.id].append(player.points)
if player.id == game_winner_id:
total_wins[player.id] += 1
print("\nTotal wins: ",total_wins)
for i in range(len(agents)):
print(agents[i].name + " " + str(i) + " won {:.2%}".format(total_wins[i]/num_evaluations), " with average points {:.2f}".format(mean(points_history[i])))
return total_wins, points_history
def main(argv=None):
'''Evaluate agent performances against RandomAgent and AIAgent'''
logger = BriscolaLogger(BriscolaLogger.LoggerLevels.TEST)
game = brisc.BriscolaGame(2, logger)
# agent to be evaluated is RandomAgent or QAgent if a model is provided
if FLAGS.model_dir:
eval_agent = QAgent(network=FLAGS.network)
eval_agent.load_model(FLAGS.model_dir)
eval_agent.make_greedy()
else:
eval_agent = RandomAgent()
# test agent against RandomAgent
agents = [eval_agent, RandomAgent()]
total_wins, points_history = evaluate(game, agents, FLAGS.num_evaluations)
stats_plotter(agents, points_history, total_wins)
# test agent against AIAgent
agents = [eval_agent, AIAgent()]
total_wins, points_history = evaluate(game, agents, FLAGS.num_evaluations)
stats_plotter(agents, points_history, total_wins)
if __name__ == '__main__':
# Parameters
# ==================================================
parser = argparse.ArgumentParser()
parser.add_argument("--model_dir", default=None, help="Provide a trained model path if you want to play against a deep agent", type=str)
parser.add_argument("--network", default=NetworkTypes.DRQN, choices=[NetworkTypes.DQN, NetworkTypes.DRQN], help="Neural Network used for approximating value function")
parser.add_argument("--num_evaluations", default=20, help="Number of evaluation games against each type of opponent for each test", type=int)
FLAGS = parser.parse_args()
tf.app.run()