Using AMPL

The MCNFP Model in AMPL

 

A convenient feature of AMPL is that it allows you to store the mathematical formulation and a data for a problem separately. For example, we can store the formulation for the more general model above in an AMPL file called ``mcnfp_model.txt'' which would look like this:


# AMPL model for the Minimum Cost Network Flow Problem
#
# By default, this model assumes that b[i] = 0, c[i,j] = 0,
# l[i,j] = 0 and u[i,j] = Infinity.
#
# Parameters not specified in the data file will get their default values.
 

set NODES; # nodes in the network
set ARCS within {NODES, NODES}; # arcs in the network

param b {NODES} default 0; # supply/demand for node i
param c {ARCS} default 0; # cost of one of flow on arc(i,j)
param l {ARCS} default 0; # lower bound on flow on arc(i,j)
param u {ARCS} default Infinity; # upper bound on flow on arc(i,j)
var x {ARCS}; # flow on arc (i,j)
 

minimize cost: sum{(i,j) in ARCS} c[i,j] * x[i,j];

# Flow Out(i) - Flow In(i) = b(i)
subject to flow_balance {i in NODES}:
sum{j in NODES: (i,j) in ARCS} x[i,j] - sum{j in NODES: (j,i) in ARCS} x[j,i] = b[i];

subject to capacity {(i,j) in ARCS}: l[i,j] <= x[i,j] <= u[i,j];


To solve the problem for a specific instance, say the first MCNFP example from the first lecture, we can create a file called "mcnfp_example_data.txt" which would look like this.
set NODES := 1 2 3 4;

set ARCS  := (1,2) (1,3) (2,3) (2,4) (3,4);
             

param b :=
1 5
2 -2
3 0
4 -3;


#	cost	lower	upper bound
param: 	c 	l 	u :=
1 2	3	2	5
1 3	2	0	2
2 3	1	0	2
2 4	4	1	3
3 4	4	0	3;

Here is how we would then solve the problem in AMPL. You can run the model by logging on to any of the School of Engineering's general use Compaq systems, downloading the two files listed above and typing the commands show in bold, italic type. In the AMPL session shown below, we use the "expand" command to see how AMPL takes the data and model files and creates an LP.
% ampl
ampl:model mcnfp_model.txt;
ampl:data mcnfp_example_data.txt;
ampl:expand;
minimize cost:
	3*x[1,2] + 2*x[1,3] + x[2,3] + 4*x[2,4] + 4*x[3,4];

s.t. flow_balance[1]:
	x[1,2] + x[1,3] = 5;

s.t. flow_balance[2]:
	-x[1,2] + x[2,3] + x[2,4] = -2;

s.t. flow_balance[3]:
	-x[1,3] - x[2,3] + x[3,4] = 0;

s.t. flow_balance[4]:
	-x[2,4] - x[3,4] = -3;

s.t. capacity[1,2]:
	2 <= x[1,2] <= 5;

s.t. capacity[1,3]:
	0 <= x[1,3] <= 2;

s.t. capacity[2,3]:
	0 <= x[2,3] <= 2;

s.t. capacity[2,4]:
	1 <= x[2,4] <= 3;

s.t. capacity[3,4]:
	0 <= x[3,4] <= 3;

ampl:solve;
CPLEX 6.6.0: 
CPLEX 6.6.0: optimal solution; objective 25
0 simplex iterations (0 in phase I)
ampl:: display x;
x :=
1 2   3
1 3   2
2 3   0
2 4   1
3 4   2
;

ampl: quit;


Run Files

 You may find it convenient to work with a run file instead of typing the same set of commands over and over again. For example, we can write the set of commands used in the previous example in the file mcnfp_runfile1.txt. Now, we can solve the LP and save the results to file with a single Unix command as shown below:

% ampl < mcnfp_runfile1.txt > mscnfp.out

Odds and Ends