Transcription of Parameters and Expressions - AMPL
1 7_____Parameters and ExpressionsA large optimization model invariably uses many numerical values. As we haveexplained before, only a concise symbolic description of these values need appear in anAMPL model, while the explicit data values are given in separate data statements, to bedescribed in Chapter single named numerical value is called aparameter. Although someparameters are defined as individual scalar values, most occur in vectors or matrices orother collections of numerical values indexed over sets. We will thus loosely refer to anindexed collection of Parameters as a parameter when the meaning is clear. To beginthis chapter, Section describes the rules for declaring Parameters and for referring tothem in and other numerical values are the building blocks of the Expressions thatmake up a model s objective and constraints.
2 Sections and describe arithmeticexpressions, which have a numerical value, and logical Expressions , which evaluate totrue or false. Along with the standard unary and binary operators of conventional alge-braic notation,AMPL provides iterated operators likesumandprod, and a conditional(if-then-else) operator that chooses between two Expressions , depending on the truthof a third Expressions in objectives and constraints necessarily involve variables, whosedeclaration and use will be discussed in Chapter 8. There are several common uses forexpressions that involve only sets and Parameters , however. Section describes howlogical Expressions are used to test the validity of data, either directly in a parameter dec-laration, or separately in acheckstatement.
3 Section introduces features for definingnew Parameters through arithmetic Expressions in previously declared Parameters andsets, and describes randomly-generated the key purpose of Parameters is to represent numerical values, they canalso represent logical values or arbitrary strings. These possibilities are covered in Sec-tions and , a range of operators for strings, but asthey are most often used inAMPL commands and programming rather than in models, wedefer their introduction to Section AND Expressions CHAPTER Parameter declarationsA parameter declaration describes certain data required by a model, and indicates howthe model will refer to data values in subsequent simplest parameter declaration consists of the keywordparamand a name:param T;At any point after this declaration,Tcan be used to refer to a numerical often, the name in a parameter declaration is followed by an indexing expres-sion:param avail { };param demand {DEST,PROD};param revenue {p in PROD, AREA[p], }.
4 One parameter is defined for each member of the set specified by the indexing expres-sion. Thus a parameter is uniquely determined by its name and its associated set mem-ber; throughout the rest of the model, you would refer to this parameter by writing thename and bracketed subscripts :avail[i]demand[j,p]revenue[p,a,t]If the indexing is over a simple set of objects as described in Chapter 5, there is one sub-script. If the indexing is over a set of pairs, triples, or longer tuples as described in Chap-ter 6, there must be a corresponding pair, triple, or longer list of subscripts separated bycommas. The subscripts can be any Expressions , so long as they evaluate to members ofthe underlying index unindexed parameter is a scalar value, but a parameter indexed over a simple sethas the characteristics of a vector or an array; when the indexing is over a sequence ofintegers, sayparam avail { };the individual subscripted Parameters areavail[1],avail[2].
5 ,avail[T], andthere is an obvious analogy to the vectors of linear algebra or the arrays of a program-ming language like Fortran or s concept of a vector is more general, however,since Parameters may also be indexed over sets of strings, which need not even beordered. Indexing over sets of strings is best suited for Parameters that correspond toplaces, products and other entities for which no numbering is especially natural. Indexingover sequences of numbers is more appropriate for Parameters that correspond to weeks,stages, and the like, which by their nature tend to be ordered and numbered; even forthese, you may prefer to use ordered sets of strings as described in Section parameter indexed over a set of pairs is like a two-dimensional array or matrix.
6 Ifthe indexing is over all pairs from two sets, as inSECTION ARITHMETIC EXPRESSIONS111set ORIG;set DEST;param cost {ORIG,DEST};then there is a parametercost[i,j]for every combination ofifromORIG andjfromDEST, and the analogy to a matrix is strongest although again the subscripts aremore likely to be strings than numbers. If the indexing is over a subset of pairs, however:set ORIG;set DEST;set LINKS within {ORIG,DEST};param cost {LINKS};thencost[i,j]exists only for thoseifromORIG andjfromDEST such that(i,j)is a member ofLINKS. In this case, you can think ofcostas being a sparse comments apply to Parameters indexed over triples and longer tuples, whichresemble arrays of higher dimension in programming Arithmetic expressionsArithmetic Expressions inAMPLare much the same as in other computer numbers consist of an optional sign preceding a sequence of digits, which may ormay not include a decimal point (for example, +.)
7 3). At the end ofa literal there may also be an exponent, consisting of the letterd,D,e, orEand anoptional sign followed by digits ( ).Literals, Parameters , and variables are combined into Expressions by the standardoperations of addition (+), subtraction (-), multiplication (*), division (/), and exponen-tiation ( ). The familiar conventions of arithmetic apply. Exponentiation has higherprecedence than multiplication and division, which have higher precedence than additionand subtraction; successive operations of the same precedence group to the left, exceptfor exponentiation, which groups to the right. Parentheses may be used to change theorder of Expressions may also use thedivoperator, which returns the truncatedquotient when its left operand is divided by its right operand; themodoperator, whichcomputes the remainder; and thelessoperator, which returns its left operand minus itsright operand if the result is positive, or zero otherwise.
8 For purposes of precedence andgrouping,AMPL treatsdivandmodlike division, andlesslike list of arithmetic operators (and logical operators, to be described shortly) is givenin Table 7-1. As much as possible,AMPL follows common programming languages in itschoice of operator symbols, such as*for multiplication and/for division. There issometimes more than one standard, however, as with exponentiation, where some lan-guages use while others use**. In this and other cases,AMPL provides alternateforms. Table 7-1 shows the more common forms to the left, and the alternatives (if any)112 Parameters AND Expressions CHAPTER 7_____Usual alternative type of type ofstyle style operands result_ _____if-then-elselogical, arithmetic arithmeticor ||logical logicalexists foralllogical logicaland &&logical logicalnot(unary)!
9 Logical logical< <= = <> > >= < <= == != > >=arithmetic logicalin not inobject, set logical+ - lessarithmetic arithmeticsum prod min maxarithmetic arithmetic* / div modarithmetic arithmetic+ -(unary) arithmetic arithmetic **arithmetic arithmeticExponentiation andif-then-elseare right-associative; the other operators areleft-associative. The logical operand ofif-then-elseappears afterif, and thearithmetic operands afterthenand (optionally) 7-1:Arithmetic and logical operators, in increasing the right; you can mix them as you like, but your models will be easier to read andunderstand if you are consistent in your way to build arithmetic Expressions is by applying functions to other expres-sions.
10 A function reference consists of a name followed by a parenthesized argument orcomma-separated list of arguments; an arithmetic argument can be any arithmetic expres-sion. Here are a few examples, which compute the minimum, absolute value, and squareroot of their arguments, respectively:min(T,20)abs(sum {i in ORIG} supply[i] - sum {j in DEST} demand[j])sqrt((tan[j]-tan[k]) 2)Table 7-2 lists the built-in arithmetic functions that are typically found in models. Exceptforminandmax, the names of any of these functions may be redefined, but their originalmeanings will become inaccessible. For example, a model may declare a parameternamedtanas in the last example above, but then it cannot also refer to the set functionscardandord, which were described in Chapter 5, also produce anarithmetic result.