コード例 #1
0
    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])
コード例 #2
0
    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])
コード例 #3
0
    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])
コード例 #4
0
    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])
コード例 #5
0
from aima.core.environment.nqueens import NQueensGoalTest, NQResultFunction, NQIActionsFunctions, NQueensBoard
from aima.core.search.framework import TreeSearch, Problem
from aima.core.search.uninformed import DepthFirstSearch

__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)