@@ -27,8 +27,8 @@ def use_modeling_tool(A, B, N, Q, R, P, x0, umax=None, umin=None, xmin=None, xma
27
27
(nx , nu ) = B .shape
28
28
29
29
# mpc calculation
30
- x = cvxpy .Variable (nx , N + 1 )
31
- u = cvxpy .Variable (nu , N )
30
+ x = cvxpy .Variable (( nx , N + 1 ) )
31
+ u = cvxpy .Variable (( nu , N ) )
32
32
33
33
costlist = 0.0
34
34
constrlist = []
@@ -40,23 +40,23 @@ def use_modeling_tool(A, B, N, Q, R, P, x0, umax=None, umin=None, xmin=None, xma
40
40
constrlist += [x [:, t + 1 ] == A * x [:, t ] + B * u [:, t ]]
41
41
42
42
if xmin is not None :
43
- constrlist += [x [:, t ] >= xmin ]
43
+ constrlist += [x [:, t ] >= xmin [:, 0 ] ]
44
44
if xmax is not None :
45
- constrlist += [x [:, t ] <= xmax ]
45
+ constrlist += [x [:, t ] <= xmax [:, 0 ] ]
46
46
47
47
costlist += 0.5 * cvxpy .quad_form (x [:, N ], P ) # terminal cost
48
48
if xmin is not None :
49
- constrlist += [x [:, N ] >= xmin ]
49
+ constrlist += [x [:, N ] >= xmin [:, 0 ] ]
50
50
if xmax is not None :
51
- constrlist += [x [:, N ] <= xmax ]
51
+ constrlist += [x [:, N ] <= xmax [:, 0 ] ]
52
52
53
- prob = cvxpy .Problem (cvxpy .Minimize (costlist ), constrlist )
54
-
55
- prob .constraints += [x [:, 0 ] == x0 ] # inital state constraints
53
+ constrlist += [x [:, 0 ] == x0 [:, 0 ]] # inital state constraints
56
54
if umax is not None :
57
- prob . constraints += [u <= umax ] # input constraints
55
+ constrlist += [u <= umax ] # input constraints
58
56
if umin is not None :
59
- prob .constraints += [u >= umin ] # input constraints
57
+ constrlist += [u >= umin ] # input constraints
58
+
59
+ prob = cvxpy .Problem (cvxpy .Minimize (costlist ), constrlist )
60
60
61
61
prob .solve (verbose = True )
62
62
@@ -130,7 +130,7 @@ def opt_mpc_with_state_constr(A, B, N, Q, R, P, x0, xmin=None, xmax=None, umax=N
130
130
# print(Ae.shape)
131
131
132
132
# calc be
133
- be = np .vstack ((A , np .zeros (((N - 1 ) * nx , nx )))) * x0
133
+ be = np .vstack ((A , np .zeros (((N - 1 ) * nx , nx )))) @ x0
134
134
# print(be)
135
135
# print(be.shape)
136
136
@@ -156,11 +156,10 @@ def opt_mpc_with_state_constr(A, B, N, Q, R, P, x0, xmin=None, xmax=None, umax=N
156
156
sol = pyecosqp .ecosqp (H , q , A = G , B = h , Aeq = Ae , Beq = be )
157
157
158
158
# print(sol)
159
- fx = np .matrix (sol ["x" ])
160
- # print(fx)
159
+ fx = np .array (sol ["x" ])
161
160
162
- u = fx [0 , 0 :N * nu ].reshape (N , nu ).T
163
- x = fx [0 , - N * nx :].reshape (N , nx ).T
161
+ u = fx [0 :N * nu ].reshape (N , nu ).T
162
+ x = fx [- N * nx :].reshape (N , nx ).T
164
163
x = np .hstack ((x0 , x ))
165
164
# print(x)
166
165
# print(u)
@@ -170,8 +169,8 @@ def opt_mpc_with_state_constr(A, B, N, Q, R, P, x0, xmin=None, xmax=None, umax=N
170
169
171
170
def test3 ():
172
171
print ("start!!" )
173
- A = np .matrix ([[0.8 , 1.0 ], [0 , 0.9 ]])
174
- B = np .matrix ([[- 1.0 ], [2.0 ]])
172
+ A = np .array ([[0.8 , 1.0 ], [0 , 0.9 ]])
173
+ B = np .array ([[- 1.0 ], [2.0 ]])
175
174
(nx , nu ) = B .shape
176
175
177
176
N = 10 # number of horizon
@@ -181,7 +180,7 @@ def test3():
181
180
umax = 0.7
182
181
umin = - 0.7
183
182
184
- x0 = np .matrix ([[1.0 ], [2.0 ]]) # init state
183
+ x0 = np .array ([[1.0 ], [2.0 ]]) # init state
185
184
x , u = use_modeling_tool (A , B , N , Q , R , P , x0 , umax = umax , umin = umin )
186
185
187
186
rx1 = np .array (x [0 , :]).flatten ()
@@ -217,16 +216,16 @@ def test3():
217
216
218
217
def test4 ():
219
218
print ("start!!" )
220
- A = np .matrix ([[0.8 , 1.0 ], [0 , 0.9 ]])
221
- B = np .matrix ([[- 1.0 ], [2.0 ]])
219
+ A = np .array ([[0.8 , 1.0 ], [0 , 0.9 ]])
220
+ B = np .array ([[- 1.0 ], [2.0 ]])
222
221
(nx , nu ) = B .shape
223
222
224
223
N = 10 # number of horizon
225
224
Q = np .eye (nx )
226
225
R = np .eye (nu )
227
226
P = np .eye (nx )
228
227
229
- x0 = np .matrix ([[1.0 ], [2.0 ]]) # init state
228
+ x0 = np .array ([[1.0 ], [2.0 ]]) # init state
230
229
231
230
x , u = use_modeling_tool (A , B , N , Q , R , P , x0 )
232
231
@@ -262,16 +261,16 @@ def test4():
262
261
263
262
def test5 ():
264
263
print ("start!!" )
265
- A = np .matrix ([[0.8 , 1.0 ], [0 , 0.9 ]])
266
- B = np .matrix ([[- 1.0 ], [2.0 ]])
264
+ A = np .array ([[0.8 , 1.0 ], [0 , 0.9 ]])
265
+ B = np .array ([[- 1.0 ], [2.0 ]])
267
266
(nx , nu ) = B .shape
268
267
269
268
N = 10 # number of horizon
270
269
Q = np .eye (nx )
271
270
R = np .eye (nu )
272
271
P = np .eye (nx )
273
272
274
- x0 = np .matrix ([[1.0 ], [2.0 ]]) # init state
273
+ x0 = np .array ([[1.0 ], [2.0 ]]) # init state
275
274
umax = 0.7
276
275
277
276
x , u = use_modeling_tool (A , B , N , Q , R , P , x0 , umax = umax )
@@ -308,23 +307,23 @@ def test5():
308
307
309
308
def test6 ():
310
309
print ("start!!" )
311
- A = np .matrix ([[0.8 , 1.0 ], [0 , 0.9 ]])
312
- B = np .matrix ([[- 1.0 ], [2.0 ]])
310
+ A = np .array ([[0.8 , 1.0 ], [0 , 0.9 ]])
311
+ B = np .array ([[- 1.0 ], [2.0 ]])
313
312
(nx , nu ) = B .shape
314
313
315
314
N = 10 # number of horizon
316
315
Q = np .eye (nx )
317
316
R = np .eye (nu )
318
317
P = np .eye (nx )
319
318
320
- x0 = np .matrix ([[1.0 ], [2.0 ]]) # init state
319
+ x0 = np .array ([[1.0 ], [2.0 ]]) # init state
321
320
umax = 0.7
322
321
umin = - 0.7
323
322
324
- x0 = np .matrix ([[1.0 ], [2.0 ]]) # init state
323
+ x0 = np .array ([[1.0 ], [2.0 ]]) # init state
325
324
326
- xmin = np .matrix ([[- 3.5 ], [- 0.5 ]]) # state constraints
327
- xmax = np .matrix ([[3.5 ], [2.0 ]]) # state constraints
325
+ xmin = np .array ([[- 3.5 ], [- 0.5 ]]) # state constraints
326
+ xmax = np .array ([[3.5 ], [2.0 ]]) # state constraints
328
327
329
328
x , u = use_modeling_tool (A , B , N , Q , R , P , x0 ,
330
329
umax = umax , umin = umin , xmin = xmin , xmax = xmax )
@@ -363,27 +362,22 @@ def test6():
363
362
def test7 ():
364
363
print ("start!!" )
365
364
366
- A = np .matrix ([[0.8 , 1.0 ], [0 , 0.9 ]])
367
- B = np .matrix ([[- 1.0 ], [2.0 ]])
365
+ A = np .array ([[0.8 , 1.0 ], [0 , 0.9 ]])
366
+ B = np .array ([[- 1.0 ], [2.0 ]])
368
367
(nx , nu ) = B .shape
369
368
370
369
N = 3 # number of horizon
371
370
Q = np .eye (nx )
372
371
R = np .eye (nu )
373
372
P = np .eye (nx )
374
373
375
- x0 = np .matrix ([[1.0 ], [2.0 ]]) # init state
374
+ x0 = np .array ([[1.0 ], [2.0 ]]) # init state
376
375
umax = 0.7
377
376
umin = - 0.7
378
377
379
- x0 = np .matrix ([[1.0 ], [2.0 ]]) # init state
380
-
381
- # xmin = np.matrix([[-3.5], [-0.5]]) # state constraints
382
- # xmax = np.matrix([[3.5], [2.0]]) # state constraints
378
+ x0 = np .array ([[1.0 ], [2.0 ]]) # init state
383
379
384
380
x , u = use_modeling_tool (A , B , N , Q , R , P , x0 , umax = umax , umin = umin )
385
- # x, u = use_modeling_tool(A, B, N, Q, R, P, x0, umax=umax, umin=umin, xmin=xmin, xmax=xmax)
386
- # x, u = use_modeling_tool(A, B, N, Q, R, P, x0)
387
381
388
382
rx1 = np .array (x [0 , :]).flatten ()
389
383
rx2 = np .array (x [1 , :]).flatten ()
@@ -422,33 +416,33 @@ def test7():
422
416
def test8 ():
423
417
print ("start!!" )
424
418
425
- A = np .matrix ([[0.8 , 1.0 ], [0 , 0.9 ]])
426
- B = np .matrix ([[- 1.0 ], [2.0 ]])
419
+ A = np .array ([[0.8 , 1.0 ], [0 , 0.9 ]])
420
+ B = np .array ([[- 1.0 ], [2.0 ]])
427
421
(nx , nu ) = B .shape
428
422
429
423
N = 5 # number of horizon
430
424
Q = np .eye (nx )
431
425
R = np .eye (nu )
432
426
P = np .eye (nx )
433
427
434
- x0 = np .matrix ([[1.0 ], [2.0 ]]) # init state
428
+ x0 = np .array ([[1.0 ], [2.0 ]]) # init state
435
429
umax = 0.7
436
430
umin = - 0.7
437
431
438
- x0 = np .matrix ([[1.0 ], [2.0 ]]) # init state
432
+ x0 = np .array ([[1.0 ], [2.0 ]]) # init state
439
433
440
434
start = time .time ()
441
435
442
- xmin = np .matrix ([[- 3.5 ], [- 0.5 ]]) # state constraints
443
- xmax = np .matrix ([[3.5 ], [2.0 ]]) # state constraints
436
+ xmin = np .array ([[- 3.5 ], [- 0.5 ]]) # state constraints
437
+ xmax = np .array ([[3.5 ], [2.0 ]]) # state constraints
444
438
445
439
# x, u = use_modeling_tool(A, B, N, Q, R, P, x0, umax=umax, umin=umin)
446
440
x , u = use_modeling_tool (A , B , N , Q , R , P , x0 ,
447
441
umax = umax , umin = umin , xmin = xmin , xmax = xmax )
448
442
449
443
# x, u = use_modeling_tool(A, B, N, Q, R, P, x0)
450
444
elapsed_time = time .time () - start
451
- print ("modeling tool modeling elapsed_time:{0}" .format (
445
+ print ("modeling tool modeling elapsed_time:{0}" .format (
452
446
elapsed_time ) + "[sec]" )
453
447
454
448
rx1 = np .array (x [0 , :]).flatten ()
@@ -470,7 +464,7 @@ def test8():
470
464
# A, B, N, Q, R, P, x0, umax=umax, umin=umin)
471
465
# x, u = opt_mpc_with_state_constr(A, B, N, Q, R, P, x0)
472
466
elapsed_time = time .time () - start
473
- print ("hand modeling elapsed_time:{0}" .format (elapsed_time ) + "[sec]" )
467
+ print ("hand modeling elapsed_time:{0}" .format (elapsed_time ) + "[sec]" )
474
468
475
469
# print(x)
476
470
print (u )
@@ -498,22 +492,22 @@ def test_output_check(rx1, rx2, ru, x1, x2, u):
498
492
print ("test x1" )
499
493
for (i , j ) in zip (rx1 , x1 ):
500
494
print (i , j )
501
- assert (i - j ) <= 0.001 , "Error"
495
+ assert (i - j ) <= 0.01 , "Error"
502
496
print ("test x2" )
503
497
for (i , j ) in zip (rx2 , x2 ):
504
498
print (i , j )
505
- assert (i - j ) <= 0.001 , "Error"
499
+ assert (i - j ) <= 0.01 , "Error"
506
500
print ("test u" )
507
501
for (i , j ) in zip (ru , u ):
508
502
print (i , j )
509
- assert (i - j ) <= 0.001 , "Error"
503
+ assert (i - j ) <= 0.01 , "Error"
510
504
511
505
512
506
if __name__ == '__main__' :
513
507
DEBUG_ = True
514
- # test3()
515
- # test4()
516
- # test5()
517
- # test6()
518
- # test7()
508
+ test3 ()
509
+ test4 ()
510
+ test5 ()
511
+ test6 ()
512
+ test7 ()
519
513
test8 ()
0 commit comments