def solve():
    initial_state = get_initial_state()
    if not initial_state:
        tkinter.messagebox.showerror('错误', '非法初始状态')
        return
    goal_state = get_goal_state()
    if not goal_state:
        tkinter.messagebox.showerror('错误', '非法目标状态')
        return
    choice = var.get()
    path = []
    if choice == 1:
        path = bfs.breadth_first_search(initial_state, goal_state)
    elif choice == 2:
        path = dfs.depth_first_search(initial_state, goal_state)
    elif choice == 3:
        path = iddfs.iterative_deepening_dfs(initial_state, goal_state)
    elif choice == 4:
        path = best_first.best_first_search(initial_state, goal_state)
    elif choice == 5:
        path = bidirectional.bidirectional_search(initial_state, goal_state)
    elif choice == 6:
        path = a_star.a_star_search(initial_state, goal_state)
    elif choice == 7:
        path = ida_star.iterative_deepening_a_star(initial_state, goal_state)
    output.delete(1.0, END)
    output.insert(1.0, auxiliary.path_to_str(path))
示例#2
0
def iterative_deepening(matrix, time_limit=True):
    # Calculate the start time
    start_time = time.time()

    # Initialize k (max depth) to 1
    k = 1

    # Execute the algorithm until it returns a solution
    while True:
        # Check if the 60s limit has been exceeded
        if time.time() - start_time > 60 and time_limit:
            return {
                "data": "No solutions - Time exceeded",
                "execution_time": time.time() - start_time,
                "search_path": "No solutions"
            }

        # Execute DFS
        results = depth_first_search(matrix, k)

        # Check if the return type of the DFS is a List
        # If the return type is a list - the DFS returned the path that lead to the goal state - which means the algorithm has
        # succeeded
        if type(results["data"]) is list:
            finish_time = time.time() - start_time

            return {
                "data": results["data"],
                "execution_time": finish_time,
                "search_path": return_search_path(results["search_path"])
            }

        # Increment Max Depth for next iteration
        k = k + 1
    def test_moves_count(self):
        root_node = Node(None, "185432_67", 0, None)

        final_node, monitor = depth_first_search(root_node)

        final_node_moves = final_node.get_path_moves()

        expected_moves_count = 11032

        self.assertEqual(expected_moves_count, len(final_node_moves),
                         "incorrect expected moves count")
    def test_node_counter(self):
        root_node = Node(None, "2_3541687", 0, None)

        final_node, monitor = depth_first_search(root_node)
        final_node_moves = final_node.get_path_moves()

        expected_path_moves_len = 96453
        expected_expansions = 103195

        self.assertEqual(expected_path_moves_len, len(final_node_moves),
                         "path is incorrect")
        self.assertEqual(expected_expansions, monitor.expansions,
                         "incorrect expected expansions")
    def test_real(self):
        root_node = Node(None, "3456_8172", 0, None)

        final_node, monitor = depth_first_search(root_node)
        final_node_moves = final_node.get_path_moves()

        expected_path_moves_len = 43786
        expected_expansions = 45136

        self.assertEqual(expected_path_moves_len, len(final_node_moves),
                         "path is incorrect")
        self.assertEqual(expected_expansions, monitor.expansions,
                         "incorrect expected expansions")
    def test_two_moves_to_objective(self):
        root_node = Node(None, "123456_78", 0, None)

        final_node, monitor = depth_first_search(root_node)
        final_node_moves = final_node.get_path_moves()

        expected_path_moves_len = 106330
        expected_expansions = 115907

        self.assertEqual(expected_path_moves_len, len(final_node_moves),
                         "path is incorrect")
        self.assertEqual(expected_expansions, monitor.expansions,
                         "incorrect expected expansions")
    def test_root_is_objective(self):
        root_node = Node(None, "12345678_", 0, None)

        final_node, monitor = depth_first_search(root_node)
        final_node_moves = final_node.get_path_moves()

        expected_path_moves = []

        expected_expansions = 0

        self.assertEqual(expected_path_moves, final_node_moves,
                         "path is incorrect")
        self.assertEqual(expected_expansions, monitor.expansions,
                         "incorrect expected expansions")
    def test_unsolvable(self):
        root_node = Node(None, "2_5341687", 0, None)

        final_node, monitor = depth_first_search(root_node)

        n = 9
        fact = 1

        for i in range(1, n + 1):
            fact = fact * i

        max_expansions = fact / 2

        self.assertFalse(final_node)
        self.assertEqual(max_expansions, monitor.expansions,
                         "incorrect expected expansions")
def initialize_graph(graph):
    global bfs_stack, dfs_stack

    vertex1 = Vertex('a')
    vertex2 = Vertex('b')
    vertex3 = Vertex('c')
    vertex4 = Vertex('s')
    vertex5 = Vertex('z')

    graph.add_vertex(vertex1)
    graph.add_vertex(vertex2)
    graph.add_vertex(vertex3)
    graph.add_edge(vertex1, vertex3)
    graph.add_edge(vertex4, vertex3)
    graph.add_edge(vertex2, vertex3)
    graph.add_edge(vertex5, vertex1)

    bfs_stack = breath_first_search(graph, vertex1)
    dfs_stack = depth_first_search(graph, graph.get_all_vertices, vertex1)
    def test_one_move_to_objective(self):
        root_node = Node(None, "1234567_8", 0, None)

        final_node, monitor = depth_first_search(root_node)
        final_node_moves = final_node.get_path_moves()

        expected_path_moves = [
            'acima', 'acima', 'direita', 'abaixo', 'abaixo', 'esquerda',
            'acima', 'acima', 'direita', 'abaixo', 'abaixo', 'esquerda',
            'acima', 'acima', 'direita', 'abaixo', 'abaixo', 'esquerda',
            'acima', 'acima', 'direita', 'abaixo', 'abaixo', 'esquerda',
            'acima', 'acima', 'direita', 'abaixo', 'abaixo'
        ]

        expected_expansions = 29

        self.assertEqual(expected_path_moves, final_node_moves,
                         "path is incorrect")
        self.assertEqual(expected_expansions, monitor.expansions,
                         "incorrect expected expansions")
示例#11
0
from id import iterative_deepening
from dfs import depth_first_search

# Number of puzzles
n = 20

# Dimensions
dimensions = 3

# Create puzzle
puzzles = generate_puzzles(n, dimensions)

# Initiate the results array for different algorithms
dfs_results = []
id_results = []
a_star_h1_results = []
a_star_h2_results = []

# Fill the array by computing each puzzle with each algorithm
for puzzle in puzzles:
    dfs_results.append(depth_first_search(puzzle, k=None))
    id_results.append(iterative_deepening(puzzle))
    a_star_h1_results.append(a_star(puzzle, h1))
    a_star_h2_results.append(a_star(puzzle, h2))

# Generate output files
generate_output("results/dfs.txt", dfs_results)
generate_output("results/id.txt", id_results)
generate_output("results/a_star_h1.txt", a_star_h1_results)
generate_output("results/a_star_h2.txt", a_star_h2_results)
from dfs import depth_first_search
from id import iterative_deepening
from a_star import a_star
from heuristics import h1, h2
from utility_functions import print_output

# Insert puzzles in array

puzzles = [((4, 6, 1), (2, 3, 7), (9, 5, 8)),
           ((5, 7, 3), (1, 6, 8), (9, 2, 4))]

for idx, puzzle in enumerate(puzzles):
    dfs_result = depth_first_search(puzzle, k=None)
    id_result = iterative_deepening(puzzle)
    a_star_h1 = a_star(puzzle, h1)
    a_star_h2 = a_star(puzzle, h2)

    print_output("DFS - Puzzle " + str(idx + 1), dfs_result)
    print_output("ID - Puzzle " + str(idx + 1), id_result)
    print_output("A* h1 - Puzzle " + str(idx + 1), a_star_h1)
    print_output("A* h2 - Puzzle " + str(idx + 1), a_star_h2)
示例#13
0
def run_dfs_algorithm(arguments: Arguments):
    root_node = Node(None, arguments.initial_state, 0, None)

    final_node, monitor = depth_first_search(root_node)

    log_results(final_node, monitor)
示例#14
0
from tarjans_strongly_connected import find_strongly_connected_components

if __name__ == '__main__':

    graph = {
        "A": ["B", "C", "D"],
        "B": ["A", "D", "E"],
        "C": ["A", "F"],
        "D": ["B", "D"],
        "E": ["B", "F"],
        "F": ["C", "E", "G"],
        "G": ["F"],
    }

    # Perform Depth-First Search using stacks
    print("DFS: ", depth_first_search(graph, "A", "G"))

    # Perform Depth-First Search using recursive function
    print("DFS(Recursive):", depth_first_search_recursive(graph, "A", "G", set()))

    # Perform Breadth-First Search to find a path between A to G
    print("BFS:", breadth_first_search(graph, "A", "G"))

    graph = {
        "A": ["D"],
        "B": ["D"],
        "C": ["A", "B"],
        "D": ["G", "H"],
        "E": ["A", "D", "F"],
        "F": ["K", "J"],
        "G": ["I"],