AMPL
A Modeling Language
for Mathematical Programming
Second Edition
AMPL
A Modeling Language
for Mathematical Programming
Second Edition
Robert Fourer
Northwestern University
David M. Gay
AMPL Optimization LLC
Brian W. Kernighan
Princeton University
DUXBURY
————————————————
THOMSON
________________________________________________________________________________
Australia • Canada • Mexico • Singapore • Spain • United Kingdom • United States
This book was typeset (grap|pic|tbl|eqn|troff -mpm) in Times and Courier by the
authors.
Copyright 2003 by Robert Fourer, David M. Gay and Brian Kernighan.
All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or
transmitted, in any form or by any means, electronic, mechanical, photocopying, recording, or
otherwise, without the prior written permission of the publisher. Printed in the United States of
America. Published simultaneously in Canada.
About the authors:
Robert Fourer received his Ph.D. in operations research from Stanford University in
1980, and is an active researcher in mathematical programming and modeling language
design. He joined the Department of Industrial Engineering and Management Sciences at
Northwestern University in 1979, and served as chair of the department from 1989 to
1995.
David M. Gay received his Ph.D. in computer science from Cornell University in 1975,
and was in the Computing Science Research Center at Bell Laboratories from 1981 to
2001; he is now CEO of AMPL Optimization LLC. His research interests include
numerical analysis, optimization, and scientific computing.
Brian Kernighan received his Ph.D. in electrical engineering from Princeton University in
1969. He was in the Computing Science Research Center at Bell Laboratories from 1969
to 2000, and now teaches in the Computer Science department at Princeton. He is the
co-author of several computer science books, including The C Programming Language
and The UNIX Programming Environment.
________________________________________________________________________
________________________
Contents
Introduction
Chapter 1. Production Models: Maximizing Profits
1.1 A two-variable linear program
1.2 The two-variable linear program in AMPL
1.3 A linear programming model
1.4 The linear programming model in AMPL
The basic model
An improved model
Catching errors
1.5 Adding lower bounds to the model
1.6 Adding resource constraints to the model
1.7 AMPL interfaces
Chapter 2. Diet and Other Input Models: Minimizing Costs
2.1 A linear program for the diet problem
2.2 An AMPL model for the diet problem
2.3 Using the AMPL diet model
2.4 Generalizations to blending, economics and scheduling
Chapter 3. Transportation and Assignment Models
3.1 A linear program for the transportation problem
3.2 An AMPL model for the transportation problem
3.3 Other interpretations of the transportation model
Chapter 4. Building Larger Models
4.1 A multicommodity transportation model
4.2 A multiperiod production model
4.3 A model of production and transportation
Chapter 5. Simple Sets and Indexing
5.1 Unordered sets
vii
xv
1
2
5
6
7
8
10
12
13
15
18
27
27
30
32
37
43
44
45
49
55
56
59
63
73
73
viii
AMPL: A MODELING LANGUAGE FOR MATHEMATICAL PROGRAMMING
5.2 Sets of numbers
5.3 Set operations
5.4 Set membership operations and functions
5.5 Indexing expressions
5.6 Ordered sets
Predefined sets and interval expressions
Chapter 6. Compound Sets and Indexing
6.1 Sets of ordered pairs
6.2 Subsets and slices of ordered pairs
6.3 Sets of longer tuples
6.4 Operations on sets of tuples
6.5 Indexed collections of sets
Chapter 7. Parameters and Expressions
7.1 Parameter declarations
7.2 Arithmetic expressions
7.3 Logical and conditional expressions
7.4 Restrictions on parameters
7.5 Computed parameters
7.6 Randomly generated parameters
7.7 Logical parameters
7.8 Symbolic parameters
Chapter 8. Linear Programs: Variables, Objectives and Constraints
8.1 Variables
8.2 Linear expressions
8.3 Objectives
8.4 Constraints
Chapter 9. Specifying Data
9.1 Formatted data: the data command
9.2 Data in lists
Lists of one-dimensional sets and parameters
Lists of two-dimensional sets and parameters
Lists of higher-dimensional sets and parameters
Combined lists of sets and parameters
9.3 Data in tables
Two-dimensional tables
Two-dimensional slices of higher-dimensional data
Higher-dimensional tables
Choice of format
9.4 Other features of data statements
Default values
Indexed collections of sets
Initial values for variables
75
76
78
79
82
86
91
91
93
96
98
100
109
110
111
114
116
118
121
122
123
129
129
132
134
137
143
143
145
145
146
148
151
154
154
156
157
159
160
160
161
162
AMPL: A MODELING LANGUAGE FOR MATHEMATICAL PROGRAMMING
9.5 Reading unformatted data: the read command
Chapter 10. Database Access
10.1 General principles of data correspondence
10.2 Examples of table-handling statements
10.3 Reading data from relational tables
Reading parameters only
Reading a set and parameters
Establishing correspondences
Reading other values
10.4 Writing data to relational tables
Writing rows inferred from the data specifications
Writing rows inferred from a key specification
10.5 Reading and writing the same table
Reading and writing using two table declarations
Reading and writing using the same table declaration
10.6 Indexed collections of tables and columns
Indexed collections of tables
Indexed collections of data columns
10.7 Standard and built-in table handlers
Using the standard ODBC table handler
Using the standard ODBC table handler with Access and Excel
Built-in table handlers for text and binary files
Chapter 11. Modeling Commands
11.1 General principles of commands and options
Commands
Options
11.2 Setting up and solving models and data
Entering models and data
Solving a model
11.3 Modifying data
Resetting
Resampling
The let command
11.4 Modifying models
Removing or redefining model components
Changing the model: fix, unfix; drop, restore
Relaxing integrality
Chapter 12. Display Commands
12.1 Browsing through results: the display command
Displaying sets
Displaying parameters and variables
Displaying indexed expressions
12.2 Formatting options for display
ix
163
169
169
174
180
180
182
184
185
186
186
189
191
192
193
193
193
196
197
198
200
201
203
203
204
204
206
206
207
209
209
209
210
212
213
214
215
219
219
220
220
224
227