Tags:
view all tags
<!-- Ready to Review --> ---+<a name="top"></a> Parameters in AMPL 1 [[#describe][Description]] 1 [[#declare][Declaring a Parameter]] 1 [[#types][Parameter Types]] 1 [[#bounds][Parameter Bounds]] 1 [[#defaults][Default Values]] 1 [[#define][Defining a Parameter]] 1 [[#access][Accessing a Parameter]] ---++<a name="describe"></a> Description Parameters hold "hard" values in AMPL. The values of parameters can be defined and changed in AMPL, but a solver will not change them while looking for an optimal solution. [[#top][Return to top]] ---++<a name="declare"></a> Declaring a Parameter AMPL parameters are created in a similar way to [[VariablesInAMPL][AMPL variables]], using the =param= keyword followed by a label. <pre> param <paramname>; </pre> ---+++ Example <pre> param MinProtein; </pre> Like [[VariablesInAMPL#declare][variables]] parameters are often defined over a set and may have several attributes: <pre> param <paramname> [{<indexname>}] [<attributes>]; </pre> ---+++ Example <pre> param ProteinPercent {INGREDIENTS} >= 0 <= 100; </pre> [[#top][Return to top]] ---++<a name="types"></a> Parameter Types Parameters have the same possible types as [[VariablesInAMPL#types][variables]]. 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. [[#top][Return to top]] ---++<a name="bounds"></a> Parameter Bounds 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. ---+++ Example <pre> param counter integer >= 0; let counter := -1; # This generates an error as counter is < 0 </pre> [[#top][Return to top]] ---++<a name="defaults"></a> Default Values 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. ---+++ Example <pre> 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 # ; </pre> The [[MiscellaneousInAMPL#macros][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). [[#top][Return to top]] ---++<a name="define"></a> Defining a Parameter Once a parameter has been declared it is usually defined in a data file. This is done simply for a single value using the assignment operator =:==: <pre> param MinProtein := 8.0 ; </pre> 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: <pre> model; param Min {REQUIREMENTS} default -Infinity; data; param Min := PROTEIN 8.0 FAT 6.0 ; </pre> ---+++ Defining 2-Dimensional Parameters In a similar way to [[SetsInAMPL#define][2-dimensional sets]], there are three different ways to define 2-dimensional sets. 1 *Using a List* For any parameter values that don't take the default value, you list the set element and value for that parameter.<pre> model; param Min {ARCS} integer, default 0; data; param Min := Youngstown 'Kansas City' 1000 Pittsburgh 'Kansas City' 2000 Cincinnati Albany 1000 </pre> 2 *Using a Table* To define parameter data in a table format you use the =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.<pre> 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)> ; </pre>If the element does not exist or the default value is correct then place a =.= in the table. Otherwise, put the parameter value.<pre> 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 ; </pre>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<pre> 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)> ; </pre> 3 *Using an Array* You define a list of column indices and values for each row index.<pre> param Cost := [Youngstown, *] Cincinnati 350 'Kansas City' 450 ... ... [Chicago, *] ... Gary 120 ; </pre> *Note* The row indices have =[=} and =]= around them (as opposed to =(= and =)= for [[SetsInAMPL#define][sets]]). ---+++ Defining Multi-Dimensional Parameters Since we have [[SetsInAMPL#define][multi-dimensional sets]], we might need multi-dimensional parameters, e.g., =Cost {TIME_ARCS}= has four dimensions. We can define these parameters in a similar way to [[Sets in AMPL#define][multi-dimensional sets]]: 1 *Using a List* <pre> param Cost := Youngstown April Albany April 0.5 # = 500 / 1000 Youngstown April Youngstown May 0.015 # = 15 / 1000 ... ; </pre> 2 *Using a Table* <pre> param Cost := [*, May, *, May] Cincinnati 'Kansas City' Albany ... := Youngstown 0.35 0.45 0.5 ... Pittsburgh 0.35 0.45 . ... ... ; </pre>Notice the =[ ]= around <tt>*, May, *, May</tt> as opposed to the =( )= for sets! 2 *Using an Array* <pre> set TIME_ARCS := (*, May, *, May) (Youngstown, Cincinnati) 0.35 ... ; </pre>or<pre> set TIME_ARCS := (Youngstown, May, *, May) Cincinnati 0.35 'Kansas City' 0.45 ... ... ; </pre> ---+++ Defining Multiple Parameters Using the =:= operator, multiple parameters may be defined at once. Simply state the names of the parameters and the =:== operator. Then list the set elements and values on the following rows. <pre> param: <name1> <name2> ... : <element1> <value1,1> <value1,2> ... <element2> <value2,1> <value2,2> ... ; </pre> If a parameter is not defined or the default value is sufficient, use the =.= operator. <pre> model; # The lower and upper bounds on the requirements param Min {REQUIREMENTS} default -Infinity; param Max {REQUIREMENTS} default Infinity; data; param: Min Max:= PROTEIN 8.0 . FAT 6.0 . FIBRE . 2.0 SALT . 0.4 ; </pre> This approach also works for 2-dimensional parameters and lists, for [[AmericanSteelTransshipment][the American Steel problem]] this allows us to "cut-and-paste" the list of arc properties | _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 ||||| becomes <pre> param: Cost Min Max:= Youngstown Cincinnati 350 0 3000 Youngstown 'Kansas City' 450 1000 5000 ... Chicago Gary 120 0 4000 ; </pre> [[#top][Return to top]] ---++<a name="access"></a> Accessing a Parameter Parameter values are accessed by specifying the indices of the parameter you want to access within =[= and =]=. ---+++ Examples See =Cost= and =Contributes= below. <pre> # Objective: minimise the cost per (100g) can minimize TotalCost: sum {i in INGREDIENTS} Cost[i] * Amount[i]; # Constraints: Meet the nutritional requirements subject to MeetRequirement {r in REQUIREMENTS}: Min[r] <= sum {i in INGREDIENTS} Contributes[i, r] * Amount[i] <= Max[r]; </pre> [[#top][Return to top]] -- Main.MichaelOSullivan - 02 Mar 2008
Edit
|
Attach
|
Watch
|
P
rint version
|
H
istory
:
r10
|
r8
<
r7
<
r6
<
r5
|
B
acklinks
|
V
iew topic
|
Raw edit
|
More topic actions...
Topic revision: r6 - 2008-04-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