-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlqg1d.py
338 lines (295 loc) · 12.2 KB
/
lqg1d.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
"""classic Linear Quadratic Gaussian Regulator task"""
from numbers import Number
import gym
from gym import spaces
from gym.utils import seeding
import numpy as np
"""
Linear quadratic gaussian regulator task.
References
----------
- Simone Parisi, Matteo Pirotta, Nicola Smacchia,
Luca Bascetta, Marcello Restelli,
Policy gradient approaches for multi-objective sequential decision making
2014 International Joint Conference on Neural Networks (IJCNN)
- Jan Peters and Stefan Schaal,
Reinforcement learning of motor skills with policy gradients,
Neural Networks, vol. 21, no. 4, pp. 682-697, 2008.
"""
#classic_control
from gym.envs.registration import register
register(
id='LQG1D-v0',
entry_point='lqg1d:LQG1D'
)
class LQG1D(gym.Env):
metadata = {
'render.modes': ['human', 'rgb_array'],
'video.frames_per_second': 30
}
def __init__(self, discrete_reward=False):
self.horizon = 20
self.gamma = 0.9
self.discrete_reward = discrete_reward
self.max_pos = 2.0
self.max_action = 2.0
self.sigma_noise = 0
self.A = np.array([1]).reshape((1, 1))
self.B = np.array([1]).reshape((1, 1))
self.Q = np.array([0.5]).reshape((1, 1))
self.R = np.array([0.5]).reshape((1, 1))
# gym attributes
self.viewer = None
high = np.array([self.max_pos])
self.action_space = spaces.Box(low=-self.max_action,
high=self.max_action,
shape=(1,))
self.observation_space = spaces.Box(low=-high, high=high)
self.initial_states = np.array([[1, 2, 5, 7, 10]]).T
# initialize state
self.seed()
self.reset()
def step(self, action, render=False):
u = np.clip(action, -self.max_action, self.max_action)
noise = 0
if self.sigma_noise > 0:
noise = self.np_random.randn() * self.sigma_noise
xn = np.clip(np.dot(self.A, self.state) + np.dot(self.B, u) + noise, -self.max_pos, self.max_pos)
cost = np.dot(self.state,
np.dot(self.Q, self.state)) + \
np.dot(u, np.dot(self.R, u))
self.state = np.array(xn.ravel())
if self.discrete_reward:
if abs(self.state[0]) <= 2 and abs(u) <= 2:
return self.get_state(), 0, False, {}
return self.get_state(), -1, False, {}
return self.get_state(), -np.asscalar(cost), False, {}
def reset(self, state=None):
if state is None:
self.state = np.array([self.np_random.uniform(low=-self.max_pos,
high=self.max_pos)])
else:
self.state = np.array(state)
return self.get_state()
def get_state(self):
return np.array(self.state)
def seed(self, seed=None):
self.np_random, seed = seeding.np_random(seed)
return [seed]
def _render(self, mode='human', close=False):
if close:
if self.viewer is not None:
self.viewer.close()
self.viewer = None
return
screen_width = 600
screen_height = 400
world_width = (self.max_pos * 2) * 2
scale = screen_width / world_width
bally = 100
ballradius = 3
if self.viewer is None:
clearance = 0 # y-offset
from gym.envs.classic_control import rendering
self.viewer = rendering.Viewer(screen_width, screen_height)
mass = rendering.make_circle(ballradius * 2)
mass.set_color(.8, .3, .3)
mass.add_attr(rendering.Transform(translation=(0, clearance)))
self.masstrans = rendering.Transform()
mass.add_attr(self.masstrans)
self.viewer.add_geom(mass)
self.track = rendering.Line((0, bally), (screen_width, bally))
self.track.set_color(0.5, 0.5, 0.5)
self.viewer.add_geom(self.track)
zero_line = rendering.Line((screen_width / 2, 0),
(screen_width / 2, screen_height))
zero_line.set_color(0.5, 0.5, 0.5)
self.viewer.add_geom(zero_line)
x = self.state[0]
ballx = x * scale + screen_width / 2.0
self.masstrans.set_translation(ballx, bally)
return self.viewer.render(return_rgb_array=mode == 'rgb_array')
def _computeP2(self, K):
"""
This function computes the Riccati equation associated to the LQG
problem.
Args:
K (matrix): the matrix associated to the linear controller K * x
Returns:
P (matrix): the Riccati Matrix
"""
I = np.eye(self.Q.shape[0], self.Q.shape[1])
if np.array_equal(self.A, I) and np.array_equal(self.B, I):
P = (self.Q + np.dot(K.T, np.dot(self.R, K))) / (I - self.gamma *
(I + 2 * K + K **
2))
else:
tolerance = 0.0001
converged = False
P = np.eye(self.Q.shape[0], self.Q.shape[1])
while not converged:
Pnew = self.Q + self.gamma * np.dot(self.A.T,
np.dot(P, self.A)) + \
self.gamma * np.dot(K.T, np.dot(self.B.T,
np.dot(P, self.A))) + \
self.gamma * np.dot(self.A.T,
np.dot(P, np.dot(self.B, K))) + \
self.gamma * np.dot(K.T,
np.dot(self.B.T,
np.dot(P, np.dot(self.B,
K)))) + \
np.dot(K.T, np.dot(self.R, K))
converged = np.max(np.abs(P - Pnew)) < tolerance
P = Pnew
return P
def computeOptimalK(self):
"""
This function computes the optimal linear controller associated to the
LQG problem (u = K * x).
Returns:
K (matrix): the optimal controller
"""
P = np.eye(self.Q.shape[0], self.Q.shape[1])
for i in range(100):
K = -self.gamma * np.dot(np.linalg.inv(
self.R + self.gamma * (np.dot(self.B.T, np.dot(P, self.B)))),
np.dot(self.B.T, np.dot(P, self.A)))
P = self._computeP2(K)
K = -self.gamma * np.dot(np.linalg.inv(self.R + self.gamma *
(np.dot(self.B.T,
np.dot(P, self.B)))),
np.dot(self.B.T, np.dot(P, self.A)))
return K
def computeJ(self, K, Sigma, n_random_x0=100):
"""
This function computes the discounted reward associated to the provided
linear controller (u = Kx + \epsilon, \epsilon \sim N(0,\Sigma)).
Args:
K (matrix): the controller matrix
Sigma (matrix): covariance matrix of the zero-mean noise added to
the controller action
n_random_x0: the number of samples to draw in order to average over
the initial state
Returns:
J (float): The discounted reward
"""
if isinstance(K, Number):
K = np.array([K]).reshape(1, 1)
if isinstance(Sigma, Number):
Sigma = np.array([Sigma]).reshape(1, 1)
P = self._computeP2(K)
W = (1 / (1 - self.gamma)) * \
np.trace(np.dot(
Sigma, (self.R + self.gamma * np.dot(self.B.T,
np.dot(P, self.B)))))
if np.size(K)==1:
return np.asscalar(-self.max_pos**2*P/3 - W)
J = 0.0
for i in range(n_random_x0):
self.reset()
x0 = self.get_state()
J -= np.dot(x0.T, np.dot(P, x0)) \
+ W
J /= n_random_x0
return J
def computeQFunction(self, x, u, K, Sigma, n_random_xn=100):
"""
This function computes the Q-value of a pair (x,u) given the linear
controller Kx + epsilon where epsilon \sim N(0, Sigma).
Args:
x (int, array): the state
u (int, array): the action
K (matrix): the controller matrix
Sigma (matrix): covariance matrix of the zero-mean noise added to
the controller action
n_random_xn: the number of samples to draw in order to average over
the next state
Returns:
Qfun (float): The Q-value in the given pair (x,u) under the given
controller
"""
if isinstance(x, Number):
x = np.array([x])
if isinstance(u, Number):
u = np.array([u])
if isinstance(K, Number):
K = np.array([K]).reshape(1, 1)
if isinstance(Sigma, Number):
Sigma = np.array([Sigma]).reshape(1, 1)
P = self._computeP2(K)
Qfun = 0
for i in range(n_random_xn):
noise = self.np_random.randn() * self.sigma_noise
action_noise = self.np_random.multivariate_normal(
np.zeros(Sigma.shape[0]), Sigma, 1)
nextstate = np.dot(self.A, x) + np.dot(self.B,
u + action_noise) + noise
Qfun -= np.dot(x.T, np.dot(self.Q, x)) + \
np.dot(u.T, np.dot(self.R, u)) + \
self.gamma * np.dot(nextstate.T, np.dot(P, nextstate)) + \
(self.gamma / (1 - self.gamma)) * \
np.trace(np.dot(Sigma,
self.R + self.gamma *
np.dot(self.B.T, np.dot(P, self.B))))
Qfun = np.asscalar(Qfun) / n_random_xn
return Qfun
# TODO check following code
# def computeM(self, K):
# kb = np.dot(K, self.B.T)
# size = self.A.shape[1] ** 2;
# AT = self.A.T
# return np.eye(size) - self.gamma * (np.kron(AT, AT) - np.kron(AT, kb) - np.kron(kb, AT) + np.kron(kb, kb))
#
# def computeL(self, K):
# return self.Q + np.dot(K, np.dot(self.R, K.T))
#
# def to_vec(self, m):
# n_dim = self.A.shape[1]
# v = m.reshape(n_dim * n_dim, 1)
# return v
#
# def to_mat(self, v):
# n_dim = self.A.shape[1]
# M = v.reshape(n_dim, n_dim)
# return M
#
# def computeJ(self, k, Sigma, n_random_x0=100):
# J = 0
# K = k
# if len(k.shape) == 1:
# K = np.diag(k)
# P = self.computeP(K)
# for i in range(n_random_x0):
# self.reset()
# x0 = self.state
# v = np.asscalar(x0.T * P * x0 + np.trace(
# np.dot(Sigma, (self.R + np.dot(self.gamma, np.dot(self.B.T, np.dot(P, self.B)))))) / (1.0 - self.gamma))
# J += -v
# J /= n_random_x0
#
# return J
#
# def solveRiccati(self, k):
# K = k
# if len(k.shape) == 1:
# K = np.diag(k)
# return self.computeP(K)
#
# def riccatiRHS(self, k, P, r):
# K = k
# if len(k.shape) == 1:
# K = np.diag(k)
# return self.Q + self.gamma * (np.dot(self.A.T, np.dot(self.P, self.A))
# - np.dot(K, np.dot(self.B.T, np.dot(self.P, self.A)))
# - np.dot(self.A.T, np.dot(self.P, np.dot(self.B, K.T)))
# + np.dot(K, np.dot(self.B.T, np.dot(self.P, np.dot(self.B, K.T))))) \
# + np.dot(K, np.dot(self.R, K.T))
#
# def computeP(self, K):
# L = self.computeL(K)
# M = self.computeM(K)
#
# vecP = np.linalg.solve(M, self.to_vec(L))
#
# P = self.to_mat(vecP)
# return P