[37] | 1 | function 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 | % ******************************** |
---|
| 19 | mpsolvertime = clock; |
---|
| 20 | showprogress('mpcvx started',p.options.showprogress); |
---|
| 21 | |
---|
| 22 | % ******************************* |
---|
| 23 | % Display-logics |
---|
| 24 | % ******************************* |
---|
| 25 | switch 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; |
---|
| 40 | end |
---|
| 41 | |
---|
| 42 | % ******************************* |
---|
| 43 | % No reason to save |
---|
| 44 | % ******************************* |
---|
| 45 | p.options.saveduals = 0; |
---|
| 46 | |
---|
| 47 | % ********************************** |
---|
| 48 | % Generate an exploration set |
---|
| 49 | % ********************************** |
---|
| 50 | p.solver.subcall = 'callsedumi'; |
---|
| 51 | solver = 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 | % ********************************** |
---|
| 59 | X = []; |
---|
| 60 | OBJ = []; |
---|
| 61 | for 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]; |
---|
| 65 | end |
---|
| 66 | |
---|
| 67 | % ********************************** |
---|
| 68 | % Partition initial set |
---|
| 69 | % ********************************** |
---|
| 70 | node_keeper; |
---|
| 71 | node_keeper(THETA,X,OBJ); |
---|
| 72 | T = delaunayn(THETA',{'Qz','Qt'}); |
---|
| 73 | |
---|
| 74 | % ********************************** |
---|
| 75 | % Do algo on all initial simplicies |
---|
| 76 | % ********************************** |
---|
| 77 | optimal_simplicies = []; |
---|
| 78 | p.options.mpcvx.eps = 5e-2; |
---|
| 79 | for i = 1:size(T,1) |
---|
| 80 | optimal_simplicies = [optimal_simplicies mp_simplex(p,solver,T(i,:)')]; |
---|
| 81 | end |
---|
| 82 | mpsolvertime = etime(clock,mpsolvertime) |
---|
| 83 | |
---|
| 84 | % ********************************** |
---|
| 85 | % Create format compatible with MPT |
---|
| 86 | % ********************************** |
---|
| 87 | Pn = polytope; |
---|
| 88 | j = 1; |
---|
| 89 | for 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 |
---|
| 103 | end |
---|
| 104 | |
---|
| 105 | output.problem = problem; |
---|
| 106 | output.Primal = nan*ones(length(p.c),1); |
---|
| 107 | output.Dual = []; |
---|
| 108 | output.Slack = []; |
---|
| 109 | output.infostr = yalmiperror(output.problem,'MPCVX'); |
---|
| 110 | output.solverinput = 0; |
---|
| 111 | output.solveroutput.Pn = Pn; |
---|
| 112 | output.solveroutput.Fi = Fi; |
---|
| 113 | output.solveroutput.Gi = Gi; |
---|
| 114 | output.solvertime = mpsolvertime; |
---|
| 115 | |
---|
| 116 | function simplex_solution = mp_simplex(p,solver,theta_indicies) |
---|
| 117 | |
---|
| 118 | [theta,x,obj] = node_keeper(theta_indicies); |
---|
| 119 | % Parametric dimension |
---|
| 120 | m = size(theta,1); |
---|
| 121 | |
---|
| 122 | Minv = inv([ones(1,m+1);theta]); |
---|
| 123 | M1 = Minv(:,1); |
---|
| 124 | M2 = zeros(size(M1,1),length(p.c)); |
---|
| 125 | M2(:,p.parametric_variables) = Minv(:,2:end); |
---|
| 126 | p.F_struc = [M1 M2;p.F_struc]; |
---|
| 127 | p.K.l = p.K.l + size(M1,1); |
---|
| 128 | |
---|
| 129 | Vbar = obj'*Minv; |
---|
| 130 | c = p.c; |
---|
| 131 | c2 = zeros(length(p.c),1);c2(p.parametric_variables) = -Vbar(2:end); |
---|
| 132 | p.c = p.c+c2;p.c; |
---|
| 133 | output = feval(solver,p); |
---|
| 134 | p.c = c; |
---|
| 135 | |
---|
| 136 | upper = obj'*Minv*[1;output.Primal(p.parametric_variables)]; |
---|
| 137 | lower = p.c'*output.Primal+output.Primal'*p.Q*output.Primal; |
---|
| 138 | |
---|
| 139 | eps_CP_S = min(upper-lower,((upper-lower)/(1+lower))); |
---|
| 140 | |
---|
| 141 | % Dig deeper? |
---|
| 142 | %plot(polytope(theta'),struct('color',rand(3,1))) |
---|
| 143 | if 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 | |
---|
| 160 | else |
---|
| 161 | % This simplex constitutes a node, report back |
---|
| 162 | simplex_solution = theta_indicies(:); |
---|
| 163 | end |
---|
| 164 | |
---|
| 165 | function varargout = node_keeper(varargin) |
---|
| 166 | |
---|
| 167 | persistent savedTHETA |
---|
| 168 | persistent savedX |
---|
| 169 | persistent savedOBJ |
---|
| 170 | |
---|
| 171 | switch 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('!') |
---|
| 187 | end |
---|
| 188 | |
---|
| 189 | |
---|
| 190 | function [THETA,problem] = ray_shoot(p,solver) |
---|
| 191 | THETA = []; |
---|
| 192 | |
---|
| 193 | p_temp = p; |
---|
| 194 | p_temp.c = p_temp.c*0; |
---|
| 195 | p_temp.Q = 0*p_temp.Q; |
---|
| 196 | for 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)]; |
---|
| 200 | end |
---|
| 201 | % Select unique and generate center |
---|
| 202 | THETA = unique(fix(THETA'*1e4)/1e4,'rows')'; |
---|
| 203 | center = sum(THETA,2)/size(THETA,2); |
---|
| 204 | THETA = [THETA*0.999+repmat(0.001*center,1,size(THETA,2))]; |
---|
| 205 | problem = 0; |
---|
| 206 | |
---|
| 207 | |
---|
| 208 | function [x_i,obj_i] = solve_node(p,solver,theta); |
---|
| 209 | p_temp = p; |
---|
| 210 | p_temp.F_struc(:,1) = p_temp.F_struc(:,1) + p_temp.F_struc(:,1+p.parametric_variables)*theta; |
---|
| 211 | p_temp.F_struc(:,1+p.parametric_variables) = []; |
---|
| 212 | |
---|
| 213 | empty_rows = find(~any(p_temp.F_struc(p.K.f+1:p.K.f+p.K.l,2:end),2)); |
---|
| 214 | if ~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 |
---|
| 221 | end |
---|
| 222 | |
---|
| 223 | x_var = find(~ismember(1:length(p.c),p.parametric_variables)); |
---|
| 224 | theta_var = p.parametric_variables; |
---|
| 225 | Q11 = p.Q(x_var,x_var); |
---|
| 226 | Q12 = p.Q(x_var,theta_var); |
---|
| 227 | Q22 = p.Q(theta_var,theta_var); |
---|
| 228 | c1 = p.c(x_var); |
---|
| 229 | c2 = p.c(theta_var); |
---|
| 230 | |
---|
| 231 | p_temp.Q = Q11; |
---|
| 232 | p_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,:) = []; |
---|
| 237 | output = feval(solver,p_temp); |
---|
| 238 | |
---|
| 239 | % Recover complete [x theta] |
---|
| 240 | x_i = zeros(length(p.c),1); |
---|
| 241 | x_i(find(~ismember(1:length(p.c),p.parametric_variables))) = output.Primal; |
---|
| 242 | x_i(p.parametric_variables) = theta; |
---|
| 243 | obj_i = x_i'*p.Q*x_i+p.c'*x_i; |
---|