-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolicy.py
255 lines (199 loc) · 6.69 KB
/
policy.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import numpy as np
import torch
class EpsilonGreedyPolicy:
'''
TODO: refactor this
'''
def __init__(self, num_states, num_actions, epsilon, Q=None):
if Q is None:
self._Q = np.zeros((num_states, num_actions))
else:
self._Q = Q
self._eps = epsilon
@property
def Q(self):
# support read-only
return np.copy(self._Q)
def query_Q_probs(self, s=None, a=None):
Q_probs = np.zeros(self._Q.shape)
for s in range(self._Q.shape[0]):
Q_probs[s, :] = self.query_Q_probs(s)
if s is None and a is None:
return Q_probs
elif a is None:
return Q_probs[s, :]
else:
return Q_probs[s, a]
def _query_Q_probs(self, s, a=None):
num_actions = self._Q.shape[1]
probs = np.ones(num_actions, dtype=float) * self._eps / num_actions
ties = np.flatnonzero(self._Q[s, :] == self._Q[s, :].max())
if a is None:
best_a = np.random.choice(ties)
probs[best_a] += 1. - self._eps
return probs
else:
if a in ties:
probs[a] += 1. - self._eps
return probs[a]
def choose_action(self, s):
probs = self._query_Q_probs(s)
return np.random.choice(len(probs), p=probs)
def update_Q_val(self, s, a, val):
self._Q[s,a] = val
class GreedyPolicy:
def __init__(self, num_states, num_actions, Q=None):
if Q is None:
# start with random policy
self._Q = np.zeros((num_states, num_actions))
else:
# in case we want to import e-greedy
self._Q = Q
@property
def Q(self):
# support read-only
return np.copy(self._Q)
def query_Q_probs(self, s=None, a=None):
Q_probs = np.zeros(self._Q.shape).astype(np.float)
for s in range(self._Q.shape[0]):
ties = np.flatnonzero(self._Q[s, :] == self._Q[s, :].max())
a = np.random.choice(ties)
Q_probs[s, a] = 1.0
if s is None and a is None:
return Q_probs
elif a is None:
return Q_probs[s, :]
else:
return Q_probs[s, a]
def choose_action(self, s):
ties = np.flatnonzero(self._Q[s, :] == self._Q[s, :].max())
return np.random.choice(ties)
def get_opt_actions(self):
opt_actions = np.zeros(self._Q.shape[0])
for s in range(opt_actions.shape[0]):
opt_actions[s] = self.choose_action(s)
return opt_actions
def update_Q_val(self, s, a, val):
self._Q[s,a] = val
class StochasticPolicy:
def __init__(self, num_states, num_actions, Q=None):
if Q is None:
# start with random policy
self._Q = np.zeros((num_states, num_actions))
else:
# in case we want to import e-greedy
# make Q non negative to be useful as probs
self._Q = Q
@property
def Q(self):
# support read-only
return np.copy(self._Q)
def query_Q_probs(self, s=None, a=None, laplacian_smoothing=True):
'''
returns:
probability distribution of actions over all states
'''
if laplacian_smoothing:
LAPLACIAN_SMOOTHER = 0.01
L = (np.max(self._Q, axis=1) - np.min(self._Q, axis=1))* LAPLACIAN_SMOOTHER
Q = self._Q - np.expand_dims(np.min(self._Q, axis=1) - L, axis=1)
else:
Q = self._Q - np.expand_dims(np.min(self._Q, axis=1), axis=1)
Q_sum = np.sum(Q, axis=1)
# if zero, we give uniform probs with some gaussian noise
num_actions = self._Q.shape[1]
Q[Q_sum==0, :] = 1.
Q_sum[Q_sum==0] = num_actions
Q_probs = Q / np.expand_dims(Q_sum, axis=1)
Q_probs[Q_sum==0, :] += np.random.normal(0, 1e-4, num_actions)
if s is None and a is None:
return Q_probs
elif a is None:
return Q_probs[s, :]
else:
return Q_probs[s, a]
def choose_action(self, s, laplacian_smoothing=True):
probs = self.query_Q_probs(s, laplacian_smoothing=laplacian_smoothing)
return np.random.choice(len(probs), p=probs)
def update_Q_val(self, s, a, val):
self._Q[s,a] = val
class RandomPolicy:
def __init__(self, num_states, num_actions):
self._Q_probs = np.ones((num_states, num_actions), dtype=float) / num_actions
@property
def Q(self):
# support read-only
return np.copy(self._Q_probs)
def choose_action(self, s):
probs = self._Q_probs[s, :]
return np.random.choice(len(probs), p=probs)
class RandomPolicy2:
def __init__(self, choices):
self._choices = choices
def choose_action(self, s):
""" sample uniformly """
return np.random.choice(self._choices)
import torch
from torch import nn, optim
from torch.autograd import Variable
class LinearQ(nn.Module):
"""Docstring for LinearQ. """
def __init__(self, phi, k):
"""TODO: to be defined1.
Parameters
----------
phi : basis function for (s, a)
k : feature dimension
"""
super().__init__()
self._phi = phi
self._l1 = nn.Linear(k, 1)
def forward(self, s, a):
"""
predict Q(s,a)
"""
x = self._phi(s, a)
out = self._l1(x)
return out
def choose_action(self, s):
"""
argmax_a Q(s, a)
"""
Q_hat = np.array([self.forward(self._phi(s, a)) for a in range(n_actions)])
ties = np.flatnonzero(Q_hat == Q_hat.max())
return np.random.choice(ties)
class LinearQ2(object):
"""LinearQ for continuous-state, discrete_action """
def __init__(self, action_list, phi, W):
"""TODO: to be defined1.
Parameters
----------
action_list : list of valid actions (assuming discrete)
phi : basis function of (s, a)
W : TODO, optional
"""
self._action_list = action_list
self._phi = phi
self._W = W
def predict(self, s, a=None):
"""TODO: Docstring for predict.
only works for discrete action space
Parameters
----------
s : state
Returns
-------
Q(s, a)
"""
if a is None:
q_list = []
for a in self._action_list:
q = self._W.T.dot(self._phi(s, a).T)
q_list.append(q)
return np.array(q_list)
else:
return self._W.T.dot(self._phi(s, a))
def choose_action(self, s):
Q_hat = self.predict(s)
ties = np.flatnonzero(Q_hat == Q_hat.max())
return np.random.choice(ties)