44
55class SearchProblem :
66 """
7- A interface to define search problems. The interface will be illustrated using
8- the example of mathematical function.
7+ An interface to define search problems.
8+ The interface will be illustrated using the example of mathematical function.
99 """
1010
1111 def __init__ (self , x : int , y : int , step_size : int , function_to_optimize ):
1212 """
1313 The constructor of the search problem.
14- x: the x coordinate of the current search state.
15- y: the y coordinate of the current search state.
16- step_size: size of the step to take when looking for neighbors.
17- function_to_optimize: a function to optimize having the signature f(x, y).
14+
15+ x: the x coordinate of the current search state.
16+ y: the y coordinate of the current search state.
17+ step_size: size of the step to take when looking for neighbors.
18+ function_to_optimize: a function to optimize having the signature f(x, y).
1819 """
1920 self .x = x
2021 self .y = y
@@ -63,6 +64,14 @@ def __hash__(self):
6364 """
6465 return hash (str (self ))
6566
67+ def __eq__ (self , obj ):
68+ """
69+ Check if the 2 objects are equal.
70+ """
71+ if isinstance (obj , SearchProblem ):
72+ return hash (str (self )) == hash (str (obj ))
73+ return False
74+
6675 def __str__ (self ):
6776 """
6877 string representation of the current search state.
@@ -85,10 +94,11 @@ def hill_climbing(
8594 max_iter : int = 10000 ,
8695) -> SearchProblem :
8796 """
88- implementation of the hill climbling algorithm. We start with a given state, find
89- all its neighbors, move towards the neighbor which provides the maximum (or
90- minimum) change. We keep doing this until we are at a state where we do not
91- have any neighbors which can improve the solution.
97+ Implementation of the hill climbling algorithm.
98+ We start with a given state, find all its neighbors,
99+ move towards the neighbor which provides the maximum (or minimum) change.
100+ We keep doing this until we are at a state where we do not have any
101+ neighbors which can improve the solution.
92102 Args:
93103 search_prob: The search state at the start.
94104 find_max: If True, the algorithm should find the maximum else the minimum.
0 commit comments