-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
trajectory_sampling.py
150 lines (121 loc) · 4.78 KB
/
trajectory_sampling.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
#######################################################################
# Copyright (C) #
# 2018 Shangtong Zhang(zhangshangtong.cpp@gmail.com) #
# Permission given to modify the code as long as you keep this #
# declaration at the top #
#######################################################################
import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from tqdm import tqdm
# 2 actions
ACTIONS = [0, 1]
# each transition has a probability to terminate with 0
TERMINATION_PROB = 0.1
# maximum expected updates
MAX_STEPS = 20000
# epsilon greedy for behavior policy
EPSILON = 0.1
# break tie randomly
def argmax(value):
max_q = np.max(value)
return np.random.choice([a for a, q in enumerate(value) if q == max_q])
class Task():
# @n_states: number of non-terminal states
# @b: branch
# Each episode starts with state 0, and state n_states is a terminal state
def __init__(self, n_states, b):
self.n_states = n_states
self.b = b
# transition matrix, each state-action pair leads to b possible states
self.transition = np.random.randint(n_states, size=(n_states, len(ACTIONS), b))
# it is not clear how to set the reward, I use a unit normal distribution here
# reward is determined by (s, a, s')
self.reward = np.random.randn(n_states, len(ACTIONS), b)
def step(self, state, action):
if np.random.rand() < TERMINATION_PROB:
return self.n_states, 0
next = np.random.randint(self.b)
return self.transition[state, action, next], self.reward[state, action, next]
# Evaluate the value of the start state for the greedy policy
# derived from @q under the MDP @task
def evaluate_pi(q, task):
# use Monte Carlo method to estimate the state value
runs = 1000
returns = []
for r in range(runs):
rewards = 0
state = 0
while state < task.n_states:
action = argmax(q[state])
state, r = task.step(state, action)
rewards += r
returns.append(rewards)
return np.mean(returns)
# perform expected update from a uniform state-action distribution of the MDP @task
# evaluate the learned q value every @eval_interval steps
def uniform(task, eval_interval):
performance = []
q = np.zeros((task.n_states, 2))
for step in tqdm(range(MAX_STEPS)):
state = step // len(ACTIONS) % task.n_states
action = step % len(ACTIONS)
next_states = task.transition[state, action]
q[state, action] = (1 - TERMINATION_PROB) * np.mean(
task.reward[state, action] + np.max(q[next_states, :], axis=1))
if step % eval_interval == 0:
v_pi = evaluate_pi(q, task)
performance.append([step, v_pi])
return zip(*performance)
# perform expected update from an on-policy distribution of the MDP @task
# evaluate the learned q value every @eval_interval steps
def on_policy(task, eval_interval):
performance = []
q = np.zeros((task.n_states, 2))
state = 0
for step in tqdm(range(MAX_STEPS)):
if np.random.rand() < EPSILON:
action = np.random.choice(ACTIONS)
else:
action = argmax(q[state])
next_state, _ = task.step(state, action)
next_states = task.transition[state, action]
q[state, action] = (1 - TERMINATION_PROB) * np.mean(
task.reward[state, action] + np.max(q[next_states, :], axis=1))
if next_state == task.n_states:
next_state = 0
state = next_state
if step % eval_interval == 0:
v_pi = evaluate_pi(q, task)
performance.append([step, v_pi])
return zip(*performance)
def figure_8_8():
num_states = [1000, 10000]
branch = [1, 3, 10]
methods = [on_policy, uniform]
# average accross 30 tasks
n_tasks = 30
# number of evaluation points
x_ticks = 100
plt.figure(figsize=(10, 20))
for i, n in enumerate(num_states):
plt.subplot(2, 1, i+1)
for b in branch:
tasks = [Task(n, b) for _ in range(n_tasks)]
for method in methods:
value = []
for task in tasks:
steps, v = method(task, MAX_STEPS / x_ticks)
value.append(v)
value = np.mean(np.asarray(value), axis=0)
plt.plot(steps, value, label='b = %d, %s' % (b, method.__name__))
plt.title('%d states' % (n))
plt.ylabel('value of start state')
plt.legend()
plt.subplot(2, 1, 2)
plt.xlabel('computation time, in expected updates')
plt.savefig('../images/figure_8_8.png')
plt.close()
if __name__ == '__main__':
figure_8_8()