From 58996fa1e4da2df12a5e0d6290ff95ed178d2603 Mon Sep 17 00:00:00 2001 From: Michael Bynum Date: Sat, 24 Feb 2024 13:12:58 -0700 Subject: [PATCH] fix division by zero error in linear presolve --- pyomo/repn/plugins/nl_writer.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/pyomo/repn/plugins/nl_writer.py b/pyomo/repn/plugins/nl_writer.py index 3fd97ac06d0..a256cd1b900 100644 --- a/pyomo/repn/plugins/nl_writer.py +++ b/pyomo/repn/plugins/nl_writer.py @@ -1829,15 +1829,6 @@ def _linear_presolve(self, comp_by_linear_var, lcon_by_linear_nnz, var_bounds): if expr_info.linear[x] == 0: nnz -= 1 coef = expr_info.linear.pop(x) - if not nnz: - if abs(expr_info.const) > TOL: - # constraint is trivially infeasible - raise InfeasibleConstraintException( - "model contains a trivially infeasible constraint " - f"{expr_info.const} == {coef}*{var_map[x]}" - ) - # constraint is trivially feasible - eliminated_cons.add(con_id) elif a: expr_info.linear[x] = c * a # replacing _id with x... NNZ is not changing, @@ -1847,6 +1838,15 @@ def _linear_presolve(self, comp_by_linear_var, lcon_by_linear_nnz, var_bounds): continue _old = lcon_by_linear_nnz[old_nnz] if con_id in _old: + if not nnz: + if abs(expr_info.const) > TOL: + # constraint is trivially infeasible + raise InfeasibleConstraintException( + "model contains a trivially infeasible constraint " + f"{expr_info.const} == {coef}*{var_map[x]}" + ) + # constraint is trivially feasible + eliminated_cons.add(con_id) lcon_by_linear_nnz[nnz][con_id] = _old.pop(con_id) # If variables were replaced by the variable that # we are currently eliminating, then we need to update