Professional Documents
Culture Documents
AC555
AC555
Contents
1 Basics on set operations, invariant set description and optimization 2
1.1 Example 1: Polyhedron and polytope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.2 Example 2: Tutorial of a constrained optimization problem . . . . . . . . . . . . . . . . . . . 4
1.3 Matlab Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1
1 Basics on set operations, invariant set description and optimization
Optimization-based control in general terms refers to the control design using an optimization criterion and
the respective resolution techniques in order to obtain the parameters of the control law, the optimality
being generally equivalent to a certain desired property as for example, stability, reactivity or robustness. A
widely used optimization-based control technique in this class is Model Predictive Control (MPC) also called,
receding horizon control
In this manipulation, our objective is to solve the different type of optimization problems in different situations
2
53 % % Polytope operation
54 % Create 2 polytopes for implementing differents operations
55 P1 = Polyhedron ( rand (5 ,2) -0.5) ;
56 P2 = Polyhedron ( rand (4 ,2) -0.5) -[1;1];
57
58 % Inclusion test :
59 P1 <= P2
60 % Equality test (2 - way inclusion ) :
61 P1 == P2
62 P1 <= P2
63
64 figure
65 subplot (2 ,2 ,1)
66 Ptotal =[ P1 P2 ]
67 plot ( Ptotal , opt {:})
68 axis ([ -2 2 -2 2])
69 title ( ’ Plot union of polytopes ’)
70 % Intersect :
71 subplot (2 ,2 ,2)
72 plot ( P1 . intersect ( P2 ) , opt {:})
73 axis ([ -2 2 -2 2])
74 title ( ’ Plot intersection of polytopes ’)
75 % Minkowski addition :
76 subplot (2 ,2 ,3)
77 plot ( P1 + P2 , opt {:})
78 axis ([ -2 2 -2 2])
79 title ( ’ Minkowski addition of polytopes ’)
80 % Pontryagin difference :
81 subplot (2 ,2 ,4)
82 plot ( P1 - P2 , opt {:})
83 axis ([ -2 2 -2 2])
84 title ( ’ Pontryagin difference of polytopes ’)
85
86 % Hausdorff distance :
87 dist = P1 . distance ( P2 )
88 % Plot 2 polytopes and the segment representing Hausdorff distance
89 figure
90 plot ([ P1 P2 ] , opt {:})
91 hold on
92 scatter ([ dist . x (1) dist . y (1) ] , [ dist . x (2) dist . y (2) ] , ’ filled ’ , ’r ’)
93 plot ([ dist . x (1) dist . y (1) ] , [ dist . x (2) dist . y (2) ] , ’r ’ , ’ LineWidth ’ ,2)
94 title ( ’ Hausdorff distance ’)
3
Figure 2: Hausdorff distance of the polytopes
In this code, we want to learn how to manipulate the Multiparametric Toolbox mpt3 in order to create the
polytopes and use some geometrical operations on this kind of sets. As shown in the figure above, we had
created 2 randomly polytopes and showing on the plot the segment representing the Hausdorff distance of
the 2 polyhedron.
4
18 % Dimension of system
19 [ dx , du ] = size ( B )
20 dy = size (C ,1)
21
22 % Weighting parameters
23 Q = eye ( dx ) * 5
24 R = eye ( du ) * 5
25 Qy = eye ( dy )
26 P = eye ( dx ) * 5
27
28
29 % NBR of simulations and predictions
30 Npred = 2
31 Npred1 = [5 2]
32 Nsim = 100
33 coef = [1 5];
34 figure ( ’ Name ’ ," Motion y ( x ) " , ’ NumberTitle ’ , ’ off ’)
35 nbr_it = [0 0];
36 flag = [0 0];
37 for j = 1:2
38
39 % % Define the variables , constraints and cost : Npred + 1 predictions
40 % Define variables : states x = [ x (1) ’, x (2) ’, ... , x ( Npred +1) ]
41 % and inputs U = [ u (0) ’, u (1) ’ ,... , u ( Npred ) ’] ’
42 u = sdpvar ( repmat ( du ,1 , Npred * coef ( j ) ) , ones (1 , Npred * coef ( j ) ) ) ;
43 x = sdpvar ( repmat ( dx ,1 , Npred * coef ( j ) +1) , ones (1 , Npred * coef ( j ) +1) ) ;
44
45 % Define initial inputs for every instant t : u (0)
46 u_init = sdpvar ( du ,1)
47 % Define state variable u
48 utmp = sdpvar ( du ,1)
49
50 % Initialize constraints and objective
51 constraints = []
52 objective = 0;
53
54 % % Write the constraints and objectives over the prediction horizon
55 for k = 1: Npred * coef ( j )
56 if ( k == 1)
57 utmp = u_init ;
58 else
59 utmp = u {k -1};
60 end
61
62 % constraints = [ constraints ,...
63 % x { k +1}== A * x { k } + B * u { k }];
64 constraints = [ constraints ,...
65 x { k +1}== A * x { k } + B * u { k } ,...
66 umin <= u { k } <= umax ,...
67 ymin <= C * x { k } + D * u { k } <= ymax ,...
68 delta_umin <= u { k } - utmp <= delta_umax ];
69
70 objective = objective + x { k } ’ * Q * x{k} + u{k}’ * R * u { k };
71
72 end
73
74 objective = objective + x { k } ’ * P * x{k} ;
75
76 % % Options
77 options = [];
78
79 % % Parameters and outputs :
80 parameters = { x {1} , u_init };
81 outputs = { u {1}};
82
83 % % Design MPC controller :
84 % Controller = optimizer ( constraints , objective , options , parameters , outputs )
85
5
86 Controller = optimizer ( constraints , objective , options , parameters , outputs )
87
88
89 % % Simultaion over Nsim steps :
90 usim = zeros ( du , Nsim ) ;
91 xsim = zeros ( dx , Nsim + 1) ;
92 ysim = zeros ( dy , Nsim ) ;
93
94 % Initial condition : x (0) or x (1) in matlab
95 xsim (: ,1) = ones ( dx ,1) * 1;
96 usim_init (: ,1) = zeros ( du ,1) ;
97
98
99 final_error = 1e -2;
100
101 tic
102 % Simulation loop
103 for i = 1: Nsim
104 u = Controller {{ xsim (: , i ) , usim_init }};
105 usim_init = u ;
106 xsim (: , i +1) = A * xsim (: , i ) + B * u ;
107 usim (: , i ) = u ;
108 ysim (: , i ) = C * xsim (: , i ) + D * usim (: , i ) ;
109 if ( flag ( j ) ==0) && all (( abs ( xsim (: , i +1) ) < final_error ) )
110 flag ( j ) = 1;
111 nbr_it ( j ) = i ;
112 end
113 end
114
115 toc
116
117 scatter ( xsim (1 ,:) , xsim (2 ,:) )
118
119 hold on
120 % title ( ’ Number of iteration : ’, num2str ( nbr_it ) )
121 end
122 legend ( strcat ( ’ Npred = ’ , num2str ( Npred * coef (1) ) , char (9) , ’ Number of iteration : ’ ,
num2str ( nbr_it (1) ) ) , strcat ( ’ Npred = ’ , num2str ( Npred * coef (2) ) , char (9) , ’ Number of
iteration : ’ , num2str ( nbr_it (2) ) ) )
123 figure ( ’ Name ’ ," Output ysim " , ’ NumberTitle ’ , ’ off ’)
124 stem ( ysim )
125
126 figure ( ’ Name ’ ," Input ysim " , ’ NumberTitle ’ , ’ off ’)
127 stem ( usim )
1 clear all
2 clc
6
3 close all
4
5 A = [1 0 0.5 0;0 1 0 0.5; 0 0 1 0; 0 0 0 1]
6 B = [0 0; 0 0; 0.5 0; 0 0.5];
7 C = [1 0 0 0; 0 1 0 0];
8 D = [0 0; 0 0];
9
10 % % Constraints
11 umin = -10 * [1; 1];
12 umax = +10 * [1; 1];
13 % delta_umin = -1 * 0.1;
14 % delta_umax = +1 * 0.1;
15
16
17 % Dimension of system
18 [ dx , du ] = size ( B )
19 dy = size (C ,1)
20
21 % Weighting parameters
22 Q = eye ( dx ) * 5
23 R = eye ( du ) * 5
24 Qy = eye ( dy ) * 5
25
26 P = eye ( dx ) * 5
27 Py = eye ( dy ) * 5
28 Q1 = C ’* Qy * C ;
29 P1 = C ’* Py * C ;
30 % NBR of simulations and predictions
31 Npred = 5
32 Nsim = 100
33
34 % % Define the variables , constraints and cost : Npred + 1 predictions
35 % Define variables : states x = [ x (1) ’, x (2) ’, ... , x ( Npred +1) ]
36 % and inputs U = [ u (0) ’, u (1) ’ ,... , u ( Npred ) ’] ’
37 u = sdpvar ( repmat ( du ,1 , Npred ) , ones (1 , Npred ) ) ;
38 x = sdpvar ( repmat ( dx ,1 , Npred +1) , ones (1 , Npred +1) ) ;
39
40 % Define initial inputs for every instant t : u (0)
41 u_init = sdpvar ( du ,1)
42 % Define state variable u
43 utmp = sdpvar ( du ,1)
44
45 % Initialize constraints and objective
46 constraints = []
47 objective = 0;
48
49 % % Write the constraints and objectives over the prediction horizon
50 for k = 1: Npred
51 if ( k == 1)
52 utmp = u_init ;
53 else
54 utmp = u {k -1};
55 end
56
57 % constraints = [ constraints ,...
58 % x { k +1}== A * x { k } + B * u { k }];
59 constraints = [ constraints ,...
60 x { k +1}== A * x { k } + B * u { k } ,...
61 umin <= u { k } <= umax ];
62
63 % constraints = [ constraints ,...
64 % x { k +1}== A * x { k } + B * u { k } ,...
65 % umin <= u { k } <= umax ,...
66 % ymin <= C * x { k } + D * u { k } <= ymax ,...
67 % delta_umin <= u { k } - utmp <= delta_umax ];
68
69 objective = objective + x { k } ’ * Q1 * x { k } + u { k } ’ * R * u { k };
70
7
71 end
72
73 objective = objective + x { k } ’ * P1 * x { k } ;
74
75 % % Options
76 options = [];
77
78 % % Parameters and outputs :
79 parameters = { x {1} , u_init };
80 outputs = { u {1}};
81
82 % % Design MPC controller :
83 % Controller = optimizer ( constraints , objective , options , parameters , outputs )
84
85 Controller = optimizer ( constraints , objective , options , parameters , outputs )
86
87 % % Simultaion over Nsim steps :
88 usim = zeros ( du , Nsim ) ;
89 xsim = zeros ( dx , Nsim + 1) ;
90 ysim = zeros ( dy , Nsim ) ;
91
92 % Initial condition : x (0) or x (1) in matlab
93 xsim (: ,1) = [20 -20 0 -10] ’;
94 usim_init (: ,1) = zeros ( du ,1) ;
95
96 nbr_it = 0;
97 flag = 0;
98 final_error = 1e -1;
99 tic
100 % Simulation loop
101 for i = 1: Nsim
102 u = Controller {{ xsim (: , i ) , usim_init }};
103 usim_init = u ;
104 xsim (: , i +1) = A * xsim (: , i ) + B * u ;
105 usim (: , i ) = u ;
106 ysim (: , i ) = C * xsim (: , i ) + D * usim (: , i ) ;
107 if ( flag ==0) && all (( abs ( xsim (: , i +1) ) < final_error ) )
108 flag = 1;
109 nbr_it = i ;
110 end
111 end
112
113 toc
114
115 figure ( ’ Name ’ ," Output ysim " , ’ NumberTitle ’ , ’ off ’)
116 hold on
117 % stem ( ysim )
118 subplot (2 ,1 ,1)
119 stem ( ysim (1 ,:) )
120 title ( " Position x " )
121 subplot (2 ,1 ,2)
122 stem ( ysim (2 ,:) )
123 title ( " Position y " )
124
125 figure ( ’ Name ’ ," Motion y ( x ) " , ’ NumberTitle ’ , ’ off ’)
126 hold on
127 scatter ( xsim (1 ,:) , xsim (2 ,:) )
128
129
130 % Velocity
131 figure ( ’ Name ’ ," Velocity " , ’ NumberTitle ’ , ’ off ’)
132 hold on
133 subplot (2 ,1 ,1)
134 stem ( xsim (1 ,:) )
135 title ( " Velocity vx " )
136 subplot (2 ,1 ,2)
137 stem ( xsim (2 ,:) )
138 title ( " Velocity vy " )
8
139
140 figure ( ’ Name ’ ," Input usim " , ’ NumberTitle ’ , ’ off ’)
141 hold on
142 % stem ( usim )
143 subplot (2 ,1 ,1)
144 stem ( usim (1 ,:) )
145 title ( " Input ux " )
146 subplot (2 ,1 ,2)
147 stem ( usim (2 ,:) )
148 title ( " Input ux " )
149
150 % data1 = [ usim ; xsim ; ysim ; nbr_it ; Npred ; Q ; R ; P ];
151 save data7 usim xsim ysim nbr_it Npred Qy R Py
152 % save data1 data1
9
48 subplot (2 ,1 ,1)
49 hold on
50 stem ( usim (1 ,:) )
51 title ( " Input ux " )
52 subplot (2 ,1 ,2)
53 hold on
54 stem ( usim (2 ,:) )
55 title ( " Input ux " )
56 hold on
57
58
59 %%
60 load data7
61 nQ (2) = Qy (1 ,1) ;
62 nR (2) = R (1 ,1) ;
63 nP (2) = Py (1 ,1) ;
64 nN (2) = Npred ;
65 text1 = strcat ( " Q = " , num2str ( nQ (1) ) , " R = " , num2str ( nR (1) ) , " P = " , num2str ( nP (1) ) , "
Npred = " , num2str ( nN (1) ) )
66 text2 = strcat ( " Q = " , num2str ( nQ (2) ) , " R = " , num2str ( nR (2) ) , " P = " , num2str ( nP (2) ) , "
Npred = " , num2str ( nN (2) ) )
67
68 figure (1)
69
70 % stem ( ysim )
71 subplot (2 ,1 ,1)
72 hold on
73 stem ( ysim (1 ,:) )
74 title ( " Position x " )
75 legend ( text1 , text2 )
76
77 subplot (2 ,1 ,2)
78 hold on
79 stem ( ysim (2 ,:) )
80 title ( " Position y " )
81
82
83 legend ( text1 , text2 )
84
85
86 figure (2)
87 scatter ( xsim (1 ,:) , xsim (2 ,:) )
88 title ( " Trajectory y ( x ) " )
89 legend ( text1 , text2 )
90
91 % Velocity
92 figure (3)
93
94 subplot (2 ,1 ,1)
95 hold on
96 stem ( xsim (1 ,:) )
97 title ( " Velocity vx " )
98 legend ( text1 , text2 )
99
100 subplot (2 ,1 ,2)
101 hold on
102 stem ( xsim (2 ,:) )
103 title ( " Velocity vy " )
104
105 legend ( text1 , text2 )
106
107 figure (4)
108
109 % stem ( usim )
110 subplot (2 ,1 ,1)
111 hold on
112 stem ( usim (1 ,:) )
113 title ( " Input ux " )
10
114 legend ( text1 , text2 )
115
116 subplot (2 ,1 ,2)
117 hold on
118 stem ( usim (2 ,:) )
119 title ( " Input ux " )
120 legend ( text1 , text2 )
The purpose of varying the parameters is to know on the one hand the time and number of simulations
needed to reach the end point trajectory and on the other hand which parameters influence more on the
results
As results we obtained the following paces :
- For Q = R = P = Npred =5 :
11
Figure 3: Input/ output and motion path of the object
12
- For Q = R = P = 5 and Npred = 100:
As we can see from the motion in 2 cases, when we increase the final cost, the number of steps required to
reach the zero point become smaller. This means that the terminal cost helps reach the target point faster
and the object will stay at this point with more stability.
13
- For Npred =2 and Npred = 10:
It is seen from the trajectories when we compute the input with longer prediction horizon, we could have a
faster response to reach the target point. However, the computational time will drastically rise and depass 1
second. This cause a dilemma in reality when we want to control a real robot
- For Q =5 and Q = 50
14
In this example, we want to consider the influence of the energy cost Q. This parameter helps minimize the
energy better, but cause the drop in the performance of the system as can be seen in the figure the difference
between 2 cases.
- For R =5 and R = 50
15
Such that constraints:
−10 ux 10
≤ ≤
−10 uy 10
1 clear all
2 clc
3 close all
4
5 A = [1 0 0.5 0;0 1 0 0.5; 0 0 1 0; 0 0 0 1]
6 B = [0 0; 0 0; 0.5 0; 0 0.5];
7 C = [1 0 0 0; 0 1 0 0];
8 D = [0 0; 0 0];
9
10 % % Constraints
11 umin = -10 * [1; 1];
12 umax = +10 * [1; 1];
13 % delta_umin = -1 * 0.1;
14 % delta_umax = +1 * 0.1;
15
16
17 % Dimension of system
18 [ dx , du ] = size ( B )
19 dy = size (C ,1)
20
21 % Weighting parameters
22 Q = eye ( dx ) * 5
23 R = eye ( du ) * 5
24 Qy = eye ( dy ) * 5
25
26 P = eye ( dx ) * 5
27 Py = eye ( dy ) * 5
28 Q1 = C ’* Qy * C ;
29 P1 = C ’* Py * C ;
30 % NBR of simulations and predictions
31 Npred = 5
32 Nsim = 100
33 ytarget = [ linspace (0 ,100 , Nsim ) ; - linspace (0 ,100 , Nsim ) ];
34
35 % % Define the variables , constraints and cost : Npred + 1 predictions
36 % Define variables : states x = [ x (1) ’, x (2) ’, ... , x ( Npred +1) ]
37 % and inputs U = [ u (0) ’, u (1) ’ ,... , u ( Npred ) ’] ’
38 u = sdpvar ( repmat ( du ,1 , Npred ) , ones (1 , Npred ) ) ;
39 x = sdpvar ( repmat ( dx ,1 , Npred +1) , ones (1 , Npred +1) ) ;
40
41 % Define initial inputs for every instant t : u (0)
42 u_init = sdpvar ( du ,1)
43 % Define state variable u
44 utmp = sdpvar ( du ,1)
45
46 % Initialize constraints and objective
47 constraints = []
48 objective = 0;
49
50 % % Write the constraints and objectives over the prediction horizon
51 for k = 1: Npred
52 if ( k == 1)
53 utmp = u_init ;
54 else
55 utmp = u {k -1};
56 end
57
16
58 % constraints = [ constraints ,...
59 % x { k +1}== A * x { k } + B * u { k }];
60 constraints = [ constraints ,...
61 x { k +1}== A * x { k } + B * u { k } ,...
62 umin <= u { k } <= umax ];
63
64 % constraints = [ constraints ,...
65 % x { k +1}== A * x { k } + B * u { k } ,...
66 % umin <= u { k } <= umax ,...
67 % ymin <= C * x { k } + D * u { k } <= ymax ,...
68 % delta_umin <= u { k } - utmp <= delta_umax ];
69
70 objective = objective + ( C * x { k } - ytarget (: , k ) ) ’ * Qy * ( C * x { k } - ytarget (: , k ) ) + u {
k } ’ * R * u { k };
71
72 end
73
74 objective = objective + ( C * x { k } - ytarget (: , k ) ) ’ * Py * ( C * x { k } - ytarget (: , k ) ) ;
75
76 % % Options
77 options = [];
78
79 % % Parameters and outputs :
80 parameters = { x {1} , u_init };
81 outputs = { u {1}};
82
83 % % Design MPC controller :
84 % Controller = optimizer ( constraints , objective , options , parameters , outputs )
85
86 Controller = optimizer ( constraints , objective , options , parameters , outputs )
87
88 % % Simultaion over Nsim steps :
89 usim = zeros ( du , Nsim ) ;
90 xsim = zeros ( dx , Nsim + 1) ;
91 ysim = zeros ( dy , Nsim ) ;
92
93 % Initial condition : x (0) or x (1) in matlab
94 xsim (: ,1) = [20 -20 0 -10] ’;
95 usim_init (: ,1) = zeros ( du ,1) ;
96
97 nbr_it = 0;
98 flag = 0;
99 final_error = 1e -1;
100 tic
101 % Simulation loop
102 for i = 1: Nsim
103 u = Controller {{ xsim (: , i ) , usim_init }};
104 usim_init = u ;
105 xsim (: , i +1) = A * xsim (: , i ) + B * u ;
106 usim (: , i ) = u ;
107 ysim (: , i ) = C * xsim (: , i ) + D * usim (: , i ) ;
108 if ( flag ==0) && all (( abs ( xsim (: , i +1) ) < final_error ) )
109 flag = 1;
110 nbr_it = i ;
111 end
112 end
113
114 toc
115
116 figure ( ’ Name ’ ," Output ysim " , ’ NumberTitle ’ , ’ off ’)
117 hold on
118 % stem ( ysim )
119 subplot (2 ,1 ,1)
120 stem ( ysim (1 ,:) )
121 title ( " Position x " )
122 subplot (2 ,1 ,2)
123 stem ( ysim (2 ,:) )
124 title ( " Position y " )
17
125
126 figure ( ’ Name ’ ," Motion y ( x ) " , ’ NumberTitle ’ , ’ off ’)
127 hold on
128 scatter ( xsim (1 ,:) , xsim (2 ,:) )
129
130
131 % Velocity
132 figure ( ’ Name ’ ," Velocity " , ’ NumberTitle ’ , ’ off ’)
133 hold on
134 subplot (2 ,1 ,1)
135 stem ( xsim (1 ,:) )
136 title ( " Velocity vx " )
137 subplot (2 ,1 ,2)
138 stem ( xsim (2 ,:) )
139 title ( " Velocity vy " )
140
141 figure ( ’ Name ’ ," Input usim " , ’ NumberTitle ’ , ’ off ’)
142 hold on
143 % stem ( usim )
144 subplot (2 ,1 ,1)
145 stem ( usim (1 ,:) )
146 title ( " Input ux " )
147 subplot (2 ,1 ,2)
148 stem ( usim (2 ,:) )
149 title ( " Input ux " )
150
151 % data1 = [ usim ; xsim ; ysim ; nbr_it ; Npred ; Q ; R ; P ];
152 save data1r usim xsim ysim nbr_it Npred Qy R Py
153 % save data1 data1
18
- Refrence : Q = R = P = Npred =5 :
Figure 9: Reference
19
- For Q = R = Npred = 5 and P = 100:
20
- For Q = R = P = 5 and Npred = 3:
- When we increase the cost of the output (Q=100), the input signal will be higher.
- When we increase the cost of the input (R=100), we need more number of steps to track the target because
of the slow dynamics also we have a big number of simulations ( Nsim > 100).
- When we increase the terminal cost (P=100) we go faster to the target and the stability is better.
- When we increase Npred : To many predictions step cost delay on computing
- For Q = R = P = 5 and Npred = 3 :with small number of predictions and small values of the input, output
and the terminal cost ==> the number of simulations is 91 so we go to the final target slowly.
21
3.2 Conclusion
Each of the diagonal elements in Q reflects how much we care about a particular error signal as we want it to
go to zero. On the other hand, each element in R reflects how much we care about performing a particular
action. In this practice, Q and R are manually choose depending on how we want the systems to perform.
The terminal cost P helps tracking error to be stable at the equilibrium point.
Otherwise, the MPC controller has the significant advantage that it can take into account the additional
constraints. However, this method can only show good performance when it can predict many steps which
would cause the computational time in a real system. So this method may not be feasible in some systems
which have fast response, but in the better situation, for example, with a faster computer, this method could
be a very efficient solution.
Np
X ⊺ ⊺ ⊺
U ∗ = arg min ∆y(k) Qy ∆y(k) + ∆u(k) R∆u(k) + ∆y(Np + 1) Py ∆y(Np + 1)
U
k=1
with ∆y(k) = y(k) − yref (k) and ∆u(k) = u(k) − uref (k)
22
32 Nsim = 100
33 ytarget = [ linspace (0 ,100 , Nsim ) ; - linspace (0 ,100 , Nsim ) ];
34
35 % % Define the variables , constraints and cost : Npred + 1 predictions
36 % Define variables : states x = [ x (1) ’, x (2) ’, ... , x ( Npred +1) ]
37 % and inputs U = [ u (0) ’, u (1) ’ ,... , u ( Npred ) ’] ’
38 u = sdpvar ( repmat ( du ,1 , Npred ) , ones (1 , Npred ) ) ;
39 x = sdpvar ( repmat ( dx ,1 , Npred +1) , ones (1 , Npred +1) ) ;
40
41 % Define initial inputs for every instant t : u (0)
42 u_init = sdpvar ( du ,1)
43 % Define state variable u
44 utmp = sdpvar ( du ,1)
45
46 % Initialize constraints and objective
47 constraints = []
48 objective = 0;
49
50 % % Write the constraints and objectives over the prediction horizon
51 for k = 1: Npred
52 if ( k == 1)
53 utmp = u_init ;
54 else
55 utmp = u {k -1};
56 end
57
58 % constraints = [ constraints ,...
59 % x { k +1}== A * x { k } + B * u { k }];
60 constraints = [ constraints ,...
61 x { k +1}== A * x { k } + B * u { k } ,...
62 umin <= u { k } <= umax ];
63
64 % constraints = [ constraints ,...
65 % x { k +1}== A * x { k } + B * u { k } ,...
66 % umin <= u { k } <= umax ,...
67 % ymin <= C * x { k } + D * u { k } <= ymax ,...
68 % delta_umin <= u { k } - utmp <= delta_umax ];
69
70 objective = objective + ( C * x { k } - ytarget (: , k ) ) ’ * Qy * ( C * x { k } - ytarget (: , k ) ) + u {
k } ’ * R * u { k };
71
72 end
73
74 objective = objective + ( C * x { k } - ytarget (: , k ) ) ’ * Py * ( C * x { k } - ytarget (: , k ) ) ;
75
76 % % Options
77 options = [];
78
79 % % Parameters and outputs :
80 parameters = { x {1} , u_init };
81 outputs = { u {1}};
82
83 % % Design MPC controller :
84 % Controller = optimizer ( constraints , objective , options , parameters , outputs )
85
86 Controller = optimizer ( constraints , objective , options , parameters , outputs )
87
88 % % Simultaion over Nsim steps :
89 usim = zeros ( du , Nsim ) ;
90 xsim = zeros ( dx , Nsim + 1) ;
91 ysim = zeros ( dy , Nsim ) ;
92
93 % Initial condition : x (0) or x (1) in matlab
94 xsim (: ,1) = [20 -20 0 -10] ’;
95 usim_init (: ,1) = zeros ( du ,1) ;
96
97 nbr_it = 0;
98 flag = 0;
23
99 final_error = 1e -1;
100 tic
101 % Simulation loop
102 for i = 1: Nsim
103 u = Controller {{ xsim (: , i ) , usim_init }};
104 usim_init = u ;
105 xsim (: , i +1) = A * xsim (: , i ) + B * u ;
106 usim (: , i ) = u ;
107 ysim (: , i ) = C * xsim (: , i ) + D * usim (: , i ) ;
108 if ( flag ==0) && all (( abs ( xsim (: , i +1) ) < final_error ) )
109 flag = 1;
110 nbr_it = i ;
111 end
112 end
113
114 toc
115
116 figure ( ’ Name ’ ," Output ysim " , ’ NumberTitle ’ , ’ off ’)
117 hold on
118 % stem ( ysim )
119 subplot (2 ,1 ,1)
120 stem ( ysim (1 ,:) )
121 title ( " Position x " )
122 subplot (2 ,1 ,2)
123 stem ( ysim (2 ,:) )
124 title ( " Position y " )
125
126 figure ( ’ Name ’ ," Motion y ( x ) " , ’ NumberTitle ’ , ’ off ’)
127 hold on
128 scatter ( xsim (1 ,:) , xsim (2 ,:) )
129
130
131 % Velocity
132 figure ( ’ Name ’ ," Velocity " , ’ NumberTitle ’ , ’ off ’)
133 hold on
134 subplot (2 ,1 ,1)
135 stem ( xsim (1 ,:) )
136 title ( " Velocity vx " )
137 subplot (2 ,1 ,2)
138 stem ( xsim (2 ,:) )
139 title ( " Velocity vy " )
140
141 figure ( ’ Name ’ ," Input usim " , ’ NumberTitle ’ , ’ off ’)
142 hold on
143 % stem ( usim )
144 subplot (2 ,1 ,1)
145 stem ( usim (1 ,:) )
146 title ( " Input ux " )
147 subplot (2 ,1 ,2)
148 stem ( usim (2 ,:) )
149 title ( " Input ux " )
150
151 % data1 = [ usim ; xsim ; ysim ; nbr_it ; Npred ; Q ; R ; P ];
152 save data1r usim xsim ysim nbr_it Npred Qy R Py
153 % save data1 data1
24
4.3 Graphical results
In this simulation, we set the initial position at the origin. The reference chosen for tracking is a straight
line going from the origin to the point [10,-10]. At the instant time k, the MPC controller will compute the
tracking error for Npred number of prediction horizon in the future between the position and the target point
at the time k+1 that we want to track. We can see that the controller can track very well the reference given.
However, the problem with the trajectory of the agent is that it move away the reference trajectory and then
return at a very long distance. This can be understood because the reference that we choose is not satisfied
the dynamics of the system. Later, we can fix this issue by generating a flat trajectory which will take into
account the conditions of the system.
25
5.1 Design of the system
In this TP, the obstacle is defined as a polytope. To avoid this obstacle, the position of the agent has to stay
in the complement of these bounded polyhedron. This means that we have to solve an optimization problem
in a non-convex region. By using the Mixed-Integer Programming, we can turn this problem into solving an
additional integer variable α as defined by the following:
with :
x
ux y
u(k) = , x(k) =
uy vx
vy
x
−A ≤ −b + M α
y
where: A and b are deduced from the H-representation of the polyhedron.
26
interactive decision optimization applications. CPLEX Optimizer has solved optimization models with
millions of constraints and variables.
27
63 constraints = []
64 objective = 0;
65
66 % % Write the constraints and objectives over the prediction horizon
67 for k = 1: Npred
68 if ( k == 1)
69 utmp = u_init ;
70 else
71 utmp = u {k -1};
72 end
73
74 % constraints = [ constraints ,...
75 % x { k +1}== A * x { k } + B * u { k }];
76 constraints = [ constraints ,...
77 x { k +1}== A * x { k } + B * u { k } ,...
78 umin <= u { k } <= umax ,...
79 - Pv . A * x { k +1}(1:2 ,:) <= - Pv . b + 1000 * alpha { k } ,...
80 sum ( alpha { k +1}) <= size_alpha -1 ,...
81 % sum ( alpha { k +1}| alpha { k }) <= size_alpha -1 % Additional constraint to avoid the
corner of the obstacle
82 sum ( abs ( alpha { k +1} - alpha { k }) ) <= 1
83 ];
84
85 % constraints = [ constraints ,...
86 % x { k +1}== A * x { k } + B * u { k } ,...
87 % umin <= u { k } <= umax ,...
88 % ymin <= C * x { k } + D * u { k } <= ymax ,...
89 % delta_umin <= u { k } - utmp <= delta_umax ];
90
91 objective = objective + ( C * x { k } - ytarget ) ’ * Qy * ( C * x { k } - ytarget ) + u { k } ’ * R *
u { k };
92
93 end
94
95 objective = objective + ( C * x { k } - ytarget ) ’ * Py * ( C * x { k } - ytarget ) ;
96
97 % % Options
98 options = [];
99
100 % % Parameters and outputs :
101 parameters = { x {1} , u_init };
102 outputs = { u {1} , alpha {1}};
103
104 % % Design MPC controller :
105 % Controller = optimizer ( constraints , objective , options , parameters , outputs )
106
107 Controller = optimizer ( constraints , objective , options , parameters , outputs )
108
109 % % Simultaion over Nsim steps :
110 usim = zeros ( du , Nsim ) ;
111 xsim = zeros ( dx , Nsim + 1) ;
112 ysim = zeros ( dy , Nsim ) ;
113 alphasim = zeros (4 , Nsim ) ;
114 % Initial condition : x (0) or x (1) in matlab
115 xsim (: ,1) = [ xi yi 0 -2] ’;
116 usim_init (: ,1) = zeros ( du ,1) ;
117 alphasim_init (: ,1) = zeros ( size_alpha ,1) ;
118
119 nbr_it = 0;
120 flag = 0;
121 final_error = 1e -1;
122 tic
123 % Simulation loop
124 for i = 1: Nsim
125 u = Controller {{ xsim (: , i ) , usim_init }};
126 usim_init = u {1};
127 alphasim_init = u {2};
128 xsim (: , i +1) = A * xsim (: , i ) + B * u {1};
28
129 usim (: , i ) = u {1};
130 alphasim (: , i ) = u {2};
131 ysim (: , i ) = C * xsim (: , i ) + D * usim (: , i ) ;
132 if ( flag ==0) && all (( abs ( xsim (: , i +1) ) < final_error ) )
133 flag = 1;
134 nbr_it = i ;
135 end
136 end
137
138 toc
139
140 figure ( ’ Name ’ ," Output ysim " , ’ NumberTitle ’ , ’ off ’)
141 hold on
142 % stem ( ysim )
143 subplot (2 ,1 ,1)
144 stem ( ysim (1 ,:) )
145 title ( " Position x " )
146 subplot (2 ,1 ,2)
147 stem ( ysim (2 ,:) )
148 title ( " Position y " )
149
150 figure ( ’ Name ’ ," Motion y ( x ) " , ’ NumberTitle ’ , ’ off ’)
151 hold on
152 scatter ( xsim (1 ,:) , xsim (2 ,:) )
153 plot ( Pv )
154
155 % Velocity
156 figure ( ’ Name ’ ," Velocity " , ’ NumberTitle ’ , ’ off ’)
157 hold on
158 subplot (2 ,1 ,1)
159 stem ( xsim (1 ,:) )
160 title ( " Velocity vx " )
161 subplot (2 ,1 ,2)
162 stem ( xsim (2 ,:) )
163 title ( " Velocity vy " )
164
165 figure ( ’ Name ’ ," Input usim " , ’ NumberTitle ’ , ’ off ’)
166 hold on
167 % stem ( usim )
168 subplot (2 ,1 ,1)
169 stem ( usim (1 ,:) )
170 title ( " Input ux " )
171 subplot (2 ,1 ,2)
172 stem ( usim (2 ,:) )
173 title ( " Input ux " )
174
175 % data1 = [ usim ; xsim ; ysim ; nbr_it ; Npred ; Q ; R ; P ];
176 save data1o usim xsim ysim nbr_it Npred Qy R Py
177 % save data1 data1
29
5.4 Graphical results
As a first step we chose a random value for the velocity, therefore we obtained the following graph:
We have concluded that in order to avoid cutting the corner, the speed must be minimised but the most
important thing is to add an additional constraint in order to optimise the path and avoid cutting the corner,
which is described by the following graph :
30
In the first time we decreased the value of the velocity but we placed the objectacle a little far away (just to
better observe the deviation) :
In order to improve the collision in the corner of the obstacle as we solve the problem in discrete-time, we
have to introduce another constraint when the trajectory changes the half-space. The constraint here: as we
want our moving object to transit between the 2 half-space, the trajectory has to pass in the space between
them, this means that the binary variable alpha should represent this space. In our TP for instance, at a
moment the object change from the half-space (1,1,1,0) to (1,1,0,1) we need the alpha pass through (1,1,0,0)
prior to going to other space.
In our case, for example, the (1,1,1,0) OR (1,1,0,1) gives (1,1,1,1), so all the variable is turn ON. (1,1,1,0)
OR (1,1,0,0) =(1,1,1,0) so only 3 of them can be turn ON. We can create a condition so that the transition
state should appear in the solution by using this condition: current alpha OR next alpha gives N-1 condition
ON.
31
Figure 17: The additional Constraint
Figure 18: Optimization with low value of velocity and additional Constraint
32
Figure 19: Optimization with High value of velocity and additional Constraint
For a system with only one obstacle we managed to optimise the trajectory in order to wait for the final
destination and without cutting the corners of the obstacle, but this optimisation is valid for a small number
of obstacles and it will be more complex when the number will be increased.
33
Figure 20: What is the solution ?
34
Figure 21: Avoidance of 3 Obstacles
With our optimization condition to avoid the cutting of corners we have, as a result, managed to avoid 4
obstacles at the same time.
35
6 Trajectory generation with differential flatness
6.1 Introduction
In this section, we will investigate the trajectory generation part in motion planning base on the properties
of differential flatness.
6.2 Implementation
Implement the trajectory generation for the model using the flat output representation with a polynomial
parameterization. We consider the model of the following system:
ẋ(t) = Va (t) cos ψ(t) + Wx
ẏ(t) = Va (t) sin ψ(t) + Wy
g tan ϕ(t)
ψ̇(t) =
Va (t)
described generally by :
ξ˙ (t) = f (ξ (t) , u (t))
where: ⊺ ⊺
The state ξ (t) = x (t) y (t) ξ (t) and the input u (t) = Va (t) ϕ (t)
⊺
Here we assume the wind velocities W (t) = Wx (t) Wy (t) are negligible.
⊺
We choose the flat output z(t) as the position components of the state: z (t) = z1 (t) z2 (t) =
⊺
x (t) y (t)
Thus we obtain :
tan−1 zz˙˙12 (t)
(t)
ψ(t) =
q
z˙1 2 (t) + z˙2 2 (t)
Va (t) =
ϕ(t) = tan−1 g1 z¨2 (t) √z˙1 (t)− z˙2 (t)z¨1 (t)
2 2
z˙1 (t)+z˙2 (t)
36
Or we can represent the matrix of coefficients in one column:
37
6.3 Matlab script
1 clear ;
2 close all ;
3 % initialize the system parameters
4 param . g =9.8;
5 param . h =0.05;
6 param . dblAlt =150;
7 % initialize the time stamp , the state and input
8 reference :
9 t = [] ;
10 xiref = [] ;
11 uref = [] ;
12 % define the initial and final conditions :
13 xi = [50 50 pi /4];
14 ui = [19 0];
15 xf = [200 200 - pi /4];
16 uf = [19 0];
17 T = 15;
18 % construct the matrix M \ alpha =\ bar { z } as in eq . (6) :
19 M =[ 1 0 0 0 0 0 0 0 0 0 0 0;
20 0 1 0 0 0 0 0 0 0 0 0 0;
21 0 0 2 0 0 0 0 0 0 0 0 0;
22 0 0 0 0 0 0 1 0 0 0 0 0;
23 0 0 0 0 0 0 0 1 0 0 0 0;
24 0 0 0 0 0 0 0 0 2 0 0 0;
25 1 T T ^2 T ^3 T ^4 T ^5 0 0 0 0 0 0;
26 0 1 2* T 3* T ^2 4* T ^3 5* T ^4 0 0 0 0 0 0;
27 0 0 2 6* T 12* T ^2 20* T ^3 0 0 0 0 0 0;
28 0 0 0 0 0 0 1 T T ^2 T ^3 T ^4 T ^5;
29 0 0 0 0 0 0 0 1 2* T 3* T ^2 4* T ^3 5* T ^4;
30 0 0 0 0 0 0 0 0 2 6* T 12* T ^2 20* T ^3];
31 b =[ xi (1) ui (1) * cos ( xi (3) ) - sin ( xi (3) ) * param . g * tan ( ui (2) ) ...
32 xi (2) ui (1) * sin ( xi (3) ) cos ( xi (3) ) * param . g * tan ( ui (2) ) ...
33 xf (1) uf (1) * cos ( xf (3) ) - sin ( xf (3) ) * param . g * tan ( uf (2) ) ...
34 xf (2) uf (1) * sin ( xf (3) ) cos ( xf (3) ) * param . g * tan ( uf (2) ) ] ’;
35 a = M\b;
36 % define the flat output and its derivatives
37 z1 = @ ( t ) ( a (1) + a (2) * t + a (3) * t .^2+ a (4) * t .^3+ a (5) * t .^4+ a (6) * t .^5) ;
38 z2 = @ ( t )
39 ( a (7) + a (8) * t + a (9) * t .^2+ a (10) * t .^3+ a (11) * t .^4+ a (12) * t .^5) ;
40 zd1 = @ ( t ) ( a (2) +2* a (3) * t +3* a (4) * t .^2+4* a (5) * t .^3+5* a (6) * t .^4) ;
41 zd2 = @ ( t ) ( a (8) +2* a (9) * t +3* a (10) * t .^2+4* a (11) * t .^3+5* a (12) * t .^4) ;
42 zdd1 = @ ( t ) (2* a (3) +6* a (4) * t +12* a (5) * t .^2+20* a (6) * t .^3) ;
43 zdd2 = @ ( t ) (2* a (9) +6* a (10) * t +12* a (11) * t .^2+20* a (12) * t .^3) ;
44 X = @ ( t ) ([ feval ( z1 , t ) ; feval ( z2 , t ) ;
45 atan2 ( feval ( zd2 , t ) , feval ( zd1 , t ) ) ]) ;
46 U = @ ( t ) ([ sqrt (( feval ( zd1 , t ) .^2) +( feval ( zd2 , t ) .^2) ) ;
47 atan2 (( feval ( zdd2 , t ) .* feval ( zd1 , t ) -
48 feval ( zd2 , t ) .* feval ( zdd1 , t ) ) ,...
49 ( param . g .*( sqrt (( feval ( zd1 , t ) .^2) +( feval ( zd2 , t ) .^2) ) ) ) ) ]) ;
50
51 % compute the state and input reference :
52 tt = param . h : param . h : T ;
53 xiref =[ xiref feval (X , tt ) ];
54 uref =[ uref feval (U , tt ) ];
55 figure ;
56 subplot (2 ,2 ,1)
57 plot ( tt , xiref (1 ,:) ) ;
58 title ( " Position X ( t ) " ) ;
59 xlabel ( " t " ) ;
60 ylabel ( " X ( t ) " ) ;
61 subplot (2 ,2 ,2)
62 plot ( tt , xiref (2 ,:) ) ;
63 title ( " Position Y ( t ) " ) ;
64 xlabel ( " t " ) ;
65 ylabel ( " Y ( t ) " ) ;
66 subplot (2 ,2 ,3)
38
67 plot ( xiref (1 ,:) , xiref (2 ,:) ) ;
68 title ( " Trajectory " ) ;
69 xlabel ( " X " ) ;
70 ylabel ( " Y " ) ;
71 subplot (2 ,2 ,4)
72 plot ( tt , uref (1 ,:) ) ;
73 title ( " Velocity " ) ;
74 xlabel ( " t " ) ;
75 ylabel ( " Va ( t ) " ) ;
6.4 Results
In this case, we have implemented the trajectory generation using classical polynomials by change of coordinate
to the flat output space. Working with the flat output, we can reduce the dimension of output and take into
account the dynamics of the system which helps increase the performance of the controller. However the
additional constraints for the velocity will not be hold. Otherwise, from this point of view, we can combine
the flat trajectory generation in our previously implemented optimization controller to handle the others
constraints of the system.
39