From d3745e6126fcf9c179289327bd66d73db386d363 Mon Sep 17 00:00:00 2001 From: Joao-Dionisio Date: Tue, 8 Sep 2026 15:34:15 +0100 Subject: [PATCH 1/3] Fix lotsizing_lazy example: lock variables in conslock so presolve cannot aggregate them away (#1228) --- CHANGELOG.md | 1 + examples/finished/lotsizing_lazy.py | 15 ++++++++++++--- tests/test_conshdlr.py | 12 ++++++++++++ 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ae37c3270..90196379a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ - Added type annotations to most methods on the `Model` class ### Fixed - Fixed Cython 3.3 compatibility (#1248) +- Fixed `lotsizing_lazy` example: the constraint handler now locks its variables in `conslock`, so presolve can no longer aggregate them away and make the lazily added (l,S) cuts cut off the optimum (#1228) - Made `test_markDoNotAggrVar_and_getStatus` robust to SCIP presolve changes by discovering the aggregated/multi-aggregated variables instead of hardcoding them ### Changed - Move magic methods (`__radd__`, `__sub__`, `__rsub__`, `__rmul__`, `__richcmp__`, `__neg__`, and `__rtruediv__`) to `ExprLike` base class (#1204) diff --git a/examples/finished/lotsizing_lazy.py b/examples/finished/lotsizing_lazy.py index d39101f15..57c3ddbfe 100644 --- a/examples/finished/lotsizing_lazy.py +++ b/examples/finished/lotsizing_lazy.py @@ -33,7 +33,8 @@ def addcut(self, checkonly, sol): if checkonly: return True else: - # add cutting plane constraint + # add the (l,S) inequality sum_{t in S} x[t] <= sum_{t in S} D[t,l] y[t] + I[l] + # with I[l] eliminated through the flow conservation constraints (I[0] = 0) self.model.addCons(quicksum([x[t] for t in L]) + \ quicksum(D[t, ell] * y[t] for t in S) >= D[1, ell], removable=True) @@ -53,7 +54,16 @@ def consenfolp(self, constraints, nusefulconss, solinfeasible): return {"result": SCIP_RESULT.FEASIBLE} def conslock(self, constraint, locktype, nlockspos, nlocksneg): - pass + # With needscons=False, SCIP calls this once with constraint=None and expects the + # handler to lock every variable it enforces. The (l,S) inequalities only have + # positive coefficients and are ">=" constraints, so they can only be violated by + # rounding x or y down. Without these locks, presolve is free to apply dual + # reductions (e.g. aggregating the relaxed y[t] away), and the cuts added later + # would then cut off the true optimum. + y, x, I = self.model.data + for t in self.data[1]: + self.model.addVarLocksType(y[t], locktype, nlockspos, nlocksneg) + self.model.addVarLocksType(x[t], locktype, nlockspos, nlocksneg) def sils(T, f, c, d, h): @@ -160,7 +170,6 @@ def mk_example(): conshdlr = Conshdlr_sils() model = sils_cut(T, f, c, d, h, conshdlr) - model.setBoolParam("misc/allowstrongdualreds", 0) model.optimize() sils_cut_obj = model.getObjVal() y, x, I = model.data diff --git a/tests/test_conshdlr.py b/tests/test_conshdlr.py index 3fcf0b37d..4717131b1 100644 --- a/tests/test_conshdlr.py +++ b/tests/test_conshdlr.py @@ -1,3 +1,6 @@ +import os +import runpy + from pyscipopt import Model, Conshdlr, SCIP_RESULT, SCIP_PRESOLTIMING, SCIP_PROPTIMING, SCIP_LOCKTYPE from sys import version_info @@ -274,3 +277,12 @@ def create_model(): #assert "consdelvars" in calls #assert "consprint" in calls assert "consgetnvars" in calls + + +def test_lotsizing_lazy_example(): + # The example solves the same instance with the plain MIP formulation and with + # (l,S) inequalities added lazily by a needscons=False constraint handler, and + # asserts that both give the same objective. It regressed silently once presolve + # started aggregating away variables the handler had not locked (#1228). + example = os.path.join(os.path.dirname(__file__), "..", "examples", "finished", "lotsizing_lazy.py") + runpy.run_path(example, run_name="__main__") From 8f13ad7f6172b12fc56e32944e7e0536f584138d Mon Sep 17 00:00:00 2001 From: Joao-Dionisio Date: Thu, 5 Nov 2026 14:12:31 +0000 Subject: [PATCH 2/3] Fix lotsizing_lazy example: lock variables in conslock --- CHANGELOG.md | 2 +- examples/finished/lotsizing_lazy.py | 9 +-------- tests/test_conshdlr.py | 10 +++++----- 3 files changed, 7 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7671d2392..4daaf48c2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ - Added tests for `getObjVal()` and `getVal()` - Added `addConsLinear()` to add linear constraints from variables and coefficients without building an expression ### Fixed +- Fixed `lotsizing_lazy` example: lock the handler's variables in `conslock` so presolve cannot aggregate them away (#1252) - Dual and Farkas getters for linear, knapsack, and logicor constraints now return zero when presolving has removed them. - Fixed incorrect `getObjVal()` result when called before `getBestSol()` (e.g., in event handlers). - Fixed `getDualsolKnapsack()` and `getDualfarkasKnapsack()` returning 0 for original constraints, and the handler check in `getVarsAnd()` that could never fail @@ -31,7 +32,6 @@ - Added tests for `getRowLinear()` and extended existing testing for `isActive()` ### Fixed - Fixed Cython 3.3 compatibility (#1248) -- Fixed `lotsizing_lazy` example: the constraint handler now locks its variables in `conslock`, so presolve can no longer aggregate them away and make the lazily added (l,S) cuts cut off the optimum (#1228) - Made `test_markDoNotAggrVar_and_getStatus` robust to SCIP presolve changes by discovering the aggregated/multi-aggregated variables instead of hardcoding them - Ensure probing has ended when freeing scip instance in benders ### Changed diff --git a/examples/finished/lotsizing_lazy.py b/examples/finished/lotsizing_lazy.py index 57c3ddbfe..20b739e27 100644 --- a/examples/finished/lotsizing_lazy.py +++ b/examples/finished/lotsizing_lazy.py @@ -33,8 +33,7 @@ def addcut(self, checkonly, sol): if checkonly: return True else: - # add the (l,S) inequality sum_{t in S} x[t] <= sum_{t in S} D[t,l] y[t] + I[l] - # with I[l] eliminated through the flow conservation constraints (I[0] = 0) + # add cutting plane constraint self.model.addCons(quicksum([x[t] for t in L]) + \ quicksum(D[t, ell] * y[t] for t in S) >= D[1, ell], removable=True) @@ -54,12 +53,6 @@ def consenfolp(self, constraints, nusefulconss, solinfeasible): return {"result": SCIP_RESULT.FEASIBLE} def conslock(self, constraint, locktype, nlockspos, nlocksneg): - # With needscons=False, SCIP calls this once with constraint=None and expects the - # handler to lock every variable it enforces. The (l,S) inequalities only have - # positive coefficients and are ">=" constraints, so they can only be violated by - # rounding x or y down. Without these locks, presolve is free to apply dual - # reductions (e.g. aggregating the relaxed y[t] away), and the cuts added later - # would then cut off the true optimum. y, x, I = self.model.data for t in self.data[1]: self.model.addVarLocksType(y[t], locktype, nlockspos, nlocksneg) diff --git a/tests/test_conshdlr.py b/tests/test_conshdlr.py index 4717131b1..1466d2c0c 100644 --- a/tests/test_conshdlr.py +++ b/tests/test_conshdlr.py @@ -1,6 +1,8 @@ import os import runpy +import pytest + from pyscipopt import Model, Conshdlr, SCIP_RESULT, SCIP_PRESOLTIMING, SCIP_PROPTIMING, SCIP_LOCKTYPE from sys import version_info @@ -280,9 +282,7 @@ def create_model(): def test_lotsizing_lazy_example(): - # The example solves the same instance with the plain MIP formulation and with - # (l,S) inequalities added lazily by a needscons=False constraint handler, and - # asserts that both give the same objective. It regressed silently once presolve - # started aggregating away variables the handler had not locked (#1228). example = os.path.join(os.path.dirname(__file__), "..", "examples", "finished", "lotsizing_lazy.py") - runpy.run_path(example, run_name="__main__") + ns = runpy.run_path(example, run_name="__main__") + assert ns["sils_obj"] == pytest.approx(57) + assert ns["sils_cut_obj"] == pytest.approx(57) From 01bc112aaa1dd2048f8af474a33d5ea464fe38e6 Mon Sep 17 00:00:00 2001 From: Joao-Dionisio Date: Thu, 5 Nov 2026 14:44:08 +0000 Subject: [PATCH 3/3] Explain the locks in the lotsizing_lazy conslock --- examples/finished/lotsizing_lazy.py | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/finished/lotsizing_lazy.py b/examples/finished/lotsizing_lazy.py index 20b739e27..98613ba2d 100644 --- a/examples/finished/lotsizing_lazy.py +++ b/examples/finished/lotsizing_lazy.py @@ -53,6 +53,7 @@ def consenfolp(self, constraints, nusefulconss, solinfeasible): return {"result": SCIP_RESULT.FEASIBLE} def conslock(self, constraint, locktype, nlockspos, nlocksneg): + # without these locks presolve may aggregate the variables away and the lazy cuts then cut off the optimum y, x, I = self.model.data for t in self.data[1]: self.model.addVarLocksType(y[t], locktype, nlockspos, nlocksneg)