Skip to content

Add access functions for row duals #986

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 6, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
- Added getLinearConsIndicator
- Added SCIP_LPPARAM, setIntParam, setRealParam, getIntParam, getRealParam, isOptimal, getObjVal, getRedcost for lpi
- Added isFeasPositive
- Added SCIP function SCIProwGetDualsol and wrapper getDualsol
- Added SCIP function SCIProwGetDualfarkas and wrapper getDualfarkas
### Fixed
- Fixed bug when accessing matrix variable attributes
### Changed
Expand Down
2 changes: 2 additions & 0 deletions src/pyscipopt/scip.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -1895,6 +1895,8 @@ cdef extern from "scip/pub_lp.h":
SCIP_Real SCIProwGetLhs(SCIP_ROW* row)
SCIP_Real SCIProwGetRhs(SCIP_ROW* row)
SCIP_Real SCIProwGetConstant(SCIP_ROW* row)
SCIP_Real SCIProwGetDualsol(SCIP_ROW* row)
SCIP_Real SCIProwGetDualfarkas(SCIP_ROW* row)
int SCIProwGetLPPos(SCIP_ROW* row)
SCIP_BASESTAT SCIProwGetBasisStatus(SCIP_ROW* row)
SCIP_Bool SCIProwIsIntegral(SCIP_ROW* row)
Expand Down
22 changes: 22 additions & 0 deletions src/pyscipopt/scip.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@
if rc == SCIP_OKAY:
pass
elif rc == SCIP_ERROR:
raise Exception('SCIP: unspecified error!')

Check failure on line 301 in src/pyscipopt/scip.pxi

View workflow job for this annotation

GitHub Actions / test-coverage (3.11)

SCIP: unspecified error!
elif rc == SCIP_NOMEMORY:
raise MemoryError('SCIP: insufficient memory error!')
elif rc == SCIP_READERROR:
Expand Down Expand Up @@ -679,6 +679,28 @@
"""
return SCIProwGetConstant(self.scip_row)

def getDualsol(self):
"""
Returns the dual solution of row.

Returns
-------
float

"""
return SCIProwGetDualsol(self.scip_row)

def getDualfarkas(self):
"""
Returns the dual Farkas solution of row.

Returns
-------
float

"""
return SCIProwGetDualfarkas(self.scip_row)

def getLPPos(self):
"""
Gets position of row in current LP, or -1 if it is not in LP.
Expand Down
96 changes: 96 additions & 0 deletions tests/test_row_dual.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
from pyscipopt import Model, Sepa, SCIP_RESULT, SCIP_PARAMSETTING


class SimpleSepa(Sepa):

def __init__(self, x, y):
self.cut = None
self.x = x
self.y = y
self.has_checked = False

def sepainit(self):
scip = self.model
self.trans_x = scip.getTransformedVar(self.x)
self.trans_y = scip.getTransformedVar(self.y)

def sepaexeclp(self):
result = SCIP_RESULT.SEPARATED
scip = self.model

if self.cut is not None and not self.has_checked:
# rhs * dual should be equal to optimal objective (= -1)
assert scip.isFeasEQ(self.cut.getDualsol(), -1.0)
self.has_checked = True

cut = scip.createEmptyRowSepa(self,
lhs=-scip.infinity(),
rhs=1.0)

scip.cacheRowExtensions(cut)

scip.addVarToRow(cut, self.trans_x, 1.)
scip.addVarToRow(cut, self.trans_y, 1.)

scip.flushRowExtensions(cut)

scip.addCut(cut, forcecut=True)

self.cut = cut

return {"result": result}

def sepaexit(self):
assert self.has_checked, "Separator called < 2 times"


def model():
# create solver instance
s = Model()

# turn off presolve
s.setPresolve(SCIP_PARAMSETTING.OFF)
# turn off heuristics
s.setHeuristics(SCIP_PARAMSETTING.OFF)
# turn off propagation
s.setIntParam("propagating/maxrounds", 0)
s.setIntParam("propagating/maxroundsroot", 0)

# turn off all other separators
s.setIntParam("separating/strongcg/freq", -1)
s.setIntParam("separating/gomory/freq", -1)
s.setIntParam("separating/aggregation/freq", -1)
s.setIntParam("separating/mcf/freq", -1)
s.setIntParam("separating/closecuts/freq", -1)
s.setIntParam("separating/clique/freq", -1)
s.setIntParam("separating/zerohalf/freq", -1)
s.setIntParam("separating/mixing/freq", -1)
s.setIntParam("separating/rapidlearning/freq", -1)
s.setIntParam("separating/rlt/freq", -1)

# only two rounds of cuts
# s.setIntParam("separating/maxroundsroot", 10)

return s


def test_row_dual():
s = model()
# add variable
x = s.addVar("x", vtype='I', obj=-1, lb=0.)
y = s.addVar("y", vtype='I', obj=-1, lb=0.)

# add constraint
s.addCons(x <= 1.5)
s.addCons(y <= 1.5)

# include separator
sepa = SimpleSepa(x, y)
s.includeSepa(sepa, "python_simple", "generates a simple cut",
priority=1000,
freq=1)

s.addCons(x + y <= 1.75)

# solve problem
s.optimize()