| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 46df735 commit cbab5f1
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1 @@ | |||
| 1 | + matplotlib | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,187 @@ | |||
| 1 | + # https://en.wikipedia.org/wiki/Hill_climbing | ||
| 2 | + import math | ||
| 3 | + | ||
| 4 | + | ||
| 5 | + class SearchProblem: | ||
| 6 | + """ | ||
| 7 | + A interface to define search problems. The interface will be illustrated using | ||
| 8 | + the example of mathematical function. | ||
| 9 | + """ | ||
| 10 | + | ||
| 11 | + def __init__(self, x: int, y: int, step_size: int, function_to_optimize): | ||
| 12 | + """ | ||
| 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). | ||
| 18 | + """ | ||
| 19 | + self.x = x | ||
| 20 | + self.y = y | ||
| 21 | + self.step_size = step_size | ||
| 22 | + self.function = function_to_optimize | ||
| 23 | + | ||
| 24 | + def score(self) -> int: | ||
| 25 | + """ | ||
| 26 | + Returns the output for the function called with current x and y coordinates. | ||
| 27 | + >>> def test_function(x, y): | ||
| 28 | + ... return x + y | ||
| 29 | + >>> SearchProblem(0, 0, 1, test_function).score() # 0 + 0 = 0 | ||
| 30 | + 0 | ||
| 31 | + >>> SearchProblem(5, 7, 1, test_function).score() # 5 + 7 = 12 | ||
| 32 | + 12 | ||
| 33 | + """ | ||
| 34 | + return self.function(self.x, self.y) | ||
| 35 | + | ||
| 36 | + def get_neighbors(self): | ||
| 37 | + """ | ||
| 38 | + Returns a list of coordinates of neighbors adjacent to the current coordinates. | ||
| 39 | + | ||
| 40 | + Neighbors: | ||
| 41 | + | 0 | 1 | 2 | | ||
| 42 | + | 3 | _ | 4 | | ||
| 43 | + | 5 | 6 | 7 | | ||
| 44 | + """ | ||
| 45 | + step_size = self.step_size | ||
| 46 | + return [ | ||
| 47 | + SearchProblem(x, y, step_size, self.function) | ||
| 48 | + for x, y in ( | ||
| 49 | + (self.x - step_size, self.y - step_size), | ||
| 50 | + (self.x - step_size, self.y), | ||
| 51 | + (self.x - step_size, self.y + step_size), | ||
| 52 | + (self.x, self.y - step_size), | ||
| 53 | + (self.x, self.y + step_size), | ||
| 54 | + (self.x + step_size, self.y - step_size), | ||
| 55 | + (self.x + step_size, self.y), | ||
| 56 | + (self.x + step_size, self.y + step_size), | ||
| 57 | + ) | ||
| 58 | + ] | ||
| 59 | + | ||
| 60 | + def __hash__(self): | ||
| 61 | + """ | ||
| 62 | + hash the string represetation of the current search state. | ||
| 63 | + """ | ||
| 64 | + return hash(str(self)) | ||
| 65 | + | ||
| 66 | + def __str__(self): | ||
| 67 | + """ | ||
| 68 | + string representation of the current search state. | ||
| 69 | + >>> str(SearchProblem(0, 0, 1, None)) | ||
| 70 | + 'x: 0 y: 0' | ||
| 71 | + >>> str(SearchProblem(2, 5, 1, None)) | ||
| 72 | + 'x: 2 y: 5' | ||
| 73 | + """ | ||
| 74 | + return f"x: {self.x} y: {self.y}" | ||
| 75 | + | ||
| 76 | + | ||
| 77 | + def hill_climbing( | ||
| 78 | + search_prob, | ||
| 79 | + find_max: bool = True, | ||
| 80 | + max_x: float = math.inf, | ||
| 81 | + min_x: float = -math.inf, | ||
| 82 | + max_y: float = math.inf, | ||
| 83 | + min_y: float = -math.inf, | ||
| 84 | + visualization: bool = False, | ||
| 85 | + max_iter: int = 10000, | ||
| 86 | + ) -> SearchProblem: | ||
| 87 | + """ | ||
| 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 untill we are at a state where we do not | ||
| 91 | + have any neighbors which can improve the solution. | ||
| 92 | + Args: | ||
| 93 | + search_prob: The search state at the start. | ||
| 94 | + find_max: If True, the algorithm should find the minimum else the minimum. | ||
| 95 | + max_x, min_x, max_y, min_y: the maximum and minimum bounds of x and y. | ||
| 96 | + visualization: If True, a matplotlib graph is displayed. | ||
| 97 | + max_iter: number of times to run the iteration. | ||
| 98 | + Returns a search state having the maximum (or minimum) score. | ||
| 99 | + """ | ||
| 100 | + current_state = search_prob | ||
| 101 | + scores = [] # list to store the current score at each iteration | ||
| 102 | + iterations = 0 | ||
| 103 | + solution_found = False | ||
| 104 | + visited = set() | ||
| 105 | + while not solution_found and iterations < max_iter: | ||
| 106 | + visited.add(current_state) | ||
| 107 | + iterations += 1 | ||
| 108 | + current_score = current_state.score() | ||
| 109 | + scores.append(current_score) | ||
| 110 | + neighbors = current_state.get_neighbors() | ||
| 111 | + max_change = -math.inf | ||
| 112 | + min_change = math.inf | ||
| 113 | + next_state = None # to hold the next best neighbor | ||
| 114 | + for neighbor in neighbors: | ||
| 115 | + if neighbor in visited: | ||
| 116 | + continue # do not want to visit the same state again | ||
| 117 | + if ( | ||
| 118 | + neighbor.x > max_x | ||
| 119 | + or neighbor.x < min_x | ||
| 120 | + or neighbor.y > max_y | ||
| 121 | + or neighbor.y < min_y | ||
| 122 | + ): | ||
| 123 | + continue # neighbor outside our bounds | ||
| 124 | + change = neighbor.score() - current_score | ||
| 125 | + if find_max: # finding max | ||
| 126 | + # going to direction with greatest ascent | ||
| 127 | + if change > max_change and change > 0: | ||
| 128 | + max_change = change | ||
| 129 | + next_state = neighbor | ||
| 130 | + else: # finding min | ||
| 131 | + # to direction with greatest descent | ||
| 132 | + if change < min_change and change < 0: | ||
| 133 | + min_change = change | ||
| 134 | + next_state = neighbor | ||
| 135 | + if next_state is not None: | ||
| 136 | + # we found at least one neighbor which improved the current state | ||
| 137 | + current_state = next_state | ||
| 138 | + else: | ||
| 139 | + # since we have no neighbor that improves the solution we stop the search | ||
| 140 | + solution_found = True | ||
| 141 | + | ||
| 142 | + if visualization: | ||
| 143 | + import matplotlib.pyplot as plt | ||
| 144 | + | ||
| 145 | + plt.plot(range(iterations), scores) | ||
| 146 | + plt.xlabel("Iterations") | ||
| 147 | + plt.ylabel("Function values") | ||
| 148 | + plt.show() | ||
| 149 | + | ||
| 150 | + return current_state | ||
| 151 | + | ||
| 152 | + | ||
| 153 | + if __name__ == "__main__": | ||
| 154 | + import doctest | ||
| 155 | + | ||
| 156 | + doctest.testmod() | ||
| 157 | + | ||
| 158 | + def test_f1(x, y): | ||
| 159 | + return (x ** 2) + (y ** 2) | ||
| 160 | + | ||
| 161 | + # starting the problem with initial coordinates (3, 4) | ||
| 162 | + prob = SearchProblem(x=3, y=4, step_size=1, function_to_optimize=test_f1) | ||
| 163 | + local_min = hill_climbing(prob, find_max=False) | ||
| 164 | + print( | ||
| 165 | + "The minimum score for f(x, y) = x^2 + y^2 found via hill climbing: " | ||
| 166 | + f"{local_min.score()}" | ||
| 167 | + ) | ||
| 168 | + | ||
| 169 | + # starting the problem with initial coordinates (12, 47) | ||
| 170 | + prob = SearchProblem(x=12, y=47, step_size=1, function_to_optimize=test_f1) | ||
| 171 | + local_min = hill_climbing( | ||
| 172 | + prob, find_max=False, max_x=100, min_x=5, max_y=50, min_y=-5, visualization=True | ||
| 173 | + ) | ||
| 174 | + print( | ||
| 175 | + "The minimum score for f(x, y) = x^2 + y^2 with the domain 100 > x > 5 " | ||
| 176 | + f"and 50 > y > - 5 found via hill climbing: {local_min.score()}" | ||
| 177 | + ) | ||
| 178 | + | ||
| 179 | + def test_f2(x, y): | ||
| 180 | + return (3 * x ** 2) - (6 * y) | ||
| 181 | + | ||
| 182 | + prob = SearchProblem(x=3, y=4, step_size=1, function_to_optimize=test_f1) | ||
| 183 | + local_min = hill_climbing(prob, find_max=True) | ||
| 184 | + print( | ||
| 185 | + "The maximum score for f(x, y) = x^2 + y^2 found via hill climbing: " | ||
| 186 | + f"{local_min.score()}" | ||
| 187 | + ) | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments