def test_a_star_search(self): print("Starting A* search test") print("---------------------------------------------") with TimeoutAlarm( 60, error_message= "A* Search and Uniform Cost Search cannot find the solution within 60s" ): puzzle = EightPuzzleState([3, 0, 5, 6, 2, 4, 7, 8, 1]) problem = PuzzleSearchProblem(puzzle) path_a_start, step_a_star = search.a_start_search(problem) path_ucs, step_ucs = search.uniform_cost_search(problem) print("Test A* on: \n") print(puzzle) self.print_result("A*", step_a_star, problem.get_costs(path_a_start), path_a_start) curr = puzzle for a in path_a_start: curr = curr.next_state(a) self.assertTrue(curr.is_goal(), "The final state is not goal test") self.assertEqual(problem.get_costs(path_a_start), 43, "The answer may not the optimal one") self.assertLessEqual( step_a_star, step_ucs, "The A* steps should be less or equal compared with UCS") print("=============================================")
def test_depth_first_search(self): print("Starting depth first search test") print("---------------------------------------------") puzzle = EightPuzzleState([1, 0, 2, 3, 4, 5, 6, 7, 8]) problem = PuzzleSearchProblem(puzzle) path, step = search.depth_first_search(problem) print("Test DFS on:") print(puzzle) self.print_result("DFS", step, problem.get_costs(path), path) curr = puzzle for a in path: curr = curr.next_state(a) self.assertTrue(curr.is_goal(), "The final state is not goal test") print("=============================================")
def test_breath_first_search(self): print( "Starting breath first search test", "If it cannot finish within 30s, " "please kill the job and review your code") print("---------------------------------------------") puzzle = EightPuzzleState([3, 0, 5, 6, 2, 4, 7, 8, 1]) problem = PuzzleSearchProblem(puzzle) path, step = search.breadth_first_search(problem) print("Test BFS on: \n") print(puzzle) self.print_result("BFS", step, problem.get_costs(path), path) curr = puzzle for a in path: curr = curr.next_state(a) self.assertTrue(curr.is_goal(), "The final state is not goal test") print("=============================================")
def test_breath_first_search(self): print("Starting breath first search test") print("---------------------------------------------") with Timer(30, error_message= "Breath First Search cannot find the solution within 30s"): puzzle = EightPuzzleState([3, 0, 5, 6, 2, 4, 7, 8, 1]) problem = PuzzleSearchProblem(puzzle) path, step = search.breadth_first_search(problem) print("Test BFS on: \n") print(puzzle) self.print_result("BFS", step, problem.get_costs(path), path) curr = puzzle for a in path: curr = curr.next_state(a) self.assertTrue(curr.is_goal(), "The final state is not goal test") print("=============================================")
def test_unit_cost_search(self): print("Starting uniform cost search test" "If it cannot finish within 30s, " "please kill the job and review your code") print("---------------------------------------------") puzzle = EightPuzzleState([3, 0, 5, 6, 2, 4, 7, 8, 1]) problem = PuzzleSearchProblem(puzzle) path, step = search.uniform_cost_search(problem) print("Test UCS on: \n") print(puzzle) self.print_result("UCS", step, problem.get_costs(path), path) curr = puzzle for a in path: curr = curr.next_state(a) self.assertTrue(curr.is_goal(), "The final state is not goal test") self.assertEqual(problem.get_costs(path), 43, "The answer may not the optimal one") print("=============================================")
def test_depth_first_search(self): print("Starting depth first search test") print("---------------------------------------------") with TimeoutAlarm( 30, error_message= "Depth First Search cannot find the solution within 30s"): puzzle = EightPuzzleState([1, 0, 2, 3, 4, 5, 6, 7, 8]) problem = PuzzleSearchProblem(puzzle) path, step = search.depth_first_search(problem) print("Test DFS on:") print(puzzle) self.print_result("DFS", step, problem.get_costs(path), path) curr = puzzle for a in path: curr = curr.next_state(a) self.assertTrue(curr.is_goal(), "The final state is not goal test") print("=============================================")
def test_unit_cost_search(self): print("Starting uniform cost search test") print("---------------------------------------------") with Timer(30, error_message= "Uniform Cost Search cannot find the solution within 30s"): puzzle = EightPuzzleState([3, 0, 5, 6, 2, 4, 7, 8, 1]) problem = PuzzleSearchProblem(puzzle) path, step = search.uniform_cost_search(problem) print("Test UCS on: \n") print(puzzle) self.print_result("UCS", step, problem.get_costs(path), path) curr = puzzle for a in path: curr = curr.next_state(a) self.assertTrue(curr.is_goal(), "The final state is not goal test") self.assertEqual(problem.get_costs(path), 43, "The answer may not the optimal one") print("=============================================")
def test_a_star_search(self): print("Starting A* search test, " "If it cannot finish within 60s, " "please kill the job and review your code") print("---------------------------------------------") puzzle = EightPuzzleState([3, 0, 5, 6, 2, 4, 7, 8, 1]) problem = PuzzleSearchProblem(puzzle) path_a_start, step_a_star = search.a_start_search(problem) path_ucs, step_ucs = search.uniform_cost_search(problem) print("Test A* on: \n") print(puzzle) self.print_result("A*", step_a_star, problem.get_costs(path_a_start), path_a_start) curr = puzzle for a in path_a_start: curr = curr.next_state(a) self.assertTrue(curr.is_goal(), "The final state is not goal test") self.assertEqual(problem.get_costs(path_a_start), 43, "The answer may not the optimal one") self.assertLessEqual( step_a_star, step_ucs, "The A* steps should be less or equal compared with UCS") print("=============================================")
from eightpuzzle import EightPuzzleState from search import a_start_heuristic puzzle = EightPuzzleState([3, 0, 5, 6, 2, 4, 7, 8, 1]) h = a_start_heuristic(None, puzzle) print(h)
from eightpuzzle import EightPuzzleState from eightpuzzle import PuzzleSearchProblem from search import Node from search import ucs_compute_node_cost import util puzzle = EightPuzzleState([3, 0, 5, 6, 2, 4, 7, 8, 1]) problem = PuzzleSearchProblem(puzzle) #print(problem.get_start_state()) #print(int(3/2)) pq = util.PriorityQueue() state1 = EightPuzzleState([3, 0, 5, 6, 2, 4, 7, 8, 1]) state2 = EightPuzzleState([3, 0, 5, 6, 2, 4, 7, 8, 1]) state3 = EightPuzzleState([3, 1, 5, 6, 2, 4, 7, 8, 0]) node1 = Node(state1, ['left', 'down']) node2 = Node(state2, ['right', 'up', 'right']) node3 = Node(state3, ['down', 'down']) pq.push(node1, ucs_compute_node_cost(problem, None, node1, None)) pq.push(node3, ucs_compute_node_cost(problem, None, node3, None)) print(ucs_compute_node_cost(problem, None, node1, None)) print(ucs_compute_node_cost(problem, None, node2, None)) print(ucs_compute_node_cost(problem, None, node3, None)) pq.update(node2, ucs_compute_node_cost(problem, None, node2, None)) while not pq.empty(): print(pq.pop().state)