Skip to content

Commit

Permalink
Use Ruff instead of flake8 and isort (#158)
Browse files Browse the repository at this point in the history
* Use ruff

* Update docstring

* Update Ruff, set case-sensitive = true
  • Loading branch information
N-Wouda committed Jul 12, 2023
1 parent c65ca9d commit 1960a0e
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 35 deletions.
1 change: 0 additions & 1 deletion .github/ISSUE_TEMPLATE/BUG_REPORT.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,3 @@ You can copy-paste the output of `import alns; alns.show_versions()` to achieve
#### Expected Output

A clear and concise description of what you expected to happen.

17 changes: 7 additions & 10 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,20 @@ repos:
rev: v3.2.0
hooks:
- id: debug-statements
- id: end-of-file-fixer

- repo: https://github.com/psf/black
rev: 22.3.0
rev: 22.12.0
hooks:
- id: black

- repo: https://github.com/pycqa/flake8
rev: 4.0.1
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.0.278
hooks:
- id: flake8

- repo: https://github.com/pycqa/isort
rev: 5.12.0
hooks:
- id: isort
- id: ruff
args: [--fix, --exit-non-zero-on-fix]

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.982
rev: v1.4.1
hooks:
- id: mypy
2 changes: 1 addition & 1 deletion alns/ALNS.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ def iterate(
.. [2] S. Røpke and D. Pisinger (2006). A unified heuristic for a large
class of vehicle routing problems with backhauls. *European
Journal of Operational Research*, 171: 750775.
Journal of Operational Research*, 171: 750-775.
"""
if len(self.destroy_operators) == 0 or len(self.repair_operators) == 0:
raise ValueError("Missing destroy or repair operators.")
Expand Down
2 changes: 1 addition & 1 deletion alns/accept/NonLinearGreatDeluge.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class NonLinearGreatDeluge(GreatDeluge):
IEEE conference intelligent systems* (2008) Vol. 1: 8-11.
.. [3] Santini, A., Ropke, S. & Hvattum, L.M. A comparison of acceptance
criteria for the adaptive large neighbourhood search metaheuristic.
*Journal of Heuristics* (2018) 24 (5): 783815.
*Journal of Heuristics* (2018) 24 (5): 783-815.
"""

def __init__(self, alpha: float, beta: float, gamma: float, delta: float):
Expand Down
2 changes: 1 addition & 1 deletion alns/accept/RecordToRecordTravel.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class RecordToRecordTravel:
----------
.. [1] Santini, A., Ropke, S. & Hvattum, L.M. A comparison of acceptance
criteria for the adaptive large neighbourhood search metaheuristic.
*Journal of Heuristics* (2018) 24 (5): 783815.
*Journal of Heuristics* (2018) 24 (5): 783-815.
.. [2] Dueck, G. New optimization heuristics: The great deluge algorithm
and the record-to-record travel. *Journal of Computational Physics*
(1993) 104 (1): 86-92.
Expand Down
2 changes: 1 addition & 1 deletion alns/accept/SimulatedAnnealing.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class SimulatedAnnealing:
----------
.. [1] Santini, A., Ropke, S. & Hvattum, L.M. A comparison of acceptance
criteria for the adaptive large neighbourhood search metaheuristic.
*Journal of Heuristics* (2018) 24 (5): 783815.
*Journal of Heuristics* (2018) 24 (5): 783-815.
.. [2] Kirkpatrick, S., Gerlatt, C. D. Jr., and Vecchi, M. P. Optimization
by Simulated Annealing. *IBM Research Report* RC 9355, 1982.
"""
Expand Down
3 changes: 2 additions & 1 deletion alns/accept/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ def update(current: float, step: float, method: str) -> float:

if method == "linear":
return current - step
elif method == "exponential":

if method == "exponential":
return current * step

raise ValueError("Method `{0}' not understood.".format(method))
19 changes: 9 additions & 10 deletions alns/select/MABSelector.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,21 +131,20 @@ def __call__( # type: ignore[override]
) -> Tuple[int, int]:
"""
Returns the (destroy, repair) operator pair from the underlying MAB
strategy
strategy.
"""
if self._mab._is_initial_fit:
has_ctx = self._mab.is_contextual
ctx = np.atleast_2d(curr.get_context()) if has_ctx else None
prediction = self._mab.predict(contexts=ctx)
return arm2ops(prediction)
else:
# This can happen when the MAB object has not yet been fit on any
# observations. In that case we return any feasible operator index
# pair as a first observation.
if not self._mab._is_initial_fit: # noqa: SLF001
# The MAB object has not yet been fit. In that case we return any
# feasible operator index pair as a first observation.
allowed = np.argwhere(self._op_coupling)
idx = rnd_state.randint(len(allowed))
return allowed[idx][0], allowed[idx][1]

has_ctx = self._mab.is_contextual
ctx = np.atleast_2d(curr.get_context()) if has_ctx else None
prediction = self._mab.predict(contexts=ctx)
return arm2ops(prediction)

def update( # type: ignore[override]
self,
cand: ContextualState,
Expand Down
14 changes: 10 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,16 @@ mabwiser = ">=2.7.0"
[tool.black]
line-length = 79

[tool.isort]
case_sensitive = true
line_length = 79
profile = "black"
[tool.ruff]
line-length = 79
select = ["E", "F", "I", "NPY", "RET", "RSE", "RUF", "SLF", "SIM", "TCH"]

[tool.ruff.per-file-ignores]
"__init__.py" = ["F401"] # unused imports

[tool.ruff.isort]
case-sensitive = true
known-first-party = ["alns"]

[tool.mypy]
ignore_missing_imports = true
Expand Down
5 changes: 0 additions & 5 deletions setup.cfg

This file was deleted.

0 comments on commit 1960a0e

Please sign in to comment.