Home > source > boundschecker.m

boundschecker

PURPOSE ^

Checks bounds that the dimension is correct and make sense w.r.t. the

SYNOPSIS ^

function [ineqmap, ineqmlt, ineqshift, eqmap, eqshift]=boundschecker(dim,lb,ub,equalok,unconstrok,filterout)

DESCRIPTION ^

 Checks bounds that the dimension is correct and make sense w.r.t. the
 settings (equality allowed? unconstrained allowed?) and create mapping from
   lb <= sth <= ub    to    constr <=0   or   constr==0
   lb_i <= sth_i    becomes     constr_k=lb-sth_i <=0   
         with  ineqmap(k)=i, ineqmlt(k)=-1, ineqshift(k)=lb_i   i.e.
         constr_k = ineqshift(k) + ineqmlt(k)*sth_{ineqmap(k)}
   if(equalok>0) ... equalities are allowed and treated as equalities
   if(equalok<0) ... equalities are allowed and treated as 2 inequalities
            (temporary solution for box equality constriants on matrices)
   if(unconstrok) ... unconstrained row (lb=-inf,ub=inf) is allowed
            (and won't be included in any map, e.g. unbounded variable)
   filterout ... array of indicies 1..dim which shouldn't be considered at all
      (e.g., constraints on empty matrix variables)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 % Checks bounds that the dimension is correct and make sense w.r.t. the
0002 % settings (equality allowed? unconstrained allowed?) and create mapping from
0003 %   lb <= sth <= ub    to    constr <=0   or   constr==0
0004 %   lb_i <= sth_i    becomes     constr_k=lb-sth_i <=0
0005 %         with  ineqmap(k)=i, ineqmlt(k)=-1, ineqshift(k)=lb_i   i.e.
0006 %         constr_k = ineqshift(k) + ineqmlt(k)*sth_{ineqmap(k)}
0007 %   if(equalok>0) ... equalities are allowed and treated as equalities
0008 %   if(equalok<0) ... equalities are allowed and treated as 2 inequalities
0009 %            (temporary solution for box equality constriants on matrices)
0010 %   if(unconstrok) ... unconstrained row (lb=-inf,ub=inf) is allowed
0011 %            (and won't be included in any map, e.g. unbounded variable)
0012 %   filterout ... array of indicies 1..dim which shouldn't be considered at all
0013 %      (e.g., constraints on empty matrix variables)
0014 function [ineqmap, ineqmlt, ineqshift, eqmap, eqshift]=boundschecker(dim,lb,ub,equalok,unconstrok,filterout)
0015 
0016   ineqmap=[];
0017   ineqmlt=[];
0018   ineqshift=[];
0019   eqmap=[];
0020   eqshift=[];
0021 
0022   if (dim>0)
0023     if (~isvector(lb) || ~isvector(ub) || ...
0024         (~isscalar(lb) && length(lb)~=dim) || ...
0025         (~isscalar(ub) && length(ub)~=dim))
0026       error('Error in dim or shape lb or ub');
0027       return;
0028     end
0029     if (isscalar(lb))
0030       lb=lb.*ones(dim,1);
0031     end
0032     if (isscalar(ub))
0033       ub=ub.*ones(dim,1);
0034     end
0035     if (size(lb,1)<size(lb,2))
0036       lb=lb';
0037     end
0038     if (size(ub,1)<size(ub,2))
0039       ub=ub';
0040     end
0041 
0042     % what if lb/ub row/column vector?
0043     if (any(lb==Inf | ub==-Inf | lb>ub))
0044       error('Error in values lb and/or ub');
0045       return;
0046     end
0047 
0048     Nineq=0;
0049     Neq=0;
0050 
0051     indices=setdiff([1:dim],filterout);
0052     for i=indices
0053       if (lb(i)==ub(i))
0054         if (equalok>0)
0055           % as equality
0056           Neq=Neq+1;
0057           eqmap(Neq)=i;
0058           eqshift(Neq)=-lb(i);
0059           %eqmap = [eqmap; i];
0060           %eqshift = [eqshift; -lb(i)];
0061         elseif (equalok<0)
0062           % as two inequalitites
0063           Nineq=Nineq+1;
0064           ineqmap(Nineq)=i;
0065           ineqmlt(Nineq)=-1;
0066           ineqshift(Nineq)=lb(i);
0067 
0068           Nineq=Nineq+1;
0069           ineqmap(Nineq)=i;
0070           ineqmlt(Nineq)=1;
0071           ineqshift(Nineq)=-ub(i);
0072         else
0073           error('equality here and forbidden!');
0074           return;
0075         end
0076       else
0077         if (lb(i)>-Inf)
0078           Nineq=Nineq+1;
0079           ineqmap(Nineq)=i;
0080           ineqmlt(Nineq)=-1;
0081           ineqshift(Nineq)=lb(i);
0082         end
0083         if (ub(i)<Inf)
0084           Nineq=Nineq+1;
0085           ineqmap(Nineq)=i;
0086           ineqmlt(Nineq)=1;
0087           ineqshift(Nineq)=-ub(i);
0088         elseif (lb(i)==-Inf && ~unconstrok)
0089           error('err: unconstr & not allowed!');
0090           return;
0091         end
0092       end
0093     end
0094     % transpose all to get column vectors instead of rows
0095     ineqmap=ineqmap';
0096     ineqmlt=ineqmlt';
0097     ineqshift=ineqshift';
0098     eqmap=eqmap';
0099     eqshift=eqshift';
0100   end
0101

Generated on Mon 26-Aug-2019 10:22:08 by m2html © 2005