Skip to content
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

HiGHS API: Add duals, slacks, reduced costs (#780) #781

Merged
merged 2 commits into from
Nov 1, 2024
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
18 changes: 17 additions & 1 deletion pulp/apis/highs_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ def buildSolverModel(self, lp):
var.index, highspy.HighsVarType.kInteger
)

for constraint in lp.constraints.values():
for i, constraint in enumerate(lp.constraints.values()):
non_zero_constraint_items = [
(var.index, coefficient)
for var, coefficient in constraint.items()
Expand All @@ -391,6 +391,8 @@ def buildSolverModel(self, lp):
else:
indices, coefficients = zip(*non_zero_constraint_items)

constraint.index = i

lb = constraint.getLb()
ub = constraint.getUb()
lp.solverModel.addRow(
Expand Down Expand Up @@ -476,10 +478,24 @@ def findSolutionValues(self, lp):
}

col_values = list(solution.col_value)
col_duals = list(solution.col_dual)

# Assign values to the variables as with lp.assignVarsVals()
for var in lp.variables():
var.varValue = col_values[var.index]
var.dj = col_duals[var.index]

for constraint in lp.constraints.values():
# PuLP returns LpConstraint.constant as if it were on the
# left-hand side, which means the signs on the following line
# are correct
constraint.slack = (
constraint.constant + solution.row_value[constraint.index]
)
# We need to flip the sign for slacks for LE constraints
if constraint.sense == constants.LpConstraintLE:
constraint.slack *= -1.0
constraint.pi = solution.row_dual[constraint.index]

if obj_value == float(inf) and status in (
HighsModelStatus.kTimeLimit,
Expand Down
1 change: 1 addition & 0 deletions pulp/tests/test_pulp.py
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,7 @@ def test_dual_variables_reduced_costs(self):
PULP_CBC_CMD,
YAPOSIB,
PYGLPK,
HiGHS,
SAS94,
SASCAS,
]:
Expand Down
Loading