Skip to content

Commit f0d47a4

Browse files
Add getVarPseudocostScore and getVarPseudocost (#1041)
* Add getVarPseudocostScore and SCIPgetVarPseudocostScore * typo * Test getVarPseudocostScore and getVarPseudocost * Apply suggestions from code review --------- Co-authored-by: João Dionísio <57299939+Joao-Dionisio@users.noreply.github.com>
1 parent 9aa8c8f commit f0d47a4

File tree

4 files changed

+74
-1
lines changed

4 files changed

+74
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
- Generalized getLhs() and getRhs() to additionally support any linear type constraint
1414
- Added recipe for getting local constraints
1515
- Added enableDebugSol() and disableDebugSol() for controlling the debug solution mechanism if DEBUGSOL=true
16+
- Added getVarPseudocostScore() and getVarPseudocost()
1617
### Fixed
1718
- Raised an error when an expression is used when a variable is required
1819
- Fixed some compile warnings

src/pyscipopt/scip.pxd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -810,6 +810,7 @@ cdef extern from "scip/scip.h":
810810
void SCIPvarMarkDeletable(SCIP_VAR* var)
811811
SCIP_Bool SCIPvarIsDeletable(SCIP_VAR* var)
812812
SCIP_Real SCIPgetVarPseudocost(SCIP* scip, SCIP_VAR* var, SCIP_BRANCHDIR dir)
813+
SCIP_Real SCIPgetVarPseudocostScore(SCIP* scip, SCIP_VAR* var, SCIP_Real solval)
813814
SCIP_Real SCIPvarGetCutoffSum(SCIP_VAR* var, SCIP_BRANCHDIR dir)
814815
SCIP_Longint SCIPvarGetNBranchings(SCIP_VAR* var, SCIP_BRANCHDIR dir)
815816
SCIP_Bool SCIPvarMayRoundUp(SCIP_VAR* var)

src/pyscipopt/scip.pxi

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4437,6 +4437,40 @@ cdef class Model:
44374437
var_dict[var.name] = self.getVal(var)
44384438
return var_dict
44394439

4440+
def getVarPseudocostScore(self, Variable var, solVal):
4441+
"""
4442+
gets the variable's pseudo cost score value for the given LP solution value
4443+
4444+
Parameters
4445+
----------
4446+
variable : Variable
4447+
problem variable
4448+
solVal : float
4449+
difference of variable's new LP value - old LP value
4450+
4451+
Returns
4452+
-------
4453+
float
4454+
"""
4455+
return SCIPgetVarPseudocostScore(self._scip, var.scip_var, solVal)
4456+
4457+
def getVarPseudocost(self, Variable var, branchdir):
4458+
"""
4459+
gets the variable's pseudo cost value for the given direction
4460+
4461+
Parameters
4462+
----------
4463+
variable : Variable
4464+
problem variable
4465+
branchdir : PY_SCIP_BRANCHDIR
4466+
branching direction (downwards, or upwards)
4467+
4468+
Returns
4469+
-------
4470+
float
4471+
"""
4472+
return SCIPgetVarPseudocost(self._scip, var.scip_var, branchdir)
4473+
44404474
def updateNodeLowerbound(self, Node node, lb):
44414475
"""
44424476
If given value is larger than the node's lower bound (in transformed problem),

tests/test_model.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import os
33
import itertools
44

5-
from pyscipopt import Model, SCIP_STAGE, SCIP_PARAMSETTING, quicksum
5+
from pyscipopt import Model, SCIP_STAGE, SCIP_PARAMSETTING, SCIP_BRANCHDIR, quicksum
66
from helpers.utils import random_mip_1
77

88
def test_model():
@@ -528,3 +528,40 @@ def test_comparisons():
528528
assert not model.isNegative(0.)
529529

530530
assert model.isHugeValue(inf)
531+
532+
def test_getVarPseudocostScore():
533+
m = Model()
534+
535+
m.addVar("x", vtype='B', obj=1.0)
536+
m.addVar("y", vtype='B', obj=2.0)
537+
538+
m.setPresolve(SCIP_PARAMSETTING.OFF)
539+
m.presolve()
540+
541+
var = m.getVars(transformed=True)[0]
542+
543+
p = m.getVarPseudocostScore(var, 1)
544+
assert m.isEQ(p, 1)
545+
546+
p = m.getVarPseudocostScore(var, 0.5)
547+
assert m.isEQ(p, 0.25)
548+
549+
def test_getVarPseudocost():
550+
m = Model()
551+
552+
m.addVar("x", vtype='B', obj=1.0)
553+
m.addVar("y", vtype='B', obj=2.0)
554+
555+
m.setPresolve(SCIP_PARAMSETTING.OFF)
556+
m.presolve()
557+
558+
var = m.getVars(transformed=True)[0]
559+
560+
p = m.getVarPseudocost(var, SCIP_BRANCHDIR.UPWARDS)
561+
assert m.isEQ(p, 1)
562+
563+
m.updateVarPseudocost(var, 1, 12, 1)
564+
p = m.getVarPseudocost(var, SCIP_BRANCHDIR.UPWARDS)
565+
566+
# Not exactly 12 because the new value is a weighted sum of all the updates
567+
assert m.isEQ(p, 12.0001)

0 commit comments

Comments
 (0)