|
|
@ -17,7 +17,7 @@ from miplearn.components.lazy_dynamic import DynamicLazyConstraintsComponent
|
|
|
|
from miplearn.components.objective import ObjectiveValueComponent
|
|
|
|
from miplearn.components.objective import ObjectiveValueComponent
|
|
|
|
from miplearn.components.primal import PrimalSolutionComponent
|
|
|
|
from miplearn.components.primal import PrimalSolutionComponent
|
|
|
|
from miplearn.instance import Instance
|
|
|
|
from miplearn.instance import Instance
|
|
|
|
from miplearn.solvers import RedirectOutput
|
|
|
|
from miplearn.solvers import _RedirectOutput
|
|
|
|
from miplearn.solvers.internal import InternalSolver
|
|
|
|
from miplearn.solvers.internal import InternalSolver
|
|
|
|
from miplearn.solvers.pyomo.gurobi import GurobiPyomoSolver
|
|
|
|
from miplearn.solvers.pyomo.gurobi import GurobiPyomoSolver
|
|
|
|
from miplearn.types import MIPSolveStats, TrainingSample
|
|
|
|
from miplearn.types import MIPSolveStats, TrainingSample
|
|
|
@ -25,7 +25,7 @@ from miplearn.types import MIPSolveStats, TrainingSample
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class GlobalVariables:
|
|
|
|
class _GlobalVariables:
|
|
|
|
def __init__(self) -> None:
|
|
|
|
def __init__(self) -> None:
|
|
|
|
self.solver: Optional[LearningSolver] = None
|
|
|
|
self.solver: Optional[LearningSolver] = None
|
|
|
|
self.instances: Optional[Union[List[str], List[Instance]]] = None
|
|
|
|
self.instances: Optional[Union[List[str], List[Instance]]] = None
|
|
|
@ -36,14 +36,14 @@ class GlobalVariables:
|
|
|
|
# Global variables used for multiprocessing. Global variables are copied by the
|
|
|
|
# Global variables used for multiprocessing. Global variables are copied by the
|
|
|
|
# operating system when the process forks. Local variables are copied through
|
|
|
|
# operating system when the process forks. Local variables are copied through
|
|
|
|
# serialization, which is a much slower process.
|
|
|
|
# serialization, which is a much slower process.
|
|
|
|
GLOBAL = [GlobalVariables()]
|
|
|
|
_GLOBAL = [_GlobalVariables()]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _parallel_solve(idx):
|
|
|
|
def _parallel_solve(idx):
|
|
|
|
solver = GLOBAL[0].solver
|
|
|
|
solver = _GLOBAL[0].solver
|
|
|
|
instances = GLOBAL[0].instances
|
|
|
|
instances = _GLOBAL[0].instances
|
|
|
|
output_filenames = GLOBAL[0].output_filenames
|
|
|
|
output_filenames = _GLOBAL[0].output_filenames
|
|
|
|
discard_outputs = GLOBAL[0].discard_outputs
|
|
|
|
discard_outputs = _GLOBAL[0].discard_outputs
|
|
|
|
if output_filenames is None:
|
|
|
|
if output_filenames is None:
|
|
|
|
output_filename = None
|
|
|
|
output_filename = None
|
|
|
|
else:
|
|
|
|
else:
|
|
|
@ -64,28 +64,26 @@ class LearningSolver:
|
|
|
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
----------
|
|
|
|
components: [Component]
|
|
|
|
components: List[Component]
|
|
|
|
Set of components in the solver. By default, includes:
|
|
|
|
Set of components in the solver. By default, includes
|
|
|
|
- ObjectiveValueComponent
|
|
|
|
`ObjectiveValueComponent`, `PrimalSolutionComponent`,
|
|
|
|
- PrimalSolutionComponent
|
|
|
|
`DynamicLazyConstraintsComponent` and `UserCutsComponent`.
|
|
|
|
- DynamicLazyConstraintsComponent
|
|
|
|
mode: str
|
|
|
|
- UserCutsComponent
|
|
|
|
|
|
|
|
mode:
|
|
|
|
|
|
|
|
If "exact", solves problem to optimality, keeping all optimality
|
|
|
|
If "exact", solves problem to optimality, keeping all optimality
|
|
|
|
guarantees provided by the MIP solver. If "heuristic", uses machine
|
|
|
|
guarantees provided by the MIP solver. If "heuristic", uses machine
|
|
|
|
learning more aggressively, and may return suboptimal solutions.
|
|
|
|
learning more aggressively, and may return suboptimal solutions.
|
|
|
|
solver:
|
|
|
|
solver: Callable[[], InternalSolver]
|
|
|
|
A callable that constructs the internal solver. If None is provided,
|
|
|
|
A callable that constructs the internal solver. If None is provided,
|
|
|
|
use GurobiPyomoSolver.
|
|
|
|
use GurobiPyomoSolver.
|
|
|
|
use_lazy_cb:
|
|
|
|
use_lazy_cb: bool
|
|
|
|
If true, use native solver callbacks for enforcing lazy constraints,
|
|
|
|
If true, use native solver callbacks for enforcing lazy constraints,
|
|
|
|
instead of a simple loop. May not be supported by all solvers.
|
|
|
|
instead of a simple loop. May not be supported by all solvers.
|
|
|
|
solve_lp_first:
|
|
|
|
solve_lp_first: bool
|
|
|
|
If true, solve LP relaxation first, then solve original MILP. This
|
|
|
|
If true, solve LP relaxation first, then solve original MILP. This
|
|
|
|
option should be activated if the LP relaxation is not very
|
|
|
|
option should be activated if the LP relaxation is not very
|
|
|
|
expensive to solve and if it provides good hints for the integer
|
|
|
|
expensive to solve and if it provides good hints for the integer
|
|
|
|
solution.
|
|
|
|
solution.
|
|
|
|
simulate_perfect:
|
|
|
|
simulate_perfect: bool
|
|
|
|
If true, each call to solve actually performs three actions: solve
|
|
|
|
If true, each call to solve actually performs three actions: solve
|
|
|
|
the original problem, train the ML models on the data that was just
|
|
|
|
the original problem, train the ML models on the data that was just
|
|
|
|
collected, and solve the problem again. This is useful for evaluating
|
|
|
|
collected, and solve the problem again. This is useful for evaluating
|
|
|
@ -150,7 +148,7 @@ class LearningSolver:
|
|
|
|
|
|
|
|
|
|
|
|
# Generate model
|
|
|
|
# Generate model
|
|
|
|
if model is None:
|
|
|
|
if model is None:
|
|
|
|
with RedirectOutput([]):
|
|
|
|
with _RedirectOutput([]):
|
|
|
|
model = instance.to_model()
|
|
|
|
model = instance.to_model()
|
|
|
|
|
|
|
|
|
|
|
|
# Initialize training sample
|
|
|
|
# Initialize training sample
|
|
|
@ -261,23 +259,23 @@ class LearningSolver:
|
|
|
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
----------
|
|
|
|
instance:
|
|
|
|
instance: Union[Instance, str]
|
|
|
|
The instance to be solved, or a filename.
|
|
|
|
The instance to be solved, or a filename.
|
|
|
|
model:
|
|
|
|
model: Any
|
|
|
|
The corresponding Pyomo model. If not provided, it will be created.
|
|
|
|
The corresponding Pyomo model. If not provided, it will be created.
|
|
|
|
output_filename:
|
|
|
|
output_filename: Optional[str]
|
|
|
|
If instance is a filename and output_filename is provided, write the
|
|
|
|
If instance is a filename and output_filename is provided, write the
|
|
|
|
modified instance to this file, instead of replacing the original one. If
|
|
|
|
modified instance to this file, instead of replacing the original one. If
|
|
|
|
output_filename is None (the default), modified the original file in-place.
|
|
|
|
output_filename is None (the default), modified the original file in-place.
|
|
|
|
discard_output:
|
|
|
|
discard_output: bool
|
|
|
|
If True, do not write the modified instances anywhere; simply discard
|
|
|
|
If True, do not write the modified instances anywhere; simply discard
|
|
|
|
them. Useful during benchmarking.
|
|
|
|
them. Useful during benchmarking.
|
|
|
|
tee:
|
|
|
|
tee: bool
|
|
|
|
If true, prints solver log to screen.
|
|
|
|
If true, prints solver log to screen.
|
|
|
|
|
|
|
|
|
|
|
|
Returns
|
|
|
|
Returns
|
|
|
|
-------
|
|
|
|
-------
|
|
|
|
dict
|
|
|
|
MIPSolveStats
|
|
|
|
A dictionary of solver statistics containing at least the following
|
|
|
|
A dictionary of solver statistics containing at least the following
|
|
|
|
keys: "Lower bound", "Upper bound", "Wallclock time", "Nodes",
|
|
|
|
keys: "Lower bound", "Upper bound", "Wallclock time", "Nodes",
|
|
|
|
"Sense", "Log", "Warm start value" and "LP value".
|
|
|
|
"Sense", "Log", "Warm start value" and "LP value".
|
|
|
@ -324,34 +322,33 @@ class LearningSolver:
|
|
|
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
----------
|
|
|
|
output_filenames:
|
|
|
|
output_filenames: Optional[List[str]]
|
|
|
|
If instances are file names and output_filenames is provided, write the
|
|
|
|
If instances are file names and output_filenames is provided, write the
|
|
|
|
modified instances to these files, instead of replacing the original
|
|
|
|
modified instances to these files, instead of replacing the original
|
|
|
|
files. If output_filenames is None, modifies the instances in-place.
|
|
|
|
files. If output_filenames is None, modifies the instances in-place.
|
|
|
|
discard_outputs:
|
|
|
|
discard_outputs: bool
|
|
|
|
If True, do not write the modified instances anywhere; simply discard
|
|
|
|
If True, do not write the modified instances anywhere; simply discard
|
|
|
|
them instead. Useful during benchmarking.
|
|
|
|
them instead. Useful during benchmarking.
|
|
|
|
label:
|
|
|
|
label: str
|
|
|
|
Label to show in the progress bar.
|
|
|
|
Label to show in the progress bar.
|
|
|
|
instances:
|
|
|
|
instances: Union[List[str], List[Instance]]
|
|
|
|
The instances to be solved
|
|
|
|
The instances to be solved
|
|
|
|
n_jobs:
|
|
|
|
n_jobs: int
|
|
|
|
Number of instances to solve in parallel at a time.
|
|
|
|
Number of instances to solve in parallel at a time.
|
|
|
|
|
|
|
|
|
|
|
|
Returns
|
|
|
|
Returns
|
|
|
|
-------
|
|
|
|
-------
|
|
|
|
Returns a list of dictionaries, with one entry for each provided instance.
|
|
|
|
List[MIPSolveStats]
|
|
|
|
This dictionary is the same you would obtain by calling:
|
|
|
|
List of solver statistics, with one entry for each provided instance.
|
|
|
|
|
|
|
|
The list is the same you would obtain by calling
|
|
|
|
[solver.solve(p) for p in instances]
|
|
|
|
`[solver.solve(p) for p in instances]`
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
self.internal_solver = None
|
|
|
|
self.internal_solver = None
|
|
|
|
self._silence_miplearn_logger()
|
|
|
|
self._silence_miplearn_logger()
|
|
|
|
GLOBAL[0].solver = self
|
|
|
|
_GLOBAL[0].solver = self
|
|
|
|
GLOBAL[0].output_filenames = output_filenames
|
|
|
|
_GLOBAL[0].output_filenames = output_filenames
|
|
|
|
GLOBAL[0].instances = instances
|
|
|
|
_GLOBAL[0].instances = instances
|
|
|
|
GLOBAL[0].discard_outputs = discard_outputs
|
|
|
|
_GLOBAL[0].discard_outputs = discard_outputs
|
|
|
|
results = p_map(
|
|
|
|
results = p_map(
|
|
|
|
_parallel_solve,
|
|
|
|
_parallel_solve,
|
|
|
|
list(range(len(instances))),
|
|
|
|
list(range(len(instances))),
|
|
|
|