Skip to content

Commit 5ef1d25

Browse files
committed
Add support for getObjective() that returns the current objective function as a linear expression
1 parent dc27d34 commit 5ef1d25

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

highs/highspy/highs.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,27 @@ def optimize(self):
179179
"""
180180
return self.solve()
181181

182+
183+
def getObjective(self):
184+
"""
185+
Retrieves the current objective function (as a linear expression) and sense.
186+
"""
187+
lp = super().getLp()
188+
189+
objective = highs_linear_expression()
190+
objective.idxs = list(range(self.numVariables))
191+
objective.vals = lp.col_cost_
192+
objective.constant = lp.offset_
193+
194+
# remove non-zero elements
195+
# TODO: performance can be improved since we know idxs are unique
196+
idx, val = objective.reduced_elements()
197+
objective.idxs = idx.tolist()
198+
objective.vals = val.tolist()
199+
200+
return objective, super().getObjectiveSense()[1]
201+
202+
182203
# reset the objective
183204
def setObjective(self, obj: Optional[Union[highs_var, highs_linear_expression]] = None, sense: Optional[ObjSense] = None):
184205
"""

0 commit comments

Comments
 (0)