Beruflich Dokumente
Kultur Dokumente
Amsss, Katterbauer u Produced as part of the course work for applied optimization Prof. Arnold Neumair University of Vienna 2010-02-28
Contents
1 Introduction 1.1 What is AMPL? . . . 1.2 Running AMPL les . 1.3 First Steps . . . . . . 1.3.1 The Files . . . 1.3.2 The methods of . . . . . . . . . . . . . . . . . . . . running . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 2 2 2 2 3 4 4 5 5 7 7 7 8 8 8 9 9 9 10
2 Writing AMPL programs 2.1 Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.2 Useful commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.3 Math functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 FAQ 3.1 What happens when I use the expression T..1? . . . . . . . . . . . . . . 3.2 I have coded set S and param b { S } . How can I let AMPL nd the minimum of the b[i] for all i in S? . . . . . . . . . . . . . . . . . . . 3.3 Why does AMPL appear to hang up and does not accept any input or returns some output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.4 Why does the set set S := 20 .. 40 only consist of 3 members? . . . . . 3.5 Is it possible to model a power set, ie the set of all subsets of a set. . . 3.6 How do I get access to the computation time (CPU) by AMPL . . . . . 3.7 How is double summation done? . . . . . . . . . . . . . . . . . . . . . . 3.8 How do I model a multi-period inventory in AMPL, e.g. the constraints Inv[t] = Inv[t-1] + Make[t] -Demand[t] ? . . . . . . . . . . . . . . . . . . 4 Further reading
. . . . . . . .
1 Introduction
1.1 What is AMPL?
AMPL1 is A Mathematical Programming Language for optimization problems (programs). AMPL is also the name of a piece of software that can parse that language and feed it to a wide variety of solvers, most of which are specialized in a particular eld of optimization.
http://www.ampl.com/ http://www.ampl.com/DOWNLOADS/index.html 3 http://ampl.com/cgi-bin/ampl/amplcgi 4 http://www-neos.mcs.anl.gov/ 5 For some applications, the .dat le is not required and its contents are input at an interactive prompt.
Listing 1: example1.mod
# exponent param a ; # bounds param b ; param c ; var x ; # t h e t a r g e t function minimize f : x(a)+ sin ( x ) ; # constraints s u b j e c t t o c1 : x>=b ; s u b j e c t t o c2 : x a + x <= c ;
Listing 2: example1.dat
l e t a := 2 ; l e t b := 3 ; l e t c := 2 5 ;
Listing 3: example1.run
solve ; display x ; display f ;
(Lines starting with a hash sign (#) are comments.) 1.3.2 The methods of running Local installation First, download on the ampl page http://www.ampl.com/DOWNLOADS the le amplcml.zip. Unzip it and it will return a folder amplcml. Open the folder and the folder will contain the program (ampl.exe) and the scrolling-window utility (sw.exe). To run the program, click on sw.exe and it will open a window. Then enter sw: ampl ampl: and you can procede with the normal ampl code that is described in this guide. Online interactive prompt Open the online interactive prompt; you will be presented an AMPL commands and a Model and data input area. In the commands area, paste the contents of the .run le. In the model and data section, paste the contents of your .mod le, followed by a line containing data;, followed by the contents of your .dat le.6 . Then, click solve.
6
For simple problems, this layout enables you to keep model and data in a single le, yet separate.
NEOS From the NEOS solver list, select an appropriate solvers (MINOS makes a reasonable default) AMPL Input hyperlink. Choose the .mod, .dat and .run le for Model File, Data File and Commands File, respectively. Then, click submit. All these methods should yield the same (or similar, if versions dont match) results:
MINOS 5 . 5 1 : o p t i m a l s o l u t i o n found . 20 i t e r a t i o n s , o b j e c t i v e 0.9336425135 Nonlin e v a l s : o b j = 6 4 , grad = 6 3 , c o n s t r s = 6 4 , Jac = 6 3 . x = 4.52494 f = 0.933643
2. set declarations (which can later be used as indices or values); these correspond to enums in imperative programming
set ENDS := { s h a f t , t i p } ; # t h e arrow metaphor f o r d i g r a p h s
3. data (which is also a parameter, but often indexed by other parameters or sets); in imperative programming, this is often loaded from les
param edge { e i n 1 . . E , i i n ENDS} ; param e d g e c o s t { e i n 1 . . E } ; # c o s t o f t r a n s p o r t v i a edge e
l e t edge [ 1 , s h a f t ] := 1 ; l e t edge [ 1 , t i p ] := 3 ; l e t e d g e c o s t [ 1 ] := 1 . 0 ;
4. variable declarations; the solvers will try to nd good values for the variables
var u s e e d g e { e i n 1 . . E} b i n a r y ; var x ; var f >= 0 , <= 1 ;
Assigning initial values to variables in the data le can be necessary to avoid singularities resulting from all variables being zero:
l e t x := 4 2 ;
6. an objective function
minimize c o s t : sum{ e i n 1 . . E} u s e [ e ] e d g e c o s t [ e ] ;
For many programs, the command les structure can be kept very simple:
solve ; display varname , v a r ; # show a l l v e r i a b l e s
subject to mod dat solve display let := in <= sum 1..N reset
set
denes the start of a comment denes a parameter denes a variable, essential for stating which elements vary ends every statement in AMPL starts an objective, an objective looks in the following way: maximize/minimize a name : objective statement starts a constrained, ie subject to a name : constrained statement ending of the model le ending of the data le command to start the optimization process of the model command that displays the value of a parameter, or variable changes a parameter value e.g. let bmin := 12 command for allocating a value to a variable, e.g. param b := 2; inclusion or counting element, e.g. param p {i in P } inequality declarator sums up the terms comming after, e.g. sumi in 1..N displays the counting from 1 to N deletes all former AMPL commands and user is able to enter new or the same model without something being overwritten declaration of elements, can also store names, summation adjusted to the number of elements in the set
Command abs(x) ceil(x) cos(x) exp(x) oor(x) log(x) x mod y max(x, y, z, ...) min(x, y, z, ...) sin(x) sqrt(x) tan(x)
Function Returns |x| Rounds x up to the next largest integer Returns the cosine of x (x in radians) Returns ex Rounds x down to the next smallest integer Natural logarithm of x The remainder of x in the coset y Returns the largest of x; y; z; Returns the smallest ofx; y; z; Returns the sine of x (x in radians) Returns x for x 0 Returns the sine of x (x in radians)
Table 2: Some mathematical functions in AMPL, quoted from An AMPL Tutorial, CE 4920, http://www.uwyo.edu/sboyles/teaching/ce4920/ampl_ tutorial.pdf.
3 FAQ
3.1 What happens when I use the expression T..1?
Generally speaking, AMPL interprets a . . . b as {a, a + 1, a + 2, . . . , b 1, b}. So for T greater than one, T . . . 1 is the empty set. So to tell AMPL that it shall go from T to one, write T . . . 1 by 1.
3.2 I have coded set S and param b { S } . How can I let AMPL nd the minimum of the b[i] for all i in S?
Generally, AMPL does not posses an operator that extracts the minimum argument from a parameter. Nevertheless, the operator arg min can be dened on ones own. Dene the arg min by {s in S: b[s] = min {i in S} b[i]} This returns a subset of S, that contains all elements of S, which take on the minimum of the b[i] values. However by order the elements in the above set via set b_argmin ordered := {s in S: b[s] = min {i in S} b[i]};] Then the rst element of b_argmin ensures that one extracts a member of S that minimizes b[i].
3.3 Why does AMPL appear to hang up and does not accept any input or returns some output
If you enter solve, write or expand then it might occur that AMPL seems to hang before you obtain some output. In that case, it might be advisable to set option times 1, gentimes 1, in order to get more output recording AMPLs progress. You get some further information about that at Memory. If AMPL appears to hang after one or more lines of a solver, then it is likely to be the case that it takes the solver longer to perform an intermediate step. As the output is solver dependent, you might have a look at the solver documentation.
3.5 Is it possible to model a power set, ie the set of all subsets of a set.
You cannot model a power set directly in AMPL, because AMPL can index model components by objects (numbers and character strings) but not by sets of objects. Though, you might obtain your desired result by constructing a numbered list of subsets. Example Index of all subsets of the set of the rst n natural numbers including 0. You declare: param n integer > 0; set S := 0 .. n - 1; set SS := 0 .. 2**n - 1; set POW {k in SS} := {i in S: (k div 2**i) mod 2 = 1}; The number of subsets of the Set S is therefore 2n . Using a simple bijection argument, the set SS := 0 .. 2n 1 is in one-to-one correspondence to the set of all subsets of S.The indexing follows then in that way that the indexed collection of sets POW is such that POW[k] is the k-th distinct subset of S. Similarily,
set S ordered; param n := card {S}; set SS := 0 .. (2**n - 1); set POW {k in SS} := {i in S: (k div 2**(ord(i)-1)) mod 2 = 1}; In either way, one indexes of the set of all subsets of S. An interesting example is about the travelling salesman problem, see TSP.
3.8 How do I model a multi-period inventory in AMPL, e.g. the constraints Inv[t] = Inv[t-1] + Make[t] -Demand[t] ?
An example of a multi-period model is the following Inventory model of a producer of cars and trucks, taken from the website http://www.student.math.uwaterloo.ca/ ~co370/faq.html. The .mod le CTInv.mod is given by set products; # set of products param T > 0; # number of weeks param demand {products,1..T} ; # demand on product each week param inv0 {products}; # amount of initial inventory param prodcost {products,1..T}; # cost per ton produced # of each product in each week param invcost {products}; # carrying cost/ton of inventory param revenue {products}; var Make {products,1..T} >= 0; var Inv {products,0..T} >= 0; # tons produced # tons inventoried
maximize
total_profit:
Inv[p,0] = inv0[p];
subject to balance {t in 1..T, p in products}: Make[p,t] + Inv[p,t-1] = demand[p,t] + Inv[p,t]; # Tons produced and taken from inventory # must equal tons sold and put into inventory
The CTInv.dat le is given by: set products := cars, trucks; param T := 4; # param avail := 1 40 2 40 3 32 4 40 ; # param rate := cars 200 trucks 140 ; param inv0 := param invcost param revenue cars := := 10 trucks 0 ; 3 ; 35 ;
trucks trucks
param prodcost: 1 2 3 4 := cars 10 15 8 12 truck 15 20 15 15 ; param demand: 1 2 3 4 := cars 6000 6000 4000 6500 truck 4000 2500 3500 4200 ;
4 Further reading
Optimisation with AMPL, University of Auckland DES, http://escwiki.esc. auckland.ac.nz/ampl/IntroductionToAMPL: work-in-progress (or, considering the edit dates, rather stalled), and with some accessibility issues (broken internal hyperlinks that can be circumvented by removing whitespace from the URLs),
10
but very good for newcomers AMPL and CPLEX tutorial, Gbor Pataki, http://www.or.unc.edu/~pataki/ or41/ampl/amplhandout.pdf: A tutorial jumping right into examples. An AMPL Tutorial, CE 4920, http://www.uwyo.edu/sboyles/teaching/ce4920/ ampl_tutorial.pdf Frequently asked questions about AMPL, CO370 , http://www.student.math. uwaterloo.ca/~co370/faq.html
11