mirror of
https://github.com/ANL-CEEESA/MIPLearn.git
synced 2025-12-06 01:18:52 -06:00
Lazy: Minor fixes; make it compatible with Pyomo
This commit is contained in:
BIN
tests/fixtures/tsp-n20-00000.h5
vendored
BIN
tests/fixtures/tsp-n20-00000.h5
vendored
Binary file not shown.
BIN
tests/fixtures/tsp-n20-00000.mps.gz
vendored
BIN
tests/fixtures/tsp-n20-00000.mps.gz
vendored
Binary file not shown.
BIN
tests/fixtures/tsp-n20-00000.pkl.gz
vendored
BIN
tests/fixtures/tsp-n20-00000.pkl.gz
vendored
Binary file not shown.
BIN
tests/fixtures/tsp-n20-00001.h5
vendored
BIN
tests/fixtures/tsp-n20-00001.h5
vendored
Binary file not shown.
BIN
tests/fixtures/tsp-n20-00001.mps.gz
vendored
BIN
tests/fixtures/tsp-n20-00001.mps.gz
vendored
Binary file not shown.
BIN
tests/fixtures/tsp-n20-00001.pkl.gz
vendored
BIN
tests/fixtures/tsp-n20-00001.pkl.gz
vendored
Binary file not shown.
BIN
tests/fixtures/tsp-n20-00002.h5
vendored
BIN
tests/fixtures/tsp-n20-00002.h5
vendored
Binary file not shown.
BIN
tests/fixtures/tsp-n20-00002.mps.gz
vendored
BIN
tests/fixtures/tsp-n20-00002.mps.gz
vendored
Binary file not shown.
BIN
tests/fixtures/tsp-n20-00002.pkl.gz
vendored
BIN
tests/fixtures/tsp-n20-00002.pkl.gz
vendored
Binary file not shown.
44
tests/test_lazy_pyomo.py
Normal file
44
tests/test_lazy_pyomo.py
Normal file
@@ -0,0 +1,44 @@
|
||||
# MIPLearn: Extensible Framework for Learning-Enhanced Mixed-Integer Optimization
|
||||
# Copyright (C) 2020-2023, UChicago Argonne, LLC. All rights reserved.
|
||||
# Released under the modified BSD license. See COPYING.md for more details.
|
||||
import logging
|
||||
from typing import Any, Hashable, List
|
||||
|
||||
import pyomo.environ as pe
|
||||
|
||||
from miplearn.solvers.pyomo import PyomoModel
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def _build_model() -> PyomoModel:
|
||||
m = pe.ConcreteModel()
|
||||
m.x = pe.Var(bounds=(0, 5), domain=pe.Integers)
|
||||
m.obj = pe.Objective(expr=-m.x)
|
||||
m.cons = pe.ConstraintList()
|
||||
|
||||
def lazy_separate(model: PyomoModel) -> List[Hashable]:
|
||||
model.solver.cbGetSolution(vars=[m.x])
|
||||
if m.x.value > 0.5:
|
||||
return [m.x.value]
|
||||
else:
|
||||
return []
|
||||
|
||||
def lazy_enforce(model: PyomoModel, violations: List[Any]) -> None:
|
||||
for v in violations:
|
||||
model.add_constr(m.cons.add(m.x <= round(v - 1)))
|
||||
|
||||
return PyomoModel(
|
||||
m,
|
||||
"gurobi_persistent",
|
||||
lazy_separate=lazy_separate,
|
||||
lazy_enforce=lazy_enforce,
|
||||
)
|
||||
|
||||
|
||||
def test_pyomo_callback() -> None:
|
||||
model = _build_model()
|
||||
model.optimize()
|
||||
assert model.lazy_constrs_ is not None
|
||||
assert len(model.lazy_constrs_) > 0
|
||||
assert model.inner.x.value == 0.0
|
||||
Reference in New Issue
Block a user