Skip to content
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased
### Added
- Added `Model.captureCons()`, `Model.releaseCons()`, `Model.getConsNUses()`, `Model.captureVar()`, `Model.releaseVar()`, and `Model.getVarNUses()`
Comment thread
Joao-Dionisio marked this conversation as resolved.
Outdated
### Fixed
### Changed
- Speed up `Expr.__add__` and `Expr.__iadd__` via the C-level API
Expand Down
2 changes: 2 additions & 0 deletions src/pyscipopt/scip.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,7 @@ cdef extern from "scip/scip.h":
SCIP_RETCODE SCIPcaptureVar(SCIP* scip, SCIP_VAR* var)
SCIP_RETCODE SCIPaddPricedVar(SCIP* scip, SCIP_VAR* var, SCIP_Real score)
Comment thread
DominikKamp marked this conversation as resolved.
Outdated
SCIP_RETCODE SCIPreleaseVar(SCIP* scip, SCIP_VAR** var)
int SCIPvarGetNUses(SCIP_VAR* var)
SCIP_RETCODE SCIPtransformVar(SCIP* scip, SCIP_VAR* var, SCIP_VAR** transvar)
SCIP_RETCODE SCIPgetTransformedVar(SCIP* scip, SCIP_VAR* var, SCIP_VAR** transvar)
SCIP_RETCODE SCIPaddVarLocks(SCIP* scip, SCIP_VAR* var, int nlocksdown, int nlocksup)
Expand Down Expand Up @@ -913,6 +914,7 @@ cdef extern from "scip/scip.h":
# Constraint Methods
SCIP_RETCODE SCIPcaptureCons(SCIP* scip, SCIP_CONS* cons)
SCIP_RETCODE SCIPreleaseCons(SCIP* scip, SCIP_CONS** cons)
int SCIPconsGetNUses(SCIP_CONS* cons)
SCIP_RETCODE SCIPtransformCons(SCIP* scip, SCIP_CONS* cons, SCIP_CONS** transcons)
SCIP_RETCODE SCIPgetTransformedCons(SCIP* scip, SCIP_CONS* cons, SCIP_CONS** transcons)
SCIP_RETCODE SCIPgetConsVars(SCIP* scip, SCIP_CONS* cons, SCIP_VAR** vars, int varssize, SCIP_Bool* success)
Expand Down
114 changes: 113 additions & 1 deletion src/pyscipopt/scip.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -4636,6 +4636,62 @@ cdef class Model:
var.scip_var = NULL
return deleted

def captureVar(self, Variable var):
"""
Increase the usage counter of a variable. Must be matched by a releaseVar.
Comment thread
Joao-Dionisio marked this conversation as resolved.
Outdated

Parameters
----------
var : Variable
variable to capture

"""
PY_SCIP_CALL(SCIPcaptureVar(self._scip, var.scip_var))

def releaseVar(self, Variable var):
"""
Decrease the usage counter of a variable.

Unlike the underlying ``SCIPreleaseVar``, this wrapper refuses to
release the last reference: it must be paired with a prior
captureVar call. This guarantees the variable is never freed via
Comment thread
DominikKamp marked this conversation as resolved.
Outdated
this method and the wrapper's pointer stays valid.

Parameters
----------
var : Variable
variable to release

Raises
------
Exception
if releasing would free the variable (no matching captureVar).

"""
cdef SCIP_VAR* scip_var = var.scip_var
if SCIPvarGetNUses(scip_var) <= 1:
raise Exception(
"releaseVar would free the variable; must be paired with "
"a prior captureVar call."
)
PY_SCIP_CALL(SCIPreleaseVar(self._scip, &scip_var))

def getVarNUses(self, Variable var):
"""
Get the number of times the variable is currently captured.

Parameters
----------
var : Variable
variable to query

Returns
-------
int
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems incomplete.


"""
return SCIPvarGetNUses(var.scip_var)

def aggregateVars(self, Variable varx, Variable vary, coefx=1.0, coefy=-1.0, rhs=0.0):
"""
Aggregate two variables by adding an aggregation constraint.
Expand Down Expand Up @@ -8568,7 +8624,63 @@ cdef class Model:

"""
PY_SCIP_CALL(SCIPdelConsLocal(self._scip, cons.scip_cons))


def captureCons(self, Constraint cons):
"""
Increase the usage counter of a constraint. Must be matched by a releaseCons.
Comment thread
Joao-Dionisio marked this conversation as resolved.
Outdated

Parameters
----------
cons : Constraint
constraint to capture

"""
PY_SCIP_CALL(SCIPcaptureCons(self._scip, cons.scip_cons))

def releaseCons(self, Constraint cons):
"""
Decrease the usage counter of a constraint.

Unlike the underlying ``SCIPreleaseCons``, this wrapper refuses to
release the last reference: it must be paired with a prior
captureCons call. This guarantees the constraint is never freed via
Comment thread
DominikKamp marked this conversation as resolved.
Outdated
this method and the wrapper's pointer stays valid.

Parameters
----------
cons : Constraint
constraint to release
Comment thread
DominikKamp marked this conversation as resolved.

Raises
------
Exception
if releasing would free the constraint (no matching captureCons).

"""
cdef SCIP_CONS* scip_cons = cons.scip_cons
if SCIPconsGetNUses(scip_cons) <= 1:
raise Exception(
"releaseCons would free the constraint; must be paired with "
"a prior captureCons call."
)
PY_SCIP_CALL(SCIPreleaseCons(self._scip, &scip_cons))

def getConsNUses(self, Constraint cons):
"""
Get the number of times the constraint is currently captured.

Parameters
----------
cons : Constraint
constraint to query

Returns
-------
int
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems incomplete.


"""
return SCIPconsGetNUses(cons.scip_cons)

def getValsLinear(self, Constraint cons):
"""
Retrieve the coefficients of a linear constraint
Expand Down
Loading
Loading