mirror of
https://github.com/ANL-CEEESA/MIPLearn.git
synced 2025-12-06 09:28:51 -06:00
Component: rename iteration_cb and lazy_cb
This commit is contained in:
@@ -22,8 +22,8 @@ class Component(ABC):
|
|||||||
def fit(self, training_instances):
|
def fit(self, training_instances):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def after_iteration(self, solver, instance, model):
|
def iteration_cb(self, solver, instance, model):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def on_lazy_callback(self, solver, instance, model):
|
def lazy_cb(self, solver, instance, model):
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ class DynamicLazyConstraintsComponent(Component):
|
|||||||
cut = instance.build_lazy_constraint(model, v)
|
cut = instance.build_lazy_constraint(model, v)
|
||||||
solver.internal_solver.add_constraint(cut)
|
solver.internal_solver.add_constraint(cut)
|
||||||
|
|
||||||
def after_iteration(self, solver, instance, model):
|
def iteration_cb(self, solver, instance, model):
|
||||||
logger.debug("Finding violated (dynamic) lazy constraints...")
|
logger.debug("Finding violated (dynamic) lazy constraints...")
|
||||||
violations = instance.find_violated_lazy_constraints(model)
|
violations = instance.find_violated_lazy_constraints(model)
|
||||||
if len(violations) == 0:
|
if len(violations) == 0:
|
||||||
|
|||||||
@@ -51,7 +51,7 @@ class StaticLazyConstraintsComponent(Component):
|
|||||||
def after_solve(self, solver, instance, model, results):
|
def after_solve(self, solver, instance, model, results):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def after_iteration(self, solver, instance, model):
|
def iteration_cb(self, solver, instance, model):
|
||||||
if solver.use_lazy_cb:
|
if solver.use_lazy_cb:
|
||||||
return False
|
return False
|
||||||
else:
|
else:
|
||||||
@@ -67,7 +67,7 @@ class StaticLazyConstraintsComponent(Component):
|
|||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def on_lazy_callback(self, solver, instance, model):
|
def lazy_cb(self, solver, instance, model):
|
||||||
self._check_and_add(instance, solver)
|
self._check_and_add(instance, solver)
|
||||||
|
|
||||||
def _check_and_add(self, instance, solver):
|
def _check_and_add(self, instance, solver):
|
||||||
|
|||||||
@@ -101,7 +101,7 @@ def test_usage_with_solver():
|
|||||||
internal.add_constraint.reset_mock()
|
internal.add_constraint.reset_mock()
|
||||||
|
|
||||||
# LearningSolver calls after_iteration (first time)
|
# LearningSolver calls after_iteration (first time)
|
||||||
should_repeat = component.after_iteration(solver, instance, None)
|
should_repeat = component.iteration_cb(solver, instance, None)
|
||||||
assert should_repeat
|
assert should_repeat
|
||||||
|
|
||||||
# Should ask internal solver to verify if constraints in the pool are
|
# Should ask internal solver to verify if constraints in the pool are
|
||||||
@@ -112,7 +112,7 @@ def test_usage_with_solver():
|
|||||||
internal.add_constraint.reset_mock()
|
internal.add_constraint.reset_mock()
|
||||||
|
|
||||||
# LearningSolver calls after_iteration (second time)
|
# LearningSolver calls after_iteration (second time)
|
||||||
should_repeat = component.after_iteration(solver, instance, None)
|
should_repeat = component.iteration_cb(solver, instance, None)
|
||||||
assert not should_repeat
|
assert not should_repeat
|
||||||
|
|
||||||
# The lazy constraint pool should be empty by now, so no calls should be made
|
# The lazy constraint pool should be empty by now, so no calls should be made
|
||||||
|
|||||||
@@ -135,7 +135,7 @@ class InternalSolver(ABC):
|
|||||||
lazy_cb: (internal_solver, model) -> None
|
lazy_cb: (internal_solver, model) -> None
|
||||||
This function is called whenever the solver finds a new candidate
|
This function is called whenever the solver finds a new candidate
|
||||||
solution and can be used to add lazy constraints to the model. Only
|
solution and can be used to add lazy constraints to the model. Only
|
||||||
two operations within the callback are allowed:
|
the following operations within the callback are allowed:
|
||||||
- Querying the value of a variable, through `get_value(var, idx)`
|
- Querying the value of a variable, through `get_value(var, idx)`
|
||||||
- Querying if a constraint is satisfied, through `is_constraint_satisfied(cobj)`
|
- Querying if a constraint is satisfied, through `is_constraint_satisfied(cobj)`
|
||||||
- Adding a new constraint to the problem, through `add_constraint`
|
- Adding a new constraint to the problem, through `add_constraint`
|
||||||
|
|||||||
@@ -209,13 +209,13 @@ class LearningSolver:
|
|||||||
def iteration_cb():
|
def iteration_cb():
|
||||||
should_repeat = False
|
should_repeat = False
|
||||||
for comp in self.components.values():
|
for comp in self.components.values():
|
||||||
if comp.after_iteration(self, instance, model):
|
if comp.iteration_cb(self, instance, model):
|
||||||
should_repeat = True
|
should_repeat = True
|
||||||
return should_repeat
|
return should_repeat
|
||||||
|
|
||||||
def lazy_cb_wrapper(cb_solver, cb_model):
|
def lazy_cb_wrapper(cb_solver, cb_model):
|
||||||
for comp in self.components.values():
|
for comp in self.components.values():
|
||||||
comp.on_lazy_callback(self, instance, model)
|
comp.lazy_cb(self, instance, model)
|
||||||
|
|
||||||
lazy_cb = None
|
lazy_cb = None
|
||||||
if self.use_lazy_cb:
|
if self.use_lazy_cb:
|
||||||
|
|||||||
Reference in New Issue
Block a user