source: proiecte/pmake3d/make3d_original/Make3dSingleImageStanford_version0.1/third_party/opt/yalmip/demos/sosex.m @ 37

Last change on this file since 37 was 37, checked in by (none), 14 years ago

Added original make3d

File size: 9.2 KB
Line 
1yalmip('clear')
2clc
3echo on
4
5%*********************************************************
6%
7% Sum-of-squares decomposition
8%
9%*********************************************************
10%
11% This example shows how to solve some simple SOS-problems.
12pause
13clc
14
15% Define variables
16x = sdpvar(1,1);
17y = sdpvar(1,1);
18z = sdpvar(1,1);
19pause
20
21% Define a polynomial to be analyzed
22p = 12+y^2-2*x^3*y+2*y*z^2+x^6-2*x^3*z^2+z^4+x^2*y^2;
23pause
24
25% and the corresponding constraint
26F = set(sos(p));
27pause
28
29% Call solvesos to calculate SOS-decomposition.
30solvesos(F);
31pause
32
33clc
34% Extract decomposition (a lot of spurious terms so the display is messy...)
35h = sosd(F);
36sdisplay(h)
37pause
38
39% Cleaned display...
40
41sdisplay(clean(h,1e-4))
42pause
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
50pause
51
52solvesos(F,[],sdpsettings('sos.traceobj',1));
53h = sosd(F);
54sdisplay(h) % Still a lot of small terms...
55pause
56
57sdisplay(clean(h,1e-4)) % cleaned...
58pause
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.
68pause
69
70solvesos(F,[],sdpsettings('sos.traceobj',1,'sos.clean',1e-4));
71h = sosd(F);
72sdisplay(h)
73pause
74
75% Is it really a SOS-decomposition
76% If so, p-h'*h=0
77pause
78sdisplay(p-h'*h)
79pause
80clc
81
82% What! Failure!? No, numerical issues.
83% Remove all terms smaller than 1e-6
84clean(p-h'*h,1e-6)
85pause
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
90checkset(F)
91
92pause
93clc
94
95clc
96% Let us take deeper look at the decomposition
97pause
98[sol,v,Q,res] = solvesos(F,[],sdpsettings('sos.traceobj',1));
99pause
100
101% Gramian (Block diagonal due to the feature sos.congruence)
102Q{1}
103pause
104
105% Should be positive definite (may depend on your solver due to numerical precision)
106eig(Q{1})
107pause
108
109% Monomials used in decomposition
110sdisplay(v{1})
111pause
112
113% ...numerical mismatch
114sdisplay(p-v{1}'*Q{1}*v{1});
115pause
116
117% Largest coefficient (in absolute value) in the error polynomial p-v'Qv
118mismatch = max(abs(getbase(p-v{1}'*Q{1}*v{1})))
119pause
120
121% Should be the same as reported in checkset.
122% (May be different if Q not is positive semidefinite)
123checkset(F)
124pause
125
126% The numerical value can easily be extracted also
127% from CHECKSET.
128mismatch = checkset(F)
129pause
130
131% Also given as 4th output from solvesos
132res
133pause
134
135clc
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)
141diag(Q{1})
142pause
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.
152usethese = find(diag(Q{1})>1e-3);
153pause
154[sol,v,Q] = solvesos(F,[],sdpsettings('sos.traceobj',1),[],v{1}(usethese));
155pause
156
157% The net result is a smaller decomposition
158sdisplay(sosd(F))
159pause
160
161% The problem is better conditioned and leads to smaller residuals
162checkset(F)
163pause
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.
168Q{1}
169pause
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
184pause
185[sol,v,Q] = solvesos(F,[],sdpsettings('sos.traceobj',1,'sos.impsparse',1),[],v{1});
186pause
187
188% Sweet...
189Q{1}
190pause
191
192clc
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.
203pause
204[sol,v,Q] = solvesos(F,[],sdpsettings('sos.traceobj',1,'sos.numblkdg',1e-5));
205pause
206
207% Sweet...
208Q{1}
209pause
210
211
212
213clc
214% As a second example, we solve a somewhat larger problem.
215%
216% We want to show that the following matrix is co-positive
217J = [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];
222pause
223
224% It is clearly co-positive if this polynomial
225% is a sum-of-squares
226x1 = sdpvar(1,1);x2 = sdpvar(1,1);x3 = sdpvar(1,1);x4 = sdpvar(1,1);x5 = sdpvar(1,1);
227z  = [x1^2 x2^2 x3^2 x4^2 x5^2]';
228p  = z'*J*z;
229pause
230
231solvesos(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
246p_new = p*(x1^2+x2^2+x3^2+x4^2+x5^2);
247
248pause
249F = set(sos(p_new));
250[sol,v,Q] = solvesos(F);
251checkset(F)
252% We found a decomposition, hence p is SOS and J is co-positive
253pause
254
255% Just for fun, let us solve the problem again, this time
256% removing some redundant terms
257pause
258usethese = find(diag(Q{1})>1e-3);
259[sol,v,Q] = solvesos(F,[],[],[],v{1}(usethese));
260pause
261
262clc
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.
269pause
270
271% Define a symmetric polynomail matrix
272sdpvar x1 x2
273P = [1+x1^2 -x1+x2+x1^2;-x1+x2+x1^2 2*x1^2-2*x1*x2+x2^2];
274pause
275
276% Call SOLVESOS
277[sol,v,Q] = solvesos(set(sos(P)));
278
279% The basis is now matrix valued
280sdisplay(v{1})
281pause
282
283% Check that the polynomials match
284clean(v{1}'*Q{1}*v{1}-P,1e-8)
285pause
286
287clc
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
297pause
298
299% Define p and t
300x = sdpvar(1,1);
301y = sdpvar(1,1);
302z = sdpvar(1,1);
303
304p = 12+y^2-2*x^3*y+2*y*z^2+x^6-2*x^3*z^2+z^4+x^2*y^2;
305t = sdpvar(1,1)
306pause
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
312solvesos(set(sos(p-t)),-t);
313pause
314
315% Lower bound
316double(t)
317pause
318clc
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
326pause
327
328% Define state-variables and system
329x = sdpvar(2,1);
330f = [-x(1)-x(1)^3+x(2);-x(2)];
331
332pause
333
334% Define z, P, Lyapunov function and derivatives
335z = [x(1);x(2);x(1)^2;x(1)*x(2);x(2)^2];
336P = sdpvar(length(z),length(z));
337V = z'*P*z;
338dVdx = jacobian(V,x);
339dVdt = dVdx*f;
340pause
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%
351F = set(sos(-dVdt)) + set(P>eye(5));
352[sol,v,Q] = solvesos(F,trace(P));
353pause
354clc
355
356% Checking the validity of the SOS-decomposition is easily done
357% (checks SOS-decomposition at the current value of P)
358checkset(F)
359pause
360
361% So, according to the theory, we should have -dVdt==v'Qv
362clean(v{1}'*Q{1}*v{1}-(-dVdt),1e-2)
363pause
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.
370pause
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)
378V = z'*double(P)*z;
379dVdx = jacobian(V,x);
380dVdt = dVdx*f;
381clean(-dVdt-v{1}'*Q{1}*v{1},1e-10)
382pause
383
384clc
385% Finally, make sure to check out the help in the HTML manual for more information.
386pause
387
388echo off
389
390
Note: See TracBrowser for help on using the repository browser.