|
|
|
@ -5,7 +5,7 @@
|
|
|
|
|
import logging
|
|
|
|
|
import re
|
|
|
|
|
import sys
|
|
|
|
|
from abc import ABC, abstractmethod
|
|
|
|
|
from abc import abstractmethod
|
|
|
|
|
from io import StringIO
|
|
|
|
|
|
|
|
|
|
import pyomo
|
|
|
|
@ -20,17 +20,6 @@ logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class PyomoSolver(InternalSolver):
|
|
|
|
|
"""
|
|
|
|
|
Base class for all Pyomo-based InternalSolvers.
|
|
|
|
|
|
|
|
|
|
Attributes
|
|
|
|
|
----------
|
|
|
|
|
instance: miplearn.Instance
|
|
|
|
|
The MIPLearn instance currently loaded to the solver
|
|
|
|
|
model: pyomo.core.ConcreteModel
|
|
|
|
|
The Pyomo model currently loaded on the solver
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
self.instance = None
|
|
|
|
|
self.model = None
|
|
|
|
@ -42,20 +31,6 @@ class PyomoSolver(InternalSolver):
|
|
|
|
|
self._varname_to_var = {}
|
|
|
|
|
|
|
|
|
|
def solve_lp(self, tee=False):
|
|
|
|
|
"""
|
|
|
|
|
Solves the LP relaxation of the currently loaded instance.
|
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
|
|
|
|
tee: bool
|
|
|
|
|
If true, prints the solver log to the screen.
|
|
|
|
|
|
|
|
|
|
Returns
|
|
|
|
|
-------
|
|
|
|
|
dict
|
|
|
|
|
A dictionary of solver statistics containing the following keys:
|
|
|
|
|
"Optimal value".
|
|
|
|
|
"""
|
|
|
|
|
for var in self._bin_vars:
|
|
|
|
|
lb, ub = var.bounds
|
|
|
|
|
var.setlb(lb)
|
|
|
|
@ -71,16 +46,6 @@ class PyomoSolver(InternalSolver):
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def get_solution(self):
|
|
|
|
|
"""
|
|
|
|
|
Returns current solution found by the solver.
|
|
|
|
|
|
|
|
|
|
If called after `solve`, returns the best primal solution found during
|
|
|
|
|
the search. If called after `solve_lp`, returns the optimal solution
|
|
|
|
|
to the LP relaxation.
|
|
|
|
|
|
|
|
|
|
The solution is a dictionary `sol`, where the optimal value of `var[idx]`
|
|
|
|
|
is given by `sol[var][idx]`.
|
|
|
|
|
"""
|
|
|
|
|
solution = {}
|
|
|
|
|
for var in self.model.component_objects(Var):
|
|
|
|
|
solution[str(var)] = {}
|
|
|
|
@ -89,14 +54,6 @@ class PyomoSolver(InternalSolver):
|
|
|
|
|
return solution
|
|
|
|
|
|
|
|
|
|
def set_warm_start(self, solution):
|
|
|
|
|
"""
|
|
|
|
|
Sets the warm start to be used by the solver.
|
|
|
|
|
|
|
|
|
|
The solution should be a dictionary following the same format as the
|
|
|
|
|
one produced by `get_solution`. Only one warm start is currently
|
|
|
|
|
supported. Calling this function when a warm start already exists will
|
|
|
|
|
remove the previous warm start.
|
|
|
|
|
"""
|
|
|
|
|
self.clear_warm_start()
|
|
|
|
|
count_total, count_fixed = 0, 0
|
|
|
|
|
for var_name in solution:
|
|
|
|
@ -112,26 +69,12 @@ class PyomoSolver(InternalSolver):
|
|
|
|
|
(count_fixed, count_total))
|
|
|
|
|
|
|
|
|
|
def clear_warm_start(self):
|
|
|
|
|
"""
|
|
|
|
|
Removes any existing warm start from the solver.
|
|
|
|
|
"""
|
|
|
|
|
for var in self._all_vars:
|
|
|
|
|
if not var.fixed:
|
|
|
|
|
var.value = None
|
|
|
|
|
self._is_warm_start_available = False
|
|
|
|
|
|
|
|
|
|
def set_instance(self, instance, model=None):
|
|
|
|
|
"""
|
|
|
|
|
Loads the given instance into the solver.
|
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
|
|
|
|
instance: miplearn.Instance
|
|
|
|
|
The instance to be loaded.
|
|
|
|
|
model: pyomo.core.ConcreteModel
|
|
|
|
|
The corresponding Pyomo model. If not provided, it will be
|
|
|
|
|
generated by calling `instance.to_model()`.
|
|
|
|
|
"""
|
|
|
|
|
if model is None:
|
|
|
|
|
model = instance.to_model()
|
|
|
|
|
assert isinstance(instance, Instance)
|
|
|
|
@ -157,13 +100,6 @@ class PyomoSolver(InternalSolver):
|
|
|
|
|
self._bin_vars += [var[idx]]
|
|
|
|
|
|
|
|
|
|
def fix(self, solution):
|
|
|
|
|
"""
|
|
|
|
|
Fixes the values of a subset of decision variables.
|
|
|
|
|
|
|
|
|
|
The values should be provided in the dictionary format generated by
|
|
|
|
|
`get_solution`. Missing values in the solution indicate variables
|
|
|
|
|
that should be left free.
|
|
|
|
|
"""
|
|
|
|
|
count_total, count_fixed = 0, 0
|
|
|
|
|
for varname in solution:
|
|
|
|
|
for index in solution[varname]:
|
|
|
|
@ -178,27 +114,9 @@ class PyomoSolver(InternalSolver):
|
|
|
|
|
(count_fixed, count_total))
|
|
|
|
|
|
|
|
|
|
def add_constraint(self, constraint):
|
|
|
|
|
"""
|
|
|
|
|
Adds a single constraint to the model.
|
|
|
|
|
"""
|
|
|
|
|
self._pyomo_solver.add_constraint(constraint)
|
|
|
|
|
|
|
|
|
|
def solve(self, tee=False):
|
|
|
|
|
"""
|
|
|
|
|
Solves the currently loaded instance.
|
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
|
|
|
|
tee: bool
|
|
|
|
|
If true, prints the solver log to the screen.
|
|
|
|
|
|
|
|
|
|
Returns
|
|
|
|
|
-------
|
|
|
|
|
dict
|
|
|
|
|
A dictionary of solver statistics containing the following keys:
|
|
|
|
|
"Lower bound", "Upper bound", "Wallclock time", "Nodes", "Sense",
|
|
|
|
|
"Log" and "Warm start value".
|
|
|
|
|
"""
|
|
|
|
|
total_wallclock_time = 0
|
|
|
|
|
streams = [StringIO()]
|
|
|
|
|
if tee:
|
|
|
|
@ -244,20 +162,12 @@ class PyomoSolver(InternalSolver):
|
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
|
def _extract_warm_start_value(self, log):
|
|
|
|
|
"""
|
|
|
|
|
Extracts and returns the objective value of the user-provided MIP start
|
|
|
|
|
from the provided solver log. If more than one value is found, returns
|
|
|
|
|
the last one. If no value is present in the logs, returns None.
|
|
|
|
|
"""
|
|
|
|
|
value = self.__extract(log, self._get_warm_start_regexp())
|
|
|
|
|
if value is not None:
|
|
|
|
|
value = float(value)
|
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
|
def _extract_node_count(self, log):
|
|
|
|
|
"""
|
|
|
|
|
Extracts and returns the number of explored branch-and-bound nodes.
|
|
|
|
|
"""
|
|
|
|
|
return int(self.__extract(log,
|
|
|
|
|
self._get_node_count_regexp(),
|
|
|
|
|
default=1))
|
|
|
|
@ -301,6 +211,3 @@ class PyomoSolver(InternalSolver):
|
|
|
|
|
@abstractmethod
|
|
|
|
|
def _get_gap_tolerance_option_name(self):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|