How to abort a script and return to the command line?

286 views
Skip to first unread message

Matthias Fripp

unread,
Jul 15, 2011, 12:10:43 PM7/15/11
to AMPL Modeling Language
There may be an easy way to do this, but I haven't found it. Under
certain conditions, I would like my ampl script (called from an
interactive session) to abort and return to the command line, e.g., so
I can use various commands to diagnose the model. I believe that
"exit" or "quit" will quit from ampl, which is not what I want. I
tried "end", but ampl treats that like an "end of file" marker even if
it's within an if statement. This is the closest I've come so far:

solve;
if solve_result = "infeasible" then {
printf "Problem is infeasible; halting."
display 0/0; # exit the script
}

I'm using "display 0/0;" because this doesn't cause an error when I
load the script, but does cause one at run time, interrupting script
execution. Is there anything I could use in place of this that would
be neater?

Matthias

Paul

unread,
Jul 15, 2011, 6:27:37 PM7/15/11
to am...@googlegroups.com
What about encasing the whole script in a for loop with a count of 1, and using break?

for {ctr in 1} {
  # script does stuff
  if <I want to be done> then break;
  # more stuff
  if <I seriously want to be done> then break;
  # still more stuff
}
# End of script file
# one way or another, I'm back at the interactive prompt

Paul

Robert Fourer

unread,
Jul 17, 2011, 7:41:56 AM7/17/11
to am...@googlegroups.com

To define for this purpose a loop that is executed only once, you should write something like

 

   for {ctr in 1..1} {

 

Another way to do the same thing is

 

   repeat {

      ... any commands including "break" ...

   } until 1;

 

Bob Fourer

4...@ampl.com

 

 

From: am...@googlegroups.com [mailto:am...@googlegroups.com]

On Behalf Of Paul
Sent: Friday, July 15, 2011 5:28 PM
To: am...@googlegroups.com
Subject: [AMPL 4815] Re: How to abort a script and return to the command line?

Matthias Fripp

unread,
Jul 17, 2011, 2:16:58 PM7/17/11
to AMPL Modeling Language
Thanks very much for these suggestions. Sometimes I need to break out
of loops within loops as well, which I suppose could be done using the
loop-naming option:

repeat main {
...
for {x in LIST} {
...
solve;
if solve_result = "infeasible" then {
print "Problem is infeasible; halting."
break main;
}
...
}
...
} until 1; # only run the main "loop" once

Matthias
Reply all
Reply to author
Forward
0 new messages