Ayuda Ampl

  • Upload
    pegoro

  • View
    217

  • Download
    0

Embed Size (px)

Citation preview

  • 7/31/2019 Ayuda Ampl

    1/11

    The Students Guide to AMPL

    Amsuss, KatterbauerProduced as part of the course work for applied optimization

    Prof. Arnold NeumairUniversity of Vienna

    2010-02-28

    Contents

    1 Introduction 21.1 What is AMPL? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21.2 Running AMPL files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21.3 First Steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

    1.3.1 The Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21.3.2 The methods of running . . . . . . . . . . . . . . . . . . . . . . . . 3

    2 Writing AMPL programs 42.1 Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

    2.2 Useful commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52.3 Math functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

    3 FAQ 73.1 What happens when I use the expression T..1? . . . . . . . . . . . . . . . 73.2 I have coded set S and param b { S } . How can I let AMPL find

    the minimum of the b[i] for all i in S? . . . . . . . . . . . . . . . . . . . . 73.3 Why does AMPL appear to hang up and does not accept any input or

    returns some output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83.4 Why does the set set S := 20 .. 40 only consist of 3 members? . . . . . . 83.5 Is it possible to model a power set, ie the set of all subsets of a set. . . . 8

    3.6 How do I get access to the computation time (CPU) by AMPL . . . . . . 93.7 How is double summation done? . . . . . . . . . . . . . . . . . . . . . . . 93.8 How do I model a multi-period inventory in AMPL, e.g. the constraints

    Inv[t] = Inv[t-1] + Make[t] -Demand[t] ? . . . . . . . . . . . . . . . . . . . 9

    4 Further reading 10

    1

  • 7/31/2019 Ayuda Ampl

    2/11

    1 Introduction

    1.1 What is AMPL?

    AMPL1 is A Mathematical Programming Language for optimization problems (pro-

    grams).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 field ofoptimization.

    1.2 Running AMPL files

    Local installation AMPL can be downloaded gratis as a student version2, but withlimitations on the number of variables and constraints.

    Online interactive prompt As a direct alternative to local installation, you can use

    AMPL online3

    subject to the same restrictions as the student version.

    NEOS To demonstrate the abilities of the solvers, many solver vendors make theirsolvers available via NEOS4. Using NEOS requires you to have your .mod and .dat filesas well as additional commands ready (no interactive system), but has far superior jobhandling.

    1.3 First Steps

    Using AMPL typically involves up to three sets of commands:

    A .mod file contains all the AMPL commands necessary to define a model; it may ormay not contain constant data.

    A .dat file contains data for a model; splitting model and data is good practice andeases solving different problems sharing a single model. It is written in a subset ofthe AMPL language.

    Output commands are strictly speaking not a part of the model; they instruct theAMPL interpreter to actially do something, e. g. to solve the problem or to displaythe solution.

    1.3.1 The Files

    As a first example, create three files5:

    1http://www.ampl.com/2http://www.ampl.com/DOWNLOADS/index.html3http://ampl.com/cgi-bin/ampl/amplcgi4http://www-neos.mcs.anl.gov/5For some applications, the .dat file is not required and its contents are input at an interactive prompt.

    2

    http://www.ampl.com/http://www.ampl.com/DOWNLOADS/index.htmlhttp://ampl.com/cgi-bin/ampl/amplcgihttp://www-neos.mcs.anl.gov/http://www-neos.mcs.anl.gov/http://ampl.com/cgi-bin/ampl/amplcgihttp://www.ampl.com/DOWNLOADS/index.htmlhttp://www.ampl.com/
  • 7/31/2019 Ayuda Ampl

    3/11

    Listing 1: example1.mod

    # e x p o n e n tparam a ;# bounds

    param b ;param c ;

    v a r x ;

    # t he t a r ge t functionm i ni m i ze f : x (a)+ si n ( x ) ;

    # c o n s t r ai n t ss u b je c t t o c1 : x>=b ;s u b je c t t o c2 : x a + x

  • 7/31/2019 Ayuda Ampl

    4/11

    NEOS From the NEOS solver list, select an appropriate solvers (MINOS makes areasonable default) AMPL Input hyperlink. Choose the .mod, .dat and .run file forModel 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 al s o l u t i o n f o un d .20 i t e r a t i o n s , o b j e c t i ve 0.9336425135N on li n e v a l s : o b j = 6 4 , g ra d = 6 3 , c o n s t r s = 6 4 , J ac = 6 3 .x = 4 . 5 2 4 9 4

    f = 0.933643

    2 Writing AMPL programs

    2.1 StructureMost of the AMPL language is purely declarative. The remaining imperative commandsare typically collected in the .run file (e. g., the instructions to solve the given problemand display the results). The .mod and .dat files stay purely declarative; as a result,sequence is irrelevant as long as parameters and variables are not used before they aredeclared. (However, it is common to declare a parameter, use it, and finally (in the .datfile, assign it a value.)

    A typical AMPL program consists of several of this list:

    1. parameter declarations for the shape of the problem (e. g., numbers of items);these correspond to constants or arguments in imperative programming

    p aram N ; # n um ber o f n o d esp aram E ; # nu mb er o f e d g e s

    l e t N := 5 ;l e t E := 1 0;

    2. set declarations (which can later be used as indices or values); these correspond toenums in imperative programming

    set ENDS := { s h a f t , t i p } ; # t h e a rr ow m et ap ho r f or 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 files

    param edge {e i n 1 . . E , i i n ENDS} ;p aram e d g e c o s t {e i n 1 . .E} ; # co st o f t ra ns po rt v ia edge e

    4

    http://neos.mcs.anl.gov/neos/solvers/index.htmlhttp://neos.mcs.anl.gov/neos/solvers/index.html
  • 7/31/2019 Ayuda Ampl

    5/11

    l e t e dg e [ 1 , s h a f t ] := 1 ;l e t ed ge [ 1 , t i p ] := 3 ;l e t e d ge co st [ 1 ] := 1 . 0 ;

    4. variable declarations; the solvers will try to find good values for the variables

    v a r u s e e dg e{e i n 1 . .E} bina ry ;v a r x ;var f >= 0 , = 1 ;

    6. an objective function

    m i ni m iz e c o s t : sum{e i n 1 . .E} us e [ e ] edg eco st [ e ] ;

    For many programs, the command files structure can be kept very simple:

    s o l v e ;d i s p l a y v ar na me , v a r ; # show a l l v e r i a b l e s

    2.2 Useful commands

    Table 1 lists the most common syntax elements in AMPL.

    2.3 Math functions

    As a user of AMPL wants to do optimization and therefore math, they need sometimesaccess to some mathematical functions, like logarithm, absolute values, sine and cosineand that is provided by AMPL.

    Therefore, a good reference to AMPL and the mathematical functions can be foundat AMPL Help Manual

    Table 2 lists the names of mathematical functions in AMPL.

    5

    http://archive.ite.journal.informs.org/Vol7No1/LeeRaffensperger/scite/amplhelp.htmlhttp://archive.ite.journal.informs.org/Vol7No1/LeeRaffensperger/scite/amplhelp.html
  • 7/31/2019 Ayuda Ampl

    6/11

    # defines the start of a commentparam defines a parametervar defines a variable, essential for stating

    which elements vary; ends every statement in AMPLmaximize/minimize starts an objective, an objective looks in

    the following way: maximize/minimizea name : objective statement

    subject to starts a constrained, ie subject to a name: constrained statement

    mod ending of the model filedat ending of the data filesolve command to start the optimization pro-

    cess of the modeldisplay command that displays the value of a pa-

    rameter, or variablelet changes a parameter value e.g. let bmin

    := 12:= command for allocating a value to a vari-

    able, e.g. param b := 2;in inclusion or counting element, e.g. param

    p {i in P}

  • 7/31/2019 Ayuda Ampl

    7/11

    Command Functionabs(x) Returns |x|ceil(x) Rounds x up to the next largest integercos(x) Returns the cosine of x (x in radians)

    exp(x) Returns ex

    floor(x) Rounds x down to the next smallest integerlog(x) Natural logarithm of x

    x mod y The remainder of x in the coset ymax(x,y,z,...) Returns the largest of x; y; z;min(x,y,z,...) Returns the smallest of x; y; z;

    sin(x) Returns the sine of x (x in radians)sqrt(x) Returns

    x for x 0

    tan(x) Returns the sine of x (x in radians)

    Table 2: Some mathematical functions in AMPL, quoted from An AMPL Tu-torial, 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 Tgreater than one, T . . . 1 is the empty set. So to tell AMPL that it shall go from T toone, write T . . . 1 by 1.

    3.2 I have coded set S and param b { S } . How can I let AMPL findthe minimum of the b[i] for all i in S?

    Generally, AMPL does not posses an operator that extracts the minimum argumentfrom a parameter. Nevertheless, the operator arg min can be defined on ones own.

    Define 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 minimumof 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 first element of

    b_argmin

    ensures that one extracts a member of S that minimizes b[i].

    7

    http://www.uwyo.edu/sboyles/teaching/ce4920/ampl_tutorial.pdfhttp://www.uwyo.edu/sboyles/teaching/ce4920/ampl_tutorial.pdfhttp://www.uwyo.edu/sboyles/teaching/ce4920/ampl_tutorial.pdfhttp://www.uwyo.edu/sboyles/teaching/ce4920/ampl_tutorial.pdfhttp://www.uwyo.edu/sboyles/teaching/ce4920/ampl_tutorial.pdf
  • 7/31/2019 Ayuda Ampl

    8/11

    3.3 Why does AMPL appear to hang up and does not accept any input orreturns some output

    If you enter solve, write or expand then it might occur that AMPL seems to hang beforeyou 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 somefurther information about that at Memory.

    If AMPL appears to hang after one or more lines of a solver, then it is likely to bethe case that it takes the solver longer to perform an intermediate step. As the outputis solver dependent, you might have a look at the solver documentation.

    3.4 Why does the set set S := 20 .. 40 only consist of 3 members?

    The reason is as follows. In the model, you have entered set S while in your .dat fileyou have declared set S := 20 .. 40. Set expressions are not recognized in AMPls datamode and therefore AMPL recognizes 20 .. 70 as a space-delimited list of members of S,

    with the result that is has found three members: the numbers 20 and 40, and the string...

    For defining 20 .. 40 in your data file, you first declare S in your model by the following

    param begin;

    param end > begin;

    set S := begin .. end;

    Then state in the data file:

    param begin := 50;

    param end := 70;

    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 modelcomponents by objects (numbers and character strings) but not by sets of objects.

    Though, you might obtain your desired result by constructing a numbered list ofsubsets.

    ExampleIndex of all subsets of the set of the first n natural numbers including 0. You declare:

    param n integer > 0;

    s e t 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 .. 2n1 is in one-to-one correspondence to the set of all subsets of S.Theindexing follows then in that way that the indexed collection of sets POW is such thatPOW[k] is the k-th distinct subset of S.

    Similarily,

    8

    http://www.ampl.com/FAQ/index.html#WhatshouldIdoifIsuspectthatthereisinsufficientmemoryformyAMPLsessionhttp://www.ampl.com/solvers.htmlhttp://www.ampl.com/solvers.htmlhttp://www.ampl.com/FAQ/index.html#WhatshouldIdoifIsuspectthatthereisinsufficientmemoryformyAMPLsession
  • 7/31/2019 Ayuda Ampl

    9/11

    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 isabout the travelling salesman problem, see TSP.

    3.6 How do I get access to the computation time (CPU) by AMPL

    This is answered in the AMPL FAQ, in particular item 6.7.

    3.7 How is double summation done?

    Ampl provides an easy way of performing a double summation. Ifj is the j-th class andi is the i-th school, then the number of students in all schools is then computed by

    sum{ i in classes, j in schools} x[i,j]

    where x[i,j] denotes the number of students in the i-th class and j-th school.

    3.8 How do I model a multi-period inventory in AMPL, e.g. theconstraints Inv[t] = Inv[t-1] + Make[t] -Demand[t] ?

    An example of a multi-period model is the following Inventory model of a producerof cars and trucks, taken from the website http://www.student.math.uwaterloo.ca/

    ~co370/faq.html.The .mod file 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; # tons produced

    var Inv {products,0..T} >= 0; # tons inventoried

    maximize total_profit:

    9

    http://www.ampl.com/FAQ/tsp.modhttp://www.ampl.com/FAQ/http://www.ampl.com/FAQ/#WheredoIfindtheamountofcomputation(CPU)timetakenbyAMPLorbyasolverhttp://www.student.math.uwaterloo.ca/~co370/faq.htmlhttp://www.student.math.uwaterloo.ca/~co370/faq.htmlhttp://www.student.math.uwaterloo.ca/~co370/faq.htmlhttp://www.student.math.uwaterloo.ca/~co370/faq.htmlhttp://%20http//www.student.math.uwaterloo.ca/~co370/ampl/inventory.modhttp://%20http//www.student.math.uwaterloo.ca/~co370/ampl/inventory.modhttp://www.student.math.uwaterloo.ca/~co370/faq.htmlhttp://www.student.math.uwaterloo.ca/~co370/faq.htmlhttp://www.ampl.com/FAQ/#WheredoIfindtheamountofcomputation(CPU)timetakenbyAMPLorbyasolverhttp://www.ampl.com/FAQ/http://www.ampl.com/FAQ/tsp.mod
  • 7/31/2019 Ayuda Ampl

    10/11

    sum {t in 1..T,p in products}(revenue[p]*demand[p,t]

    - prodcost[p,t]*Make[p,t] - invcost[p]*Inv[p,t]);

    subject to initial {p in products}: Inv[p,0] = inv0[p];

    # Initial inventory must equal given value

    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 file 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 := cars 10 trucks 0 ;

    param invcost := cars 2.5 trucks 3 ;

    param revenue := cars 25 trucks 35 ;

    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, consideringthe edit dates, rather stalled), and with some accessibility issues (broken inter-nal hyperlinks that can be circumvented by removing whitespace from the URLs),

    10

    http://www.student.math.uwaterloo.ca/~co370/ampl/inventory.dathttp://escwiki.esc.auckland.ac.nz/ampl/IntroductionToAMPLhttp://escwiki.esc.auckland.ac.nz/ampl/IntroductionToAMPLhttp://escwiki.esc.auckland.ac.nz/ampl/IntroductionToAMPLhttp://escwiki.esc.auckland.ac.nz/ampl/IntroductionToAMPLhttp://escwiki.esc.auckland.ac.nz/ampl/IntroductionToAMPLhttp://www.student.math.uwaterloo.ca/~co370/ampl/inventory.dat
  • 7/31/2019 Ayuda Ampl

    11/11

    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/ce492 0/ampl_tutorial.pdf

    Frequently asked questions about AMPL, CO370 , http://www.student.math.uwaterloo.ca/~co370/faq.html

    11

    http://www.or.unc.edu/~pataki/or41/ampl/amplhandout.pdfhttp://www.or.unc.edu/~pataki/or41/ampl/amplhandout.pdfhttp://www.or.unc.edu/~pataki/or41/ampl/amplhandout.pdfhttp://www.or.unc.edu/~pataki/or41/ampl/amplhandout.pdfhttp://www.uwyo.edu/sboyles/teaching/ce4920/ampl_tutorial.pdfhttp://www.uwyo.edu/sboyles/teaching/ce4920/ampl_tutorial.pdfhttp://www.student.math.uwaterloo.ca/~co370/faq.htmlhttp://www.student.math.uwaterloo.ca/~co370/faq.htmlhttp://www.student.math.uwaterloo.ca/~co370/faq.htmlhttp://www.student.math.uwaterloo.ca/~co370/faq.htmlhttp://www.student.math.uwaterloo.ca/~co370/faq.htmlhttp://www.student.math.uwaterloo.ca/~co370/faq.htmlhttp://www.uwyo.edu/sboyles/teaching/ce4920/ampl_tutorial.pdfhttp://www.uwyo.edu/sboyles/teaching/ce4920/ampl_tutorial.pdfhttp://www.or.unc.edu/~pataki/or41/ampl/amplhandout.pdfhttp://www.or.unc.edu/~pataki/or41/ampl/amplhandout.pdf