$title Large Random Min Cost Flow example $offuellist offuelxref offsymlist offsymxref option limrow=0, limcol=0, solprint=off; set nodes /1*5000/ sources(nodes) sinks(nodes); alias (nodes,i,j,k); set arcs(i,j); parameter cost(i,j), supply(nodes), randomVector(nodes); variables f(i,j), totalcost; positive variable f; scalar counter; * first define a random set of arcs of a given density. * (If density is too low, the problem may not have a solution - * there may not be a path from sources to sinks.) option seed=25671; arcs(i,j) = yes$(uniform(0,1) < .025); * no self-directed arcs arcs(i,i) = no; * now define costs on these arcs cost(i,j) = uniform(0,50) $ arcs(i,j); * determine eligible source nodes (those with outflowing arcs) * and sinks (those with inflowing arcs); sources(nodes) = no; sinks(nodes) = no; loop(arcs(i,j), sources(i) = yes; sinks(j) = yes; ); supply(nodes) = 0; * pick two sources to have positive supplies counter = 0; loop(nodes$(sources(nodes) and counter<2), supply(nodes) = 10; counter = counter+1; ); * and five sinks to have balancing negative supplies counter = 0; loop(nodes$(sinks(nodes) and counter<5 and supply(nodes)=0), supply(nodes) = -4; counter = counter+1; ); * display sources, sinks, supply; equations balance(i), objective; balance(i).. sum(arcs(i,k), f(arcs)) - sum(arcs(j,i), f(arcs)) =e= supply(i); objective.. totalcost =e= sum(arcs, cost(arcs)*f(arcs)); model mcf/balance, objective/; solve mcf using lp minimizing totalcost; option f:0:0:2; display f.l;