optlens

Pyomo model infeasible: how to find which constraints conflict

Pyomo reports termination_condition: infeasible and stops there. This guide tries Pyomo's own tools on a small infeasible model, shows where each one helps and where it doesn't, and then gets the conflicting constraints in a few lines, on HiGHS or Gurobi.

The model

One week of furniture production: two plants, three products, contracts for 120 chairs, 60 tables and 70 desks. The south plant is in maintenance, so it has 150 hours instead of 300.

"""A week's furniture production plan, infeasible: the south plant is down for maintenance (150 hours instead of 300),
and the contracts still need 650 hours. The model behind the guides on optlens.dev."""
import pyomo.environ as pyo

PRODUCTS = {"chairs": 1, "tables": 3, "desks": 5}         # hours per unit
PROFIT = {"chairs": 20, "tables": 70, "desks": 130}
CONTRACT = {"chairs": 120, "tables": 60, "desks": 70}     # units promised to customers this week
MARKET = {"chairs": 300, "tables": 120, "desks": 100}     # most the market takes
HOURS = {"north": 400, "south": 150}                      # south: maintenance week
STORAGE = 400                                             # units the warehouse holds

m = pyo.ConcreteModel("furniture_week")
m.make = pyo.Var(PRODUCTS, HOURS, domain=pyo.NonNegativeReals)
m.make["desks", "south"].setub(0)                         # desks are made only in the north
m.profit = pyo.Objective(expr=sum(PROFIT[p] * m.make[p, k] for p in PRODUCTS for k in HOURS), sense=pyo.maximize)
m.capacity = pyo.Constraint(HOURS, rule=lambda m, k: sum(PRODUCTS[p] * m.make[p, k] for p in PRODUCTS) <= HOURS[k])
m.contract = pyo.Constraint(PRODUCTS, rule=lambda m, p: sum(m.make[p, k] for k in HOURS) >= CONTRACT[p])
m.market = pyo.Constraint(PRODUCTS, rule=lambda m, p: sum(m.make[p, k] for k in HOURS) <= MARKET[p])
m.storage = pyo.Constraint(expr=sum(m.make[p, k] for p in PRODUCTS for k in HOURS) <= STORAGE)

The contracts need 650 hours and the plants have 550, so no plan exists. In this small model you can work that out by hand. Let's see what the tools find.

log_infeasible_constraints: not after an infeasible solve

The first tool most answers point to is pyomo.util.infeasible.log_infeasible_constraints:

import logging
import pyomo.environ as pyo
from pyomo.util.infeasible import log_infeasible_constraints
from furniture_week import m

logging.basicConfig(format="%(message)s")
logging.getLogger("pyomo.util.infeasible").setLevel(logging.INFO)
pyo.SolverFactory("appsi_highs").solve(m, load_solutions=False)
log_infeasible_constraints(m)
CONSTR capacity[north]: evaluation error <?= 400.0
CONSTR capacity[south]: evaluation error <?= 150.0
CONSTR contract[desks]: 70.0 <?= evaluation error
CONSTR contract[chairs]: 120.0 <?= evaluation error
CONSTR contract[tables]: 60.0 <?= evaluation error
CONSTR market[desks]: evaluation error <?= 100.0
CONSTR market[chairs]: evaluation error <?= 300.0
CONSTR market[tables]: evaluation error <?= 120.0
CONSTR storage: evaluation error <?= 400.0

It lists every constraint, and none of them has a value. The function checks the variable values the model holds against each constraint. After an infeasible solve there are no values (the solver has no plan to return), so it can't evaluate anything. It's useful when a solve returns a plan you think breaks a constraint, but it can't explain an infeasible model.

compute_infeasibility_explanation: works with Gurobi and IPOPT, failed with HiGHS

pyomo.contrib.iis.compute_infeasibility_explanation finds a minimal infeasible set (an MIS, the same idea as an IIS) by relaxing constraints and solving many smaller models. Its documentation says to use IPOPT, even for an LP, and with IPOPT it finds the five constraints below. With HiGHS through Pyomo it stopped with an error:

import pyomo.environ as pyo
from pyomo.contrib.iis import compute_infeasibility_explanation
from furniture_week import m

compute_infeasibility_explanation(m, solver=pyo.SolverFactory("appsi_highs"))
RuntimeError: A feasible solution was not found, so no solution can be loaded. If using the appsi.solvers.Highs interface, you can set opt.config.load_solution=False. If using the environ.SolverFactory interface, you can set opt.solve(model, load_solutions = False). Then you can check results.termination_condition and results.best_feasible_objective before loading a solution.

The highs interface stopped with the same kind of error. It's a Pyomo bug, confirmed on issue #4060: both HiGHS interfaces raise when one of the smaller models is infeasible instead of returning a result, and a contributor has offered a fix. With Gurobi it works:

import logging
import pyomo.environ as pyo
from pyomo.contrib.iis import compute_infeasibility_explanation
from furniture_week import m

logging.basicConfig(level=logging.INFO, format="%(message)s")
compute_infeasibility_explanation(m, solver=pyo.SolverFactory("gurobi_direct"))
Model furniture_week may be infeasible. A feasible solution was found with only the following inequality constraints and/or variable bounds relaxed:
    constraint: contract[desks] by 19.999999999999996
Another feasible solution was found with only the following inequality constraints and/or variable bounds relaxed:
    constraint: contract[tables] by 33.333333333333336
Another feasible solution was found with only the following inequality constraints and/or variable bounds relaxed:
    constraint: capacity[north] by 100.00000000000001
Another feasible solution was found with only the following inequality constraints and/or variable bounds relaxed:
    constraint: capacity[south] by 30.0
    constraint: contract[chairs] by 70.0
Computed Minimal Intractable System (MIS)!
Constraints / bounds in MIS:
    constraint: contract[desks]
    constraint: contract[tables]
    constraint: capacity[north]
    constraint: capacity[south]
    constraint: contract[chairs]
Constraints / bounds in guards for stability:

The five constraints at the end are the conflict: the two plants' hours and the three contracts. The lines above them are relaxations it tried along the way.

pyomo.contrib.iis.write_iis calls the solver's own IIS routine instead, and it supports only commercial solvers (Gurobi, CPLEX, Xpress).

With HiGHS: optlens

optlens is an open-source library that finds the IIS on HiGHS (free, the default), SCIP or your own Gurobi. It reads the Pyomo model directly:

import optlens as od
from optlens.session import Session, Version
from furniture_week import m

if __name__ == "__main__":
    s = Session({"v0": Version(od.from_object(m), None, "this week's plan")})
    print(s.compute_iis())
IIS (highs:irreducible): 5 constraints; by family: contract: 3, capacity: 2
capacity[south]: 1 make[chairs,south] +3 make[tables,south] +5 make[desks,south] <= 150
capacity[north]: 1 make[chairs,north] +3 make[tables,north] +5 make[desks,north] <= 400
contract[chairs]: 1 make[chairs,north] +1 make[chairs,south] >= 120
contract[desks]: 1 make[desks,north] +1 make[desks,south] >= 70
contract[tables]: 1 make[tables,north] +1 make[tables,south] >= 60
variable bounds in the IIS:
  make[chairs,north]: [0, inf]
  make[chairs,south]: [0, inf]
  make[tables,north]: [0, inf]
  make[tables,south]: [0, inf]
  make[desks,north]: [0, inf]
  make[desks,south]: [0, 0]

The same five constraints, grouped by family: 3 contracts and 2 capacities. The market and warehouse limits aren't in it, so the problem isn't there. od.load("furniture_week.py") works as well: it runs the script up to its first solve call and reads that model.

From the conflict to a fix

The IIS says the contracts ask for more than the capacity gives. The next question is what to change, and by how much. optlens's fix_menu gives the smallest change in each family and solves the model with it: here, 20 fewer desks, or 100 more hours in the south plant. The general guide walks through it.

Run with optlens 0.0.3, Pyomo 6.10.1, HiGHS (highspy) 1.15.1 and gurobipy 13.0.3.

← Back to optlens