I am using pyomo with gurobi (and other solvers) to solve some optimization programs. Sometimes these programs don't have a solution and throw an error such as

WARNING: Loading a SolverResults object with a warning status into
      - termination condition: infeasible
      - message from solver: Model was proven to be infeasible.
ERROR: evaluating object as numeric value: x[0]
        (object: <class 'pyomo.core.base.var._GeneralVarData'>)
    No value for uninitialized NumericValue object x[0]
ERROR: evaluating object as numeric value: objective
        (object: <class 'pyomo.core.base.objective.SimpleObjective'>)
    No value for uninitialized NumericValue object x[0]


  File "pyomo\core\expr\numvalue.pyx", line 246, in pyomo.core.expr.numvalue.value
  File "pyomo\core\expr\numvalue.pyx", line 233, in pyomo.core.expr.numvalue.value
ValueError: No value for uninitialized NumericValue object x[0]

In this case, I would like to run a simpler program without some of the constraints, so something like

solve program
if program doesn't have a solution:
   solve simpler program 

How could I do this?


1 Answer 1


From Pyomo's website:

from pyomo.opt import SolverStatus, TerminationCondition


results = opt.solve(instance) # Solving a model instance  
instance.load(results) # Loading solution into results object

if (results.solver.status == SolverStatus.ok) and (results.solver.termination_condition == TerminationCondition.optimal):
    # Do something when the solution in optimal and feasible
elif (results.solver.termination_condition == TerminationCondition.infeasible):
    # Do something when model in infeasible
    # Something else is wrong
    print “Solver Status: ”,  result.solver.status

But be careful, because you want to ensure that your simpler model doesn't return an infeasible solution too.

  • $\begingroup$ A solver can (1) find the optimal solution, (2) prove there is no optimal solution (infeasible or unbounded, (3) reach a time limit and find a feasible (possibly suboptimal) solution and (4) reach a time limit and not find any feasible solution. How can we differentiate between 3 and 4? $\endgroup$ Feb 13, 2022 at 1:37
  • $\begingroup$ @Chicoscience Pyomo has different solver statuses and terminations conditions and the combinations of the two, should give you what you want. Check out this link from their website. $\endgroup$
    – EhsanK
    Feb 14, 2022 at 13:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.