Skip to content

Commit 5056501

Browse files
committed
Add getNLocksDown, getNLocksUp, getNLocksUpType, getNLocksDownType and tests
1 parent 17427ce commit 5056501

File tree

5 files changed

+91
-2
lines changed

5 files changed

+91
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
### Added
55
- Added support for knapsack constraints
66
- Added isPositive(), isNegative(), isFeasLE(), isFeasLT(), isFeasGE(), isFeasGT(), isHugeValue(), and tests
7-
- Added SCIP_LOCKTYPE, addVarLocksType
7+
- Added SCIP_LOCKTYPE, addVarLocksType, getNLocksDown, getNLocksUp, getNLocksUpType, getNLocksDownType, and tests
88
### Fixed
99
### Changed
1010
### Removed

src/pyscipopt/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
from pyscipopt.scip import PY_SCIP_PRESOLTIMING as SCIP_PRESOLTIMING
4949
from pyscipopt.scip import PY_SCIP_HEURTIMING as SCIP_HEURTIMING
5050
from pyscipopt.scip import PY_SCIP_EVENTTYPE as SCIP_EVENTTYPE
51+
from pyscipopt.scip import PY_SCIP_LOCKTYPE as SCIP_LOCKTYPE
5152
from pyscipopt.scip import PY_SCIP_LPSOLSTAT as SCIP_LPSOLSTAT
5253
from pyscipopt.scip import PY_SCIP_BRANCHDIR as SCIP_BRANCHDIR
5354
from pyscipopt.scip import PY_SCIP_BENDERSENFOTYPE as SCIP_BENDERSENFOTYPE

src/pyscipopt/scip.pxd

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,10 @@ cdef extern from "scip/scip.h":
295295
SCIP_EVENTTYPE SCIP_EVENTTYPE_ROWCHANGED
296296
SCIP_EVENTTYPE SCIP_EVENTTYPE_ROWEVENT
297297

298+
ctypedef int SCIP_LOCKTYPE
299+
cdef extern from "scip/type_var.h":
300+
SCIP_LOCKTYPE SCIP_LOCKTYPE_MODEL
301+
SCIP_LOCKTYPE SCIP_LOCKTYPE_CONFLICT
298302

299303
ctypedef int SCIP_LPSOLQUALITY
300304
cdef extern from "lpi/type_lpi.h":
@@ -793,6 +797,10 @@ cdef extern from "scip/scip.h":
793797
SCIP_RETCODE SCIPgetTransformedVar(SCIP* scip, SCIP_VAR* var, SCIP_VAR** transvar)
794798
SCIP_RETCODE SCIPaddVarLocks(SCIP* scip, SCIP_VAR* var, int nlocksdown, int nlocksup)
795799
SCIP_RETCODE SCIPaddVarLocksType(SCIP* scip, SCIP_VAR* var, SCIP_LOCKTYPE locktype, int nlocksdown, int nlocksup)
800+
int SCIPvarGetNLocksDown(SCIP_VAR* var)
801+
int SCIPvarGetNLocksUp(SCIP_VAR* var)
802+
int SCIPvarGetNLocksUpType(SCIP_VAR* var, SCIP_LOCKTYPE locktype)
803+
int SCIPvarGetNLocksDownType(SCIP_VAR* var, SCIP_LOCKTYPE locktype)
796804
SCIP_VAR** SCIPgetVars(SCIP* scip)
797805
SCIP_VAR** SCIPgetOrigVars(SCIP* scip)
798806
const char* SCIPvarGetName(SCIP_VAR* var)

src/pyscipopt/scip.pxi

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1674,6 +1674,60 @@ cdef class Variable(Expr):
16741674
"""
16751675
return SCIPvarGetAvgSol(self.scip_var)
16761676

1677+
def getNLocksDown(self):
1678+
"""
1679+
Returns the number of locks for rounding down.
1680+
1681+
Returns
1682+
-------
1683+
int
1684+
1685+
"""
1686+
return SCIPvarGetNLocksDown(self.scip_var)
1687+
1688+
def getNLocksUp(self):
1689+
"""
1690+
Returns the number of locks for rounding up.
1691+
1692+
Returns
1693+
-------
1694+
int
1695+
1696+
"""
1697+
return SCIPvarGetNLocksUp(self.scip_var)
1698+
1699+
def getNLocksUpType(self, locktype):
1700+
"""
1701+
Returns the number of locks for rounding up of a certain type.
1702+
1703+
Parameters
1704+
----------
1705+
locktype: PY_SCIP_LOCKTYPE
1706+
types of variable locks
1707+
1708+
Returns
1709+
-------
1710+
int
1711+
1712+
"""
1713+
return SCIPvarGetNLocksUpType(self.scip_var, locktype)
1714+
1715+
def getNLocksDownType(self, locktype):
1716+
"""
1717+
Returns the number of locks for rounding down of a certain type.
1718+
1719+
Parameters
1720+
----------
1721+
locktype: PY_SCIP_LOCKTYPE
1722+
types of variable locks
1723+
1724+
Returns
1725+
-------
1726+
int
1727+
1728+
"""
1729+
return SCIPvarGetNLocksDownType(self.scip_var, locktype)
1730+
16771731
def varMayRound(self, direction="down"):
16781732
"""
16791733
Checks whether it is possible to round variable up / down and stay feasible for the relaxation.

tests/test_conshdlr.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from pyscipopt import Model, Conshdlr, SCIP_RESULT, SCIP_PRESOLTIMING, SCIP_PROPTIMING
1+
from pyscipopt import Model, Conshdlr, SCIP_RESULT, SCIP_PRESOLTIMING, SCIP_PROPTIMING, SCIP_LOCKTYPE
22
from sys import version_info
33

44
if version_info >= (3, 3):
@@ -42,6 +42,7 @@ class MyConshdlr(Conshdlr):
4242
def __init__(self, shouldtrans, shouldcopy):
4343
self.shouldtrans = shouldtrans
4444
self.shouldcopy = shouldcopy
45+
self.first_time = True
4546

4647
def createData(self, constraint, nvars, othername):
4748
print("Creating data for my constraint: %s"%constraint.name)
@@ -68,6 +69,31 @@ def conslock(self, constraint, locktype, nlockspos, nlocksneg):
6869
calls.add("conslock")
6970
assert id(constraint) in ids
7071

72+
if self.first_time:
73+
self.first_time = False
74+
var = self.model.getVars()[0]
75+
n_locks_up = var.getNLocksUp()
76+
n_locks_down = var.getNLocksDown()
77+
n_locks_up_model = var.getNLocksUpType(SCIP_LOCKTYPE.MODEL)
78+
n_locks_up_conflict = var.getNLocksUpType(SCIP_LOCKTYPE.CONFLICT)
79+
n_locks_down_model = var.getNLocksDownType(SCIP_LOCKTYPE.MODEL)
80+
n_locks_down_conflict = var.getNLocksDownType(SCIP_LOCKTYPE.CONFLICT)
81+
assert n_locks_down == 2
82+
assert n_locks_up == 2
83+
assert n_locks_up_model == 2
84+
assert n_locks_up_conflict == 0
85+
assert n_locks_down_model == 2
86+
assert n_locks_down_conflict == 0
87+
88+
if locktype == SCIP_LOCKTYPE.MODEL:
89+
self.model.addVarLocks(var, nlockspos, nlocksneg)
90+
assert var.getNLocksUpType(SCIP_LOCKTYPE.MODEL) != n_locks_up_model or var.getNLocksDownType(SCIP_LOCKTYPE.MODEL) != n_locks_down_model
91+
elif locktype == SCIP_LOCKTYPE.CONFLICT:
92+
self.model.addVarLocks(var, nlockspos, nlocksneg)
93+
assert var.getNLocksUpType(SCIP_LOCKTYPE.CONFLICT) != n_locks_up_conflict or var.getNLocksDownType(SCIP_LOCKTYPE.CONFLICT) != n_locks_down_conflict
94+
95+
assert var.getNLocksDown() != n_locks_down or var.getNLocksUp() != n_locks_up
96+
7197
## callbacks ##
7298
def consfree(self):
7399
calls.add("consfree")

0 commit comments

Comments
 (0)