|
3 | 3 |
|
4 | 4 |
|
5 | 5 | romania_problem = GraphProblem('Arad', 'Bucharest', romania_map)
|
6 |
| -vacumm_world = GraphProblemStochastic('State_1', ['State_7', 'State_8'], vacumm_world) |
| 6 | +vacuum_world = GraphProblemStochastic('State_1', ['State_7', 'State_8'], vacuum_world) |
7 | 7 | LRTA_problem = OnlineSearchProblem('State_3', 'State_5', one_dim_state_space)
|
8 | 8 | eight_puzzle = EightPuzzle((1, 2, 3, 4, 5, 7, 8, 6, 0))
|
9 | 9 | eight_puzzle2 = EightPuzzle((1, 0, 6, 8, 7, 5, 4, 2), (0, 1, 2, 3, 4, 5, 6, 7, 8))
|
10 | 10 | nqueens = NQueensProblem(8)
|
11 | 11 |
|
| 12 | + |
12 | 13 | def test_find_min_edge():
|
13 | 14 | assert romania_problem.find_min_edge() == 70
|
14 | 15 |
|
@@ -151,7 +152,33 @@ def test_conflict():
|
151 | 152 | def test_recursive_best_first_search():
|
152 | 153 | assert recursive_best_first_search(
|
153 | 154 | romania_problem).solution() == ['Sibiu', 'Rimnicu', 'Pitesti', 'Bucharest']
|
| 155 | + assert recursive_best_first_search( |
| 156 | + EightPuzzle((2, 4, 3, 1, 5, 6, 7, 8, 0))).solution() == [ |
| 157 | + 'UP', 'LEFT', 'UP', 'LEFT', 'DOWN', 'RIGHT', 'RIGHT', 'DOWN' |
| 158 | + ] |
| 159 | + |
| 160 | + def manhattan(node): |
| 161 | + state = node.state |
| 162 | + index_goal = {0:[2,2], 1:[0,0], 2:[0,1], 3:[0,2], 4:[1,0], 5:[1,1], 6:[1,2], 7:[2,0], 8:[2,1]} |
| 163 | + index_state = {} |
| 164 | + index = [[0,0], [0,1], [0,2], [1,0], [1,1], [1,2], [2,0], [2,1], [2,2]] |
| 165 | + x, y = 0, 0 |
| 166 | + |
| 167 | + for i in range(len(state)): |
| 168 | + index_state[state[i]] = index[i] |
| 169 | + |
| 170 | + mhd = 0 |
| 171 | + |
| 172 | + for i in range(8): |
| 173 | + for j in range(2): |
| 174 | + mhd = abs(index_goal[i][j] - index_state[i][j]) + mhd |
| 175 | + |
| 176 | + return mhd |
154 | 177 |
|
| 178 | + assert recursive_best_first_search( |
| 179 | + EightPuzzle((2, 4, 3, 1, 5, 6, 7, 8, 0)), h=manhattan).solution() == [ |
| 180 | + 'LEFT', 'UP', 'UP', 'LEFT', 'DOWN', 'RIGHT', 'DOWN', 'UP', 'DOWN', 'RIGHT' |
| 181 | + ] |
155 | 182 |
|
156 | 183 | def test_hill_climbing():
|
157 | 184 | prob = PeakFindingProblem((0, 0), [[0, 5, 10, 20],
|
@@ -200,23 +227,31 @@ def run_plan(state, problem, plan):
|
200 | 227 | return False
|
201 | 228 | predicate = lambda x: run_plan(x, problem, plan[1][x])
|
202 | 229 | return all(predicate(r) for r in problem.result(state, plan[0]))
|
203 |
| - plan = and_or_graph_search(vacumm_world) |
204 |
| - assert run_plan('State_1', vacumm_world, plan) |
| 230 | + plan = and_or_graph_search(vacuum_world) |
| 231 | + assert run_plan('State_1', vacuum_world, plan) |
| 232 | + |
| 233 | + |
| 234 | +def test_online_dfs_agent(): |
| 235 | + odfs_agent = OnlineDFSAgent(LRTA_problem) |
| 236 | + keys = [key for key in odfs_agent('State_3')] |
| 237 | + assert keys[0] in ['Right', 'Left'] |
| 238 | + assert keys[1] in ['Right', 'Left'] |
| 239 | + assert odfs_agent('State_5') is None |
205 | 240 |
|
206 | 241 |
|
207 | 242 | def test_LRTAStarAgent():
|
208 |
| - my_agent = LRTAStarAgent(LRTA_problem) |
209 |
| - assert my_agent('State_3') == 'Right' |
210 |
| - assert my_agent('State_4') == 'Left' |
211 |
| - assert my_agent('State_3') == 'Right' |
212 |
| - assert my_agent('State_4') == 'Right' |
213 |
| - assert my_agent('State_5') is None |
214 |
| - |
215 |
| - my_agent = LRTAStarAgent(LRTA_problem) |
216 |
| - assert my_agent('State_4') == 'Left' |
217 |
| - |
218 |
| - my_agent = LRTAStarAgent(LRTA_problem) |
219 |
| - assert my_agent('State_5') is None |
| 243 | + lrta_agent = LRTAStarAgent(LRTA_problem) |
| 244 | + assert lrta_agent('State_3') == 'Right' |
| 245 | + assert lrta_agent('State_4') == 'Left' |
| 246 | + assert lrta_agent('State_3') == 'Right' |
| 247 | + assert lrta_agent('State_4') == 'Right' |
| 248 | + assert lrta_agent('State_5') is None |
| 249 | + |
| 250 | + lrta_agent = LRTAStarAgent(LRTA_problem) |
| 251 | + assert lrta_agent('State_4') == 'Left' |
| 252 | + |
| 253 | + lrta_agent = LRTAStarAgent(LRTA_problem) |
| 254 | + assert lrta_agent('State_5') is None |
220 | 255 |
|
221 | 256 |
|
222 | 257 | def test_genetic_algorithm():
|
|
0 commit comments