Skip to content

Commit

Permalink
HiGHS API: Add duals, slacks, reduced costs (#780) (#781)
Browse files Browse the repository at this point in the history
This adds duals, slacks, and reduced costs to the highspy HiGHS interface.

Closes: #780

Co-authored-by: Franco Peschiera <[email protected]>
  • Loading branch information
WPettersson and pchtsp authored Nov 1, 2024
1 parent 19c7b75 commit 1527a4f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
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

0 comments on commit 1527a4f

Please sign in to comment.