1
$\begingroup$

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
    model.name="unknown";
      - 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?

$\endgroup$

1 Answer 1

4
$\begingroup$

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
else:
    # 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.

$\endgroup$
2
  • $\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 that you have read and understand our privacy policy and code of conduct.

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