source: proiecte/pmake3d/make3d_original/Make3dSingleImageStanford_version0.1/third_party/opt/yalmip/solvers/mpcvx.m @ 37

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

Added original make3d

File size: 6.7 KB
Line 
1function output = mpcvx(p)
2%MPCVX          Approximate multi-parametric programming
3%
4% MPCVX is never called by the user directly, but is called by
5% YALMIP from SOLVESDP, by choosing the solver tag 'mpcvx' in sdpsettings
6%
7% The behaviour of MPCVX can be altered using the fields
8% in the field 'mpcvx' in SDPSETTINGS
9%
10% WARNING: THIS IS EXPERIMENTAL CODE
11%
12
13% Author Johan Löfberg
14% $Id: mpcvx.m,v 1.6 2005/05/07 13:53:20 joloef Exp $
15
16% ********************************
17% INITIALIZE DIAGNOSTICS IN YALMIP
18% ********************************
19mpsolvertime = clock;
20showprogress('mpcvx started',p.options.showprogress);
21
22% *******************************
23% Display-logics
24% *******************************
25switch max(min(p.options.verbose,3),0)
26    case 0
27        p.options.bmibnb.verbose = 0;
28    case 1
29        p.options.bmibnb.verbose = 1;
30        p.options.verbose = 0;
31    case 2
32        p.options.bmibnb.verbose = 2;
33        p.options.verbose = 0;
34    case 3
35        p.options.bmibnb.verbose = 2;
36        p.options.verbose = 1;
37    otherwise
38        p.options.bmibnb.verbose = 0;
39        p.options.verbose = 0;
40end
41
42% *******************************
43% No reason to save
44% *******************************
45p.options.saveduals = 0;
46
47% **********************************
48% Generate an exploration set
49% **********************************
50p.solver.subcall = 'callsedumi';
51solver    = eval(['@' p.solver.subcall]);    % LP solver
52[THETA,problem] = ray_shoot(p,solver);
53
54%plot(polytope(THETA'));hold on;
55
56% **********************************
57% Calculate optimal x in each initial vertex
58% **********************************
59X   = [];
60OBJ = [];
61for i = 1:size(THETA,2)
62    [x_i,obj_i] = solve_node(p,solver,THETA(:,i));
63    X = [X x_i];
64    OBJ = [OBJ obj_i];
65end
66
67% **********************************
68% Partition initial set
69% **********************************
70node_keeper;
71node_keeper(THETA,X,OBJ);
72T = delaunayn(THETA',{'Qz','Qt'});
73
74% **********************************
75% Do algo on all initial simplicies
76% **********************************
77optimal_simplicies = [];
78p.options.mpcvx.eps = 5e-2;
79for i = 1:size(T,1)
80    optimal_simplicies = [optimal_simplicies mp_simplex(p,solver,T(i,:)')];
81end
82mpsolvertime = etime(clock,mpsolvertime)
83
84% **********************************
85% Create format compatible with MPT
86% **********************************
87Pn = polytope;
88j = 1;
89for i = 1:size(optimal_simplicies,2)
90    [theta,x,obj] = node_keeper(optimal_simplicies(:,i));
91    m = size(theta,1);
92    Minv = inv([ones(1,m+1);theta]);
93
94    try
95        Pn = [Pn polytope(theta')];
96        Gi{j} = x(find(~ismember(1:length(p.c),p.parametric_variables)),:)*Minv(:,1);
97        Fi{j} = x(find(~ismember(1:length(p.c),p.parametric_variables)),:)*Minv(:,2:end);       
98        j = j + 1;
99    catch
100        %Gi{j} = polytope([]);
101        %Fi{j} = polytope([]);
102    end
103end
104
105output.problem = problem;
106output.Primal            = nan*ones(length(p.c),1);
107output.Dual        = [];
108output.Slack       = [];
109output.infostr      = yalmiperror(output.problem,'MPCVX');
110output.solverinput  = 0;
111output.solveroutput.Pn = Pn;
112output.solveroutput.Fi = Fi;
113output.solveroutput.Gi = Gi;
114output.solvertime   = mpsolvertime;
115
116function simplex_solution = mp_simplex(p,solver,theta_indicies)
117
118[theta,x,obj] = node_keeper(theta_indicies);
119% Parametric dimension
120m = size(theta,1);
121
122Minv = inv([ones(1,m+1);theta]);
123M1 = Minv(:,1);
124M2 = zeros(size(M1,1),length(p.c));
125M2(:,p.parametric_variables) = Minv(:,2:end);
126p.F_struc = [M1 M2;p.F_struc];
127p.K.l = p.K.l + size(M1,1);
128
129Vbar = obj'*Minv;
130c = p.c;
131c2 = zeros(length(p.c),1);c2(p.parametric_variables) = -Vbar(2:end);
132p.c = p.c+c2;p.c;
133output = feval(solver,p);
134p.c = c;
135
136upper = obj'*Minv*[1;output.Primal(p.parametric_variables)];
137lower = p.c'*output.Primal+output.Primal'*p.Q*output.Primal;
138
139eps_CP_S = min(upper-lower,((upper-lower)/(1+lower)));
140
141% Dig deeper?
142%plot(polytope(theta'),struct('color',rand(3,1)))
143if eps_CP_S > p.options.mpcvx.eps
144
145    thetac = output.Primal(p.parametric_variables);
146
147    [x_i,obj_i] = solve_node(p,solver,thetac);
148    new_index = node_keeper(thetac(:),x_i(:),obj_i);
149
150    simplex_solution = [];
151    for i = 1:(size(theta,1)+1)
152        j = 1:(size(theta,1)+1);
153        j(i)=[];
154        theta_test = [theta(:,j) thetac];
155        if min(svd([ones(1,size(theta_test,2));theta_test]))>1e-4
156            simplex_solution =  [simplex_solution mp_simplex(p,solver,[theta_indicies(j);new_index])];
157        end
158    end
159
160else
161    % This simplex constitutes a node, report back
162    simplex_solution = theta_indicies(:);
163end
164
165function varargout = node_keeper(varargin)
166
167persistent savedTHETA
168persistent savedX
169persistent savedOBJ
170
171switch nargin
172    case 0 % CLEAR
173        savedTHETA = [];
174        savedX     = [];
175        savedOBJ   = [];
176    case 3 % Append
177        savedTHETA = [savedTHETA varargin{1}];
178        savedX     = [savedX varargin{2}];
179        savedOBJ   = [savedOBJ varargin{3}];
180        varargout{1} = size(savedTHETA,2);
181    case 1 % Get data
182        varargout{1} = savedTHETA(:,varargin{1});
183        varargout{2} = savedX(:,varargin{1});
184        varargout{3} = savedOBJ(:,varargin{1});varargout{3} = varargout{3}(:);
185    otherwise
186        error('!')
187end
188
189
190function [THETA,problem] = ray_shoot(p,solver)
191THETA = [];
192
193p_temp = p;
194p_temp.c = p_temp.c*0;
195p_temp.Q = 0*p_temp.Q;
196for i = 1:25
197    p_temp.c(p.parametric_variables) = randn(length(p.parametric_variables),1);
198    output = feval(solver,p_temp);
199    THETA = [THETA output.Primal(p.parametric_variables)];
200end
201% Select unique and generate center
202THETA = unique(fix(THETA'*1e4)/1e4,'rows')';
203center = sum(THETA,2)/size(THETA,2);
204THETA = [THETA*0.999+repmat(0.001*center,1,size(THETA,2))];
205problem = 0;
206
207
208function [x_i,obj_i] = solve_node(p,solver,theta);
209p_temp = p;
210p_temp.F_struc(:,1) = p_temp.F_struc(:,1) + p_temp.F_struc(:,1+p.parametric_variables)*theta;
211p_temp.F_struc(:,1+p.parametric_variables) = [];
212
213empty_rows = find(~any(p_temp.F_struc(p.K.f+1:p.K.f+p.K.l,2:end),2));
214if ~isempty(empty_rows)
215    if all(p_temp.F_struc(p.K.f+empty_rows,1)>=-1e-7)
216        p_temp.F_struc(p.K.f+empty_rows,:)=[];
217        p_temp.K.l = p_temp.K.l - length(empty_rows);
218    else
219        feasible = 0;
220    end
221end
222
223x_var = find(~ismember(1:length(p.c),p.parametric_variables));
224theta_var = p.parametric_variables;
225Q11 = p.Q(x_var,x_var);
226Q12 = p.Q(x_var,theta_var);
227Q22 = p.Q(theta_var,theta_var);
228c1 = p.c(x_var);
229c2 = p.c(theta_var);
230
231p_temp.Q = Q11;
232p_temp.c = c1+2*Q12*theta;
233
234%p_temp.c(p.parametric_variables) = [];
235%p_temp.Q(:,p.parametric_variables) = [];
236%p_temp.Q(p.parametric_variables,:) = [];
237output = feval(solver,p_temp);
238
239% Recover complete [x theta]
240x_i = zeros(length(p.c),1);
241x_i(find(~ismember(1:length(p.c),p.parametric_variables))) = output.Primal;
242x_i(p.parametric_variables) = theta;
243obj_i = x_i'*p.Q*x_i+p.c'*x_i;
Note: See TracBrowser for help on using the repository browser.