1 | function [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 | |
---|
20 | if nargin < 3 |
---|
21 | ops = []; |
---|
22 | end |
---|
23 | |
---|
24 | if nargin < 4 |
---|
25 | w = []; |
---|
26 | end |
---|
27 | |
---|
28 | if 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 |
---|
36 | end |
---|
37 | |
---|
38 | if isempty(ops) |
---|
39 | ops = sdpsettings; |
---|
40 | end |
---|
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); |
---|
45 | if failure |
---|
46 | return |
---|
47 | end |
---|
48 | |
---|
49 | % Integer variables are OK in x, but not in the uncertainty (robustification |
---|
50 | % is based on strong duality in w-space) |
---|
51 | integervars = [yalmip('binvariables') yalmip('intvariables')]; |
---|
52 | ind = find(is(F,'integer') | is(F,'binary')); |
---|
53 | if ~isempty(ind) |
---|
54 | integervars = [integervars getvariables(F(ind))]; |
---|
55 | if any(ismember(w_variables,integervars)) |
---|
56 | failure = 1; |
---|
57 | return |
---|
58 | end |
---|
59 | end |
---|
60 | |
---|
61 | % Find uncertainty description, uncertain and certain constraints |
---|
62 | F_w = set([]); |
---|
63 | F_x = set([]); |
---|
64 | F_xw = set([]); |
---|
65 | for 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 |
---|
76 | end |
---|
77 | |
---|
78 | % Limitation in the modelling language... |
---|
79 | if ~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)'); |
---|
84 | end |
---|
85 | |
---|
86 | if length(F_w)==0 |
---|
87 | error('There is no uncertainty description in the model.'); |
---|
88 | end |
---|
89 | |
---|
90 | % Some pre-calc |
---|
91 | xw = [x;w]; |
---|
92 | xind = find(ismembc(getvariables(xw),getvariables(x))); |
---|
93 | wind = find(ismembc(getvariables(xw),getvariables(w))); |
---|
94 | % Analyze the objective and try to rewrite any uncertainty into the format |
---|
95 | % assumed by YALMIP ( |
---|
96 | if ~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 |
---|
140 | else |
---|
141 | h_fixed = []; |
---|
142 | end |
---|
143 | |
---|
144 | % Convert quadratic constraints in uncertainty model to SOCPs. |
---|
145 | F_w = convertquadratics(F_w); |
---|
146 | |
---|
147 | % Export uncertainty model to numerical format |
---|
148 | ops.solver = ''; |
---|
149 | [aux1,aux2,aux3,Zmodel] = export(F_w,[],ops,[],[],1); |
---|
150 | |
---|
151 | if ~isempty(Zmodel) |
---|
152 | if length(Zmodel.c) ~= length(w) |
---|
153 | error('Some uncertain variables are unconstrained.') |
---|
154 | end |
---|
155 | else |
---|
156 | error('Failed when exporting a model of the uncertainty.') |
---|
157 | end |
---|
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 | |
---|
165 | F_robust = set([]); |
---|
166 | |
---|
167 | % Pick out the uncertain linear equalities and robustify |
---|
168 | F_lp = F_xw(find(is(F_xw,'elementwise'))); |
---|
169 | F_xw = F_xw - F_lp; |
---|
170 | F_robust = F_robust + robustify_lp_conic(F_lp,Zmodel,x,w); |
---|
171 | |
---|
172 | % Pick out uncertain SOCP & SDP constraints and robustify |
---|
173 | F_sdp = F_xw(find(is(F_xw,'sdp') | is(F_xw,'socc'))); |
---|
174 | F_xw = F_xw - F_sdp; |
---|
175 | F_robust = F_robust + robustify_sdp_conic(F_sdp,Zmodel,x,w); |
---|
176 | |
---|
177 | % Pick out the uncertain equalities and robustify |
---|
178 | F_eq = F_xw(find(is(F_xw,'equality'))); |
---|
179 | F_xw = F_xw - F_eq; |
---|
180 | F_robust = F_robust + robustify_eq_conic(F_eq,Zmodel,x,w); |
---|
181 | |
---|
182 | |
---|
183 | if length(F_xw) > 0 |
---|
184 | error('There are some uncertain constraints that not are supported by YALMIP') |
---|
185 | end |
---|
186 | |
---|
187 | % Return the robustfied model |
---|
188 | F = F_robust+F_x; |
---|
189 | h = 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) |
---|
193 | F = expanded(F,1); % This is actually done already in expandmodel |
---|
194 | h = expanded(h,1); % But this one has to be done manually |
---|