15
15
16
16
DEBUG_ = False
17
17
18
+ print ("cvxpy version:" , cvxpy .__version__ )
19
+
18
20
19
21
def use_modeling_tool (A , B , N , Q , R , P , x0 , umax = None , umin = None , xmin = None , xmax = None ):
20
22
"""
@@ -23,8 +25,8 @@ def use_modeling_tool(A, B, N, Q, R, P, x0, umax=None, umin=None, xmin=None, xma
23
25
(nx , nu ) = B .shape
24
26
25
27
# 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 ) )
28
30
29
31
costlist = 0.0
30
32
constrlist = []
@@ -36,23 +38,24 @@ def use_modeling_tool(A, B, N, Q, R, P, x0, umax=None, umin=None, xmin=None, xma
36
38
constrlist += [x [:, t + 1 ] == A * x [:, t ] + B * u [:, t ]]
37
39
38
40
if xmin is not None :
39
- constrlist += [x [:, t ] >= xmin ]
41
+ constrlist += [x [:, t ] >= xmin [:, 0 ] ]
40
42
if xmax is not None :
41
- constrlist += [x [:, t ] <= xmax ]
43
+ constrlist += [x [:, t ] <= xmax [:, 0 ] ]
42
44
43
45
costlist += 0.5 * cvxpy .quad_form (x [:, N ], P ) # terminal cost
44
46
if xmin is not None :
45
- constrlist += [x [:, N ] >= xmin ]
47
+ constrlist += [x [:, N ] >= xmin [:, 0 ] ]
46
48
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 ]]
50
50
51
- prob .constraints += [x [:, 0 ] == x0 ] # inital state constraints
52
51
if umax is not None :
53
- prob . constraints += [u <= umax ] # input constraints
52
+ constrlist += [u <= umax ] # input constraints
54
53
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 )
56
59
57
60
prob .solve (verbose = True )
58
61
@@ -74,25 +77,25 @@ def opt_mpc_with_input_const(A, B, N, Q, R, P, x0, umax=None, umin=None):
74
77
Ai = A
75
78
AA = Ai
76
79
for i in range (2 , N + 1 ):
77
- Ai = A * Ai
80
+ Ai = A @ Ai
78
81
AA = np .vstack ((AA , Ai ))
79
82
# print(AA)
80
83
81
84
# calc BB
82
85
AiB = B
83
86
BB = np .kron (np .eye (N ), AiB )
84
87
for i in range (1 , N ):
85
- AiB = A * AiB
88
+ AiB = A @ AiB
86
89
BB += np .kron (np .diag (np .ones (N - i ), - i ), AiB )
87
90
# print(BB)
88
91
89
92
RR = np .kron (np .eye (N ), R )
90
93
QQ = scipy .linalg .block_diag (np .kron (np .eye (N - 1 ), Q ), P )
91
94
92
- H = (BB .T * QQ * BB + RR )
95
+ H = (BB .T @ QQ @ BB + RR )
93
96
# print(H)
94
97
95
- gx0 = BB .T * QQ * AA * x0
98
+ gx0 = BB .T @ QQ @ AA @ x0
96
99
# print(gx0)
97
100
P = matrix (H )
98
101
q = matrix (gx0 )
@@ -121,10 +124,10 @@ def opt_mpc_with_input_const(A, B, N, Q, R, P, x0, umax=None, umin=None):
121
124
122
125
sol = cvxopt .solvers .qp (P , q , G , h )
123
126
124
- u = np .matrix (sol ["x" ])
127
+ u = np .array (sol ["x" ])
125
128
126
129
# recover x
127
- xx = AA * x0 + BB * u
130
+ xx = AA @ x0 + BB @ u
128
131
x = np .vstack ((x0 .T , xx .reshape (N , nx )))
129
132
130
133
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
191
194
# print(Ae.shape)
192
195
193
196
# 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
195
198
# print(be)
196
199
197
200
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
221
224
sol = cvxopt .solvers .qp (P , q , G , h , A = A , b = b )
222
225
223
226
# print(sol)
224
- fx = np .matrix (sol ["x" ])
227
+ fx = np .array (sol ["x" ])
225
228
# print(fx)
226
229
227
230
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
236
239
237
240
def test1 ():
238
241
print ("start!!" )
239
- A = np .matrix ([[0.8 , 1.0 ], [0 , 0.9 ]])
242
+ A = np .array ([[0.8 , 1.0 ], [0 , 0.9 ]])
240
243
# print(A)
241
- B = np .matrix ([[- 1.0 ], [2.0 ]])
244
+ B = np .array ([[- 1.0 ], [2.0 ]])
242
245
# print(B)
243
246
(nx , nu ) = B .shape
244
247
# print(nx, nu)
@@ -252,7 +255,8 @@ def test1():
252
255
# print(P)
253
256
# umax = 0.7
254
257
255
- x0 = np .matrix ([[1.0 ], [2.0 ]]) # init state
258
+ x0 = np .array ([[1.0 ],
259
+ [2.0 ]]) # init state
256
260
257
261
x , u = use_modeling_tool (A , B , N , Q , R , P , x0 )
258
262
@@ -274,7 +278,6 @@ def test1():
274
278
u = np .array (u ).flatten ()
275
279
276
280
if DEBUG_ :
277
- # flg, ax = plt.subplots(1)
278
281
plt .plot (x1 , '*r' , label = "x1" )
279
282
plt .plot (x2 , '*b' , label = "x2" )
280
283
plt .plot (u , '*k' , label = "u" )
@@ -288,8 +291,8 @@ def test1():
288
291
289
292
def test2 ():
290
293
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 ]])
293
296
(nx , nu ) = B .shape
294
297
295
298
N = 10 # number of horizon
@@ -299,10 +302,9 @@ def test2():
299
302
umax = 0.7
300
303
umin = - 0.7
301
304
302
- x0 = np .matrix ([[1.0 ], [2.0 ]]) # init state
305
+ x0 = np .array ([[1.0 ], [2.0 ]]) # init state
303
306
304
307
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)
306
308
307
309
rx1 = np .array (x [0 , :]).flatten ()
308
310
rx2 = np .array (x [1 , :]).flatten ()
@@ -336,8 +338,8 @@ def test2():
336
338
337
339
def test3 ():
338
340
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 ]])
341
343
(nx , nu ) = B .shape
342
344
343
345
N = 10 # number of horizon
@@ -347,7 +349,7 @@ def test3():
347
349
umax = 0.7
348
350
umin = - 0.7
349
351
350
- x0 = np .matrix ([[1.0 ], [2.0 ]]) # init state
352
+ x0 = np .array ([[1.0 ], [2.0 ]]) # init state
351
353
x , u = use_modeling_tool (A , B , N , Q , R , P , x0 , umax = umax , umin = umin )
352
354
353
355
rx1 = np .array (x [0 , :]).flatten ()
@@ -383,16 +385,16 @@ def test3():
383
385
384
386
def test4 ():
385
387
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 ]])
388
390
(nx , nu ) = B .shape
389
391
390
392
N = 10 # number of horizon
391
393
Q = np .eye (nx )
392
394
R = np .eye (nu )
393
395
P = np .eye (nx )
394
396
395
- x0 = np .matrix ([[1.0 ], [2.0 ]]) # init state
397
+ x0 = np .array ([[1.0 ], [2.0 ]]) # init state
396
398
397
399
x , u = use_modeling_tool (A , B , N , Q , R , P , x0 )
398
400
@@ -429,16 +431,16 @@ def test4():
429
431
430
432
def test5 ():
431
433
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 ]])
434
436
(nx , nu ) = B .shape
435
437
436
438
N = 10 # number of horizon
437
439
Q = np .eye (nx )
438
440
R = np .eye (nu )
439
441
P = np .eye (nx )
440
442
441
- x0 = np .matrix ([[1.0 ], [2.0 ]]) # init state
443
+ x0 = np .array ([[1.0 ], [2.0 ]]) # init state
442
444
umax = 0.7
443
445
444
446
x , u = use_modeling_tool (A , B , N , Q , R , P , x0 , umax = umax )
@@ -475,23 +477,23 @@ def test5():
475
477
476
478
def test6 ():
477
479
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 ]])
480
482
(nx , nu ) = B .shape
481
483
482
484
N = 10 # number of horizon
483
485
Q = np .eye (nx )
484
486
R = np .eye (nu )
485
487
P = np .eye (nx )
486
488
487
- x0 = np .matrix ([[1.0 ], [2.0 ]]) # init state
489
+ x0 = np .array ([[1.0 ], [2.0 ]]) # init state
488
490
umax = 0.7
489
491
umin = - 0.7
490
492
491
- x0 = np .matrix ([[1.0 ], [2.0 ]]) # init state
493
+ x0 = np .array ([[1.0 ], [2.0 ]]) # init state
492
494
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
495
497
496
498
x , u = use_modeling_tool (A , B , N , Q , R , P , x0 ,
497
499
umax = umax , umin = umin , xmin = xmin , xmax = xmax )
@@ -529,23 +531,23 @@ def test6():
529
531
530
532
def test7 ():
531
533
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 ]])
534
536
(nx , nu ) = B .shape
535
537
536
538
N = 3 # number of horizon
537
539
Q = np .eye (nx )
538
540
R = np .eye (nu )
539
541
P = np .eye (nx )
540
542
541
- x0 = np .matrix ([[1.0 ], [2.0 ]]) # init state
543
+ x0 = np .array ([[1.0 ], [2.0 ]]) # init state
542
544
umax = 0.7
543
545
umin = - 0.7
544
546
545
- x0 = np .matrix ([[1.0 ], [2.0 ]]) # init state
547
+ x0 = np .array ([[1.0 ], [2.0 ]]) # init state
546
548
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
549
551
550
552
x , u = use_modeling_tool (A , B , N , Q , R , P , x0 , umax = umax , umin = umin )
551
553
# 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):
590
592
print ("test x1" )
591
593
for (i , j ) in zip (rx1 , x1 ):
592
594
print (i , j )
593
- assert (i - j ) <= 0.0001 , "Error"
595
+ assert (i - j ) <= 0.01 , "Error"
594
596
print ("test x2" )
595
597
for (i , j ) in zip (rx2 , x2 ):
596
598
print (i , j )
597
- assert (i - j ) <= 0.0001 , "Error"
599
+ assert (i - j ) <= 0.01 , "Error"
598
600
print ("test u" )
599
601
for (i , j ) in zip (ru , u ):
600
602
print (i , j )
601
- assert (i - j ) <= 0.0001 , "Error"
603
+ assert (i - j ) <= 0.01 , "Error"
602
604
603
605
604
606
if __name__ == '__main__' :
605
607
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 ()
612
614
test7 ()
0 commit comments