Script with loops

19 views
Skip to first unread message

julc...@gmail.com

unread,
Jan 13, 2017, 9:30:22 AM1/13/17
to AMPL Modeling Language
Hello

I have a script that has multiple solves.  After each solve I need to examine the results and reset some parameters depending on the results.  At the moment my script has the following structure:

objective #1
solve
examine results and reset parameters 
objective #2
solve 
examine results and reset parameters
objective #3 
solve
examine results and reset parameters 
etc. 

 The "examine results and reset parameters" is performed over several rows and is identical (after each solve).

Rather than duplicate the same rows for "examine results and reset parameters" is it possible to include these row for "examine results and reset parameters" within a named loop?  So, for example, after performing "examine results and reset parameters" after the 2nd solve the next step taken is to change the objective to "objective 3"?
 
Thanking you, Julius

Robert Fourer

unread,
Jan 15, 2017, 4:01:59 PM1/15/17
to am...@googlegroups.com
Many solvers have an "objno" option that lets you select an objective function. So you can write, as an example,

for {i in 1.._nobjs} {
option gurobi_options ('objno=' & i);
solve;
<<< examine results and reset parameters >>>
}

The numbering of the objectives corresponds to the order in which they were defined in the model; _nobjs is a built-in parameter that gives the number of objectives that have been defined. But if you have an indexed collection of objectives, defined by some statement like

minimize TotalCost {i in 1..m}: <<< expression for objective >>> ;

then instead you can specify the objective by name in the AMPL loop:

for {i in 1..m} {
objective TotalCost[i];
solve;
<<< examine results and reset parameters >>>
}

Bob Fourer
am...@googlegroups.com

=======

julc...@gmail.com

unread,
Jan 16, 2017, 12:02:19 PM1/16/17
to AMPL Modeling Language, 4...@ampl.com
That's really interesting and helpful.

Many thanks Bob
Reply all
Reply to author
Forward
0 new messages