The best place to check is the solver.termination_condition member on the results
object returned by a solver plugin. There are many statuses that can show up here. For a list, see the output from:
from pyomo.opt import TerminationCondition
print(TerminationCondition._keys)
The following code is an example of how to check that a solution is optimal before loading it into the model. It works with Pyomo 4.1 and later.
from pyomo.environ import *
from pyomo.opt import TerminationCondition
model = …
with SolverFactory("cplex") as opt:
results = opt.solve(model, load_solutions=False)
if results.solver.termination_condition != TerminationCondition.optimal:
# something went wrong
print(results.solver)
else:
model.solutions.load_from(results)
You can tweak this to check for more specific cases (e.g., infeasible, unbounded) by comparing against other statuses on the TerminationCondition enum.
Gabe