Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions searches/hill_climbing.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
# https://en.wikipedia.org/wiki/Hill_climbing
import math

from collections.abc import Callable

Check failure on line 3 in searches/hill_climbing.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

searches/hill_climbing.py:2:1: I001 Import block is un-sorted or un-formatted

Check failure on line 3 in searches/hill_climbing.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

searches/hill_climbing.py:2:1: I001 Import block is un-sorted or un-formatted

class SearchProblem:
"""
An interface to define search problems.
The interface will be illustrated using the example of mathematical function.
"""

def __init__(self, x: int, y: int, step_size: int, function_to_optimize):
def __init__(
self,

Check failure on line 12 in searches/hill_climbing.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

searches/hill_climbing.py:12:14: W291 Trailing whitespace

Check failure on line 12 in searches/hill_climbing.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

searches/hill_climbing.py:12:14: W291 Trailing whitespace
x: int,

Check failure on line 13 in searches/hill_climbing.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

searches/hill_climbing.py:13:16: W291 Trailing whitespace

Check failure on line 13 in searches/hill_climbing.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

searches/hill_climbing.py:13:16: W291 Trailing whitespace
y: int,

Check failure on line 14 in searches/hill_climbing.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

searches/hill_climbing.py:14:16: W291 Trailing whitespace

Check failure on line 14 in searches/hill_climbing.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

searches/hill_climbing.py:14:16: W291 Trailing whitespace
step_size: int,

Check failure on line 15 in searches/hill_climbing.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

searches/hill_climbing.py:15:24: W291 Trailing whitespace

Check failure on line 15 in searches/hill_climbing.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

searches/hill_climbing.py:15:24: W291 Trailing whitespace
function_to_optimize: Callable[[int, int], int]
) -> None:
"""
The constructor of the search problem.

Expand All @@ -34,7 +40,7 @@
"""
return self.function(self.x, self.y)

def get_neighbors(self):
def get_neighbors(self) -> list["SearchProblem"]:
"""
Returns a list of coordinates of neighbors adjacent to the current coordinates.

Expand All @@ -58,21 +64,21 @@
)
]

def __hash__(self):
def __hash__(self) -> int:
"""
hash the string representation of the current search state.
"""
return hash(str(self))

def __eq__(self, obj):
def __eq__(self, obj: object) -> bool:
"""
Check if the 2 objects are equal.
"""
if isinstance(obj, SearchProblem):
return hash(str(self)) == hash(str(obj))
return False

def __str__(self):
def __str__(self) -> str:
"""
string representation of the current search state.
>>> str(SearchProblem(0, 0, 1, None))
Expand All @@ -84,7 +90,7 @@


def hill_climbing(
search_prob,
search_prob: SearchProblem,
find_max: bool = True,
max_x: float = math.inf,
min_x: float = -math.inf,
Expand Down
Loading