1 / 23

AMPL

AMPL. A M athematical P rogramming L anguage Presentation #2. Outline. Reasons for using two files Sets Format of .dat files Example Logical Operators. Limitations. In a LP with many constraints, it’s a hassle to add new variables

bernadetter
Download Presentation

AMPL

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. AMPL AMathematical Programming Language Presentation #2

  2. Outline • Reasons for using two files • Sets • Format of .dat files • Example • Logical Operators

  3. Limitations • In a LP with many constraints, it’s a hassle to add new variables • It may be difficult to find a coefficient that needs to be changed • Files can get very large • Solution: Use a .dat file

  4. Sets

  5. Advantages • It’s easy to add extra parameters • It’s more natural, notation-wise, in that you can use summation notation • It allows the data to be changed easily, or it even allows the model to be run with different sets of data

  6. Definition • A set is defined at the beginning of the model (.mod) file as: set SETNAME; • Nothing else needs to be added; the size and the members of the set are declared in a different place

  7. Implementation • Now we want to use the set in the model. • To declare a parameter that takes on a value for each member of the set: param PARAMETER {i in SETNAME}; • Or, even better: param PARAMETER {SETNAME}; • These parameters can then be accessed just like an array in other programming languages

  8. Summations • While writing an objective function, it is more compact to write it as a sum. • If the set is declared above, then the syntax is very intuitive: param cost {SETNAME}; var MAKE {SETNAME}; maximize COST: sum {i in SETNAME} cost [i]*MAKE [i]; • Notice that there are no parenthesis around the objective function; the * operator has higher precedence than the sum operator.

  9. Constraints • If you need to base the number of constraints on the elements of the set, say if there is an upper bound on each variable (VAR): subject to UPPERBOUND {i in SETNAME }: VAR [i] <= UB [i]; • This will create |SETNAME | constraints.

  10. A very simple example set CHARS; param worth {CHARS} >=0; param avail {CHARS} >=0; param totalAvail >=0; var hours {i in CHARS} >=0, <= avail[i]; maximize WORK: sum {i in CHARS} hours[i]*worth[i]; subject to AVAILIABLE: sum {i in CHARS} hours[i] <= totalAvail;

  11. No Numbers?! • You may have noticed that the previous slide had no numbers. • So how does the solver know which values we intend for the parameters? • We have to create a data file, a file with extension .dat

  12. General Format • A lot of the data in a data file is presented in tables. • The naming convention has to be identical, down to case because AMPL is case sensitive. • Start a data file by naming all sets used in the model. set CHARS := Homestar StrongMad Trogdor; • Note that there are no commas, and it ends with a semicolon

  13. General Format (Cont) • Then any parameters need to be defined. param: worth avail := Homestar 7 8 StrongMad 2 16 Trogdor 10 3; • The parameter names go along the top and the set elements are along the side.

  14. General Format (Cont) • Finally, any parameters that are independent from the set are declared: param totalAvail := 14;

  15. Put it all together... set CHARS := Homestar StrongMad Trogdor; param: worth avail := Homestar 7 8 StrongMad 2 16 Trogdor 10 3; param totalAvail := 14;

  16. Into AMPL • We know how to input model files into AMPL. How do we do data files? • The same way! model Homestar.mod; data Homestar.dat; • If there is an error in the data file, the compiler will let you know.

  17. Output • After executing our simple example, AMPL outputs: ampl: model Homestar.mod; ampl: data Homestar.dat; ampl: solve; MINOS 5.5: optimal solution found. 3 iterations, objective 92

  18. Display • It is easier to display the variable values when a set is declared. • Instead of typing display hours[Homestar] hours[StrongMad] hours[Trogdor]; • We can just type ampl: display hours; hours [*] := Homestar 8 StrongMad 3 Trogdor 3 ;

  19. The power of sets • Now, say we want to add one character to our previous example. • Before sets, this would have been very difficult. • Now, it’s a breeze.

  20. Adding Marzipan • All we need to do is change the .dat file: set CHARS := Homestar StrongMad Trogdor Marzipan; param: worth avail := Homestar 7 8 StrongMad 2 16 Trogdor 10 3 Marzipan 9 4; • That’s it!

  21. Output • After running the same model with the new data, we get: MINOS 5.5: optimal solution found. 3 iterations, objective 115 ampl: display hours; hours [*] := Homestar 7 Marzipan 4 StrongMad 0 Trogdor 3 ; • We could have kept the same session of AMPL open, and used the command reset data; This would allow us to use the same model with different data.

  22. Logical Operators • Simple logical statements are common in problems • AMPL allows us to use simple logical statements such as: • Binary variables (true or false) • IF-THEN • IF-THEN-ELSE

  23. Logical Example • Say that all of the variables have a fixed upper bound, but one of the variables has twice that. • Then you can use the IF-THEN-ELSE to introduce this constraint. subject to UB {i in SETNAME }: variable[i] <= 10*(if i=1 then 2 else 1);

More Related