diff --git a/CHANGELOG.md b/CHANGELOG.md index 6704f6da7..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 diff --git a/examples/finished/lotsizing_lazy.py b/examples/finished/lotsizing_lazy.py index d39101f15..98613ba2d 100644 --- a/examples/finished/lotsizing_lazy.py +++ b/examples/finished/lotsizing_lazy.py @@ -53,7 +53,11 @@ def consenfolp(self, constraints, nusefulconss, solinfeasible): return {"result": SCIP_RESULT.FEASIBLE} def conslock(self, constraint, locktype, nlockspos, nlocksneg): - pass + # 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) + self.model.addVarLocksType(x[t], locktype, nlockspos, nlocksneg) def sils(T, f, c, d, h): @@ -160,7 +164,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..1466d2c0c 100644 --- a/tests/test_conshdlr.py +++ b/tests/test_conshdlr.py @@ -1,3 +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 @@ -274,3 +279,10 @@ def create_model(): #assert "consdelvars" in calls #assert "consprint" in calls assert "consgetnvars" in calls + + +def test_lotsizing_lazy_example(): + example = os.path.join(os.path.dirname(__file__), "..", "examples", "finished", "lotsizing_lazy.py") + ns = runpy.run_path(example, run_name="__main__") + assert ns["sils_obj"] == pytest.approx(57) + assert ns["sils_cut_obj"] == pytest.approx(57)