function [x,fval,exitflag,output,lambda]=linprog(f,A,B,Aeq,Beq,lb,ub,x0,options)
defaultopt = struct('Display','final',...
'TolFun',[],'Diagnostics','off',...
'LargeScale','on','MaxIter',[], ...
'Simplex','off');
if nargin==1 && nargout <= 1 && isequal(f,'defaults')
x = defaultopt;
return
end
if nargin < 9, options = [];
if nargin < 8, x0 = [];
if nargin < 7, ub = [];
if nargin < 6, lb = [];
if nargin < 5, Beq = [];
if nargin < 4, Aeq = [];
end, end, end, end, end, end
try
dataType = superiorfloat(f,A,B,Aeq,Beq,lb,ub,x0);
if ~isequal('double', dataType)
error('optim:linprog:NonDoubleInput', ...
'LINPROG only accepts inputs of data type double.')
end
catch
error('optim:linprog:NonDoubleInput', ...
'LINPROG only accepts inputs of data type double.')
end
if nargout > 4
computeLambda = 1;
else
computeLambda = 0;
end
if isfield(options,'Simplex')
useSimplex = isequal(optimget(options,'Simplex',defaultopt,'fast'), 'on');
else
useSimplex = isequal(defaultopt.Simplex, 'on');
end
largescale = isequal(optimget(options,'LargeScale',defaultopt,'fast'),'on');
diagnostics = isequal(optimget(options,'Diagnostics',defaultopt,'fast'),'on');
switch optimget(options,'Display',defaultopt,'fast')
case {'off','none'}
verbosity = 0;
case 'iter'
verbosity = 2;
case 'final'
verbosity = 1;
otherwise
verbosity = 1;
end
[nineqcstr,nvarsineq]=size(A);
[neqcstr, nvarseq]=size(Aeq);
nvars = max([length(f),nvarsineq,nvarseq]);
ncstr = nineqcstr + neqcstr;
if isempty(f), f=zeros(nvars,1); end
if isempty(A), A=zeros(0,nvars); end
if isempty(B), B=zeros(0,1); end
if isempty(Aeq), Aeq=zeros(0,nvars); end
if isempty(Beq), Beq=zeros(0,1); end
f = f(:);
B = B(:);
Beq = Beq(:);
if ~isequal(length(B),nineqcstr)
error('optim:linprog:SizeMismatchRowsOfA', ...
'The number of rows in A must be the same as the length of b.');
elseif ~isequal(length(Beq),neqcstr)
error('optim:linprog:SizeMismatchRowsOfAeq', ...
'The number of rows in Aeq must be the same as the length of beq.');
elseif ~isequal(length(f),nvarsineq) && ~isempty(A)
error('optim:linprog:SizeMismatchColsOfA', ...
'The number of columns in A must be the same as the length of f.');
elseif ~isequal(length(f),nvarseq) && ~isempty(Aeq)
error('optim:linprog:SizeMismatchColsOfAeq', ...
'The number of columns in Aeq must be the same as the length of f.');
end
[x0,lb,ub,msg] = checkbounds(x0,lb,ub,nvars);
if ~isempty(msg)
exitflag = -2;
x=x0; fval = []; lambda = [];
output.iterations = 0;
output.algorithm = '';
output.cgiterations = [];
output.message = msg;
if verbosity > 0
disp(msg)
end
return
end
caller = 'linprog';
ncstr = nineqcstr + neqcstr;
if largescale
OUTPUT.algorithm = 'large-scale: interior point';
elseif useSimplex
OUTPUT.algorithm = 'medium-scale: simplex';
else
OUTPUT.algorithm = 'medium-scale: active-set';
end
if diagnostics
gradflag = []; hessflag = []; line_search=[];
constflag = 0; gradconstflag = 0; non_eq=0;non_ineq=0;
lin_eq=size(Aeq,1); lin_ineq=size(A,1); XOUT=ones(nvars,1);
funfcn{1} = [];ff=[]; GRAD=[];HESS=[];
confcn{1}=[];c=[];ceq=[];cGRAD=[];ceqGRAD=[];
msg = diagnose('linprog',OUTPUT,gradflag,hessflag,constflag,gradconstflag,...
line_search,options,defaultopt,XOUT,non_eq,...
non_ineq,lin_eq,lin_ineq,lb,ub,funfcn,confcn,ff,GRAD,HESS,c,ceq,cGRAD,ceqGRAD);
end
if (largescale)
if (useSimplex)
warning('optim:linprog:IgnoreSimplexOn', ...
['Simplex method does not currently solve large-scale problems;\n' ...
'calling large-scale interior point method. (To run simplex, set\n' ...
'OPTIONS.LargeScale to ''off''.)'])
end
if ~isempty(x0) && verbosity > 0
warning('optim:linprog:IgnoreStartPoint', ...
['Large scale (interior point) method uses a built-in starting point;\n' ...
'ignoring user-supplied X0.'])
end
defaultopt.TolFun = 1e-8;
defaultopt.MaxIter = 85;
[x,fval,lambda,exitflag,output] = lipsol(f,A,B,Aeq,Beq,lb,ub,options,defaultopt,computeLambda);
elseif (useSimplex)
if ~isempty(x0) && verbosity > 0
warning('optim:linprog:IgnoreStartPoint', ...
['Simplex method uses a built-in starting point; ignoring user-supplied X0.'])
end
defaultopt.TolFun = 1e-6;
defaultopt.MaxIter = '100*NumberOfVariables';
[x,fval,lambda,exitflag,output] = simplex(f,A,B,Aeq,Beq,lb,ub,options,defaultopt,computeLambda);
if exitflag == -1
exitflag = -2;
elseif exitflag == -2
exitflag = -3;
end
else
if ~largescale && (issparse(A) || issparse(Aeq) )
if verbosity > 0
disp('The medium-scale (active-set) algorithm does not currently handle sparse matrices.')
disp('Converting to full matrices to solve.')
end
end
if isempty(x0), x0=zeros(nvars,1); end
defaultopt.MaxIter = '10*max(NumberOfVariables,NumberOfInequalities+NumberOfBounds)';
[x,lambdaqp,exitflag,output,dum1,dum2,msg]= ...
qpsub([],full(f),full([Aeq;A]),full([Beq;B]),lb,ub,x0,neqcstr,verbosity,caller,ncstr, ...
nvars,options,defaultopt);
output.algorithm = 'medium-scale: active-set';
end
if isequal(OUTPUT.algorithm , 'medium-scale: active-set')
fval = f'*x;
if computeLambda
llb = length(lb);
lub = length(ub);
lambda.lower = zeros(llb,1);
lambda.upper = zeros(lub,1);
arglb = ~isinf(lb); lenarglb = nnz(arglb);
argub = ~isinf(ub); lenargub = nnz(argub);
lambda.eqlin = lambdaqp(1:neqcstr,1);
lambda.ineqlin = lambdaqp(neqcstr+1:neqcstr+nineqcstr,1);
lambda.lower(arglb) = lambdaqp(neqcstr+nineqcstr+1:neqcstr+nineqcstr+lenarglb);
lambda.upper(argub) = lambdaqp(neqcstr+nineqcstr+lenarglb+1:neqcstr+nineqcstr+lenarglb+lenargub);
end
output.cgiterations =[];
if exitflag == 1
normalTerminationMsg = sprintf('Optimization terminated.');
if verbosity > 0
disp(normalTerminationMsg)
end
if isempty(msg)
output.message = normalTerminationMsg;
else
output.message = sprintf('%s\n%s',msg,normalTerminationMsg);
end
else
output.message = msg;
end
end
Error using linprog (line 99)
LINPROG only accepts inputs of data type double.