AMPL Book Examples#

AMPL: A Modeling Language for Mathematical Programming
by Robert Fourer, David M. Gay, and Brian W. Kernighan
Second edition
517 + xxi pp., ISBN 0-534-38809-4

Use this page to download all the model, data, and script files that appear as examples in the AMPL book (second edition). You can download everything in one file:

Or download individual files from our listings below, by filename or by figure number.

Book examples by filename#

Model file

Figure

Data file

Figure

Script file

Figure

assign.dat

3-2

blend.mod

2-4

cut.mod

14-2

cut.dat

14-4

cut.run

14-3

cut2.mod

14-6

cut2.run

diet.mod

2-1

diet.dat

2-2

diet2.dat

2-3

diet.run

14-1

diet2a.dat

2-3

dietu.mod

5-1

dietu.dat

5-2

econ.mod

19-3

econ.dat

19-2

econmin.mod

19-1

econ2.mod

19-4

econ2.dat

econ2min.mod

econnl.mod

19-5

econnl.dat

iocol1.mod

16-2

iocol2.mod

16-3

iorow.mod

16-1

minmax.mod

17-9

multi.mod

4-1

multi.dat

4-2

multic.mod

6-5

multmip1.mod

20-1a

multmip1.dat

20-1b

multmip2.mod

multmip2.dat

multmip3.mod

20-2a

multmip3.dat

20-2b

net1.mod

15-2a

net1.dat

15-2b

net1node.mod

15-10

net2.mod

15-3a

net2.dat

15-3b

net3.mod

15-4a

net3.dat

15-4b

net3node.mod

15-11

netasgn.mod

15-9

netfeeds.mod

15-14

netmax.mod

15-6

netmax3.mod

15-12

netmcol.mod

16-6

netmulti.mod

15-13

netshort.mod

15-7

netthru.mod

15-15

nltrans.mod

18-4

nltrans.dat

18-5

nltransb.mod

nltransc.mod

nltransd.mod

prod0.mod

prod.mod

1-2

prod.dat

1-3

sched.mod

16-4

sched.dat

16-5

steel.mod

1-4a

steel.dat

1-4b

steel2.dat

steel3.mod

1-5a

steel3.dat

1-5b

steel4.mod

1-6a

steel4.dat

1-6b

steel4r.mod

steel4r.dat

steelP.mod

4-6

steelP.dat

4-7

steelpl1.mod

17-5a

steelpl1.dat

17-5b

steelpl2.mod

steelpl2.dat

steelpl3.mod

steelpl4.mod

steelpl4.dat

steelpl5.mod

steelpl5.dat

steelT0.mod

4-3

steelT.sa1

steelT.sa2

steelT.mod

4-4

steelT.dat

4-5

steelT.sa3

13-1

steelT.sa4

13-3

steelT.sa5

13-4

steelT.sa7

13-5

steelT.tab1

13-2

steelT2.mod

5-3

steelT2.dat

5-4

steelT3.mod

6-3

steelT3.dat

6-4

transp.mod

3-1a

transp.dat

3-1b

transp2.mod

6-1

transp3.mod

6-2a

transp3.dat

6-2b

Book examples by figure number#

Additional Scripts: Looping and Testing - 1#

Writing “scripts” in the AMPL command language

All examples use steelT.mod as the model file and the steelT.dat as the data file.

Implements

Script file

One pass of sensitivity analysis on avail[3] in the multi-period production problem (Section 4.2)

steelT.sa1

Same as steelT.sa1 but split into two scripts, using the commands command

steelT.sa1a steelT.sa1b

Iterated sensitivity analysis, using contents of steelT.sa1a and steelT.sa1b within a for loop

steelT.sa2

… also building a table of results

steelT.sa3

… also using repeat until, and building up an index set for the table

steelT.sa4

… also using if to record only those parameter values at which the dual price changes

steelT.sa5

Binary search to find a breakpoint in the dual value

steelT.sa6

… also handling the case of more than one breakpoint in the initial interval

steelT.sa6a

… also counting the extra breakpoints detected

steelT.sa6b

Sensitivity analysis using break and continue

steelT.sa7

… also for each t, printing at the end of each pass

steelT.sa7a

… also for each t, storing all values until the end

steelT.sa7b

… also incorporating a break criterion requiring a named loop

steelT.sa7c

Data-specific script for a formatted table

steelT.tab0

General script for formatted table, using a for loop

steelT.tab1

… also with if statement to suppress printing of 100%

steelT.tab2

Additional Scripts: Looping and Testing - 2#

Implementing algorithms through AMPL scripts

Implements

Script

Uses

Gilmore-Gomory column generation procedure for the cutting-stock (roll trim) problem

cut1.run

cut1.mod cut.dat

Same as cut1.run, but using an alternative arrangement wherein problems are defined immediately before before their members are declared

cut2.run

cut2.mod cut.dat

Same as cut1.run, but with better formatting of output

cut3.run

cut1.mod cut.dat

Dantzig-Wolfe decomposition for a multi-commodity transportation problem, using a single subproblem

multi1.run

multi1.mod multi1.dat

Same as multi1.run, but using the same repeat loop for both phase I (infeasible) and phase II (feasible).

multi1a.run

multi1.mod multi1.dat

Same as multi1.run, but using a separate subproblem for each product; subproblems are represented in AMPL by an indexed collection of named problems

multi2.run

multi2.mod multi2.dat

Same as multi2.run, except that the separate subproblems are realized by changing the data to a single AMPL named problem

multi3.run

multi3.mod multi3.dat

Benders decomposition for a stochastic programming variant of a multi-period production problem (see Exercise 4-5)

stoch1.run

stoch1.mod stoch.dat

Same as stoch1.run, but using a separate subproblem for each scenario; subproblems are represented in AMPL by an indexed collection of named problems

stoch2.run

stoch2.mod stoch.dat

Same as stoch2.run, except that the separate subproblems are realized by changing the data to a single AMPL named problem

stoch3.run

stoch3.mod stoch.dat

Benders decomposition for a location-transportation problem (original model in trnloc.mod)

trnloc1.run

trnloc1.mod trnloc.dat

Same as trnloc2a.run, but model has upper limits on the Ship variables: LP relaxation bound is still poor, but subproblem does not have the integrality property and considerable improvement is made

trnloc2b.run

trnloc2b.mod trnloc2.dat

Same as trnloc2b.run, but model has 0-1 constraints disaggregated: LP relaxation bound is good, but subproblem has the integrality property and no further improvement can be made

trnloc2c.run

trnloc2c.mod trnloc2.dat

Lagrangian relaxation for a location-transportation problem: LP relaxation bound is poor, and subproblem has the integrality property so no improvement can be made

trnloc2a.run

trnloc2a.mod trnloc2.dat

Logic & Constraint Programming Examples#

This is a preliminary set of examples to offer some starting points for experimenting with AMPL’s “logic” and constraint programming interfaces. We welcome comments for improvements or other examples.

Represents

Model File

Integer program for assignment of people of groups so that each group is “balanced” by various criteria.

balAssign0.mod balAssign0.dat

Constraint program for balanced assignment of people to groups, using the numberof operator.

balAssign1.mod balAssign1.dat

Model for map coloring using not-equals constraint.

mapColoring.mod

Integer program for the n queens problem: put n queens on an n-by-n chessboard so that no queen is attacking any other queen.

nQueens0.mod

Constraint program for the n queens problem, using the alldiff operator.

nQueens.mod

Open-shop scheduling model using disjunctive constraints to avoid job and machine conflicts.

openShop.mod openShop.dat

Line up a group of people to maximize the number of adjacency preferences satisfied, modeling with alldiff, equivalence (<==>), and strict inequality (<)

photo.mod photo9.dat photo11.dat

Hoist scheduling model using disjunctive constraints

singleHoist.mod singleHoist3.dat singleHoist4.dat

Model to solve instances of the 3 x 3 Sudoku problem, using the alldiff operator

sudoku.mod sudokuVeryEasy.dat sudokuHard.dat

List of example files#