def test_successful_search(self): ids = IterativeDeepeningSearch() problem = Problem(1, TestActionsFunction(), TestResultFunction(), TestGoalTest(4)) result = ids.search(problem) self.assertEquals(3, len(result))
def main(): for i in range(0, NUMBER_OF_TESTS): sa = SimulateAnnealingSearch(AttackingPairHeuristic()) random_board = create_random_board() print("Test number: " + str(i)) print("Random init board:") print(random_board) # Create N queen problem with N queens on the board p = Problem(random_board, NQCActionsFunction(), NQResultFunction(), NQueensGoalTest()) # Search for a solution actions = sa.search(p) if not sa.failed(): print("Test succeeded") print("Actions:") for action in actions: print(action) else: print("Search failed") print("Result state:") print(sa.last_state) print("\n\n")
def test_successful_search(self): dls = DepthLimitedSearch(10) problem = Problem(1, TestActionsFunction(), TestResultFunction(), TestGoalTest(4)) result = dls.search(problem) self.assertEquals(3, len(result))
def test_failed_search(self): ts = TreeSearch() bfs = BreadthFirstSearch(ts) problem = Problem(1, TestActionsFunction(3), TestResultFunction(), TestGoalTest(5)) result = bfs.search(problem) self.assertTrue(bfs.is_failure(result))
def test_search_failure(self): ids = IterativeDeepeningSearch() # 5 - is a goal state but it's unreachable problem = Problem(1, TestActionsFunction(2), TestResultFunction(), TestGoalTest(5)) result = ids.search(problem) self.assertTrue(ids.is_failure(result))
def test_search_failure(self): dls = DepthLimitedSearch(10) # 5 - is a goal state but it's unreachable problem = Problem(1, TestActionsFunction(2), TestResultFunction(), TestGoalTest(5)) result = dls.search(problem) self.assertFalse(dls.is_cutoff(result)) self.assertTrue(dls.is_failure(result))
def test_search_succeed(self): values = [4, 3, 5, 6, 10, 3] problem = Problem(1, LocalActionFunction(len(values)), LocalResultFunction(), LocalGoalTestFunction(4)) hcs = HillClimbingSearch(LocalHeuristicFunction(values)) actions = hcs.search(problem) self.assertFalse(hcs.is_failure()) self.assertEqual(4, hcs.last_state)
def test_search_cutoff(self): # Here we set depth limit 3... dls = DepthLimitedSearch(3) # But result can be found only with limit 4 or more problem = Problem(1, TestActionsFunction(), TestResultFunction(), TestGoalTest(5)) result = dls.search(problem) self.assertTrue(dls.is_cutoff(result)) self.assertFalse(dls.is_failure(result))
def test_successful_search(self): ts = TreeSearch() dfs = DepthFirstSearch(ts) problem = Problem(1, TestActionsFunction(), TestResultFunction(), TestGoalTest(4)) result = dfs.search(problem) self.assertEqual(3, len(result)) metrics = dfs.get_metrics() self.assertEqual(3, metrics[ts.METRIC_NODES_EXPANDED]) self.assertEqual(3, metrics[ts.METRIC_PATH_COST])
def test_search_failure(self): ts = TreeSearch() dfs = DepthFirstSearch(ts) # 5 - is a goal state but it's unreachable problem = Problem(1, TestActionsFunction(3), TestResultFunction(), TestGoalTest(5)) result = dfs.search(problem) self.assertTrue(dfs.is_failure(result)) metrics = dfs.get_metrics() self.assertEqual(13, metrics[ts.METRIC_NODES_EXPANDED])
def test_successful_search_with_graph_search(self): gs = GraphSearch() bfs = BreadthFirstSearch(gs) problem = Problem(1, TestActionsFunction(), TestResultFunction(), TestGoalTest(5)) result = bfs.search(problem) self.assertEqual(4, len(result)) metrics = bfs.get_metrics() self.assertEqual(4, metrics[gs.METRIC_NODES_EXPANDED]) self.assertEqual(4, metrics[gs.METRIC_PATH_COST])
def test_search_start_at_goal_state(self): finish = RomaniaCities.BUCHAREST start = RomaniaCities.BUCHAREST rm = get_simplified_road_map_of_part_of_romania() rbfs = RecursiveBestFirstSearch( AStarEvaluationFunction(MapHeuristicFunction(rm, finish))) p = Problem(start, MapActionFunction(rm), MapResultFunction(), MapGoalTestFunction(finish), MapStepCostFunction(rm)) result = rbfs.search(p) self.assertFalse(rbfs.is_failure(result)) self.assertEqual(1, len(result)) self.assertEqual(NoOpAction(), result[0])
def test_search_start_at_goal_state(self): finish = RomaniaCities.BUCHAREST start = RomaniaCities.BUCHAREST rm = get_simplified_road_map_of_part_of_romania() ts = TreeSearch() ass = AStarSearch(ts, MapHeuristicFunction(rm, finish)) p = Problem(start, MapActionFunction(rm), MapResultFunction(), MapGoalTestFunction(finish), MapStepCostFunction(rm)) result = ass.search(p) self.assertFalse(ass.is_failure(result)) self.assertEqual(1, len(result)) self.assertEqual(NoOpAction(), result[0])
def test_aima2_figure_4_4(self): finish = RomaniaCities.BUCHAREST start = RomaniaCities.ARAD rm = get_simplified_road_map_of_part_of_romania() rbfs = RecursiveBestFirstSearch( AStarEvaluationFunction(MapHeuristicFunction(rm, finish))) p = Problem(start, MapActionFunction(rm), MapResultFunction(), MapGoalTestFunction(finish), MapStepCostFunction(rm)) result = rbfs.search(p) self.assertFalse(rbfs.is_failure(result)) self.assertEquals(4, len(result)) self.assertEqual(RomaniaCities.SIBIU, result[0].location) self.assertEqual(RomaniaCities.RIMNICU_VILCEA, result[1].location) self.assertEqual(RomaniaCities.PITESTI, result[2].location) self.assertEqual(RomaniaCities.BUCHAREST, result[3].location)
__author__ = 'Ivan Mushketik' ## # Example of solving N-queens problem with help of DFS # Size of the chess board SIZE = 5 # Using tree DFS ts = TreeSearch() dfs = DepthFirstSearch(ts) # Create empty board empty_board = NQueensBoard(SIZE) # Define N-queen board p = Problem(empty_board, NQIActionsFunctions(), NQResultFunction(), NQueensGoalTest()) # Get list of actions to solve a problem actions = dfs.search(p) board_to_show = NQueensBoard(SIZE) rf = NQResultFunction() # Print each action and perform each action (putting new queen on a board) for action in actions: print(action) board_to_show = rf.result(board_to_show, action) # Print result board print(board_to_show)