ampl / amplpy

Python API for AMPL

Home Page:https://amplpy.ampl.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to get sentence from lpopt outputs with output handler?

rfigueiredo2022 opened this issue · comments

Hi,

I am new in AMPL and Python and I am using amplpy to run an AMPL model, using lpopt solver. I need to get the output result of the solver in a way that I can act over it, i.e. I need to know if the optimal solution was found to decide if I can use the variables' final values or not; for lpopt, in the middle of all other outputs, it prints automatically "EXIT: Optimal Solution Found". I tried using the get_output_handler and OutputHandler classes, but I could not find a proper example on how it works and how to actually implement it. My idea is to get the "EXIT: ..." sentence and decide whether to save the variables' results or not, conceptually, it would look something like this:

'''
output_sentence = ampl.OutputHandler(...)
if output_sentence == "EXIT: Optimal Solution Found":
x = ampl.get_variable("x").get_values()
else:
x = nan
'''
Thank you in advance!

Hi again! I made it by using Objective class and 'result':

'''
print("Optim. result is:", ampl.get_objective("X").result())
'''

Now, I am trying to understand all the options possible for this output (I am aware of 'limit' and 'solved', but not sure where to find all the other options (if any).
Can you help me?

Thanks again in advance!

You'll find the options listed in section 14.2 of the AMPL book, in the discussion of the built-in parameter solve_result. Under "Solver statuses of objectives and problems" later in the section, it is explained that solve_result and the suffix .result are equivalent.

In terms of capturing output, you can use ampl.get_output('solve;') to get the output of any AMPL statement as a string (it is a lot easier than implementing an OutputHandler). However, in this case since you are looking for the solve status only, you can use ampl.get_value('solve_result') or ampl.get_value('solve_result_num') in order to check the solver status.