$title Min-Cost Network Flow (Williams, p.82) $offuellist offuelxref offsymlist offsymxref option limrow=0, limcol=0, solprint=off; set node /0*7/; parameter supply(node) /0 10, 1 15, 5 -9, 6 -10, 7 -6/; parameter cost(node,node) / 0.2 5 1.3 4 2.3 2 2.4 6 2.5 5 3.4 1 3.7 2 4.2 4 4.5 6 4.6 3 7.6 4 /; alias (node,i,j,k); * define a dynamic set that indicates the "legal" arcs set arc(node,node); arc(i,j) = yes$(cost(i,j) > 0); positive variable f(node,node); variable totalcost; equations balance(node), objective; balance(i).. sum(arc(i,k), f(arc)) - sum(arc(j,i), f(arc)) =e= supply(i); objective.. totalcost =e= sum(arc, cost(arc)*f(arc)); model mcf/balance, objective/; solve mcf using lp minimizing totalcost; option f:0:0:2; display f.l; * output all arcs to output file, along with their flows file plotit/mincost.dat/; put plotit; loop((i,j)$arc(i,j), put i.tl:4:0, j.tl:4:0, f.l(i,j):8:0; put /; ); putclose plotit;