[37] | 1 | function varargout = and(varargin) |
---|
| 2 | %ANd (overloaded) |
---|
| 3 | % |
---|
| 4 | % z = and(x,y) |
---|
| 5 | % z = x & y |
---|
| 6 | % |
---|
| 7 | % The AND operator is implemented using the concept of nonlinear operators |
---|
| 8 | % in YALMIP. X|Y defines a new so called derived variable that can be |
---|
| 9 | % treated as any other variable in YALMIP. When SOLVESDP is issued, |
---|
| 10 | % constraints are added to the problem to model the AND operator. The new |
---|
| 11 | % constraints add constraints to ensure that z, x and y satisfy the |
---|
| 12 | % truth-table for AND. |
---|
| 13 | % |
---|
| 14 | % The model for ARE is set(z<=x)+set(z<=y)+set(1+z>=x+y)+set(binary(z)) |
---|
| 15 | % |
---|
| 16 | % It is assumed that x and y are binary variables (either explicitely |
---|
| 17 | % declared using BINVAR, or constrained using BINARY.) |
---|
| 18 | % |
---|
| 19 | % See also SDPVAR/AND, BINVAR, BINARY |
---|
| 20 | |
---|
| 21 | % Author Johan Löfberg |
---|
| 22 | % $Id: and.m,v 1.12 2006/04/28 14:47:32 joloef Exp $ |
---|
| 23 | |
---|
| 24 | switch class(varargin{1}) |
---|
| 25 | case 'char' |
---|
| 26 | z = varargin{2}; |
---|
| 27 | x = varargin{3}; |
---|
| 28 | y = varargin{4}; |
---|
| 29 | |
---|
| 30 | % ******************************************************* |
---|
| 31 | % For *some* efficiency,we merge expressions like A&B&C&D |
---|
| 32 | xvars = getvariables(x); |
---|
| 33 | yvars = getvariables(y); |
---|
| 34 | allextvars = yalmip('extvariables'); |
---|
| 35 | |
---|
| 36 | if (length(xvars)==1) & ismembc(xvars,allextvars) |
---|
| 37 | x = expandand(x,allextvars); |
---|
| 38 | end |
---|
| 39 | |
---|
| 40 | if (length(yvars)==1) & ismembc(yvars,allextvars) |
---|
| 41 | y = expandand(y,allextvars); |
---|
| 42 | end |
---|
| 43 | % ******************************************************* |
---|
| 44 | |
---|
| 45 | varargout{1} = set(x >= z) + set(y >= z) + set(length(x)+length(y)-1+z >= sum(x)+sum(y)) + set(binary(z)); |
---|
| 46 | varargout{2} = struct('convexity','milp','monotoncity','milp','definiteness','milp'); |
---|
| 47 | varargout{3} = []; |
---|
| 48 | |
---|
| 49 | case 'sdpvar' |
---|
| 50 | x = varargin{1}; |
---|
| 51 | y = varargin{2}; |
---|
| 52 | |
---|
| 53 | varargout{1} = yalmip('addextendedvariable','and',varargin{:}); |
---|
| 54 | |
---|
| 55 | otherwise |
---|
| 56 | end |
---|
| 57 | |
---|
| 58 | function x = expandand(x,allextvars) |
---|
| 59 | |
---|
| 60 | xmodel = yalmip('extstruct',getvariables(x)); |
---|
| 61 | |
---|
| 62 | if isequal(xmodel.fcn,'and') |
---|
| 63 | x1 = xmodel.arg{1}; |
---|
| 64 | x2 = xmodel.arg{2}; |
---|
| 65 | if ismembc(getvariables(xmodel.arg{1}),allextvars) |
---|
| 66 | x1 = expandand(xmodel.arg{1},allextvars); |
---|
| 67 | end |
---|
| 68 | if ismembc(getvariables(xmodel.arg{2}),allextvars) |
---|
| 69 | x2 = expandand(xmodel.arg{2},allextvars); |
---|
| 70 | end |
---|
| 71 | x = [x1 x2]; |
---|
| 72 | end |
---|