Skip to content

Commit c704c67

Browse files
committed
update cvxpy to v1.0
1 parent 39aeb02 commit c704c67

File tree

2 files changed

+60
-57
lines changed

2 files changed

+60
-57
lines changed

Diff for: inverted_pendulum_mpc_control/inverted_pendulum_mpc_control.py

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ def mpc_control(x0):
7777
cost += cvxpy.quad_form(x[:, t + 1], Q)
7878
cost += cvxpy.quad_form(u[:, t], R)
7979
constr += [x[:, t + 1] == A * x[:, t] + B * u[:, t]]
80+
# print(x0)
8081
constr += [x[:, 0] == x0[:, 0]]
8182
prob = cvxpy.Problem(cvxpy.Minimize(cost), constr)
8283

Diff for: mpc_modeling/mpc_modeling.py

+59-57
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
DEBUG_ = False
1717

18+
print("cvxpy version:", cvxpy.__version__)
19+
1820

1921
def use_modeling_tool(A, B, N, Q, R, P, x0, umax=None, umin=None, xmin=None, xmax=None):
2022
"""
@@ -23,8 +25,8 @@ def use_modeling_tool(A, B, N, Q, R, P, x0, umax=None, umin=None, xmin=None, xma
2325
(nx, nu) = B.shape
2426

2527
# mpc calculation
26-
x = cvxpy.Variable(nx, N + 1)
27-
u = cvxpy.Variable(nu, N)
28+
x = cvxpy.Variable((nx, N + 1))
29+
u = cvxpy.Variable((nu, N))
2830

2931
costlist = 0.0
3032
constrlist = []
@@ -36,23 +38,24 @@ def use_modeling_tool(A, B, N, Q, R, P, x0, umax=None, umin=None, xmin=None, xma
3638
constrlist += [x[:, t + 1] == A * x[:, t] + B * u[:, t]]
3739

3840
if xmin is not None:
39-
constrlist += [x[:, t] >= xmin]
41+
constrlist += [x[:, t] >= xmin[:, 0]]
4042
if xmax is not None:
41-
constrlist += [x[:, t] <= xmax]
43+
constrlist += [x[:, t] <= xmax[:, 0]]
4244

4345
costlist += 0.5 * cvxpy.quad_form(x[:, N], P) # terminal cost
4446
if xmin is not None:
45-
constrlist += [x[:, N] >= xmin]
47+
constrlist += [x[:, N] >= xmin[:, 0]]
4648
if xmax is not None:
47-
constrlist += [x[:, N] <= xmax]
48-
49-
prob = cvxpy.Problem(cvxpy.Minimize(costlist), constrlist)
49+
constrlist += [x[:, N] <= xmax[:, 0]]
5050

51-
prob.constraints += [x[:, 0] == x0] # inital state constraints
5251
if umax is not None:
53-
prob.constraints += [u <= umax] # input constraints
52+
constrlist += [u <= umax] # input constraints
5453
if umin is not None:
55-
prob.constraints += [u >= umin] # input constraints
54+
constrlist += [u >= umin] # input constraints
55+
56+
constrlist += [x[:, 0] == x0[:, 0]] # inital state constraints
57+
58+
prob = cvxpy.Problem(cvxpy.Minimize(costlist), constrlist)
5659

5760
prob.solve(verbose=True)
5861

@@ -74,25 +77,25 @@ def opt_mpc_with_input_const(A, B, N, Q, R, P, x0, umax=None, umin=None):
7477
Ai = A
7578
AA = Ai
7679
for i in range(2, N + 1):
77-
Ai = A * Ai
80+
Ai = A @ Ai
7881
AA = np.vstack((AA, Ai))
7982
# print(AA)
8083

8184
# calc BB
8285
AiB = B
8386
BB = np.kron(np.eye(N), AiB)
8487
for i in range(1, N):
85-
AiB = A * AiB
88+
AiB = A @ AiB
8689
BB += np.kron(np.diag(np.ones(N - i), -i), AiB)
8790
# print(BB)
8891

8992
RR = np.kron(np.eye(N), R)
9093
QQ = scipy.linalg.block_diag(np.kron(np.eye(N - 1), Q), P)
9194

92-
H = (BB.T * QQ * BB + RR)
95+
H = (BB.T @ QQ @ BB + RR)
9396
# print(H)
9497

95-
gx0 = BB.T * QQ * AA * x0
98+
gx0 = BB.T @ QQ @ AA @ x0
9699
# print(gx0)
97100
P = matrix(H)
98101
q = matrix(gx0)
@@ -121,10 +124,10 @@ def opt_mpc_with_input_const(A, B, N, Q, R, P, x0, umax=None, umin=None):
121124

122125
sol = cvxopt.solvers.qp(P, q, G, h)
123126

124-
u = np.matrix(sol["x"])
127+
u = np.array(sol["x"])
125128

126129
# recover x
127-
xx = AA * x0 + BB * u
130+
xx = AA @ x0 + BB @ u
128131
x = np.vstack((x0.T, xx.reshape(N, nx)))
129132

130133
return x, u
@@ -191,7 +194,7 @@ def opt_mpc_with_state_constr(A, B, N, Q, R, P, x0, xmin=None, xmax=None, umax=N
191194
# print(Ae.shape)
192195

193196
# calc be
194-
be = np.vstack((A, np.zeros(((N - 1) * nx, nx)))) * x0
197+
be = np.vstack((A, np.zeros(((N - 1) * nx, nx)))) @ x0
195198
# print(be)
196199

197200
np.set_printoptions(precision=3)
@@ -221,7 +224,7 @@ def opt_mpc_with_state_constr(A, B, N, Q, R, P, x0, xmin=None, xmax=None, umax=N
221224
sol = cvxopt.solvers.qp(P, q, G, h, A=A, b=b)
222225

223226
# print(sol)
224-
fx = np.matrix(sol["x"])
227+
fx = np.array(sol["x"])
225228
# print(fx)
226229

227230
u = fx[0:N * nu].reshape(N, nu).T
@@ -236,9 +239,9 @@ def opt_mpc_with_state_constr(A, B, N, Q, R, P, x0, xmin=None, xmax=None, umax=N
236239

237240
def test1():
238241
print("start!!")
239-
A = np.matrix([[0.8, 1.0], [0, 0.9]])
242+
A = np.array([[0.8, 1.0], [0, 0.9]])
240243
# print(A)
241-
B = np.matrix([[-1.0], [2.0]])
244+
B = np.array([[-1.0], [2.0]])
242245
# print(B)
243246
(nx, nu) = B.shape
244247
# print(nx, nu)
@@ -252,7 +255,8 @@ def test1():
252255
# print(P)
253256
# umax = 0.7
254257

255-
x0 = np.matrix([[1.0], [2.0]]) # init state
258+
x0 = np.array([[1.0],
259+
[2.0]]) # init state
256260

257261
x, u = use_modeling_tool(A, B, N, Q, R, P, x0)
258262

@@ -274,7 +278,6 @@ def test1():
274278
u = np.array(u).flatten()
275279

276280
if DEBUG_:
277-
# flg, ax = plt.subplots(1)
278281
plt.plot(x1, '*r', label="x1")
279282
plt.plot(x2, '*b', label="x2")
280283
plt.plot(u, '*k', label="u")
@@ -288,8 +291,8 @@ def test1():
288291

289292
def test2():
290293
print("start!!")
291-
A = np.matrix([[0.8, 1.0], [0, 0.9]])
292-
B = np.matrix([[-1.0], [2.0]])
294+
A = np.array([[0.8, 1.0], [0, 0.9]])
295+
B = np.array([[-1.0], [2.0]])
293296
(nx, nu) = B.shape
294297

295298
N = 10 # number of horizon
@@ -299,10 +302,9 @@ def test2():
299302
umax = 0.7
300303
umin = -0.7
301304

302-
x0 = np.matrix([[1.0], [2.0]]) # init state
305+
x0 = np.array([[1.0], [2.0]]) # init state
303306

304307
x, u = use_modeling_tool(A, B, N, Q, R, P, x0, umax=umax, umin=umin)
305-
# x, u = use_modeling_tool(A, B, N, Q, R, P, x0, umin=umin)
306308

307309
rx1 = np.array(x[0, :]).flatten()
308310
rx2 = np.array(x[1, :]).flatten()
@@ -336,8 +338,8 @@ def test2():
336338

337339
def test3():
338340
print("start!!")
339-
A = np.matrix([[0.8, 1.0], [0, 0.9]])
340-
B = np.matrix([[-1.0], [2.0]])
341+
A = np.array([[0.8, 1.0], [0, 0.9]])
342+
B = np.array([[-1.0], [2.0]])
341343
(nx, nu) = B.shape
342344

343345
N = 10 # number of horizon
@@ -347,7 +349,7 @@ def test3():
347349
umax = 0.7
348350
umin = -0.7
349351

350-
x0 = np.matrix([[1.0], [2.0]]) # init state
352+
x0 = np.array([[1.0], [2.0]]) # init state
351353
x, u = use_modeling_tool(A, B, N, Q, R, P, x0, umax=umax, umin=umin)
352354

353355
rx1 = np.array(x[0, :]).flatten()
@@ -383,16 +385,16 @@ def test3():
383385

384386
def test4():
385387
print("start!!")
386-
A = np.matrix([[0.8, 1.0], [0, 0.9]])
387-
B = np.matrix([[-1.0], [2.0]])
388+
A = np.array([[0.8, 1.0], [0, 0.9]])
389+
B = np.array([[-1.0], [2.0]])
388390
(nx, nu) = B.shape
389391

390392
N = 10 # number of horizon
391393
Q = np.eye(nx)
392394
R = np.eye(nu)
393395
P = np.eye(nx)
394396

395-
x0 = np.matrix([[1.0], [2.0]]) # init state
397+
x0 = np.array([[1.0], [2.0]]) # init state
396398

397399
x, u = use_modeling_tool(A, B, N, Q, R, P, x0)
398400

@@ -429,16 +431,16 @@ def test4():
429431

430432
def test5():
431433
print("start!!")
432-
A = np.matrix([[0.8, 1.0], [0, 0.9]])
433-
B = np.matrix([[-1.0], [2.0]])
434+
A = np.array([[0.8, 1.0], [0, 0.9]])
435+
B = np.array([[-1.0], [2.0]])
434436
(nx, nu) = B.shape
435437

436438
N = 10 # number of horizon
437439
Q = np.eye(nx)
438440
R = np.eye(nu)
439441
P = np.eye(nx)
440442

441-
x0 = np.matrix([[1.0], [2.0]]) # init state
443+
x0 = np.array([[1.0], [2.0]]) # init state
442444
umax = 0.7
443445

444446
x, u = use_modeling_tool(A, B, N, Q, R, P, x0, umax=umax)
@@ -475,23 +477,23 @@ def test5():
475477

476478
def test6():
477479
print("start!!")
478-
A = np.matrix([[0.8, 1.0], [0, 0.9]])
479-
B = np.matrix([[-1.0], [2.0]])
480+
A = np.array([[0.8, 1.0], [0, 0.9]])
481+
B = np.array([[-1.0], [2.0]])
480482
(nx, nu) = B.shape
481483

482484
N = 10 # number of horizon
483485
Q = np.eye(nx)
484486
R = np.eye(nu)
485487
P = np.eye(nx)
486488

487-
x0 = np.matrix([[1.0], [2.0]]) # init state
489+
x0 = np.array([[1.0], [2.0]]) # init state
488490
umax = 0.7
489491
umin = -0.7
490492

491-
x0 = np.matrix([[1.0], [2.0]]) # init state
493+
x0 = np.array([[1.0], [2.0]]) # init state
492494

493-
xmin = np.matrix([[-3.5], [-0.5]]) # state constraints
494-
xmax = np.matrix([[3.5], [2.0]]) # state constraints
495+
xmin = np.array([[-3.5], [-0.5]]) # state constraints
496+
xmax = np.array([[3.5], [2.0]]) # state constraints
495497

496498
x, u = use_modeling_tool(A, B, N, Q, R, P, x0,
497499
umax=umax, umin=umin, xmin=xmin, xmax=xmax)
@@ -529,23 +531,23 @@ def test6():
529531

530532
def test7():
531533
print("start!!")
532-
A = np.matrix([[0.8, 1.0], [0, 0.9]])
533-
B = np.matrix([[-1.0], [2.0]])
534+
A = np.array([[0.8, 1.0], [0, 0.9]])
535+
B = np.array([[-1.0], [2.0]])
534536
(nx, nu) = B.shape
535537

536538
N = 3 # number of horizon
537539
Q = np.eye(nx)
538540
R = np.eye(nu)
539541
P = np.eye(nx)
540542

541-
x0 = np.matrix([[1.0], [2.0]]) # init state
543+
x0 = np.array([[1.0], [2.0]]) # init state
542544
umax = 0.7
543545
umin = -0.7
544546

545-
x0 = np.matrix([[1.0], [2.0]]) # init state
547+
x0 = np.array([[1.0], [2.0]]) # init state
546548

547-
# xmin = np.matrix([[-3.5], [-0.5]]) # state constraints
548-
# xmax = np.matrix([[3.5], [2.0]]) # state constraints
549+
# xmin = np.array([[-3.5], [-0.5]]) # state constraints
550+
# xmax = np.array([[3.5], [2.0]]) # state constraints
549551

550552
x, u = use_modeling_tool(A, B, N, Q, R, P, x0, umax=umax, umin=umin)
551553
# x, u = use_modeling_tool(A, B, N, Q, R, P, x0, umax=umax, umin=umin, xmin=xmin, xmax=xmax)
@@ -590,23 +592,23 @@ def test_output_check(rx1, rx2, ru, x1, x2, u):
590592
print("test x1")
591593
for (i, j) in zip(rx1, x1):
592594
print(i, j)
593-
assert (i - j) <= 0.0001, "Error"
595+
assert (i - j) <= 0.01, "Error"
594596
print("test x2")
595597
for (i, j) in zip(rx2, x2):
596598
print(i, j)
597-
assert (i - j) <= 0.0001, "Error"
599+
assert (i - j) <= 0.01, "Error"
598600
print("test u")
599601
for (i, j) in zip(ru, u):
600602
print(i, j)
601-
assert (i - j) <= 0.0001, "Error"
603+
assert (i - j) <= 0.01, "Error"
602604

603605

604606
if __name__ == '__main__':
605607
DEBUG_ = True
606-
# test1()
607-
# test2()
608-
# test3()
609-
# test4()
610-
# test5()
611-
# test6()
608+
test1()
609+
test2()
610+
test3()
611+
test4()
612+
test5()
613+
test6()
612614
test7()

0 commit comments

Comments
 (0)