diff --git a/miplearn/components/component.py b/miplearn/components/component.py index 5c353e0..45dbbeb 100644 --- a/miplearn/components/component.py +++ b/miplearn/components/component.py @@ -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 diff --git a/miplearn/components/lazy_dynamic.py b/miplearn/components/lazy_dynamic.py index 02eafe7..09f978c 100644 --- a/miplearn/components/lazy_dynamic.py +++ b/miplearn/components/lazy_dynamic.py @@ -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: diff --git a/miplearn/components/lazy_static.py b/miplearn/components/lazy_static.py index b685fd4..38e1552 100644 --- a/miplearn/components/lazy_static.py +++ b/miplearn/components/lazy_static.py @@ -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): diff --git a/miplearn/components/tests/test_lazy_static.py b/miplearn/components/tests/test_lazy_static.py index 5bfa76e..e027856 100644 --- a/miplearn/components/tests/test_lazy_static.py +++ b/miplearn/components/tests/test_lazy_static.py @@ -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 diff --git a/miplearn/solvers/internal.py b/miplearn/solvers/internal.py index c42d524..72e54ed 100644 --- a/miplearn/solvers/internal.py +++ b/miplearn/solvers/internal.py @@ -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` diff --git a/miplearn/solvers/learning.py b/miplearn/solvers/learning.py index 98bbac9..9768dc1 100644 --- a/miplearn/solvers/learning.py +++ b/miplearn/solvers/learning.py @@ -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: