-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathevaluate.py
51 lines (34 loc) · 1.4 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
import pickle
import click
import gym
from utils import select_optimal_action
NUM_EPISODES = 100
def evaluate_agent(q_table, env, num_trials):
total_epochs, total_penalties = 0, 0
print("Running episodes...")
for _ in range(num_trials):
state = env.reset()
epochs, num_penalties, reward = 0, 0, 0
while reward != 20:
next_action = select_optimal_action(q_table,state)
state, reward, _, _ = env.step(next_action)
if reward == -10:
num_penalties += 1
epochs += 1
total_penalties += num_penalties
total_epochs += epochs
average_time = total_epochs / float(num_trials)
average_penalties = total_penalties / float(num_trials)
print("Evaluation results after {} trials".format(num_trials))
print("Average time steps taken: {}".format(average_time))
print("Average number of penalties incurred: {}".format(average_penalties))
@click.command()
@click.option('--num-episodes', default=NUM_EPISODES, help='Number of episodes to train on', show_default=True)
@click.option('--q-path', default="q_table.pickle", help='Path to read the q-table values from', show_default=True)
def main(num_episodes, q_path):
env = gym.make("Taxi-v3")
with open(q_path, 'rb') as f:
q_table = pickle.load(f)
evaluate_agent(q_table, env, num_episodes)
if __name__ == "__main__":
main()