# introduce sets for indexing
set V;
set A within V cross V ;
# declare parameters:
param l{A} default 0; # lower capacities on the arcs. Those are set to 0 unless specified otherwise.
param u{A}; # upper capacities on the arcs.
param c{A} >=0; # costs on the arcs, required to be nonnegative.
# variable declarations:
# we have one variable for each arc of the network,
# representing the amount of flow along that arc.
var x{A};
# for the problem to make sense, lower capacities must not exceed upper capacities;
# otherwise, the problem is trivially infeasible.
# We can check the data for this condition by the following construction:
check{(v,w) in A}: l[v,w] <= u[v,w];
# when executing the solve command, AMPL will complain if a check fails.
# objective function: minimize the total cost of the flow.
minimize TotalCost:
sum{(v,w) in A} x[v,w]*c[v,w];
# flow conservation constraints for all nodes
subject to FlowCons{v in V}:
sum{(w,v) in A} x[w,v] = sum{(v,r) in A} x[v,r];
# constraints for enforcing lower and upper capacities
subject to LowerCap{(w,v) in A}: x[w,v] >= l[w,v];
subject to UpperCap{(w,v) in A}: x[w,v] <= u[w,v];