1 | function varargout = semivar(varargin) |
---|
2 | %SEMIVAR Create symbolic semicontinuous variable |
---|
3 | % |
---|
4 | % SEMIVAR works exactly as SDPVAR, with the only difference that |
---|
5 | % the elements in the variable automatically will be constrained |
---|
6 | % to be semicontinous (x = 0 or |x| > 1) |
---|
7 | % |
---|
8 | % See also SDPVAR, BINVAR, INTVAR, BINARY, INTEGER |
---|
9 | |
---|
10 | % Author Johan Löfberg |
---|
11 | % $Id: semivar.m,v 1.2 2006/06/07 09:24:14 joloef Exp $ |
---|
12 | |
---|
13 | if isa(varargin{1},'char') |
---|
14 | switch varargin{1} |
---|
15 | case {'graph','milp'} |
---|
16 | t = varargin{2}; |
---|
17 | d = binvar(3,1); |
---|
18 | [M,m] = derivebounds(t); |
---|
19 | % Zero case |
---|
20 | F = set(m.*(1-d(3)) < t < M.*(1-d(3))); |
---|
21 | % Special cases to speed up integer solve |
---|
22 | if M <=0 |
---|
23 | F = F + set(t < -1+(M+1)*(1-d(2))); |
---|
24 | F = F + set(d(3) + d(2)==1); |
---|
25 | elseif m>=0 |
---|
26 | F = set(t >= 1+(1-d(1))*(m-1)); |
---|
27 | F = F + set(d(3) + d(1)==1); |
---|
28 | else |
---|
29 | F = set(t >= 1+(1-d(1))*(m-1)) + set(t < -1+(M+1)*(1-d(2))); |
---|
30 | F = F + set(m.*(1-d(3)) < t < M.*(1-d(3))); |
---|
31 | F = F + set(sum(d)==1); |
---|
32 | end |
---|
33 | varargout{1} = F; |
---|
34 | varargout{2} = struct('convexity','milp','monotonicity','milp','definiteness','milp'); |
---|
35 | varargout{3} = 1; |
---|
36 | return |
---|
37 | otherwise |
---|
38 | % Command line creation |
---|
39 | all_variable_names = 1; |
---|
40 | i = 1; |
---|
41 | while all_variable_names & i<=nargin |
---|
42 | all_variable_names = all_variable_names & isvarname(varargin{i}); |
---|
43 | i = i + 1; |
---|
44 | end |
---|
45 | if all_variable_names |
---|
46 | for k = 1:nargin |
---|
47 | varname = varargin{k}; |
---|
48 | assignin('caller',varname,semicont(1,1)); |
---|
49 | end |
---|
50 | return |
---|
51 | end |
---|
52 | end |
---|
53 | end |
---|
54 | |
---|
55 | % Create dummy SDPVAR object |
---|
56 | sys = sdpvar(varargin{:}); |
---|
57 | n = length(getvariables(sys)); |
---|
58 | % Create new variables |
---|
59 | y = []; |
---|
60 | for i = 1:n |
---|
61 | y = [y;yalmip('addextendedvariable',mfilename,rand)]; |
---|
62 | end |
---|
63 | % Replace variables to the semicontinous variables |
---|
64 | varargout{1} = reshape(getbase(sys)*[1;y],size(sys,1),size(sys,2)); |
---|