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

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

Added original make3d

File size: 6.6 KB
Line 
1clc
2echo on
3%*********************************************************
4%
5% Moment relaxations
6%
7%*********************************************************
8%
9% This example shows how to solve some polynomial problems
10% using the theory of moment-relxations.
11pause
12clc
13
14% Define variables
15x1 = sdpvar(1,1);
16x2 = sdpvar(1,1);
17x3 = sdpvar(1,1);
18pause
19
20% Define a polynomial to be analyzed...
21p = -2*x1+x2-x3;
22pause
23
24% and a set of polynomial inequalities (concave constraint)
25F = set(x1*(4*x1-4*x2+4*x3-20)+x2*(2*x2-2*x3+9)+x3*(2*x3-13)+24>0);
26F = F + set(4-(x1+x2+x3)>0);
27F = F + set(6-(3*x2+x3)>0);
28F = F + set(x1>0);
29F = F + set(2-x1>0);
30F = F + set(x2>0);
31F = F + set(x3>0);
32F = F + set(3-x3>0);
33pause
34
35% A lower bound {min p(x) | F(x)>0} can be found using solvemoment
36solvemoment(F,p);
37pause
38clc
39% The lower bound can be be found by checking the
40% value of the RELAXED variables. When the moment relaxation
41% is calculated, x1 and x1^2 etc. are treated as independant
42% variables. To extract the values of the relaxed variables,
43% use the command relaxdouble
44relaxdouble(p)
45[double([x1;x2;x3;x1^2;x2^2;x3^2]) relaxdouble([x1;x2;x3;x1^2;x2^2;x3^2])]
46
47pause
48clc
49
50% Better lower bound can be obtained by using higher order
51% relaxations
52pause
53solvemoment(F,p,[],2);
54relaxdouble(p)
55
56pause
57clc
58
59% Another way to obtain better bounds is to add more valid constraints
60% One simple trick is to use the current lower bound by adding
61% the constraint p>double(p)
62pause
63solvemoment(F+set(p>double(p)),p,[],2);
64relaxdouble(p)
65
66pause
67clc
68% ...or square some linear constraints
69pause
70solvemoment(F+set(9>x3^2)+set(4>x1^2),p,[],2);
71relaxdouble(p)
72
73pause
74clc
75% or use an even higher relaxation
76pause
77solvemoment(F,p,[],4);
78relaxdouble(p)
79
80pause
81
82% The value -4 is known to be the global optimum, but our relaxed solution
83% is still not a valid solution.
84checkset(F)
85pause
86
87% YALMIP can try to recover globally optimal solutions, but to
88% do this, three output arguments are needed. The global solutions,
89% if sucessfully extracted, are returned in the second output.
90pause
91[solution,xoptimal] = solvemoment(F,p,[],4);
92
93% Use the first extracted global solution.
94setsdpvar([x1;x2;x3],xoptimal{1});
95double(p)
96checkset(F)
97
98pause
99
100% In this example, there are only three variables and they
101% correpond to the variables in the output x.
102%
103% In principle, the code setsdpvar(recover([depends(p) depends(F)]),x{1})
104% should work, but to be on the safe side in a more complex scenario,
105% a third output argument should be used to keep track of the variables.
106pause
107[solution,xoptimal,momentdata] = solvemoment(F,p,[],4);
108
109% The variables used in the relaxation are returned in
110% the fourth output, in the field 'x'
111momentdata
112pause
113
114setsdpvar(momentdata.x,xoptimal{1});
115pause
116
117double(p)
118checkset(F)
119pause
120
121clc
122
123% Polynomial semidefinite constraints can be addressed also
124sdpvar x1 x2
125p = -x1^2-x2^2;
126F = set([1-4*x1*x2 x1;x1 4-x1^2-x2^2]);
127pause
128[sol,xoptimal] = solvemoment(F,p,[],2);
129setsdpvar([x1;x2],xoptimal{1});
130double(p)
131checkset(F)
132
133pause
134clc
135
136% Note that the moment relaxation solver can be called in
137% a fashion that is more consistent with the rest of the YALMIP framework.
138%
139% To do this, just use the solver tag 'moment'.
140pause
141solution = solvesdp(F,p,sdpsettings('solver','moment','moment.order',2));
142setsdpvar(solution.momentdata.x,solution.xoptimal{1});
143double(p)
144checkset(F)
145pause
146
147clc
148
149% The extraction of the global solution is numerically sensitive and
150% can easily lead to low accuracy, or even completly wrong solutions.
151%
152% Some tweaking can be done to improve upon this.
153%
154% Consider the following problem with known optima (0,2) and (0,-2)
155clear all
156yalmip('clear')
157x1 = sdpvar(1,1);
158x2 = sdpvar(1,1);
159obj = -x1^2-x2^2;
160g1 = 5-4*x1*x2-x1^2-x2^2;
161g2 = 4-16*x1*x2-2*x1^2-x2^2+4*x1^3*x2+4*x1*x2^3;
162F = set([g1 g2]);
163pause
164
165% We solve and extract solutions (requires a rather high order to find a solution)
166% Fo reasons that will become clear later, we also specify the tolerance
167% for the Gaussian elimination, used during the extraction process.
168pause
169[sol,xe] = solvemoment(F,obj,sdpsettings('moment.rceftol',1e-6),7);
170
171% The accuracy can easily become rather poor for this problem
172% NOTE : There is a randomization step in the extraction algorithm,
173% so these numbers may differ.
174[xe{1} xe{2}]
175pause
176
177% To improve the solutions, YALMIP can perform a number of Newton
178% iterations on a set of polynomial equalities that are solved using
179% numerical linear algebra techniques during the extraction algorithm.
180% (Note, these are not the original polynomials, but polynomials that
181% define the global solutions, given the moment matrices)
182pause
183[sol,xe] = solvemoment(F,obj,sdpsettings('moment.refine',5),7);
184[xe{1} xe{2}]
185pause
186
187% The main reason for the poor accuracy in this problem is actually
188% the bad conditioning during a Gaussian elimination. To improve
189% this situation, it is possible to let YALMIP select a tolerance using
190% some heuristics.
191%
192% This can be done by specifying the tolerance rceftol to -1.
193% In fact, this is the default setting, so we just run standard settings!
194pause
195[sol,xe] = solvemoment(F,obj,[],7);
196[xe{1} xe{2}]
197pause
198
199% For this problem we needed a rather high relaxation order to be able to
200% extract the solution.
201%
202% It is possible to tell YALMIP to try to extract a solution, even though
203% YALMIP belives this is impossible.
204%
205% To force YALMIP to try to extract global solutions, set the option
206% moment.extractrank to the desired number of solutions.
207%
208% Let us try to extract a solution from a 6th order relaxation.
209pause
210[sol,xe] = solvemoment(F,obj,sdpsettings('moment.extractrank',2),6);
211[xe{1} xe{2}]
212pause
213
214% Hmm, that's poor. No wonder, YALMIP did not expect it to be possible to
215% extract these solutions, so numerical problems are very likely during the
216% extraction process. Let us try to refine it.
217pause
218[sol,xe] = solvemoment(F,obj,sdpsettings('moment.extractrank',2,'moment.refine',5),6);
219[xe{1} xe{2}]
220pause
221
222% Pretty annoying to resolve the problem when you just want to change a
223% setting in the extraction algorithm, right?
224%
225% To avoid this, use 4 output arguments
226[sol,xe,momentdata] = solvemoment(F,obj,sdpsettings('moment.extractrank',2,'moment.refine',5),6);
227pause
228
229% The variable momentdata contains all information needed to extract
230% solutions.
231%
232% Hence, we can compare solutions for different settings easily.
233xe0 = extractsolution(momentdata,sdpsettings('moment.extractrank',2,'moment.refine',0));
234xe1 = extractsolution(momentdata,sdpsettings('moment.extractrank',2,'moment.refine',2));
235xe2 = extractsolution(momentdata,sdpsettings('moment.extractrank',2,'moment.refine',5));
236[xe0{1} xe0{2} xe1{1} xe1{2} xe2{1} xe2{2}]
237pause
238
239echo off
240
Note: See TracBrowser for help on using the repository browser.