-
Notifications
You must be signed in to change notification settings - Fork 1
/
ql_4x4.py
103 lines (81 loc) · 3.3 KB
/
ql_4x4.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
import time
import gym
import numpy as np
from tqdm import tqdm
from qlearning_lib import QLAgent
seed = 91
def accuracy(results):
"""
Evaluate the accuracy of results, considering victories and defeats.
Args:
results: List of 2 elements representing the number of victories and defeats
Returns:
results accuracy
"""
return results[1] / (results[0] + results[1]) * 100
def experiment(n_episodes, default_policy=False, policy=None, render=False):
"""
Run a RL experiment that can be either training or testing
Args:
n_episodes: number of train/test episodes
default_policy: boolean to enable testing/training phase
policy: numpy tensor with a trained policy
render: enable OpenAI environment graphical rendering
Returns:
Dictionary with:
cumulative experiments outcomes
list of steps per episode
list of cumulative rewards
trained policy
"""
res = [0, 0] # array of results accumulator: {[0]: Loss, [1]: Victory}
scores = [] # Cumulative rewards
steps = [] # Steps per episode
env = gym.make('FrozenLake-v0')
env.seed(seed)
if (default_policy):
agent = QLAgent([env.observation_space.n, env.action_space.n], policy=policy)
else:
agent = QLAgent([env.observation_space.n, env.action_space.n],
epsilon_decay_function=lambda e: e - 0.000036)
for _ in tqdm(range(n_episodes)):
state = env.reset()
cumulative_reward = 0
for t in range(env._max_episode_steps):
if (render):
env.render()
time.sleep(1)
next_action = agent.act(state)
new_state, reward, end, _ = env.step(next_action)
if policy is None:
agent.update_q(state, new_state, next_action, reward)
if end:
res[int(reward)] += 1
steps.append(t)