Tags:
view all tags
<ol><br /><li><a href="#describe">Description</a><br /><li><a href="#declare">Declaring a Parameter</a><br /><li><a href="#types">Parameter Types</a><br /><li><a href="#bounds">Parameter Bounds</a><br /><li><a href="#defaults">Default Values</a><br /><li><a href="#define">Defining a Parameter</a><br /><li><a href="#acess">Accessing a Parameter</a><br /></ol><br /><br /><a name="describe"><h3>Description</h3></a><br />Parameters hold "hard" values in AMPL. The values of parameters can be defined and changed in AMPL, but once a solver will not change them while looking for an optimal solution.<br /><br /><a name="declare"><h3>Declaring a Parameter</h3></a><br />AMPL parameters are created in a similar way to <a href="Variables in AMPL#declare">AMPL variables</a>, using the {\tt param} keyword followed by a label.<br /><p>\begin{verbatim}<br />param <paramname>;<br />\end{verbatim}<p><br /><h4>Example</h4><br /><p>\begin{verbatim}<br />param MinProtein;<br />\end{verbatim}<p><br />Like <a href="Variables in AMPL#declare">variables</a> parameters are often defined over a set and may have several attributes:<br /><p>\begin{verbatim}<br />param <paramname> [{<indexname>}] [<attributes>];<br />\end{verbatim}<p><br /><h4>Example</h4><br /><p>\begin{verbatim}<br />param ProteinPercent {INGREDIENTS} >= 0 <= 100;<br />\end{verbatim}<p><br /><br /><a name="types"><h3>Parameter Types</h3></a><br />Parameters have the same possible types as <a href="Variables in AMPL#types>variables</a>. However, since parameter values are defined (not searched for), declaring a parameter type means that AMPL will ensure that the parameter is only ever assigned that type of data. For example, you cannot assign an integer parameter the value 1.5. This behaviour is very useful for automatically checking the validity of data files.<br /><br /><a name="bounds"><h3>Parameter Bounds</h3></a><br />As well as using parameter types to check the validity of data, real and integer parameters can also have bounds set during their declaration. These bounds will be checked by AMPL any time the value of the parameter changes and, if they are violated, and error will be generated.<br /><h4>Example</h4><br /><p>\begin{verbatim}<br />param counter integer >= 0;<br /><br />let counter := -1; # This generates an error as counter is < 0<br />\end{verbatim}<p><br /><br /><a name="defaults"><h3>Default Values</h3></a><br />Default parameter values can be used to quickly set a large number of parameter values automatically. If a parameter is used without being explicitly assigned a value the default value is used for that parameter. AMPL uses a default value of 0 if no default value is given.<br /><h4>Example</h4><br /><p>\begin{verbatim}<br />set DIGITS := 1..5;<br />param isok {DIGITS} binary default 1;<br /><br />let isok[3] := 0;<br /><br />display {i in DIGITS} isok[i];<br /><br /># Result<br /># ======<br /># isok[i] [*] :=<br /># 1 1<br /># 2 1<br /># 3 0<br /># 4 1<br /># 5 1<br /># ;<br />\end{verbatim}<p><br />The <a href="Miscellaneous AMPL#macros">AMPL macros</a> {\tt Infinity} and {\tt -Infinity} are useful as defaults for parameters that act as bounds ({\tt Infinity} as a default upper bound, 0 or {\tt -Infinity} as a default lower bound).<br /><br /><a name="define"><h3>Defining a Parameter</h3></a><br />Once a parameter has been declared it is usually defined in a data file. This is done simply for a single value<br />using the assignment operator {\tt :=}:<br /><p>\begin{verbatim}<br />param MinProtein := 8.0 ;<br />\end{verbatim}<p><br />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:<br /><p>\begin{verbatim}<br />model;<br /><br />param Min {REQUIREMENTS} default -Infinity;<br /><br />data;<br /><br />param Min :=<br />PROTEIN 8.0<br />FAT 6.0<br /> ;<br />\end{verbatim}<p><br />In a similar way to <a href="Sets in AMPL#define">2-dimensional sets</a>, there are three different ways to define 2-dimensional sets.<br /><ol><br /><li>**Using a List** For any parameter values that don't take the default value, you list the set element and value for that parameter.<br /><p>\begin{verbatim}<br />model;<br /><br />param Min {ARCS} integer, default 0;<br /><br />data;<br /><br />param Min :=<br />Youngstown 'Kansas City' 1000<br />Pittsburgh 'Kansas City' 2000<br />Cincinnati Albany 1000<br />\end{verbatim}<p><br /><br /><br /><li>**Using a Table** To define parameter data in a table format you use the {\tt param} keyword and the parameter’s name followed by the {\tt :} operator, a list of the second index set elements followed by the {\tt := } 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.<br /><p>\begin{verbatim}<br />param <paramname> :<br /> <j1> <j2> ... <jn> :=<br /><i1> <value(i1, j1)> <value(i1, j2)> ... <value(i1, jn)><br />...<br /><im> <value(im, j1)> <value(im, j2)> ... <value(im, jn)><br /> ;<br />\end{verbatim}<p><br />If the element does not exist or the default value is correct then place a {\tt .} in the table. Otherwise, put the parameter value.<br /><p>\begin{verbatim}<br />param Cost: Cincinnati 'Kansas City' Chicago Albany Houston Tempe Gary :=<br />Youngstown 350 450 375 500 . . .<br />Pittsburgh 350 450 400 . . . 450<br />Cincinnati . . . 350 550 . .<br />'Kansas City' . . . . 375 650 .<br />Chicago . . . . . 600 120 ;<br />\end{verbatim}<p><br /><br />You can also define parameter data in a transposed table using almost the same syntax, but with the {\tt (tr)} keyword and reversing the indexing sets<br /><p>\begin{verbatim}<br />param <paramname> (tr) :<br /> <i1> <i2> ... <im> :=<br /><j1> <value(i1, j1)> <value(i2, j1)> ... <value(im, j1)><br />...<br /><jn> <value(i1, jn)> <value(i2, jn)> ... <value(im, jn)><br /> ;<br />\end{verbatim}<p><br /><br /><li>**Using an Array** You define a list of column indices and values for each row index.<br /><p>\begin{verbatim}<br />param Cost :=<br />[Youngstown, *] Cincinnati 350 'Kansas City' 450 ...<br />...<br />[Chicago, *] ... Gary 120 ;<br />\end{verbatim}<p><br />**Note** The row indices have {\tt [} and {\tt ]} around them (as opposed to {\tt (} and {\tt )} for <a href="Sets in AMPL#define">sets</a>).<br /></ol><br /><br /><h4>Defining Multiple Parameters</h4><br />Using the {\tt :} operator, multiple parameters may be defined at once. Simply state the names of the parameters and the {\tt :=} operator. Then list the set elements and values on the following rows.<br /><p>\begin{verbatim}<br />param: <name1> <name2> ... :<br /><element1> <value1,1> <value1,2> ...<br /><element2> <value2,1> <value2,2> ...<br /> ;<br />\end{verbatim}<p><br />If a parameter is not defined or the default value is sufficient, use the {\tt .} operator.<br /><p>\begin{verbatim}<br />model;<br /><br /># The lower and upper bounds on the requirements<br />param Min {REQUIREMENTS} default -Infinity;<br />param Max {REQUIREMENTS} default Infinity;<br /><br />data;<br />param: Min Max:=<br />PROTEIN 8.0 .<br />FAT 6.0 .<br />FIBRE . 2.0<br />SALT . 0.4<br /> ;<br />\end{verbatim}<p><br />This approach also works for 2-dimensional parameters and lists, for the <a href="A Transshipment Problem">American Steel</a> problem this allows us to "cut-and-paste" the list of arc properties<br /><p>\begin{verbatim}<br /> From node To node Cost Minimum Maximum<br /> Youngstown Albany 500 - 1000<br /> Youngstown Cincinnati 350 - 3000<br /> Youngstown Kansas City 450 1000 5000<br /> Youngstown Chicago 375 - 5000<br /> etc<br />\end{verbatim}<p><br />becomes<br /><p>\begin{verbatim}<br />param: Cost Min Max:=<br />Youngstown Cincinnati 350 0 3000<br />Youngstown 'Kansas City' 450 1000 5000<br />...<br />Chicago Gary 120 0 4000<br /> ;<br />\end{verbatim}<p><br /><br /><a name="access"><h3>Accessing a Parameter</h3></a><br />Parameter values are accessed by specifying the indices of the parameter you want to access within {\tt [} and {\tt ]}.<br /><h4>Examples</h4><br />See {\tt Cost} and {\tt Contributes} below.<br /><p>\begin{verbatim}<br /># Objective: minimise the cost per (100g) can<br />minimize TotalCost: sum {i in INGREDIENTS} Cost[i] * Percentage[i];<br /><br /># Constraints: Meet the nutritional requirements<br /><br />subject to MeetRequirement {r in REQUIREMENTS}:<br /> Min[r] <= sum {i in INGREDIENTS} Contributes[i, r] * Percentage[i]<br /> <= Max[r];<br />\end{verbatim}<p><br /><br /><h1>Under Construction Below Here</h1><br /><h4>Multi-dimensional Parameters</h4><br />Since we have been using multi-dimensional sets, we might need multi-dimensional parameters, e.g., {\tt Cost {TIME_ARCS}}. We can define these parameters in a similar way to <a href="Sets in AMPL#define>sets</a><br /><br />List<br />param Cost :=<br />Youngstown April Albany April 0.5 # = 500 / 1000<br />Youngstown April Youngstown May 0.015 # = 15 / 1000<br />... ;<br />Table<br />param Cost : =<br />[*, May, *, May] Cincinnati 'Kansas City' Albany ... := Youngstown 0.35 0.45 0.5 ... Pittsburgh 0.35 0.45 . ...<br />... ;<br />Notice the [ ] around *, May, *, May as opposed to the ( ) for sets!<br />Array<br />set TIME_ARCS :=<br />(*, May, *, May) (Youngstown, Cincinnati) 0.35<br />... ;<br />or<br />set TIME_ARCS :=<br />(Youngstown, May, *, May) Cincinnati 0.35 'Kansas City' 0.45 ...<br />... ;<br /> -- Main.MichaelOSullivan - 02 Mar 2008
Edit
|
Attach
|
Watch
|
P
rint version
|
H
istory
:
r10
|
r4
<
r3
<
r2
<
r1
|
B
acklinks
|
V
iew topic
|
Raw edit
|
More topic actions...
Topic revision: r1 - 2008-03-02
-
MichaelOSullivan
Home
Site map
Forum web
Main web
NDSG web
ORUA web
OpsRes web
Sandbox web
TWiki web
OpsRes Web
Create New Topic
Index
Search
Changes
Notifications
RSS Feed
Statistics
Preferences
P
P
View
Raw View
Print version
Find backlinks
History
More topic actions
Edit
Raw edit
Attach file or image
Edit topic preference settings
Set new parent
More topic actions
Account
Log In
Edit
Attach
Copyright © 2008-2025 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki?
Send feedback