Skip to content

Commit 7277e55

Browse files
Merge branch 'master' into fix/1043
2 parents 2be1117 + a3c2a48 commit 7277e55

File tree

4 files changed

+66
-2
lines changed

4 files changed

+66
-2
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: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
from pyscipopt import Model
1+
from pyscipopt import Model, SCIP_PARAMSETTING, SCIP_BRANCHDIR
2+
from helpers.utils import random_mip_1
23

34
def test_variablebounds():
4-
55
m = Model()
66

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

0 commit comments

Comments
 (0)