You are on page 1of 14

An introduction to AMPL syntax

Christian Valente

Agenda

• Introduction
Introduction
• Simple introductory LP model
Example
– Mathematical representation
Syntax – AMPL representation
Architecture • Exploring AMPL syntax
– Modelling constructs
– Others...
• AMPL/AMPL Studio Architecture

1
Introduction
• Mathematical Programming:
– Modeler’s understanding of the problem leads to
Introduction “modeler’s form”
Example
– Solvers accept a different format: “algorithm’s
Syntax
form”
Architecture
– -> algebraic languages, AMPL being one of those
• It allows a separation between model and data,
recommended for most applications
AMPL
Model AMPL Model
Instance
Data

Simple LP model – Model


• Maximize profits of steel company
– The mill produces two products (bands and coils)
Introduction
– The products come off the rolling lane at different
Example
rates; the mill is available for a number of hours
Syntax
– The products are having different profitability
Architecture
– There is a maximum in the production amount,
dictated by market requirements
– We have to decide how many coils and bands to
produce taking care not exceed the number of
hours in which the mill is available

2
Simple LP model - Algebraic
• Maximizing profits of a steel company
Variables
Introduction
Maximize :  c Xj
j P
j
Parameters
Indexing
Example Subject to :  ( 1/a )X
jP
j j b
expressions
Syntax

Architecture
0  Xj  uj j  p
Where : P  the types of products
aj  tons of product j/hour produced
b  hours available at the mill
cj  profit per ton of product j
uj  maximum tons of product j

Simple LP model-Components
• Set: PROD, identifies different products
• Parameters:
Introduction
– ratePROD: production rate
Example

Syntax
– profitPROD: profit rate
Architecture
– avail: availability of the mill
– marketPROD: upper limit in production
• Variables: MakePROD, values to be determined
• Objective function to be maximized
• Constraints to be satisfied

3
AMPL preliminary concepts
• Distinction between declarations and
definitions
Introduction • set PROD; declares the set
Example • set PROD := bands coils; defines set’s membership
Syntax
• It allows a separation between model and
Architecture
data, recommended for most applications
AMPL
Model Model
AMPL
Instance
Data

Simple LP model – AMPL


model
set PROD;
param rate{ j in PROD } > 0;
param avail >=0;
Introduction
param profit { j in PROD };
Example
param market { j in PROD };
Syntax
var Make{ j in PROD } >= 0;
Architecture
maximize total_profit: sum { j in PROD } profit[ j ] * Make[ j ];
subject to:
time: sum { j in PROD } (1 / rate[ j ]) * Make[ j ] <= avail ;
limit { j in PROD } : 0 <= Make[ j ] <= market [ j ];

4
Simple LP model – AMPL data

set PROD := bands coils;


param: rate profit market :=
Introduction

Example
bands 200 25 6000
Syntax coils 140 30 4050 ;
Architecture param avail := 40;

AMPL syntax: sets


• The most fundamental component of an AMPL
model
Introduction • Almost all the parameters, variables and
Example
constraints are indexed over sets.
Syntax

Architecture
• There is a broad variety of set types and
operations:
• Members can be strings or numbers
• Ordered or unordered
• Can be defined by listing or computing their
members

5
AMPL syntax : sets of strings
• Set declaration:
set PROD;
Introduction • Usually all of the strings in a set of strings are
Example
representing the instances of the same kind of
Syntax
entity
Architecture
Unordered set of strings definition, data file:
set PROD := bands coils;
Unordered set of strings definition, model file:
set PROD := {“bands”, “coils”};

AMPL syntax: sets of numbers


• Sets of numbers can be given listing the
entities:
Introduction set years := 2000 2005 2010 2015 2020;
Example
• By intervals:
Syntax
set years := 2000 .. 2020 by 5;
Architecture

• Parametric:
param start integer; #defined in the data file
param end integer;
param interval integer;
set years := start .. end by interval;

6
AMPL syntax: set operations
• New sets can be created from existing ones:
set A := 1990 .. 2020 by 5;
Introduction set B := 2000 .. 2025 by 5;
Example

Syntax
A union B; # union of the two sets
Architecture

A inter B; # intersection of the two sets


A diff B; # Items in A but not in B
A symdiff B; #Items in A or B but not in both (XOR)

AMPL syntax: ordered sets


• When ordering is essential (i.e. time periods)
number are often used to represent the entities;
Introduction
ordered sets can be clearer:
Example set weeks ordered := {“27sep”, “04oct”, “11oct”};
Syntax • Accessing the members:
Architecture
prev (“04oct”, weeks); # previous member
next (“04oct”, weeks); # next member
ord (“04oct”, weeks); # ordinal position (from 1)
first (weeks); # first element
last (weeks); # last element

7
AMPL syntax: parameters
• In the models, only a symbolic description of
numerical values appears: a parameter
Introduction
• They can be scalar numbers matrices, or vectors
Example
indexed over an arbitrary amount of sets
Syntax • Data consistency checks can be integrated in the
Architecture parameters declarations:
param T > 1 integer; # will refuse any data that is not
integer or <=1
or in a separate statement:
check {p in PROD } : sum {i in ORIG} supply[i,p] =
sum {j in DEST} demand [j,p];

AMPL syntax:
indexing expressions
• Used whenever we specify the set over which
Introduction a model component is indexed, or the set over
Example which a summation runs
Syntax
• Simplest form:
Architecture
param a {P}; # will be referred as a[p]
param b {1 .. 4}; # will be referred as b[number]
param c {1 .. T}; # will be referred as c[number]

8
AMPL syntax:
indexing expressions
• Dummy indexes, to refer to the instances:
Introduction param f_min {FOOD} >=0;
Example variable X {p in PROD} >= 0, <= market[p];
Syntax
• Dummy indexes, used for index constraints
Architecture
and summations:
maximize Profit: sum { j in PROD} c[ j ] * x[ j ];
subject to limit { j in PROD} : 0 <= x[ j ] <= u[ j ];

AMPL syntax:
indexing expressions
• Conditional indexing:
Introduction • In declarations:
Example var amounthold {a in ASSETS : ifhold[a] =1} >= 0;
Syntax

Architecture
• In indexing:
subject to:
limitexpprod{ j in PROD : price[j] >= PRICELIMIT } :
0 <= buy[ j ] <= UPPERLIMIT;

9
AMPL syntax: scripting..
• AMPL is an interpreted language
• Thus there are commands that are not used to
Introduction define models, but that control options and the
Example
execution flow of the system
Syntax

Architecture
• Some of those commands are having an
equivalent in AMPLDev GUI
• Sequences of these commands can be into the
model file or in separate script files

AMPL syntax: scripting..


• Examples of uses:
• Read model and data files
Introduction • Solve a model
Example
• Specify option values
Syntax
• Modify models on the fly
Architecture
• drop constraints
• fix variables
• relax integrality
• Display specific data

10
AMPL syntax: display..

• Used to examine and report the result of the


optimization
Introduction

Example
• AMPL and AMPLDEV offer some data exploring
Syntax
facilities, but the use of display and indexed
Architecture
expression enables much more customized data
visualization

AMPL syntax: display..


• Displaying sets:
– display setname, ... , setname;
Introduction • Displaying members of indexed sets:
Example
– display setname[“membername”];
Syntax
• Displaying expressions:(
Architecture
– i.e. display {p in PROD} :
profit[p] >= 3000;
(displays all the members of the set PROD whose profit is
greater or equal than 3000)

11
AMPL syntax: display..
• Displaying variables and parameters:
– display varname, ... , varname;
Introduction • Displaying indexed expressions:
Example
– display sum{p in PROD, t in 1..T}
Syntax revenue[p, t] * Sell[p, t];
Architecture
• Indexing display command:
– display {p in PROD}: {i in ORIG},
{j in DEST} Trans[i, j, p];

AMPL syntax: display..


• Displaying constraints and objective
– display constraintname;
Introduction – display objectivename;
Example
• Displaying other properties (suffixes):
Syntax
– variablename.lb – lower bound
Architecture
– variablename.ub – upper bound
– variablename.slack – difference
– constraint.dual – dual value
– ...

12
AMPL syntax: Modifying the
model
• Looping
– for {indexing exp} { body }
Introduction

Example
• Fix a variable to a value
Syntax – fix variablename := value;
Architecture • Drop constraint
– drop constraintname;

AMPL syntax: other scripting..


• Read model file:
– model modelfilename;
Introduction • Read data file:
Example
– data datafilename;
Syntax
• Choose the solver:
Architecture
– option solver afortmp;
• Solve the model:
– solve;

13
AMPL/AMPLDev Architecture
Script file

Introduction

AMPL
Example
AMPL Solver
Syntax
Models
Architecture

AMPL
Data
AMPL matrix
language DB (MPS or binary)
algebraic model
model instance

14

You might also like