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

Change in highs api and status handling in both cases #682

Merged
merged 6 commits into from
Jan 12, 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
104 changes: 83 additions & 21 deletions pulp/apis/highs_api.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# PuLP : Python LP Modeler
# Version 2.4
from math import inf

# Copyright (c) 2002-2005, Jean-Sebastien Roy ([email protected])
# Modifications Copyright (c) 2007- Stuart Anthony Mitchell ([email protected])
Expand Down Expand Up @@ -180,12 +181,12 @@ def actualSolve(self, lp):
elif model_status.lower() == "infeasible": # infeasible
status, status_sol = (
constants.LpStatusInfeasible,
constants.LpSolutionNoSolutionFound,
constants.LpSolutionInfeasible,
)
elif model_status.lower() == "unbounded": # unbounded
status, status_sol = (
constants.LpStatusUnbounded,
constants.LpSolutionNoSolutionFound,
constants.LpSolutionUnbounded,
)
else: # no solution
status, status_sol = (
Expand Down Expand Up @@ -363,47 +364,108 @@ def buildSolverModel(self, lp):

def findSolutionValues(self, lp):
status = lp.solverModel.getModelStatus()
obj_value = lp.solverModel.getObjectiveValue()

solution = lp.solverModel.getSolution()
HighsModelStatus = highspy.HighsModelStatus

status_dict = {
HighsModelStatus.kNotset: constants.LpStatusNotSolved,
HighsModelStatus.kLoadError: constants.LpStatusNotSolved,
HighsModelStatus.kModelError: constants.LpStatusNotSolved,
HighsModelStatus.kPresolveError: constants.LpStatusNotSolved,
HighsModelStatus.kSolveError: constants.LpStatusNotSolved,
HighsModelStatus.kPostsolveError: constants.LpStatusNotSolved,
HighsModelStatus.kModelEmpty: constants.LpStatusNotSolved,
HighsModelStatus.kOptimal: constants.LpStatusOptimal,
HighsModelStatus.kInfeasible: constants.LpStatusInfeasible,
HighsModelStatus.kUnboundedOrInfeasible: constants.LpStatusInfeasible,
HighsModelStatus.kUnbounded: constants.LpStatusUnbounded,
HighsModelStatus.kObjectiveBound: constants.LpStatusNotSolved,
HighsModelStatus.kObjectiveTarget: constants.LpStatusNotSolved,
HighsModelStatus.kTimeLimit: constants.LpStatusNotSolved,
HighsModelStatus.kIterationLimit: constants.LpStatusNotSolved,
HighsModelStatus.kUnknown: constants.LpStatusNotSolved,
HighsModelStatus.kNotset: (
constants.LpStatusNotSolved,
constants.LpSolutionNoSolutionFound,
),
HighsModelStatus.kLoadError: (
constants.LpStatusNotSolved,
constants.LpSolutionNoSolutionFound,
),
HighsModelStatus.kModelError: (
constants.LpStatusNotSolved,
constants.LpSolutionNoSolutionFound,
),
HighsModelStatus.kPresolveError: (
constants.LpStatusNotSolved,
constants.LpSolutionNoSolutionFound,
),
HighsModelStatus.kSolveError: (
constants.LpStatusNotSolved,
constants.LpSolutionNoSolutionFound,
),
HighsModelStatus.kPostsolveError: (
constants.LpStatusNotSolved,
constants.LpSolutionNoSolutionFound,
),
HighsModelStatus.kModelEmpty: (
constants.LpStatusNotSolved,
constants.LpSolutionNoSolutionFound,
),
HighsModelStatus.kOptimal: (
constants.LpStatusOptimal,
constants.LpSolutionOptimal,
),
HighsModelStatus.kInfeasible: (
constants.LpStatusInfeasible,
constants.LpSolutionInfeasible,
),
HighsModelStatus.kUnboundedOrInfeasible: (
constants.LpStatusInfeasible,
constants.LpSolutionInfeasible,
),
HighsModelStatus.kUnbounded: (
constants.LpStatusUnbounded,
constants.LpSolutionUnbounded,
),
HighsModelStatus.kObjectiveBound: (
constants.LpStatusOptimal,
constants.LpSolutionIntegerFeasible,
),
HighsModelStatus.kObjectiveTarget: (
constants.LpStatusOptimal,
constants.LpSolutionIntegerFeasible,
),
HighsModelStatus.kTimeLimit: (
constants.LpStatusOptimal,
constants.LpSolutionIntegerFeasible,
),
HighsModelStatus.kIterationLimit: (
constants.LpStatusOptimal,
constants.LpSolutionIntegerFeasible,
),
HighsModelStatus.kUnknown: (
constants.LpStatusNotSolved,
constants.LpSolutionNoSolutionFound,
),
}

col_values = list(solution.col_value)

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

return status_dict[status]
if obj_value == float(inf) and status in (
HighsModelStatus.kTimeLimit,
HighsModelStatus.kIterationLimit,
):
return constants.LpStatusNotSolved, constants.LpSolutionNoSolutionFound
else:
return status_dict[status]

def actualSolve(self, lp):
self.createAndConfigureSolver(lp)
self.buildSolverModel(lp)
self.callSolver(lp)

solutionStatus = self.findSolutionValues(lp)
status, sol_status = self.findSolutionValues(lp)

for var in lp.variables():
var.modified = False

for constraint in lp.constraints.values():
constraint.modifier = False

return solutionStatus
lp.assignStatus(status, sol_status)

return status

def actualResolve(self, lp, **kwargs):
raise PulpSolverError("HiGHS: Resolving is not supported")
21 changes: 20 additions & 1 deletion pulp/tests/test_pulp.py
Original file line number Diff line number Diff line change
Expand Up @@ -1290,7 +1290,8 @@ def test_measuring_solving_time(self):
return
prob = create_bin_packing_problem(bins=bins, seed=99)
self.solver.timeLimit = time_limit
prob.solve(self.solver)
status = prob.solve(self.solver)

delta = 20
reported_time = prob.solutionTime
if self.solver.name in ["PULP_CBC_CMD", "COIN_CMD"]:
Expand All @@ -1303,9 +1304,27 @@ def test_measuring_solving_time(self):
msg=f"optimization time for solver {self.solver.name}",
)
self.assertTrue(prob.objective.value() is not None)
self.assertEqual(status, const.LpStatusOptimal)
for v in prob.variables():
self.assertTrue(v.varValue is not None)

@gurobi_test
def test_time_limit_no_solution(self):
print("\t Test time limit with no solution")

time_limit = 1
solver_settings = dict(HiGHS=50, PULP_CBC_CMD=30, COIN_CMD=30)
bins = solver_settings.get(self.solver.name)
if bins is None:
# not all solvers have timeLimit support
return
prob = create_bin_packing_problem(bins=bins, seed=99)
self.solver.timeLimit = time_limit
status = prob.solve(self.solver)
self.assertEqual(prob.status, const.LpStatusNotSolved)
self.assertEqual(status, const.LpStatusNotSolved)
self.assertEqual(prob.sol_status, const.LpSolutionNoSolutionFound)

def test_invalid_var_names(self):
prob = LpProblem(self._testMethodName, const.LpMinimize)
x = LpVariable("a")
Expand Down