jump-dev / AmplNLWriter.jl

A Julia interface to AMPL-enabled solvers
http://ampl.com/products/solvers/all-solvers-for-ampl/
MIT License
67 stars 18 forks source link

Error Message with Incorrect Executable Specified #153

Closed ccoffrin closed 2 years ago

ccoffrin commented 2 years ago

This program's output confused me,

import JuMP, AmplNLWriter
model = JuMP.Model(() -> AmplNLWriter.Optimizer("foo"))
JuMP.@variable(model, x)
JuMP.@objective(model, Min, x^2)
JuMP.optimize!(model)
# everything is fine so far!
cost = JuMP.objective_value(model)
# ERROR: Result index of attribute MathOptInterface.ObjectiveValue(1) out of bounds. There are currently 0 solution(s) in the model.

I was expecting at the point of optimize!(model) to get an error if there was some issue calling foo. Instead the issue does not appear to come up until objective_value(model). In a less obvious example, it took some time for me to understand that foo was the root cause.

Thoughts?

odow commented 2 years ago

You should look at termination_status(model) and then raw_status_string(model). We've gone the route of not throwing an error in optimize!.

ccoffrin commented 2 years ago

Ok. So if my workflow was originally,

model = JuMP.Model(() -> AmplNLWriter.Optimizer("foo"))
...
JuMP.optimize!(model)
cost = JuMP.objective_value(model)

I am thinking it should be instead,

model = JuMP.Model(() -> AmplNLWriter.Optimizer("foo"))
...
JuMP.optimize!(model)
if JuMP.primal_status(model) == JuMP.NO_SOLUTION
    error("my helpful message...")
end
cost = JuMP.objective_value(model)
odow commented 2 years ago

Yes. You should never query solutions without check the status. Perhaps something like:

if JuMP.primal_status(model) == JuMP.NO_SOLUTION
    error("No solution found. Solver reports: $(JuMP.raw_status(model))")
end
ccoffrin commented 2 years ago

Got it! Thanks.