Component: rename iteration_cb and lazy_cb

pull/3/head
Alinson S. Xavier 5 years ago
parent 87a89eaf96
commit 51b5d8e549

@ -22,8 +22,8 @@ class Component(ABC):
def fit(self, training_instances):
pass
def after_iteration(self, solver, instance, model):
def iteration_cb(self, solver, instance, model):
return False
def on_lazy_callback(self, solver, instance, model):
def lazy_cb(self, solver, instance, model):
return

@ -38,7 +38,7 @@ class DynamicLazyConstraintsComponent(Component):
cut = instance.build_lazy_constraint(model, v)
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...")
violations = instance.find_violated_lazy_constraints(model)
if len(violations) == 0:

@ -51,7 +51,7 @@ class StaticLazyConstraintsComponent(Component):
def after_solve(self, solver, instance, model, results):
pass
def after_iteration(self, solver, instance, model):
def iteration_cb(self, solver, instance, model):
if solver.use_lazy_cb:
return False
else:
@ -67,7 +67,7 @@ class StaticLazyConstraintsComponent(Component):
else:
return False
def on_lazy_callback(self, solver, instance, model):
def lazy_cb(self, solver, instance, model):
self._check_and_add(instance, solver)
def _check_and_add(self, instance, solver):

@ -101,7 +101,7 @@ def test_usage_with_solver():
internal.add_constraint.reset_mock()
# 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
# 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()
# 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
# 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
This function is called whenever the solver finds a new candidate
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 if a constraint is satisfied, through `is_constraint_satisfied(cobj)`
- Adding a new constraint to the problem, through `add_constraint`

@ -209,13 +209,13 @@ class LearningSolver:
def iteration_cb():
should_repeat = False
for comp in self.components.values():
if comp.after_iteration(self, instance, model):
if comp.iteration_cb(self, instance, model):
should_repeat = True
return should_repeat
def lazy_cb_wrapper(cb_solver, cb_model):
for comp in self.components.values():
comp.on_lazy_callback(self, instance, model)
comp.lazy_cb(self, instance, model)
lazy_cb = None
if self.use_lazy_cb:

Loading…
Cancel
Save