parent
f3fd1e0cda
commit
a8224b5a38
@ -1,3 +0,0 @@
|
|||||||
# MIPLearn: Extensible Framework for Learning-Enhanced Mixed-Integer Optimization
|
|
||||||
# Copyright (C) 2020-2021, UChicago Argonne, LLC. All rights reserved.
|
|
||||||
# Released under the modified BSD license. See COPYING.md for more details.
|
|
@ -1,44 +0,0 @@
|
|||||||
# MIPLearn: Extensible Framework for Learning-Enhanced Mixed-Integer Optimization
|
|
||||||
# Copyright (C) 2020-2021, UChicago Argonne, LLC. All rights reserved.
|
|
||||||
# Released under the modified BSD license. See COPYING.md for more details.
|
|
||||||
|
|
||||||
from typing import Any
|
|
||||||
|
|
||||||
from overrides import overrides
|
|
||||||
from pyomo import environ as pe
|
|
||||||
|
|
||||||
from miplearn.instance.base import Instance
|
|
||||||
from miplearn.solvers.gurobi import GurobiSolver
|
|
||||||
from miplearn.solvers.pyomo.base import BasePyomoSolver
|
|
||||||
from tests.solvers import _is_subclass_or_instance
|
|
||||||
|
|
||||||
|
|
||||||
class InfeasiblePyomoInstance(Instance):
|
|
||||||
@overrides
|
|
||||||
def to_model(self) -> pe.ConcreteModel:
|
|
||||||
model = pe.ConcreteModel()
|
|
||||||
model.x = pe.Var([0], domain=pe.Binary)
|
|
||||||
model.OBJ = pe.Objective(expr=model.x[0], sense=pe.maximize)
|
|
||||||
model.eq = pe.Constraint(expr=model.x[0] >= 2)
|
|
||||||
return model
|
|
||||||
|
|
||||||
|
|
||||||
class InfeasibleGurobiInstance(Instance):
|
|
||||||
@overrides
|
|
||||||
def to_model(self) -> Any:
|
|
||||||
import gurobipy as gp
|
|
||||||
from gurobipy import GRB
|
|
||||||
|
|
||||||
model = gp.Model()
|
|
||||||
x = model.addVars(1, vtype=GRB.BINARY, name="x")
|
|
||||||
model.addConstr(x[0] >= 2)
|
|
||||||
model.setObjective(x[0])
|
|
||||||
return model
|
|
||||||
|
|
||||||
|
|
||||||
def get_infeasible_instance(solver: Any) -> Instance:
|
|
||||||
if _is_subclass_or_instance(solver, BasePyomoSolver):
|
|
||||||
return InfeasiblePyomoInstance()
|
|
||||||
if _is_subclass_or_instance(solver, GurobiSolver):
|
|
||||||
return InfeasibleGurobiInstance()
|
|
||||||
assert False
|
|
@ -1,50 +0,0 @@
|
|||||||
# MIPLearn: Extensible Framework for Learning-Enhanced Mixed-Integer Optimization
|
|
||||||
# Copyright (C) 2020-2021, UChicago Argonne, LLC. All rights reserved.
|
|
||||||
# Released under the modified BSD license. See COPYING.md for more details.
|
|
||||||
|
|
||||||
from typing import List, Any, Tuple
|
|
||||||
|
|
||||||
from miplearn.instance.base import Instance
|
|
||||||
from miplearn.problems.knapsack import KnapsackInstance, GurobiKnapsackInstance
|
|
||||||
from miplearn.solvers.gurobi import GurobiSolver
|
|
||||||
from miplearn.solvers.internal import InternalSolver
|
|
||||||
from miplearn.solvers.learning import LearningSolver
|
|
||||||
from miplearn.solvers.pyomo.base import BasePyomoSolver
|
|
||||||
from tests.solvers import _is_subclass_or_instance
|
|
||||||
|
|
||||||
|
|
||||||
def get_test_pyomo_instances() -> Tuple[List[Instance], List[Any]]:
|
|
||||||
instances: List[Instance] = [
|
|
||||||
KnapsackInstance(
|
|
||||||
weights=[23.0, 26.0, 20.0, 18.0],
|
|
||||||
prices=[505.0, 352.0, 458.0, 220.0],
|
|
||||||
capacity=67.0,
|
|
||||||
),
|
|
||||||
KnapsackInstance(
|
|
||||||
weights=[25.0, 30.0, 22.0, 18.0],
|
|
||||||
prices=[500.0, 365.0, 420.0, 150.0],
|
|
||||||
capacity=70.0,
|
|
||||||
),
|
|
||||||
]
|
|
||||||
models = [instance.to_model() for instance in instances]
|
|
||||||
solver = LearningSolver()
|
|
||||||
for i in range(len(instances)):
|
|
||||||
solver.solve(instances[i], models[i])
|
|
||||||
return instances, models
|
|
||||||
|
|
||||||
|
|
||||||
def get_knapsack_instance(solver: InternalSolver) -> Instance:
|
|
||||||
if _is_subclass_or_instance(solver, BasePyomoSolver):
|
|
||||||
return KnapsackInstance(
|
|
||||||
weights=[23.0, 26.0, 20.0, 18.0],
|
|
||||||
prices=[505.0, 352.0, 458.0, 220.0],
|
|
||||||
capacity=67.0,
|
|
||||||
)
|
|
||||||
elif _is_subclass_or_instance(solver, GurobiSolver):
|
|
||||||
return GurobiKnapsackInstance(
|
|
||||||
weights=[23.0, 26.0, 20.0, 18.0],
|
|
||||||
prices=[505.0, 352.0, 458.0, 220.0],
|
|
||||||
capacity=67.0,
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
assert False
|
|
@ -1,42 +0,0 @@
|
|||||||
# MIPLearn: Extensible Framework for Learning-Enhanced Mixed-Integer Optimization
|
|
||||||
# Copyright (C) 2020-2021, UChicago Argonne, LLC. All rights reserved.
|
|
||||||
# Released under the modified BSD license. See COPYING.md for more details.
|
|
||||||
from typing import Any
|
|
||||||
|
|
||||||
import pyomo.environ as pe
|
|
||||||
|
|
||||||
from miplearn.instance.base import Instance
|
|
||||||
from miplearn.solvers.gurobi import GurobiSolver
|
|
||||||
from miplearn.solvers.pyomo.base import BasePyomoSolver
|
|
||||||
from tests.solvers import _is_subclass_or_instance
|
|
||||||
|
|
||||||
|
|
||||||
class PyomoInstanceWithRedundancy(Instance):
|
|
||||||
def to_model(self) -> pe.ConcreteModel:
|
|
||||||
model = pe.ConcreteModel()
|
|
||||||
model.x = pe.Var([0, 1], domain=pe.Binary)
|
|
||||||
model.OBJ = pe.Objective(expr=model.x[0] + model.x[1], sense=pe.maximize)
|
|
||||||
model.eq1 = pe.Constraint(expr=model.x[0] + model.x[1] <= 1)
|
|
||||||
model.eq2 = pe.Constraint(expr=model.x[0] + model.x[1] <= 2)
|
|
||||||
return model
|
|
||||||
|
|
||||||
|
|
||||||
class GurobiInstanceWithRedundancy(Instance):
|
|
||||||
def to_model(self) -> Any:
|
|
||||||
import gurobipy as gp
|
|
||||||
from gurobipy import GRB
|
|
||||||
|
|
||||||
model = gp.Model()
|
|
||||||
x = model.addVars(2, vtype=GRB.BINARY, name="x")
|
|
||||||
model.addConstr(x[0] + x[1] <= 1)
|
|
||||||
model.addConstr(x[0] + x[1] <= 2)
|
|
||||||
model.setObjective(x[0] + x[1], GRB.MAXIMIZE)
|
|
||||||
return model
|
|
||||||
|
|
||||||
|
|
||||||
def get_instance_with_redundancy(solver: Any) -> Instance:
|
|
||||||
if _is_subclass_or_instance(solver, BasePyomoSolver):
|
|
||||||
return PyomoInstanceWithRedundancy()
|
|
||||||
if _is_subclass_or_instance(solver, GurobiSolver):
|
|
||||||
return GurobiInstanceWithRedundancy()
|
|
||||||
assert False
|
|
@ -1,33 +0,0 @@
|
|||||||
# MIPLearn: Extensible Framework for Learning-Enhanced Mixed-Integer Optimization
|
|
||||||
# Copyright (C) 2020-2021, UChicago Argonne, LLC. All rights reserved.
|
|
||||||
# Released under the modified BSD license. See COPYING.md for more details.
|
|
||||||
|
|
||||||
from inspect import isclass
|
|
||||||
from typing import Any
|
|
||||||
|
|
||||||
from miplearn.instance.base import Instance
|
|
||||||
from miplearn.problems.knapsack import KnapsackInstance, GurobiKnapsackInstance
|
|
||||||
from miplearn.solvers.gurobi import GurobiSolver
|
|
||||||
from miplearn.solvers.pyomo.base import BasePyomoSolver
|
|
||||||
|
|
||||||
|
|
||||||
def _is_subclass_or_instance(obj: Any, parent_class: Any) -> bool:
|
|
||||||
return isinstance(obj, parent_class) or (
|
|
||||||
isclass(obj) and issubclass(obj, parent_class)
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def _get_knapsack_instance(solver: Any) -> Instance:
|
|
||||||
if _is_subclass_or_instance(solver, BasePyomoSolver):
|
|
||||||
return KnapsackInstance(
|
|
||||||
weights=[23.0, 26.0, 20.0, 18.0],
|
|
||||||
prices=[505.0, 352.0, 458.0, 220.0],
|
|
||||||
capacity=67.0,
|
|
||||||
)
|
|
||||||
if _is_subclass_or_instance(solver, GurobiSolver):
|
|
||||||
return GurobiKnapsackInstance(
|
|
||||||
weights=[23.0, 26.0, 20.0, 18.0],
|
|
||||||
prices=[505.0, 352.0, 458.0, 220.0],
|
|
||||||
capacity=67.0,
|
|
||||||
)
|
|
||||||
assert False
|
|
Loading…
Reference in new issue