Skip to content

Commit

Permalink
Bump version, slight docstring and internal variable name changes (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
N-Wouda authored May 24, 2022
1 parent 8e22651 commit 9506774
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 17 deletions.
21 changes: 9 additions & 12 deletions alns/ALNS.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,16 @@ class ALNS:
"""

def __init__(self, rnd_state: rnd.RandomState = rnd.RandomState()):
self._destroy_operators: Dict[str, _OperatorType] = {}
self._repair_operators: Dict[str, _OperatorType] = {}
self._d_ops: Dict[str, _OperatorType] = {}
self._r_ops: Dict[str, _OperatorType] = {}
self._only_after: Dict[_OperatorType, set] = defaultdict(set)

# Optional callback that may be used to improve a new best solution
# further, via e.g. local search.
self._on_best: Optional[_OperatorType] = None

self._rnd_state = rnd_state

self._only_after: Dict[_OperatorType, set] = defaultdict(set)

@property
def destroy_operators(self) -> List[Tuple[str, _OperatorType]]:
"""
Expand All @@ -69,7 +68,7 @@ def destroy_operators(self) -> List[Tuple[str, _OperatorType]]:
A list of (name, operator) tuples. Their order is the same as the one in
which they were passed to the ALNS instance.
"""
return list(self._destroy_operators.items())
return list(self._d_ops.items())

@property
def repair_operators(self) -> List[Tuple[str, _OperatorType]]:
Expand All @@ -81,7 +80,7 @@ def repair_operators(self) -> List[Tuple[str, _OperatorType]]:
A list of (name, operator) tuples. Their order is the same as the one in
which they were passed to the ALNS instance.
"""
return list(self._repair_operators.items())
return list(self._r_ops.items())

def add_destroy_operator(self, op: _OperatorType, name: str = None):
"""
Expand All @@ -98,7 +97,7 @@ def add_destroy_operator(self, op: _OperatorType, name: str = None):
function name is used instead.
"""
logger.debug(f"Adding destroy operator {op.__name__}.")
self._destroy_operators[op.__name__ if name is None else name] = op
self._d_ops[op.__name__ if name is None else name] = op

def add_repair_operator(
self,
Expand Down Expand Up @@ -127,9 +126,9 @@ def add_repair_operator(
with the new repair operator.
"""
logger.debug(f"Adding repair operator {op.__name__}.")
self._repair_operators[name if name else op.__name__] = op
self._r_ops[name if name else op.__name__] = op

if only_after is not None:
if only_after:
self._only_after[op].update(only_after)

def _compute_op_coupling(self) -> np.ndarray:
Expand All @@ -142,9 +141,7 @@ def _compute_op_coupling(self) -> np.ndarray:
If the only_after keyword-only argument was not used when adding
the repair operators, then all entries of the matrix are 1.
"""
op_coupling = np.ones(
(len(self.destroy_operators), len(self.repair_operators))
)
op_coupling = np.ones((len(self._d_ops), len(self._r_ops)))

for r_idx, (_, r_op) in enumerate(self.repair_operators):
coupled_d_ops = self._only_after[r_op]
Expand Down
7 changes: 3 additions & 4 deletions alns/weights/WeightScheme.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,16 @@ def select_operators(
Parameters
----------
rnd_state
Random state object, to be used for number generation.
Random state object, to be used for random number generation.
op_coupling
Matrix that indicates coupling between destroy and repair
operators. Entry (i, j) is 1 if destroy operator i can be used in
conjunction with repair operator j and 0 otherwise.
Returns
-------
A tuple of (d_idx, r_idx), which are indices into the destroy and repair
operator lists, respectively.
A tuple of (d_idx, r_idx), which are indices into the destroy and
repair operator lists, respectively.
"""

def select(op_weights):
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 = "3.0.1"
version = "4.0.0"
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 9506774

Please sign in to comment.