[37] | 1 | function x_opt = plot(varargin) |
---|
| 2 | %plot plots feasible set |
---|
| 3 | % |
---|
| 4 | % p = plot(F,x,c,n,options) |
---|
| 5 | % |
---|
| 6 | % F: set object |
---|
| 7 | % x: projected variables [At most three variables] |
---|
| 8 | % c: color [double] ([r g b] format) |
---|
| 9 | % n: #vertices [double ] |
---|
| 10 | % options: options structure from sdpsettings |
---|
| 11 | |
---|
| 12 | |
---|
| 13 | % Author Johan Löfberg |
---|
| 14 | % $Id: plot.m,v 1.13 2006/04/25 11:22:57 joloef Exp $ |
---|
| 15 | |
---|
| 16 | % Get the onstraints |
---|
| 17 | if nargin<1 |
---|
| 18 | return |
---|
| 19 | end |
---|
| 20 | |
---|
| 21 | F = varargin{1}; |
---|
| 22 | |
---|
| 23 | if length(F)==0 |
---|
| 24 | return; |
---|
| 25 | end |
---|
| 26 | |
---|
| 27 | if nargin < 5 |
---|
| 28 | opts = sdpsettings('verbose',0); |
---|
| 29 | else |
---|
| 30 | opts = varargin{5}; |
---|
| 31 | if isempty(opts) |
---|
| 32 | opts = sdpsettings('verbose',0); |
---|
| 33 | end |
---|
| 34 | end |
---|
| 35 | |
---|
| 36 | if nargin < 3 |
---|
| 37 | color = [1 0.1 0.1]; |
---|
| 38 | else |
---|
| 39 | color = varargin{3}; |
---|
| 40 | if isempty(color) |
---|
| 41 | color = [1 0.1 0.1]; |
---|
| 42 | end |
---|
| 43 | end |
---|
| 44 | |
---|
| 45 | % Plot onto this projection (at most in 3D) |
---|
| 46 | if nargin < 2 |
---|
| 47 | x = []; |
---|
| 48 | else |
---|
| 49 | x = varargin{2}; |
---|
| 50 | if ~isempty(x) |
---|
| 51 | x = x(:); |
---|
| 52 | x = x(1:min(3,length(x))); |
---|
| 53 | end |
---|
| 54 | end |
---|
| 55 | |
---|
| 56 | if isempty(F) |
---|
| 57 | return |
---|
| 58 | end |
---|
| 59 | |
---|
| 60 | % Create a model in YALMIPs low level format |
---|
| 61 | % All we change later is the cost vector |
---|
| 62 | %sol = solvesdp(F,sum(x),opts); |
---|
| 63 | [model,recoverdata,diagnostic,internalmodel] = export(F,[],opts,[],[],0); |
---|
| 64 | if isempty(internalmodel) | (~isempty(diagnostic) & diagnostic.problem) |
---|
| 65 | error('Could not create model. Can you actually solve problems with this model?') |
---|
| 66 | end |
---|
| 67 | internalmodel.options.saveduals = 0; |
---|
| 68 | internalmodel.getsolvertime = 0; |
---|
| 69 | internalmodel.options.dimacs = 0; |
---|
| 70 | |
---|
| 71 | % Try to find a suitable set to plot |
---|
| 72 | if isempty(x) |
---|
| 73 | if isempty(internalmodel.extended_variables) |
---|
| 74 | x = depends(F); |
---|
| 75 | x = x(1:min(3,length(x))); |
---|
| 76 | localindex = 1; |
---|
| 77 | localindex = find(ismember(recoverdata.used_variables,x)); |
---|
| 78 | else |
---|
| 79 | % not extended variables |
---|
| 80 | candidates = setdiff(1:length(internalmodel.c),internalmodel.extended_variables); |
---|
| 81 | % Not nonlinear variables |
---|
| 82 | candidates = candidates(find(internalmodel.variabletype(candidates)==0)); |
---|
| 83 | % Settle with this guess |
---|
| 84 | localindex = candidates(1:min(3,length(candidates))); |
---|
| 85 | x = localindex; |
---|
| 86 | end |
---|
| 87 | else |
---|
| 88 | localindex = []; |
---|
| 89 | for i = 1:length(x) |
---|
| 90 | localindex = [localindex find(ismember(recoverdata.used_variables,getvariables(x(i))))]; |
---|
| 91 | end |
---|
| 92 | end |
---|
| 93 | |
---|
| 94 | if nargin < 4 |
---|
| 95 | if length(x)==3 |
---|
| 96 | n = 100; |
---|
| 97 | else |
---|
| 98 | n = 25; |
---|
| 99 | end |
---|
| 100 | else |
---|
| 101 | n = varargin{4}; |
---|
| 102 | if isempty(n) |
---|
| 103 | if length(x)==3 |
---|
| 104 | n = 100; |
---|
| 105 | else |
---|
| 106 | n = 25; |
---|
| 107 | end |
---|
| 108 | end |
---|
| 109 | if ~isa(n,'double') |
---|
| 110 | error('4th argument should be an integer>0'); |
---|
| 111 | end |
---|
| 112 | end |
---|
| 113 | |
---|
| 114 | |
---|
| 115 | x_opt = []; |
---|
| 116 | phi = []; |
---|
| 117 | status = 0; |
---|
| 118 | try % Try to ensure that we close h |
---|
| 119 | if length(x)==2 |
---|
| 120 | mu =0.5; |
---|
| 121 | else |
---|
| 122 | mu=1; |
---|
| 123 | end |
---|
| 124 | n_ = n; |
---|
| 125 | n = ceil(mu*n); |
---|
| 126 | h = waitbar(0,['Please wait, solving ' num2str(n_) ' problems using ' internalmodel.solver.tag]); |
---|
| 127 | angles = (0:(n))*2*pi/n; |
---|
| 128 | if length(x)==2 |
---|
| 129 | c = [cos(angles);sin(angles)]; |
---|
| 130 | else |
---|
| 131 | c = randn(3,n); |
---|
| 132 | end |
---|
| 133 | i=1; |
---|
| 134 | while i<=n & status == 0 |
---|
| 135 | xi = solvefordirection(c(:,i),internalmodel,localindex); |
---|
| 136 | x_opt = [x_opt xi]; |
---|
| 137 | waitbar(i/n_,h) |
---|
| 138 | i=i+1; |
---|
| 139 | end |
---|
| 140 | |
---|
| 141 | if status==0 & length(x)==2 |
---|
| 142 | % Close the set |
---|
| 143 | x_opt = [x_opt x_opt(:,1)]; |
---|
| 144 | |
---|
| 145 | % Add points adaptively |
---|
| 146 | pick = 1; |
---|
| 147 | n = floor((1-mu)*n_); |
---|
| 148 | for i = 1:1:n |
---|
| 149 | for j= 1:(size(x_opt,2)-1) |
---|
| 150 | d = x_opt(:,j)-x_opt(:,j+1); |
---|
| 151 | distance(j,1) = d'*d; |
---|
| 152 | end |
---|
| 153 | [dist,pos]=sort(-distance); |
---|
| 154 | % Select insertion point |
---|
| 155 | phii=(angles(pos(pick))+angles(pos(pick)+1))/2; |
---|
| 156 | xi = solvefordirection([cos(phii);sin(phii)],internalmodel,localindex); |
---|
| 157 | d1=xi-x_opt(:,pos(pick)); |
---|
| 158 | d2=xi-x_opt(:,pos(pick)+1); |
---|
| 159 | if d1'*d1<1e-3 | d2'*d2<1e-3 |
---|
| 160 | pick = pick+1; |
---|
| 161 | else |
---|
| 162 | angles = [angles(1:pos(pick)) phii angles((pos(pick))+1:end)]; |
---|
| 163 | x_opt = [x_opt(:,1:pos(pick)) xi x_opt(:,(pos(pick))+1:end)]; |
---|
| 164 | end |
---|
| 165 | waitbar((ceil(n_*mu)+i)/n_,h); |
---|
| 166 | end |
---|
| 167 | end |
---|
| 168 | close(h); |
---|
| 169 | catch |
---|
| 170 | close(h); |
---|
| 171 | end |
---|
| 172 | |
---|
| 173 | if status |
---|
| 174 | if nargout==0 |
---|
| 175 | plot(0); |
---|
| 176 | end |
---|
| 177 | end |
---|
| 178 | |
---|
| 179 | if nargout == 0 & status==0 |
---|
| 180 | if length(x)==2 |
---|
| 181 | patch(x_opt(1,:),x_opt(2,:),color); |
---|
| 182 | else |
---|
| 183 | K = convhulln(x_opt'); |
---|
| 184 | p = patch('Vertices', x_opt', 'Faces', K, 'FaceVertexCData', color, 'FaceColor', color);%, 'FaceAlpha', 0.5); |
---|
| 185 | set(p,'LineStyle','none') |
---|
| 186 | lighting gouraud; |
---|
| 187 | view(3); |
---|
| 188 | camlight('headlight','local'); |
---|
| 189 | camlight('headlight','local'); |
---|
| 190 | camlight('right','local'); |
---|
| 191 | camlight('left','local'); |
---|
| 192 | end |
---|
| 193 | end |
---|
| 194 | |
---|
| 195 | |
---|
| 196 | function [xout,status] = solvefordirection(c,internalmodel,uv) |
---|
| 197 | internalmodel.c = 0*internalmodel.c; |
---|
| 198 | internalmodel.c(uv) = c; |
---|
| 199 | sol = feval(internalmodel.solver.call,internalmodel); |
---|
| 200 | xout = sol.Primal; |
---|
| 201 | xout = xout(uv(:)); |
---|
| 202 | status = sol.problem; |
---|
| 203 | |
---|
| 204 | |
---|
| 205 | |
---|
| 206 | |
---|