source: proiecte/pmake3d/make3d_original/Make3dSingleImageStanford_version0.1/third_party/opt/yalmip/modules/robust/robustify.m @ 37

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

Added original make3d

File size: 5.7 KB
Line 
1function [F,h,failure] = robustify(F,h,ops,w)
2%ROBUSTIFY  Derives robust counterpart.
3%
4% [Frobust,objrobust,failure] = ROBUSTIFY(F,h,options) is used to derive
5% the robust counterpart of an uncertain YALMIP model.
6%
7%   min        h(x,w)
8%   subject to
9%           F(x,w) >(=) 0  for all w in W
10%
11% The constraints and objective have to satisfy a number of conditions for
12% the robustification to be tractable. Please refer to the YALMIP Wiki for
13% the current assumptions (this is constantly developing)
14%
15% See also SOLVEROBUST, UNCERTAIN
16
17% Author Johan Löfberg
18% $Id: robustify.m,v 1.19 2006/10/24 12:02:04 joloef Exp $
19
20if nargin < 3
21    ops = [];
22end
23
24if nargin < 4
25    w = [];
26end
27
28if isempty(w)
29    unc_declarations = is(F,'uncertain');
30    if any(unc_declarations)
31        w = recover(getvariables(sdpvar(F(find(unc_declarations)))));
32        F = F(find(~unc_declarations));
33    else
34        error('There is no uncertainty definition in the model.')
35    end
36end
37
38if isempty(ops)
39    ops = sdpsettings;
40end
41
42% Figure out which variables are uncertain, certain, and lifted variables
43% in the uncertainty description (this code is buggy as ....)
44[x,w,x_variables,w_variables,aux_variables,F,failure] = robust_classify_variables(F,h,ops,w);
45if failure
46    return
47end
48
49% Integer variables are OK in x, but not in the uncertainty (robustification
50% is based on strong duality in w-space)
51integervars = [yalmip('binvariables') yalmip('intvariables')];
52ind = find(is(F,'integer') | is(F,'binary'));
53if ~isempty(ind)
54    integervars = [integervars getvariables(F(ind))];
55    if any(ismember(w_variables,integervars))
56        failure = 1;
57        return
58    end
59end
60
61% Find  uncertainty description, uncertain and certain constraints
62F_w = set([]);
63F_x = set([]);
64F_xw = set([]);
65for i = 1:length(F)
66    if all(ismember(depends(F(i)),w_variables))
67        % Uncertainty definition
68        F_w = F_w + F(i);
69    elseif all(ismember(depends(F(i)),x_variables))
70        % Certain constraint
71        F_x = F_x +  F(i);
72    else
73        % Uncertain constraint
74        F_xw = F_xw + F(i);
75    end
76end
77
78% Limitation in the modelling language...
79if ~isempty(intersect(intersect(depends(F_xw),depends(F_w)),aux_variables))
80    disp('You are most likely using a nonlinear operator to describe the');
81    disp('uncertainty set (such as norm(w,1) <=1). This is currently not');
82    disp('supported. Please model the constraint manually.');
83    error('Uncertain model does not satisfy assumptions (nonlinear operator on uncertainty in uncertain constraint)');
84end
85
86if length(F_w)==0
87    error('There is no uncertainty description in the model.');
88end
89
90% Some pre-calc
91xw = [x;w];
92xind = find(ismembc(getvariables(xw),getvariables(x)));
93wind = find(ismembc(getvariables(xw),getvariables(w)));
94% Analyze the objective and try to rewrite any uncertainty into the format
95% assumed by YALMIP (
96if ~isempty(h)
97    %
98    %[Q,c,f] = quadratic_model(h,xw);
99    if 0
100        [Q,c,f,dummy,nonquadratic] = quaddecomp(h,xw);
101    else
102        [Q,c,f,dummy,nonquadratic] = vecquaddecomp(h,xw);
103        Q = Q{1};
104        c = c{1};
105        f = f{1};
106    end
107    if nonquadratic
108        error('Objective can be at most quadratic, with the linear term uncertain');
109    end
110    Q_ww = Q(wind,wind);
111    Q_xw = Q(xind,wind);
112    Q_xx = Q(xind,xind);
113    c_x = c(xind);
114    c_w = c(wind);
115 
116    if nnz(Q_ww) > 0
117        error('Objective can be at most quadratic, with the linear term uncertain');
118    end
119    % Separate certain and uncertain terms, place uncertain terms in the
120    % constraints instead
121    if is(h,'linear')
122        if isempty(intersect(getvariables(w),getvariables(h)))
123            h_fixed = h;
124        else
125            sdpvar t
126            F_xw = F_xw + set(h < t);
127            h_fixed = t;
128            x = [x;t];         
129        end
130    else
131        h_fixed     = x'*Q_xx*x + c_x'*x + f;
132        h_uncertain = 2*w'*Q_xw'*x + c_w'*w;
133        if ~isa(h_uncertain,'double')
134            sdpvar t
135            F_xw = F_xw + set(h_uncertain < t);
136            h_fixed = h_fixed + t;
137            x = [x;t];         
138        end
139    end
140else
141    h_fixed = [];
142end
143
144% Convert quadratic constraints in uncertainty model to SOCPs.
145F_w = convertquadratics(F_w);
146
147% Export uncertainty model to numerical format
148ops.solver = '';
149[aux1,aux2,aux3,Zmodel] = export(F_w,[],ops,[],[],1);
150
151if ~isempty(Zmodel)
152    if length(Zmodel.c) ~= length(w)
153        error('Some uncertain variables are unconstrained.')
154    end
155else
156    error('Failed when exporting a model of the uncertainty.')   
157end
158
159% OK, we are done with the initial analysis of the involved variables, and
160% check of the objective function.
161%
162% At this point, we apply algorithms to robustify constraints (currently we
163% only have code for the uncertain conic LP case and polytopic SDP)
164
165F_robust = set([]);
166
167% Pick out the uncertain linear equalities and robustify
168F_lp = F_xw(find(is(F_xw,'elementwise')));
169F_xw = F_xw - F_lp;
170F_robust = F_robust + robustify_lp_conic(F_lp,Zmodel,x,w);
171
172% Pick out uncertain SOCP & SDP constraints and robustify
173F_sdp = F_xw(find(is(F_xw,'sdp') | is(F_xw,'socc')));
174F_xw = F_xw - F_sdp;
175F_robust = F_robust + robustify_sdp_conic(F_sdp,Zmodel,x,w);
176
177% Pick out the uncertain equalities and robustify
178F_eq = F_xw(find(is(F_xw,'equality')));
179F_xw = F_xw - F_eq;
180F_robust = F_robust + robustify_eq_conic(F_eq,Zmodel,x,w);
181
182
183if length(F_xw) > 0
184    error('There are some uncertain constraints that not are supported by YALMIP')
185end
186
187% Return the robustfied model
188F = F_robust+F_x;
189h = h_fixed;
190
191% The model has been expanded, so we have to remember this (trying to
192% expand an expanded model leads to nonconvexity error)
193F = expanded(F,1); % This is actually done already in expandmodel
194h = expanded(h,1); % But this one has to be done manually
Note: See TracBrowser for help on using the repository browser.