Skip to content

Commit

Permalink
inform include three depth and pass while generating cuts
Browse files Browse the repository at this point in the history
  • Loading branch information
h-g-s committed Aug 3, 2020
1 parent f119888 commit 5e51c4e
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 11 deletions.
2 changes: 1 addition & 1 deletion examples/tsp-cuts-ulysses22.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class SubTourCutGenerator(ConstrsGenerator):
def __init__(self, Fl: List[Tuple[int, int]], x_):
self.F, self.x = Fl, x_

def generate_constrs(self, m_: Model):
def generate_constrs(self, m_: Model, depth: int = 0, npass: int = 0):
xf, cp, Gl = m_.translate(self.x), CutPool(), nx.DiGraph()
Ar = [(i, j) for (i, j) in Arcs if xf[i][j] and xf[i][j].x >= 1e-4]
for (u, v) in Ar:
Expand Down
2 changes: 1 addition & 1 deletion examples/tsp-cuts.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class SubTourCutGenerator(ConstrsGenerator):
def __init__(self, Fl: List[Tuple[int, int]], x_, V_):
self.F, self.x, self.V = Fl, x_, V_

def generate_constrs(self, model: Model):
def generate_constrs(self, model: Model, depth: int = 0, npass: int = 0):
xf, V_, cp, G = model.translate(self.x), self.V, CutPool(), nx.DiGraph()
for (u, v) in [(k, l) for (k, l) in product(V_, V_) if k != l and xf[k][l]]:
G.add_edge(u, v, capacity=xf[u][v].x)
Expand Down
2 changes: 1 addition & 1 deletion examples/tsp-lazy-ulysses22.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class SubTourLazyGenerator(ConstrsGenerator):
def __init__(self, xv):
self._x = xv

def generate_constrs(self, model: Model):
def generate_constrs(self, model: Model, depth: int = 0, npass: int = 0):
x_, N, cp = model.translate(self._x), range(len(self._x)), CutPool()
outa = [[j for j in N if x_[i][j].x >= 0.99] for i in N]

Expand Down
8 changes: 6 additions & 2 deletions mip/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,14 @@ def generate_columns(self, model: "mip.Model"):


class ConstrsGenerator:
"""Abstract class for implementing cuts and lazy constraints generators."""
"""Abstract class for implementing cuts and lazy constraints generators.
"""

def __init__(self):
pass

def generate_constrs(self, model: "mip.Model"):
def generate_constrs(self, model: "mip.Model", depth: int = 0, npass: int = 0):
"""Method called by the solver engine to generate *cuts* or *lazy constraints*.
After analyzing the contents of the solution in model
Expand Down Expand Up @@ -80,6 +82,8 @@ def generate_constrs(self, model: "mip.Model"):
query model properties and add cuts (:meth:`~mip.Model.add_cut`) or lazy constraints
(:meth:`~mip.Model.add_lazy_constr`), but you cannot
perform other model modifications, such as add columns.
depth(int): depth of the search tree (0 is the root node)
npass(int): current number of cut passes in this node
"""
raise NotImplementedError()

Expand Down
13 changes: 7 additions & 6 deletions mip/cbc.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,7 @@
const double *dvec, int nint, const int *ivec,
int nchar, char **cvec);
typedef void(*cbc_cut_callback)(void *osiSolver,
void *osiCuts, void *appdata);
typedef void(*cbc_cut_callback)(void *osiSolver, void *osiCuts, void *appdata, int level, int npass);
typedef int (*cbc_incumbent_callback)(void *cbcModel,
double obj, int nz,
Expand Down Expand Up @@ -985,10 +984,10 @@ def cbc_inc_callback(
# cut callback
@ffi.callback(
"""
void (void *osi_solver, void *osi_cuts, void *app_data)
void (void *osi_solver, void *osi_cuts, void *app_data, int level, int npass)
"""
)
def cbc_cut_callback(osi_solver, osi_cuts, app_data):
def cbc_cut_callback(osi_solver, osi_cuts, app_data, depth, npass):
if (
osi_solver == ffi.NULL
or osi_cuts == ffi.NULL
Expand Down Expand Up @@ -1017,9 +1016,11 @@ def cbc_cut_callback(osi_solver, osi_cuts, app_data):
osi_model.solver.osi_cutsp = osi_cuts
osi_model.fractional = fractional
if fractional and self.model.cuts_generator:
self.model.cuts_generator.generate_constrs(osi_model)
self.model.cuts_generator.generate_constrs(osi_model, depth, npass)
if (not fractional) and self.model.lazy_constrs_generator:
self.model.lazy_constrs_generator.generate_constrs(osi_model)
self.model.lazy_constrs_generator.generate_constrs(
osi_model, depth, npass
)

if self.__verbose == 0:
cbclib.Cbc_setLogLevel(self._model, 0)
Expand Down

0 comments on commit 5e51c4e

Please sign in to comment.