1

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!

matrj
  • 21
  • 4

1 Answers1

0

Instead of an output handler, the easiest way to obtain the output from the solver would be to use the following:

solve_output = ampl.get_output("solve;")
if "EXIT: Optimal Solution Found" in solve_output:
     x = ampl.get_variable("x").get_values()
else:
     x = None

However, you can also achieve what you need by simply checking the solve_result with ampl.get_value("solve_result") as follows:

ampl.solve()
solve_result = ampl.get_value("solve_result")
if solve_result == "solved":
    x = ampl.get_variable("x").get_values()
else:
    x = None

This later option would work for other solvers, while the first one only applied to Ipopt since it checks for specific output.

fdabrandao
  • 81
  • 4