function [x,z,msg]=mip(c,A,b,Ae,be,lb,ub,I,dflag) % % Function MIP solves a mixed integer program using either the function % GLPK (Octave) or INTLINPROG (Matlab). The inputs are modeled after the % requirements for INTLINPROG which solves % % min c'x % s.t. A * x <= b % Ae * x = be % lb <= x <= ub % x(I) is integer % % If this function is run under Octave, the inputs will be translated to % use GLPK. This function uses display function SHOWSOLUTION. An example % call to this function is given in the MYEXAMPLE.m script. % Version: October 30, 2017 % % USAGE: % % [x,z,msg]=mip(c,A,b,Ae,be,lb,ub,I,dflag) % % INPUTS: % % c,A,b,Ae,be,lb,ub are problem variables as described above. % A and b are optional but should be entered as [] if no inequality % constraints exist. Ae and be are optional but should be entered % as [] if no equality constraints exist. lb and ub should equal [] % if no bounds exist, otherwise they must be specified element-wise. % % I is the index set indicating which of the variables are restricted % to be integer. % % If dflag is true then formatted results will be output to termainal. % % OUTPUTS: % % x and z comprise an optimal solution if one exists. % % msg is a string message indicating the type of result obtained by % the optimization algorithm. % %% Matlab or Octave? %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% OM=ver; if strcmp(OM(1).Name,'Octave') octave=true; else octave=false; end %% Main Routine %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% if ~octave % call intlinprog [x,z,errnum]=intlinprog(c,I,A,b,Ae,be,lb,ub); else % create single A and b matrices AA=[A;Ae]; bb=[b;be]; % create ctype string ctype=[repmat('U',1,size(A,1)) repmat('S',1,size(Ae,1))]; % create vtype string vtype=repmat('C',1,length(c)); vtype(I)='I'; % integer-ize bounds for integer variables lb(I)=ceil(lb(I)); ub(I)=floor(ub(I)); % set sense sense=1; % call glpk [x,z,errnum]=glpk(c,AA,bb,lb,ub,ctype,vtype,sense); end %% Set Output Message %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% if (octave && errnum==0) || (~octave && errnum==1) msg='Optimal solution found.'; elseif (octave && errnum==10) || (~octave && errnum==-2) msg='Problem appears to be infeasible.'; elseif (octave && errnum==11) || (~octave && errnum==-3) msg='Problem appears to be unbounded.'; else msg='Algorithmic termination. Result uncertain.'; end %% Display Results %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% if ~isempty(dflag) && dflag==true fprintf(['\n\n',msg,'\n\n']) if (octave && errnum==0) || (~octave && errnum==1) ShowSolution(x,z); end end %% Done %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% return