1 | yalmip('clear') |
---|
2 | clc |
---|
3 | echo on |
---|
4 | |
---|
5 | %********************************************************* |
---|
6 | % |
---|
7 | % Sum-of-squares decomposition |
---|
8 | % |
---|
9 | %********************************************************* |
---|
10 | % |
---|
11 | % This example shows how to solve some simple SOS-problems. |
---|
12 | pause |
---|
13 | clc |
---|
14 | |
---|
15 | % Define variables |
---|
16 | x = sdpvar(1,1); |
---|
17 | y = sdpvar(1,1); |
---|
18 | z = sdpvar(1,1); |
---|
19 | pause |
---|
20 | |
---|
21 | % Define a polynomial to be analyzed |
---|
22 | p = 12+y^2-2*x^3*y+2*y*z^2+x^6-2*x^3*z^2+z^4+x^2*y^2; |
---|
23 | pause |
---|
24 | |
---|
25 | % and the corresponding constraint |
---|
26 | F = set(sos(p)); |
---|
27 | pause |
---|
28 | |
---|
29 | % Call solvesos to calculate SOS-decomposition. |
---|
30 | solvesos(F); |
---|
31 | pause |
---|
32 | |
---|
33 | clc |
---|
34 | % Extract decomposition (a lot of spurious terms so the display is messy...) |
---|
35 | h = sosd(F); |
---|
36 | sdisplay(h) |
---|
37 | pause |
---|
38 | |
---|
39 | % Cleaned display... |
---|
40 | |
---|
41 | sdisplay(clean(h,1e-4)) |
---|
42 | pause |
---|
43 | |
---|
44 | |
---|
45 | % To obtain more sparse solutions, it can sometimes be |
---|
46 | % beneficial to minimize the trace of the Gramian Q |
---|
47 | % in the decomposition p(x) = v(x)'Qv(x)=h'(x)h(x) |
---|
48 | % |
---|
49 | % This can be done by specifying sos.traceobj=1 |
---|
50 | pause |
---|
51 | |
---|
52 | solvesos(F,[],sdpsettings('sos.traceobj',1)); |
---|
53 | h = sosd(F); |
---|
54 | sdisplay(h) % Still a lot of small terms... |
---|
55 | pause |
---|
56 | |
---|
57 | sdisplay(clean(h,1e-4)) % cleaned... |
---|
58 | pause |
---|
59 | |
---|
60 | |
---|
61 | % To clean the decomposition from small monomials already in solvesos, |
---|
62 | % use the option 'sos.clean'. This will remove terms in chol(Q)v(x) with |
---|
63 | % coefficients smaller than sos.clean. |
---|
64 | % |
---|
65 | % NOTE, this means that the match between p and h'h will be detoriate, since we |
---|
66 | % clean h after the SOS computations are done. This should only be used if |
---|
67 | % you only want to display the polynomial. |
---|
68 | pause |
---|
69 | |
---|
70 | solvesos(F,[],sdpsettings('sos.traceobj',1,'sos.clean',1e-4)); |
---|
71 | h = sosd(F); |
---|
72 | sdisplay(h) |
---|
73 | pause |
---|
74 | |
---|
75 | % Is it really a SOS-decomposition |
---|
76 | % If so, p-h'*h=0 |
---|
77 | pause |
---|
78 | sdisplay(p-h'*h) |
---|
79 | pause |
---|
80 | clc |
---|
81 | |
---|
82 | % What! Failure!? No, numerical issues. |
---|
83 | % Remove all terms smaller than 1e-6 |
---|
84 | clean(p-h'*h,1e-6) |
---|
85 | pause |
---|
86 | |
---|
87 | % The largest coefficient in the polynomial |
---|
88 | % p-h'*h is displayed in checkset as the |
---|
89 | % primal residual for a SOS constraint |
---|
90 | checkset(F) |
---|
91 | |
---|
92 | pause |
---|
93 | clc |
---|
94 | |
---|
95 | clc |
---|
96 | % Let us take deeper look at the decomposition |
---|
97 | pause |
---|
98 | [sol,v,Q,res] = solvesos(F,[],sdpsettings('sos.traceobj',1)); |
---|
99 | pause |
---|
100 | |
---|
101 | % Gramian (Block diagonal due to the feature sos.congruence) |
---|
102 | Q{1} |
---|
103 | pause |
---|
104 | |
---|
105 | % Should be positive definite (may depend on your solver due to numerical precision) |
---|
106 | eig(Q{1}) |
---|
107 | pause |
---|
108 | |
---|
109 | % Monomials used in decomposition |
---|
110 | sdisplay(v{1}) |
---|
111 | pause |
---|
112 | |
---|
113 | % ...numerical mismatch |
---|
114 | sdisplay(p-v{1}'*Q{1}*v{1}); |
---|
115 | pause |
---|
116 | |
---|
117 | % Largest coefficient (in absolute value) in the error polynomial p-v'Qv |
---|
118 | mismatch = max(abs(getbase(p-v{1}'*Q{1}*v{1}))) |
---|
119 | pause |
---|
120 | |
---|
121 | % Should be the same as reported in checkset. |
---|
122 | % (May be different if Q not is positive semidefinite) |
---|
123 | checkset(F) |
---|
124 | pause |
---|
125 | |
---|
126 | % The numerical value can easily be extracted also |
---|
127 | % from CHECKSET. |
---|
128 | mismatch = checkset(F) |
---|
129 | pause |
---|
130 | |
---|
131 | % Also given as 4th output from solvesos |
---|
132 | res |
---|
133 | pause |
---|
134 | |
---|
135 | clc |
---|
136 | % By studying the diagonal of the Gramian, we see that |
---|
137 | % a lot of monomials are not used in the decomposition |
---|
138 | % (zero diagonal means that the corresponding row and |
---|
139 | % column is zero, hence the corresponding monomial is |
---|
140 | % only multiplied by 0) |
---|
141 | diag(Q{1}) |
---|
142 | pause |
---|
143 | |
---|
144 | % Let us re-solve the problem, but this time we manually |
---|
145 | % specify what monomials to use. |
---|
146 | % Since we know that the monomials generated by YALMIP |
---|
147 | % are guaranteed to be sufficient, but Q indicates that |
---|
148 | % some actually are redundant, let us re-use the old ones, |
---|
149 | % but skip those corresponding to small diagonals. |
---|
150 | % |
---|
151 | % User specified monomials is the fifth input. |
---|
152 | usethese = find(diag(Q{1})>1e-3); |
---|
153 | pause |
---|
154 | [sol,v,Q] = solvesos(F,[],sdpsettings('sos.traceobj',1),[],v{1}(usethese)); |
---|
155 | pause |
---|
156 | |
---|
157 | % The net result is a smaller decomposition |
---|
158 | sdisplay(sosd(F)) |
---|
159 | pause |
---|
160 | |
---|
161 | % The problem is better conditioned and leads to smaller residuals |
---|
162 | checkset(F) |
---|
163 | pause |
---|
164 | |
---|
165 | % The Gramian is of-course smaller now. |
---|
166 | % No zero diagonal, hence we have no simple way to |
---|
167 | % obtain a smaller decomposition. |
---|
168 | Q{1} |
---|
169 | pause |
---|
170 | |
---|
171 | % However, rather annoyingly there are a lot of terms |
---|
172 | % in Q that are almost 0, but not quite (once again, this |
---|
173 | % depends on what solver you have, how succesful YALMIPs |
---|
174 | % post-processing is etc.) |
---|
175 | % |
---|
176 | % It is possible to tell YALMIP to analyse the sparsity |
---|
177 | % of the Gramian after it has computed it, and re-solve |
---|
178 | % the problem, but this time forcing the elements to be 0. |
---|
179 | % |
---|
180 | % Note, we are not cleaning the Gramian a-posteriori, but |
---|
181 | % resolving the problem. Hence, the decomposition is correct. |
---|
182 | % |
---|
183 | % This can be obtained with the option sos.impsparse |
---|
184 | pause |
---|
185 | [sol,v,Q] = solvesos(F,[],sdpsettings('sos.traceobj',1,'sos.impsparse',1),[],v{1}); |
---|
186 | pause |
---|
187 | |
---|
188 | % Sweet... |
---|
189 | Q{1} |
---|
190 | pause |
---|
191 | |
---|
192 | clc |
---|
193 | % Alternatively, we can tell YALMIP to study the almost-zero pattern |
---|
194 | % of the Grmamians, and derive a block-diagonalization based on this. |
---|
195 | % By setting the options sos.numblkdg to a number larger than zero, |
---|
196 | % YALMIP will declare number smaller than this tolerance as zero, and |
---|
197 | % detect any hidden block-structure, re-solve the problem with this |
---|
198 | % more economic parameterization, and repeat until the block-structure |
---|
199 | % not changes any more. Note that this not only gives "nicer" solutions, |
---|
200 | % but also improves the numerical conditioning of the problem. |
---|
201 | % |
---|
202 | % This is the prefered way to post-process the solution. |
---|
203 | pause |
---|
204 | [sol,v,Q] = solvesos(F,[],sdpsettings('sos.traceobj',1,'sos.numblkdg',1e-5)); |
---|
205 | pause |
---|
206 | |
---|
207 | % Sweet... |
---|
208 | Q{1} |
---|
209 | pause |
---|
210 | |
---|
211 | |
---|
212 | |
---|
213 | clc |
---|
214 | % As a second example, we solve a somewhat larger problem. |
---|
215 | % |
---|
216 | % We want to show that the following matrix is co-positive |
---|
217 | J = [1 -1 1 1 -1; |
---|
218 | -1 1 -1 1 1; |
---|
219 | 1 -1 1 -1 1; |
---|
220 | 1 1 -1 1 -1; |
---|
221 | -1 1 1 -1 1]; |
---|
222 | pause |
---|
223 | |
---|
224 | % It is clearly co-positive if this polynomial |
---|
225 | % is a sum-of-squares |
---|
226 | x1 = sdpvar(1,1);x2 = sdpvar(1,1);x3 = sdpvar(1,1);x4 = sdpvar(1,1);x5 = sdpvar(1,1); |
---|
227 | z = [x1^2 x2^2 x3^2 x4^2 x5^2]'; |
---|
228 | p = z'*J*z; |
---|
229 | pause |
---|
230 | |
---|
231 | solvesos(set(sos(p))) |
---|
232 | |
---|
233 | % Hmm, failure... |
---|
234 | % |
---|
235 | % Note that the error-message displayed can be somewhat mis-guiding. |
---|
236 | % Depending on the solver and the option sos.model, infeasible problems can |
---|
237 | % be declared as unbounded, and vice versa. In most cases (using SeDuMi, |
---|
238 | % PENSDP, SDPT3,... and kernel representation model) infeasibility is |
---|
239 | % reported as unbounded and an unbounded objective is reported as |
---|
240 | % infeasible (the reason is that YALMIP solves a problem related to the |
---|
241 | % dual of the SOS problem when the kernel representation model is used.) |
---|
242 | % |
---|
243 | % Let's multiply the polynomial with the positive definite function |
---|
244 | % sum(x_i^2). If this new polynomial is SOS, then so is the original. |
---|
245 | |
---|
246 | p_new = p*(x1^2+x2^2+x3^2+x4^2+x5^2); |
---|
247 | |
---|
248 | pause |
---|
249 | F = set(sos(p_new)); |
---|
250 | [sol,v,Q] = solvesos(F); |
---|
251 | checkset(F) |
---|
252 | % We found a decomposition, hence p is SOS and J is co-positive |
---|
253 | pause |
---|
254 | |
---|
255 | % Just for fun, let us solve the problem again, this time |
---|
256 | % removing some redundant terms |
---|
257 | pause |
---|
258 | usethese = find(diag(Q{1})>1e-3); |
---|
259 | [sol,v,Q] = solvesos(F,[],[],[],v{1}(usethese)); |
---|
260 | pause |
---|
261 | |
---|
262 | clc |
---|
263 | % The basic idea in sum of squares readily |
---|
264 | % extend also to the matrix valued case. |
---|
265 | % |
---|
266 | % In other words, find a decomposition of |
---|
267 | % the symmetric polynomial matrix P(x), |
---|
268 | % hence proving global postive definiteness. |
---|
269 | pause |
---|
270 | |
---|
271 | % Define a symmetric polynomail matrix |
---|
272 | sdpvar x1 x2 |
---|
273 | P = [1+x1^2 -x1+x2+x1^2;-x1+x2+x1^2 2*x1^2-2*x1*x2+x2^2]; |
---|
274 | pause |
---|
275 | |
---|
276 | % Call SOLVESOS |
---|
277 | [sol,v,Q] = solvesos(set(sos(P))); |
---|
278 | |
---|
279 | % The basis is now matrix valued |
---|
280 | sdisplay(v{1}) |
---|
281 | pause |
---|
282 | |
---|
283 | % Check that the polynomials match |
---|
284 | clean(v{1}'*Q{1}*v{1}-P,1e-8) |
---|
285 | pause |
---|
286 | |
---|
287 | clc |
---|
288 | % Let us now solve a parameterized SOS-problem. |
---|
289 | % |
---|
290 | % A typical application is to find a lower bound |
---|
291 | % on the global minima. |
---|
292 | % |
---|
293 | % This is done by solving |
---|
294 | % |
---|
295 | % max t |
---|
296 | % subject to p(x)-t is SOS |
---|
297 | pause |
---|
298 | |
---|
299 | % Define p and t |
---|
300 | x = sdpvar(1,1); |
---|
301 | y = sdpvar(1,1); |
---|
302 | z = sdpvar(1,1); |
---|
303 | |
---|
304 | p = 12+y^2-2*x^3*y+2*y*z^2+x^6-2*x^3*z^2+z^4+x^2*y^2; |
---|
305 | t = sdpvar(1,1) |
---|
306 | pause |
---|
307 | |
---|
308 | % maximize t subject to SOS-constraint |
---|
309 | % |
---|
310 | % SOLVESOS will automatically categorize t as a |
---|
311 | % parametric variable since it is part of the objective |
---|
312 | solvesos(set(sos(p-t)),-t); |
---|
313 | pause |
---|
314 | |
---|
315 | % Lower bound |
---|
316 | double(t) |
---|
317 | pause |
---|
318 | clc |
---|
319 | |
---|
320 | |
---|
321 | % Ok, now for some more advanced coding |
---|
322 | % |
---|
323 | % Given the nonlinear system dxdt=f(x), |
---|
324 | % we will try to prove that z'Pz is a lyapunov function |
---|
325 | % where z = [x1;x2;x1x2;x1^2;x2^2] and P positive definite |
---|
326 | pause |
---|
327 | |
---|
328 | % Define state-variables and system |
---|
329 | x = sdpvar(2,1); |
---|
330 | f = [-x(1)-x(1)^3+x(2);-x(2)]; |
---|
331 | |
---|
332 | pause |
---|
333 | |
---|
334 | % Define z, P, Lyapunov function and derivatives |
---|
335 | z = [x(1);x(2);x(1)^2;x(1)*x(2);x(2)^2]; |
---|
336 | P = sdpvar(length(z),length(z)); |
---|
337 | V = z'*P*z; |
---|
338 | dVdx = jacobian(V,x); |
---|
339 | dVdt = dVdx*f; |
---|
340 | pause |
---|
341 | |
---|
342 | % Try to prove that dVdt<0, while minimizing |
---|
343 | % trace(P), subject to P>0 |
---|
344 | % |
---|
345 | % All parametric variables (i.e. P) are constrained |
---|
346 | % hence SOLVESOS will find them automatically. |
---|
347 | % |
---|
348 | % Alternative |
---|
349 | % solvesos(F,trace(P),[],P(:)); |
---|
350 | % |
---|
351 | F = set(sos(-dVdt)) + set(P>eye(5)); |
---|
352 | [sol,v,Q] = solvesos(F,trace(P)); |
---|
353 | pause |
---|
354 | clc |
---|
355 | |
---|
356 | % Checking the validity of the SOS-decomposition is easily done |
---|
357 | % (checks SOS-decomposition at the current value of P) |
---|
358 | checkset(F) |
---|
359 | pause |
---|
360 | |
---|
361 | % So, according to the theory, we should have -dVdt==v'Qv |
---|
362 | clean(v{1}'*Q{1}*v{1}-(-dVdt),1e-2) |
---|
363 | pause |
---|
364 | |
---|
365 | % No! v{1}'*Q{1}*v{1} is a decomposition of -dVdt |
---|
366 | % when P is *fixed* to the computed optimal value. |
---|
367 | % |
---|
368 | % v{1}'*Q{1}*v{1} is a polynomial in x only, while |
---|
369 | % dVdt is a polynomial in x and P. |
---|
370 | pause |
---|
371 | |
---|
372 | % To check the decomposition manually, we need to |
---|
373 | % define the polynomials with the computed value |
---|
374 | % of P |
---|
375 | % |
---|
376 | % (Of-course, in practise it is most often more convenient |
---|
377 | % to use CHECKSET where this is done automatically) |
---|
378 | V = z'*double(P)*z; |
---|
379 | dVdx = jacobian(V,x); |
---|
380 | dVdt = dVdx*f; |
---|
381 | clean(-dVdt-v{1}'*Q{1}*v{1},1e-10) |
---|
382 | pause |
---|
383 | |
---|
384 | clc |
---|
385 | % Finally, make sure to check out the help in the HTML manual for more information. |
---|
386 | pause |
---|
387 | |
---|
388 | echo off |
---|
389 | |
---|
390 | |
---|