Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 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
21 changes: 20 additions & 1 deletion pyomo/contrib/solver/solvers/highs.py
Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,26 @@ def _postsolve(self, stream: io.StringIO):
results.objective_bound = None
else:
results.objective_bound = info.mip_dual_bound
results.iteration_count = info.simplex_iteration_count

if info.valid:
# The method that ran will have a non-negative iteration count
# and the others will be 0 or -1.
counts = [
info.simplex_iteration_count,
info.ipm_iteration_count,
info.mip_node_count,
info.pdlp_iteration_count,
info.qp_iteration_count,
]
assert (
len([c for c in counts if c > 1]) <= 1
), "Only one method should have a positive iteration count"
assert any(
[c >= 0 for c in counts]
), "Should have a non-negative count if info valid"
results.iteration_count = max(counts)
else:
results.iteration_count = 0

if config.load_solutions:
if has_feasible_solution:
Expand Down
46 changes: 46 additions & 0 deletions pyomo/contrib/solver/tests/solvers/test_highs.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import pyomo.environ as pyo

from pyomo.contrib.solver.solvers.highs import Highs
from pyomo.contrib.solver.common.results import SolutionStatus

opt = Highs()
if not opt.available():
Expand Down Expand Up @@ -109,3 +110,48 @@ def test_fix_and_unfix(self):
self.assertAlmostEqual(m.fx.value, 1, places=5)
self.assertAlmostEqual(m.fy.value, 0, places=5)
self.assertAlmostEqual(r.objective_bound, 0.5, places=5)


class TestHighsMiniDemos(unittest.TestCase):
def test_lp_methods(self):
for method in ("simplex", "ipm", "pdlp"):
# Build LP
m = pyo.ConcreteModel()
m.x = pyo.Var(domain=pyo.NonNegativeReals)
m.y = pyo.Var(domain=pyo.NonNegativeReals)
m.c = pyo.Constraint(expr=m.x + m.y >= 1)
m.obj = pyo.Objective(expr=m.x + m.y, sense=pyo.minimize)

solver = Highs()
solver.config.solver_options["solver"] = (
method # 'simplex' | 'ipm' | 'pdlp'
)
results = solver.solve(m)
self.assertTrue(results.solution_status == SolutionStatus.optimal)
self.assertTrue(results.iteration_count >= 0)

def test_mip_demo(self):
# Build MIP
m = pyo.ConcreteModel()
m.a = pyo.Var(domain=pyo.Binary)
m.b = pyo.Var(domain=pyo.Binary)
m.cap = pyo.Constraint(expr=m.a + m.b <= 1)
m.obj = pyo.Objective(expr=3 * m.a + 2 * m.b, sense=pyo.maximize)

solver = Highs()
results = solver.solve(m)
self.assertTrue(results.solution_status == SolutionStatus.optimal)
self.assertTrue(results.iteration_count >= 0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't the iteration count be strictly greater than 0 in this case?


def test_qp_demo(self):
# Build convex QP
m = pyo.ConcreteModel()
m.x = pyo.Var(domain=pyo.Reals, bounds=(0, None))
m.y = pyo.Var(domain=pyo.Reals, bounds=(0, None))
m.c = pyo.Constraint(expr=m.x + m.y >= 1)
m.obj = pyo.Objective(expr=(m.x - 1) ** 2 + (m.y - 2) ** 2, sense=pyo.minimize)

solver = Highs()
results = solver.solve(m)
self.assertTrue(results.solution_status == SolutionStatus.optimal)
self.assertTrue(results.iteration_count >= 0)
Loading