From f478e0cfb76719f26c1797c65ec5b34eed892c44 Mon Sep 17 00:00:00 2001 From: Niels Wouda Date: Mon, 16 May 2022 16:58:25 +0200 Subject: [PATCH] Fix bug where on_best did not receive iterate() kwargs (#63) * Fix bug where on_best did not receive iterate() kwargs * Bump version --- alns/ALNS.py | 8 ++++++-- alns/tests/test_alns.py | 19 +++++++++++++++++++ pyproject.toml | 2 +- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/alns/ALNS.py b/alns/ALNS.py index cec8f28..6664109 100644 --- a/alns/ALNS.py +++ b/alns/ALNS.py @@ -176,7 +176,11 @@ class of vehicle routing problems with backhauls. *European Journal of destroyed = d_operator(curr, self._rnd_state, **kwargs) cand = r_operator(destroyed, self._rnd_state, **kwargs) - best, curr, s_idx = self._consider_candidate(crit, best, curr, cand) + best, curr, s_idx = self._eval_cand(crit, + best, + curr, + cand, + **kwargs) weight_scheme.update_weights(d_idx, r_idx, s_idx) @@ -201,7 +205,7 @@ def on_best(self, func: _OperatorType): """ self._on_best = func - def _consider_candidate( + def _eval_cand( self, crit: AcceptanceCriterion, best: State, diff --git a/alns/tests/test_alns.py b/alns/tests/test_alns.py index 32cf16c..714f6e9 100644 --- a/alns/tests/test_alns.py +++ b/alns/tests/test_alns.py @@ -192,6 +192,25 @@ def test_operator(state, rnd, item): alns.iterate(init_sol, weights, HillClimbing(), 10, item=orig_item) +def test_bugfix_pass_kwargs_to_on_best(): + """ + Exercises a bug where the on_best callback did not receive the kwargs passed + to iterate(). + """ + def test_operator(state, rnd, item): + assert_(item is orig_item) + return Zero() # better, so on_best is triggered + + alns = get_alns_instance([lambda state, rnd, item: state], [test_operator]) + alns.on_best(lambda state, rnd, item: state) + + init_sol = One() + weights = SimpleWeights([1, 1, 1, 1], 1, 1, .5) + orig_item = object() + + alns.iterate(init_sol, weights, HillClimbing(), 10, item=orig_item) + + # EXAMPLES --------------------------------------------------------------------- diff --git a/pyproject.toml b/pyproject.toml index f4c0793..544ef2c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "alns" -version = "2.1.1" +version = "2.1.2" description = "A flexible implementation of the adaptive large neighbourhood search (ALNS) algorithm." authors = ["Niels Wouda "] license = "MIT"