param keyword followed by a label.
param <paramname>;
param MinProtein;Like variables parameters are often defined over a set and may have several attributes:
param <paramname> [{<indexname>}] [<attributes>];
param ProteinPercent {INGREDIENTS} >= 0 <= 100;
Return to top
param counter integer >= 0; let counter := -1; # This generates an error as counter is < 0Return to top
set DIGITS := 1..5;
param isok {DIGITS} binary default 1;
let isok[3] := 0;
display {i in DIGITS} isok[i];
# Result
# ======
# isok[i] [*] :=
# 1 1
# 2 1
# 3 0
# 4 1
# 5 1
# ;
The AMPL macros Infinity and -Infinity are useful as defaults for parameters that act as bounds (Infinity as a default upper bound, 0 or -Infinity as a default lower bound).
Return to top
:=:
param MinProtein := 8.0 ;For parameters declared over a 1-dimensional set this can be done using default values and a list for those parameters that don't take default values:
model;
param Min {REQUIREMENTS} default -Infinity;
data;
param Min :=
PROTEIN 8.0
FAT 6.0
;
model;
param Min {ARCS} integer, default 0;
data;
param Min :=
Youngstown 'Kansas City' 1000
Pittsburgh 'Kansas City' 2000
Cincinnati Albany 1000
param keyword and the parameter's name followed by the : operator, a list of the second index set elements followed by the := operator, then rows of the table with an element of the first index set followed by the values corresponding to the second index set's element in that column.
param <paramname> :
<j1> <j2> ... <jn> :=
<i1> <value(i1, j1)> <value(i1, j2)> ... <value(i1, jn)>
...
<im> <value(im, j1)> <value(im, j2)> ... <value(im, jn)>
;
If the element does not exist or the default value is correct then place a . in the table. Otherwise, put the parameter value.param Cost: Cincinnati 'Kansas City' Chicago Albany Houston Tempe Gary := Youngstown 350 450 375 500 . . . Pittsburgh 350 450 400 . . . 450 Cincinnati . . . 350 550 . . 'Kansas City' . . . . 375 650 . Chicago . . . . . 600 120 ;You can also define parameter data in a transposed table using almost the same syntax, but with the
(tr) keyword and reversing the indexing sets
param <paramname> (tr) :
<i1> <i2> ... <im> :=
<j1> <value(i1, j1)> <value(i2, j1)> ... <value(im, j1)>
...
<jn> <value(i1, jn)> <value(i2, jn)> ... <value(im, jn)>
;
param Cost := [Youngstown, *] Cincinnati 350 'Kansas City' 450 ... ... [Chicago, *] ... Gary 120 ;Note The row indices have
[=} and =] around them (as opposed to ( and ) for sets).
Cost {TIME_ARCS} has four dimensions. We can define these parameters in a similar way to multi-dimensional sets: param Cost := Youngstown April Albany April 0.5 # = 500 / 1000 Youngstown April Youngstown May 0.015 # = 15 / 1000 ... ;
param Cost : = [*, May, *, May] Cincinnati 'Kansas City' Albany ... := Youngstown 0.35 0.45 0.5 ... Pittsburgh 0.35 0.45 . ... ... ;Notice the
[ ] around *, May, *, May as opposed to the ( ) for sets!
set TIME_ARCS := (*, May, *, May) (Youngstown, Cincinnati) 0.35 ... ;or
set TIME_ARCS := (Youngstown, May, *, May) Cincinnati 0.35 'Kansas City' 0.45 ... ... ;
\begin{verbatim}
param:
If a parameter is not defined or the default value is sufficient, use the {\tt .} operator.
\begin{verbatim}
model;
This approach also works for 2-dimensional parameters and lists, for the American Steel problem this allows us to "cut-and-paste" the list of arc properties
\begin{verbatim}
From node To node Cost Minimum Maximum
Youngstown Albany 500 - 1000
Youngstown Cincinnati 350 - 3000
Youngstown Kansas City 450 1000 5000
Youngstown Chicago 375 - 5000
etc
\end{verbatim}
becomes
\begin{verbatim}
param: Cost Min Max:=
Youngstown Cincinnati 350 0 3000
Youngstown 'Kansas City' 450 1000 5000
...
Chicago Gary 120 0 4000
;
\end{verbatim}
\begin{verbatim}
# Objective: minimise the cost per (100g) can
minimize TotalCost: sum {i in INGREDIENTS} Cost[i] * Percentage[i];
Accessing a Parameter
Parameter values are accessed by specifying the indices of the parameter you want to access within {\tt [} and {\tt ]}.
Examples
See {\tt Cost} and {\tt Contributes} below.