Resources

Migration: Maximum Flow (lpsole07)

/**********************************************************************/
/*                                                                    */
/*                   S A S   S A M P L E   L I B R A R Y              */
/*                                                                    */
/*    NAME: lpsole07                                                    */
/*   TITLE: Migration: Maximum Flow (lpsole07)                          */
/* PRODUCT: OR                                                        */
/*  SYSTEM: ALL                                                       */
/*    KEYS: OR                                                        */
/*   PROCS: OPTMODEL                                                  */
/*    DATA:                                                           */
/*                                                                    */
/* SUPPORT:                             UPDATE:                       */
/*     REF:                                                           */
/*    MISC: Example 7 from the Linear Programming Solver              */
/*          chapter of Mathematical Programming.                      */
/*                                                                    */
/**********************************************************************/

title 'Maximum Flow Problem';

data arcs;
   input _from_ $ _to_ $ _cost_ _capac_;
   datalines;
S a . .
S b . .
a c 1 7
b c 2 9
a d 3 5
b d 4 8
c e 5 15
d f 6 20
e g 7 11
f g 8 6
e h 9 12
f h 10 4
g T . .
h T . .
;

proc optmodel;
   str source = 'S';
   str sink = 'T';

   set <str> NODES;
   num _supdem_ {i in NODES} = (if i in {source, sink} then . else 0);

   set <str,str> ARCS;
   num _lo_ {ARCS} init 0;
   num _capac_ {ARCS} init .;
   num _cost_ {ARCS} init 0;
   read data arcs nomiss into ARCS=[_from_ _to_] _cost_ _capac_;
   NODES = (union {<i,j> in ARCS} {i,j});

   var Flow {<i,j> in ARCS} >= _lo_[i,j];
   for {<i,j> in ARCS: _capac_[i,j] ne .} Flow[i,j].ub = _capac_[i,j];
   max obj = sum {<i,j> in ARCS: j = sink} Flow[i,j];
   con balance {i in NODES diff {source, sink}}:
      sum {<(i),j> in ARCS} Flow[i,j]
      - sum {<j,(i)> in ARCS} Flow[j,i] = _supdem_[i];

   solve;

   num _supply_ {<i,j> in ARCS} =
      (if _supdem_[i] ne 0 then _supdem_[i] else .);
   num _demand_ {<i,j> in ARCS} =
      (if _supdem_[j] ne 0 then -_supdem_[j] else .);
   num _fcost_ {<i,j> in ARCS} = _cost_[i,j] * Flow[i,j].sol;

   create data gout3 from [_from_ _to_]
      _cost_ _capac_ _lo_ _supply_ _demand_ _flow_=Flow _fcost_;
quit;

proc print data=gout3;
run;