-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMPC_LCS_R.py
394 lines (313 loc) · 14 KB
/
MPC_LCS_R.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
from casadi import *
from models.LCS import LCDyn
class MPCLCSR:
MPC_PARAM = dict(
x_lb=None,
x_ub=None,
u_lb=None,
u_ub=None,
cp_param=np.array([]),
cf_param=np.array([]),
)
def __init__(self, lcs: LCDyn, epsilon=1e-2):
# some constant
self.my_inf = 1e6
# relaxing parameter for the complementarity condition
self.epsilon = epsilon
self.lcs = lcs
assert hasattr(self.lcs, 'comple_fn'), "Please initialize the NLCS system"
if not hasattr(self.lcs, 'dyn_explicit_fn'):
if self.lcs.n_x == self.lcs.n_z:
self.lcs.initDyn()
else:
assert False, "please use initDyn method to initialize the full dynamics equation"
# define the variables
self.n_dyn_aux = self.lcs.n_aux
self.dyn_aux = SX.sym('aux', self.n_dyn_aux)
self.n_x = self.lcs.n_x
self.x = SX.sym('x', self.n_x)
self.n_u = self.lcs.n_u
self.u = SX.sym('u', self.n_u)
self.n_lam = self.lcs.n_lam
self.lam = SX.sym('lam', self.n_lam)
# define the slack variable for the complementarity equation
self.s = SX.sym('s', self.n_lam)
self.comple_equ = self.lcs.comple_fn(self.dyn_aux, self.x, self.u, self.lam)
self.dyn_equ = self.lcs.dyn_equ_fn(self.dyn_aux, self.x, self.u, self.lam)
# set cost function
def setCost(self, cost_path_fn, cost_final_fn=None):
# set the path cost
if cost_path_fn.n_in() > 2:
self.n_cp_param = cost_path_fn.numel_in(2)
self.cp_param = SX.sym('cp_param', self.n_cp_param)
path_cost = cost_path_fn(self.x, self.u, self.cp_param)
else:
self.n_cp_param = 0
self.cp_param = SX.sym('cp_param', self.n_cp_param)
path_cost = cost_path_fn(self.x, self.u)
# set the final cost
if cost_final_fn is None:
cost_final_fn = Function('cost_final_fn', [self.x], [0.0])
else:
cost_final_fn = cost_final_fn
if cost_final_fn.n_in() > 1:
self.n_cf_param = cost_final_fn.numel_in(1)
self.cf_param = SX.sym('cf_param', self.n_cf_param)
final_cost = cost_final_fn(self.x, self.cf_param)
else:
self.n_cf_param = 0
self.cf_param = SX.sym('cf_param', self.n_cf_param)
final_cost = cost_final_fn(self.x)
# tunable params
self.aux = self.dyn_aux
self.n_aux = self.aux.numel()
# compose the dynamics
self.dyn_fn = Function('dyn_fn', [self.aux, self.x, self.u, self.lam], [self.dyn_equ])
# cost function
self.cp_fn = Function('cp_fn', [self.x, self.u, self.cp_param], [path_cost])
self.jac_cp2x_fn = Function('jac_cp2x_fn', [self.x, self.u, self.cp_param], [jacobian(path_cost, self.x)])
self.jac_cp2u_fn = Function('jac_cp2u_fn', [self.x, self.u, self.cp_param], [jacobian(path_cost, self.u)])
self.cf_fn = Function('cf_fn', [self.x, self.cf_param], [final_cost])
self.jac_cf2x_fn = Function('jac_cf2x_fn', [self.x, self.cf_param], [jacobian(final_cost, self.x)])
# complementarity constraints
eq_cstr = vertcat(self.lam * self.s - self.epsilon * DM.ones(self.n_lam), self.comple_equ - self.s)
self.eq_cstr_fn = Function('eq_cstr_fn', [self.aux, self.x, self.u, self.lam, self.s], [eq_cstr])
# initialize the trajectory opt solver
def initTrajSolver(self, horizon):
# set the system horizon
self.horizon = horizon
# set the initial condition
x0 = SX.sym('x0', self.n_x)
# Start with an empty NLP
w = []
w0 = []
lbw = []
ubw = []
J = 0.0
g = []
lbg = []
ubg = []
# initial condition
xk = x0
# control lower bound
self.u_lb = SX.sym('u_lb', self.n_u)
self.u_ub = SX.sym('u_ub', self.n_u)
self.x_lb = SX.sym('x_lb', self.n_x)
self.x_ub = SX.sym('x_ub', self.n_x)
for k in range(self.horizon):
# control at time k
uk = SX.sym('U' + str(k), self.n_u)
w += [uk]
lbw += [self.u_lb]
ubw += [self.u_ub]
w0 += [0.5 * (self.u_lb + self.u_ub)]
# lam at time k
lamk = SX.sym('Lam' + str(k), self.n_lam)
w += [lamk]
lbw += [DM.zeros(self.n_lam)]
ubw += [self.my_inf * DM.ones(self.n_lam)]
w0 += [DM.ones(self.n_lam)]
# s at time k
sk = SX.sym('S' + str(k), self.n_lam)
w += [sk]
lbw += [DM.zeros(self.n_lam)]
ubw += [self.my_inf * DM.ones(self.n_lam)]
w0 += [DM.ones(self.n_lam)]
# predicted next state
yk = self.dyn_fn(self.aux, xk, uk, lamk)
# constraints
g += [self.eq_cstr_fn(self.aux, xk, uk, lamk, sk)]
lbg += [DM.zeros(self.n_lam + self.n_lam)]
ubg += [DM.zeros(self.n_lam + self.n_lam)]
# compute the current cost + barrier cost
ck = self.cp_fn(xk, uk, self.cp_param)
J = J + ck
# New NLP variable for state at end of interval
xk = SX.sym('X' + str(k + 1), self.n_x)
w += [xk]
lbw += [self.x_lb]
ubw += [self.x_ub]
w0 += [0.5 * (self.x_lb + self.x_ub)]
# Add equality constraint between predicted next state and true next state
g += [yk - xk]
lbg += [DM.zeros(self.n_x)]
ubg += [DM.zeros(self.n_x)]
# also add the final cost
J = J + self.cf_fn(xk, self.cf_param)
# initial guess and bounds
self.nlp_w0_fn = Function('nlp_w0', [self.x_lb, self.x_ub, self.u_lb, self.u_ub], [vcat(w0)])
self.nlp_lbw_fn = Function('nlp_w0', [self.x_lb, self.x_ub, self.u_lb, self.u_ub], [vcat(lbw)])
self.nlp_ubw_fn = Function('nlp_w0', [self.x_lb, self.x_ub, self.u_lb, self.u_ub], [vcat(ubw)])
self.nlp_ubg = vcat(ubg)
self.nlp_lbg = vcat(lbg)
# control parameter
self.nlp_param = vertcat(self.aux, x0, self.cp_param, self.cf_param)
# Create an NLP solver
opts = {'ipopt.print_level': 0, 'ipopt.sb': 'yes', 'print_time': 0}
prob = {'f': J, 'x': vertcat(*w), 'g': vertcat(*g), 'p': self.nlp_param}
self.traj_solver = nlpsol('solver', 'ipopt', prob, opts)
# solve the trajectory
def solveTraj(self, aux_val, x0, mpc_param=None, nlp_guess=None):
if mpc_param is None:
mpc_param = self.MPC_PARAM
assert hasattr(self, 'traj_solver'), \
"please initialize trajectory solve using <initTrajSolver()> method "
if mpc_param['x_lb'] is None:
x_lb = -self.my_inf * np.ones(self.n_x)
else:
x_lb = mpc_param['x_lb']
if mpc_param['x_ub'] is None:
x_ub = self.my_inf * np.ones(self.n_x)
else:
x_ub = mpc_param['x_ub']
if mpc_param['u_lb'] is None:
u_lb = -self.my_inf * np.ones(self.n_u)
else:
u_lb = mpc_param['u_lb']
if mpc_param['u_ub'] is None:
u_ub = self.my_inf * np.ones(self.n_u)
else:
u_ub = mpc_param['u_ub']
# the cost function parameter
if self.n_cp_param == 0:
cp_param = np.array([])
else:
assert self.n_cp_param == len(mpc_param['cp_param']), 'Path cost param is not set yet!'
cp_param = mpc_param['cp_param']
if self.n_cf_param == 0:
cf_param = np.array([])
else:
assert self.n_cf_param == len(mpc_param['cf_param']), 'Path cost param is not set yet!'
cf_param = mpc_param['cf_param']
if nlp_guess is None:
nlp_guess = self.nlp_w0_fn(x_lb, x_ub, u_lb, u_ub)
nlp_lbw = self.nlp_lbw_fn(x_lb, x_ub, u_lb, u_ub)
nlp_ubw = self.nlp_ubw_fn(x_lb, x_ub, u_lb, u_ub)
# construct the control parameter
nlp_param = vertcat(aux_val, x0, cp_param, cf_param)
# Solve the NLP (i.e., trajectory optimization)
raw_sol = self.traj_solver(x0=nlp_guess,
lbx=nlp_lbw, ubx=nlp_ubw,
lbg=self.nlp_lbg, ubg=self.nlp_ubg,
p=nlp_param)
w_opt = raw_sol['x'].full().flatten()
opt_cost = raw_sol['f'].full().item()
# extract the solution from the raw solution
sol_traj = np.reshape(w_opt, (-1, self.n_u + self.n_lam + self.n_lam + self.n_x))
u_opt_traj = sol_traj[:, 0:self.n_u]
lam_opt_traj = sol_traj[:, self.n_u:self.n_u + self.n_lam]
s_opt_traj = sol_traj[:, self.n_u + self.n_lam:self.n_u + self.n_lam + self.n_lam]
x_opt_traj = sol_traj[:, self.n_u + self.n_lam + self.n_lam:]
x_opt_traj = np.vstack((x0, x_opt_traj))
# this is used for differentiation
control_opt_traj = sol_traj[:, 0:self.n_u + self.n_lam + self.n_lam]
# extract the dual variable
dual_g = raw_sol['lam_g'].full().flatten()
dual_traj = np.reshape(dual_g, (-1, self.n_lam + self.n_lam + self.n_x))
dual_dyn_traj = dual_traj[:, -self.n_x:]
dual_cstr_traj = dual_traj[:, 0:self.n_lam + self.n_lam]
return dict(u_opt_traj=u_opt_traj,
lam_opt_traj=lam_opt_traj,
s_opt_traj=s_opt_traj,
x_opt_traj=x_opt_traj,
opt_cost=opt_cost,
dual_dyn_traj=dual_dyn_traj,
dual_cstr_traj=dual_cstr_traj,
raw_nlp_sol=w_opt,
raw_nlp_dual=dual_g,
control_opt_traj=control_opt_traj,
aux_val=aux_val,
)
# sample trajs around a nominal traj, temporally smoothing can be applied
def samlpeTraj(self, nominal_traj, n_sample=1, time_smooth=0.5, distribution='uniform', scale=0.1):
horizon = nominal_traj.shape[0]
prev_noise = 0
tiled_sample = []
for t in range(horizon):
# current control input
ut = nominal_traj[t]
tiled_ut = np.tile(ut, n_sample)
# draw sample
if distribution == 'gaussian':
noise_t = np.random.normal(loc=0, scale=scale, size=tiled_ut.shape)
else:
noise_t = np.random.uniform(low=-scale, high=scale, size=tiled_ut.shape)
if t == 0:
noise_t = noise_t
else:
noise_t = time_smooth * noise_t + (1 - time_smooth) * prev_noise
# store
tiled_sample.append(tiled_ut + noise_t)
prev_noise = noise_t
tiled_sample = np.array(tiled_sample)
return np.split(tiled_sample, n_sample, axis=1)
# forward dynamics given u traj and differentiate cost w.r.t. aux in dynamics
def rolloutTrajGrad(self, aux_val, x0, u_traj, solver='barrier', cp_param=np.array([]), cf_param=np.array([])):
# forward dynamics to obtain state trajectory
horizon = u_traj.shape[0]
x_traj = [x0]
# `accumulated' gradient at each step, w.r.t, aux,
grad_x2aux_traj = [np.zeros((self.n_x, self.n_aux))] # as x0 is given
grad_c2aux = np.zeros((1, self.n_aux))
sum_cost = 0.0
# 'instant' gradient each step
jac_y2x_traj = []
jac_y2u_traj = []
jac_y2aux_traj = []
jac_c2x_traj = []
jac_c2u_traj = []
for t in range(horizon):
# ------ dynamics things
ut = u_traj[t]
xt = x_traj[-1]
grad_xt2aux = grad_x2aux_traj[-1]
# forward and diff at one step
sol = self.lcs.forwardDiff(mu=self.epsilon, aux_val=aux_val, x_val=xt, u_val=ut, solver=solver)
yt = sol['y_val']
jac_y2xt = sol['jac_y2x_val']
jac_y2ut = sol['jac_y2u_val']
jac_y2aux = sol['jac_y2aux_val']
# store
x_traj.append(yt)
grad_x2aux_traj.append(jac_y2xt @ grad_xt2aux + jac_y2aux)
jac_y2x_traj.append(jac_y2xt)
jac_y2u_traj.append(jac_y2ut)
jac_y2aux_traj.append(jac_y2aux)
# ------ cost things
sum_cost += self.cp_fn(xt, ut, cp_param).full().item()
# jacobian of path cost
jac_c2xt = self.jac_cp2x_fn(xt, ut, cp_param).full()
jac_c2ut = self.jac_cp2u_fn(xt, ut, cp_param).full()
# accumulated grad w.r.t. aux
grad_c2aux += jac_c2xt @ grad_xt2aux
# store
jac_c2x_traj.append(jac_c2xt)
jac_c2u_traj.append(jac_c2ut)
# final time step
xt = x_traj[-1]
grad_xt2aux = grad_x2aux_traj[-1]
sum_cost += self.cf_fn(xt, cf_param).full().item()
jac_c2xt = self.jac_cf2x_fn(xt, cf_param).full()
jac_c2x_traj.append(jac_c2xt)
grad_c2aux += jac_c2xt @ grad_xt2aux
# post-processing
x_traj = np.array(x_traj)
# accumulated grad
grad_c2aux = grad_c2aux.ravel()
grad_x2aux_traj = np.array(grad_x2aux_traj)
# instant grad
jac_y2x_traj_vstack = np.vstack(jac_y2x_traj) # note without the info about x0
jac_y2u_traj_vstack = np.vstack(jac_y2u_traj) # note without the info about x0
jac_y2aux_traj_vstack = np.vstack(jac_y2aux_traj) # note without the info about x0
jac_c2x_traj_vstack = np.vstack(jac_c2x_traj)
jac_c2u_traj_vstack = np.vstack(jac_c2u_traj)
return dict(x_traj=x_traj,
cost=sum_cost,
grad_c2aux=grad_c2aux.ravel(),
grad_x2aux_traj=grad_x2aux_traj,
jac_c2x_traj_vstack=jac_c2x_traj_vstack, # horizon +1
jac_c2u_traj_vstack=jac_c2u_traj_vstack,
jac_y2x_traj_vstack=jac_y2x_traj_vstack,
jac_y2u_traj_vstack=jac_y2u_traj_vstack,
jac_y2aux_traj_vstack=jac_y2aux_traj_vstack,
)