230 likes | 254 Views
Learn how using .dat files and sets in AMPL can streamline adding variables and changing coefficients in linear programming models. Sets can be easily implemented to declare parameters and run models with different data sets, enhancing model flexibility and efficiency.
E N D
AMPL AMathematical Programming Language 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 • It may be difficult to find a coefficient that needs to be changed • Files can get very large • Solution: Use a .dat file
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
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
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
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.
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.
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;
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
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
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.
General Format (Cont) • Finally, any parameters that are independent from the set are declared: param totalAvail := 14;
Put it all together... set CHARS := Homestar StrongMad Trogdor; param: worth avail := Homestar 7 8 StrongMad 2 16 Trogdor 10 3; param totalAvail := 14;
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.
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
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 ;
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.
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!
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.
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
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);