Using AMPL
• AMPL a modeling language for mathematical programming (hence the name). While AMPL does not actually solve mathematical programs by itself, it is an extremely useful language for formulating problems which can then be solved by CPLEX or another solver.
•  AMPL is available on all of the SEAS machines named after Simpsons characters: Homer, Marge, Bart, Lisa and Maggie.
•  The AMPL book by Fourer, Gay and Kernighan comes with a CD-rom that has a student version of AMPL and CPLEX for Windows and Windows NT. You can also download the student versions of AMPL and CPLEX from www.ampl.com.

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
• To end an AMPL session, type "quit;" at the ampl prompt.
•  You can't print directly from AMPL. Instead, you need to save your output to a file. To write output to a file called "results", use the command

•

ampl: display x > results;

This will write the values of the x variables to the file "results".

•  Sometimes you may mistakes in your model or data. And so, you read in the file, discover the error, edit the file and then try to read it in again. If you do this, AMPL will complain. There are a couple of ways to get around this. One way is to just quit and restart AMPL. Another way is to type the command reset before reading in the model the second time.

•