Skip to content

Commit

Permalink
Fix bug where on_best did not receive iterate() kwargs (#63)
Browse files Browse the repository at this point in the history
* Fix bug where on_best did not receive iterate() kwargs

* Bump version
  • Loading branch information
N-Wouda authored May 16, 2022
1 parent ff2c863 commit f478e0c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
8 changes: 6 additions & 2 deletions alns/ALNS.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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,
Expand Down
19 changes: 19 additions & 0 deletions alns/tests/test_alns.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 ---------------------------------------------------------------------


Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>"]
license = "MIT"
Expand Down

0 comments on commit f478e0c

Please sign in to comment.