Linear Programming

/****************************************************************/
/*          S A S   S A M P L E   L I B R A R Y                 */
/*                                                              */
/*    NAME: LINPROG                                             */
/*   TITLE: Linear Programming                                  */
/* PRODUCT: IML                                                 */
/*  SYSTEM: ALL                                                 */
/*    KEYS: MATRIX  OR      SUGI6                               */
/*   PROCS: IML                                                 */
/*    DATA:                                                     */
/*                                                              */
/* SUPPORT: MDC                         UPDATE: SEP2013         */
/*     REF:                                                     */
/*    MISC:                                                     */
/*                                                              */
/****************************************************************/


proc iml;
names={'product 1' 'product 2' 'product 3' 'product 4'};
/* coefficients of the linear objective function             */
c = {5.24 7.30 8.34 4.18};
/* coefficients of the constraint equation                   */
A = { 1.5  1  2.4  1   ,
      1    5  1    3.5 ,
      1.5  3  3.5  1   };

/* right-hand side of constraint equation                    */
b = { 2000, 8000, 5000};
/* operators: 'L' for <=, 'G' for >=, 'E' for =              */
ops = { 'L', 'L', 'L' };

n=ncol(A);                            /* number of variables */
cntl = j(1,7,.);                           /* control vector */
cntl[1] = -1;               /* 1 for minimum; -1 for maximum */
call lpsolve(rc, value, x, dual, redcost,
             c, A, b, cntl, ops);

print x[r=names L='Optimal Product Mix'];
print value[L='Maximum Profit'];
lhs = A*x;
Constraints = lhs || b;
print Constraints[r={"Machine1" "Machine2" "Machine3"}
                  c={"Actual" "Upper Bound"}
                  L="Time Constraints"];

arcs = { 'ab' 'bd' 'ad' 'bc' 'ce' 'de' 'ae' }; /* decision variables  */
n=ncol(arcs);                                  /* number of variables */
nodes = {'a', 'b', 'c', 'd', 'e'};
inode = substr(arcs, 1, 1);
onode = substr(arcs, 2, 1);
/* coefficients of the constraint equation */
A = j(nrow(nodes), n, 0);
do j = 1 to n;
   A[,j] = (inode[j]=nodes) - (onode[j]=nodes);
end;

/* coefficients of the linear objective function             */
cost = { 1 2 4 3 3 2 9 };
/* right-hand side of constraint equation                    */
supply = { 2, 0, 0, -1, -1 };
/* operators: 'L' for <=, 'G' for >=, 'E' for =              */
ops = repeat('E',nrow(nodes),1);

cntl = j(1,7,.);                           /* control vector */
cntl[1] = 1;                /* 1 for minimum; -1 for maximum */
call lpsolve(rc, value, x, dual, redcost,
             cost, A, supply, cntl, ops);

print value[L='Minimum Cost'];
print x[r=arcs L='Optimal Flow'];