Skip to content

Potential conshdlr conslock life cycle problem #1193

@Koen-Ligthart

Description

@Koen-Ligthart

Describe the bug
Cleaning up the SCIP model results in a ReferenceError in conslock of a constraint handler.

To Reproduce
Construct a single file with the following contents and execute it:

import pyscipopt as ps


class DummyConshdlr(ps.Conshdlr):

    def createCons(self, vars, model):
        cons = self.model.createCons(self, "dummy")
        # cons.data = {"vars": vars, "model": model} # alt 1
        cons.data = {"vars": vars} # alt 2
        return cons

    def conscheck(self, constraints, solution, check_integrality, check_lp_rows, print_reason, completely, **results):
        return {"result": ps.SCIP_RESULT.FEASIBLE}

    def consenfolp(self, constraints, n_useful_conss, sol_infeasible):
        return {"result": ps.SCIP_RESULT.FEASIBLE}

    def conslock(self, constraint, locktype, nlockspos, nlocksneg):
        model = self.model
        for var in constraint.data["vars"]:
            model.addVarLocks(var, nlockspos + nlocksneg, nlockspos + nlocksneg)


def test():

    model = ps.Model()
    vars = [ model.addVar() ]

    conshdlr = DummyConshdlr()
    model.includeConshdlr(conshdlr, "dummy", "dummy")
    model.addPyCons(conshdlr.createCons(vars, model))

    model.optimize()
    

test()

This then first successfully solves the problem, but also results in an unexpected ReferenceError. See the following log:

feasible solution found by trivial heuristic after 0.0 seconds, objective value 0.000000e+00
presolving:
presolving (1 rounds: 1 fast, 0 medium, 0 exhaustive):
 0 deleted vars, 0 deleted constraints, 0 added constraints, 0 tightened bounds, 0 added holes, 0 changed sides, 0 changed coefficients
 0 implications, 0 cliques, 0 implied integral variables (0 bin, 0 int, 0 cont)
transformed 3/3 original solutions to the transformed problem space
presolving solved problem
Presolving Time: 0.00

SCIP Status        : problem is solved [optimal solution found]
Solving Time (sec) : 0.00
Solving Nodes      : 0
Primal Bound       : +0.00000000000000e+00 (3 solutions)
Dual Bound         : +0.00000000000000e+00
Gap                : 0.00 %
Traceback (most recent call last):
  File "XXXXXXXXX", line 21, in conslock
    model.addVarLocks(var, nlockspos + nlocksneg, nlockspos + nlocksneg)
    ^^^^^^^^^^^^^^^^^
ReferenceError: weakly-referenced object no longer exists
Exception ignored in: 'pyscipopt.scip.PyConsLock'
Traceback (most recent call last):
  File "XXXXXXXXX.py", line 21, in conslock
    model.addVarLocks(var, nlockspos + nlocksneg, nlockspos + nlocksneg)
ReferenceError: weakly-referenced object no longer exists
[cons.c:7592] ERROR: Error <0> in function call
[prob.c:1602] ERROR: Error <0> in function call
[prob.c:458] ERROR: Error <0> in function call
[scip_solve.c:2033] ERROR: Error <0> in function call
[scip_solve.c:3511] ERROR: Error <0> in function call
[scip_prob.c:847] ERROR: Error <0> in function call
[scip_general.c:412] ERROR: Error <0> in function call
Traceback (most recent call last):
  File "src/pyscipopt/scip.pxi", line 319, in pyscipopt.scip.PY_SCIP_CALL
Exception: SCIP: unspecified error!
Exception ignored in: 'pyscipopt.scip.Model.__dealloc__'
Traceback (most recent call last):
  File "src/pyscipopt/scip.pxi", line 319, in pyscipopt.scip.PY_SCIP_CALL
Exception: SCIP: unspecified error!

Expected behavior
I expect the code to run without such error and produce a log identical to the situation where alt 1 is uncommented and alt 2 is commented, which yields the same log, except for the part after Gap:, which is not present with alt 1 uncommented. Additionally, I do not expect a different result from the code depending on whether alt 1 or alt 2 is commented.

System

  • OS: Windows
  • Version 11 Enterprise
  • SCIP version 10.0.0
  • Installed pyscipopt version 6.0.0 through pip

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions