Skip to content

Commit 7b55a7b

Browse files
Add getNBranchings and getNBranchingsCurrentRun (#1049)
* getNBranchings and getNBranchingsCurrentRun * Fix imports * Update src/pyscipopt/scip.pxi Co-authored-by: João Dionísio <[email protected]> * Update src/pyscipopt/scip.pxi Co-authored-by: João Dionísio <[email protected]> * Tests improved * Update test_vars.py --------- Co-authored-by: João Dionísio <[email protected]>
1 parent 8267aa4 commit 7b55a7b

File tree

4 files changed

+68
-1
lines changed

4 files changed

+68
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
- Added recipe for getting local constraints
1515
- Added enableDebugSol() and disableDebugSol() for controlling the debug solution mechanism if DEBUGSOL=true
1616
- Added getVarPseudocostScore() and getVarPseudocost()
17+
- Added getNBranchings() and getNBranchingsCurrentRun()
1718
### Fixed
1819
- Raised an error when an expression is used when a variable is required
1920
- Fixed some compile warnings

src/pyscipopt/scip.pxd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,7 @@ cdef extern from "scip/scip.h":
813813
SCIP_Real SCIPgetVarPseudocostScore(SCIP* scip, SCIP_VAR* var, SCIP_Real solval)
814814
SCIP_Real SCIPvarGetCutoffSum(SCIP_VAR* var, SCIP_BRANCHDIR dir)
815815
SCIP_Longint SCIPvarGetNBranchings(SCIP_VAR* var, SCIP_BRANCHDIR dir)
816+
SCIP_Longint SCIPvarGetNBranchingsCurrentRun(SCIP_VAR* var, SCIP_BRANCHDIR dir)
816817
SCIP_Bool SCIPvarMayRoundUp(SCIP_VAR* var)
817818
SCIP_Bool SCIPvarMayRoundDown(SCIP_VAR* var)
818819

src/pyscipopt/scip.pxi

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1804,6 +1804,37 @@ cdef class Variable(Expr):
18041804
mayround = SCIPvarMayRoundUp(self.scip_var)
18051805
return mayround
18061806

1807+
def getNBranchings(self, branchdir):
1808+
"""
1809+
returns the number of times a bound of the variable was changed in given direction due to branching
1810+
1811+
Parameters
1812+
----------
1813+
branchdir : PY_SCIP_BRANCHDIR
1814+
branching direction (downwards, or upwards)
1815+
1816+
Returns
1817+
-------
1818+
int
1819+
"""
1820+
return SCIPvarGetNBranchings(self.scip_var, branchdir)
1821+
1822+
def getNBranchingsCurrentRun(self, branchdir):
1823+
"""
1824+
returns the number of times a bound of the variable was changed in given direction due to branching in the
1825+
current run
1826+
1827+
Parameters
1828+
----------
1829+
branchdir : PY_SCIP_BRANCHDIR
1830+
branching direction (downwards, or upwards)
1831+
1832+
Returns
1833+
-------
1834+
int
1835+
"""
1836+
return SCIPvarGetNBranchingsCurrentRun(self.scip_var, branchdir)
1837+
18071838
class MatrixVariable(MatrixExpr):
18081839

18091840
def vtype(self):

tests/test_vars.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
from pyscipopt import Model
1+
from pyscipopt import Model, SCIP_PARAMSETTING, SCIP_BRANCHDIR
2+
3+
from tests.helpers.utils import random_mip_1
4+
25

36
def test_variablebounds():
47

@@ -80,3 +83,34 @@ def test_markRelaxationOnly():
8083
assert x.isDeletable()
8184
assert not y.isRelaxationOnly()
8285
assert not y.isDeletable()
86+
87+
def test_getNBranchings():
88+
m = random_mip_1(True, True, True, 100, True)
89+
m.setParam("branching/mostinf/priority", 999999)
90+
m.setParam("limits/restarts", 0)
91+
92+
m.optimize()
93+
94+
m.setParam("limits/nodes", 200)
95+
m.restartSolve()
96+
m.optimize()
97+
98+
n_branchings = 0
99+
for var in m.getVars():
100+
n_branchings += var.getNBranchings(SCIP_BRANCHDIR.UPWARDS)
101+
n_branchings += var.getNBranchings(SCIP_BRANCHDIR.DOWNWARDS)
102+
103+
assert n_branchings == m.getNTotalNodes() - 2 # "-2" comes from the two root nodes because of the restart
104+
105+
def test_getNBranchingsCurrentRun():
106+
m = random_mip_1(True, True, True, 100, True)
107+
m.setParam( "branching/mostinf/priority", 999999)
108+
109+
m.optimize()
110+
111+
n_branchings = 0
112+
for var in m.getVars():
113+
n_branchings += var.getNBranchingsCurrentRun(SCIP_BRANCHDIR.UPWARDS)
114+
n_branchings += var.getNBranchingsCurrentRun(SCIP_BRANCHDIR.DOWNWARDS)
115+
116+
assert n_branchings == m.getNNodes() - 1

0 commit comments

Comments
 (0)