4
4
5
5
class SearchProblem :
6
6
"""
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.
9
9
"""
10
10
11
11
def __init__ (self , x : int , y : int , step_size : int , function_to_optimize ):
12
12
"""
13
13
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).
18
19
"""
19
20
self .x = x
20
21
self .y = y
@@ -63,6 +64,14 @@ def __hash__(self):
63
64
"""
64
65
return hash (str (self ))
65
66
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
+
66
75
def __str__ (self ):
67
76
"""
68
77
string representation of the current search state.
@@ -85,10 +94,11 @@ def hill_climbing(
85
94
max_iter : int = 10000 ,
86
95
) -> SearchProblem :
87
96
"""
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.
92
102
Args:
93
103
search_prob: The search state at the start.
94
104
find_max: If True, the algorithm should find the maximum else the minimum.
0 commit comments