Exemple #1
0
def test_by_hand(verbose=True):
    """Run a graph-search."""
    goal_state = EightPuzzleState([[1, 2, 3], [4, 5, 6], [7, 8, 0]])

    init_state = EightPuzzleState.initializeState()
    # eightPuzzleH2(init_state)
    init_state = EightPuzzleState([[7, 2, 4], [5, 0, 6], [8, 3, 1]])

    # print(goal_state.board)
    # print(init_state.board)
    rount = 0
    while not _is_reachable(goal_state.board, init_state.board):

        # print("Hi")
        # round += 1
        init_state = EightPuzzleState.initializeState()
        # print("hi")
    # if _is_reachable(goal_state.board, init_state.board) == True:
    #     print("yes")
    frontier = DFSFrontier()  # Change this to your own implementation.
    print(frontier.add(1))

    # eightPuzzleH1(init_state)
    if verbose:
        print(init_state)
    plan, num_nodes = graph_search(init_state, goal_state, frontier)
    if verbose:
        print(f'A solution is found after generating {num_nodes} nodes.')
    if verbose:
        for action in plan:
            print("T")
            print(f'- {action}')
    return len(plan), num_nodes
Exemple #2
0
def test_by_hand(verbose=True):
    """Run a graph-search."""
    goal_state = EightPuzzleState([[1, 2, 3], [4, 5, 6], [7, 8, 0]])
    init_state = EightPuzzleState.initializeState()
    while not _is_reachable(goal_state.board, init_state.board):
        init_state = EightPuzzleState.initializeState()

    frontier = DFSFrontier()  # Change this to your own implementation.
    if verbose:
        print(init_state)
    plan, num_nodes = graph_search(init_state, goal_state, frontier)
    if verbose:
        print(f'A solution is found after generating {num_nodes} nodes.')
    if verbose:
        for action in plan:
            print(f'- {action}')
    return len(plan), num_nodes
Exemple #3
0
def test_by_hand(verbose=True):
    """Run a graph-search."""
    """Run a graph-search."""
    goal_state = EightPuzzleState([[1, 2, 3], [4, 5, 6], [7, 8, 0]])
    init_state = EightPuzzleState.initializeState()
    while not _is_reachable(goal_state.board, init_state.board):
        init_state = EightPuzzleState.initializeState()

    # frontier = GreedyFrontier(eightPuzzleH2,goal_state)  # Change this to your own implementation.
    # frontier = AStarFrontier(eightPuzzleH2,goal_state)
    frontier = DFSFrontier()
    # frontier.stack
    if verbose:
        print(init_state)
    # print(frontier.stack.board[0])
    plan, num_nodes = graph_search(init_state, goal_state, frontier)
    # print(frontier.stack[0])
    if verbose:
        print(f'A solution is found after generating {num_nodes} nodes.')
    if verbose:
        for action in plan:
            print(f'- {action}')
            pass

    # ========================== checking solution paet ==========================
    # print(init_state)
    cur_node = EightPuzzleNode(init_state, action="INIT")
    for i in plan:
        new_state = cur_node.state.successor(i)
        cur_node = EightPuzzleNode(new_state, cur_node, i)
        # print(new_state)
        # print()
        if new_state.is_goal():
            # print('Congratuations!')
            break

    return len(plan), num_nodes
Exemple #4
0
def test_by_hand(verbose=True):
    """Run a graph-search."""
    goal_state = EightPuzzleState([[1, 2, 3], [4, 5, 6], [7, 8, 0]])
    init_state = EightPuzzleState.initializeState()
    while not _is_reachable(goal_state.board, init_state.board):
        init_state = EightPuzzleState.initializeState()

    frontier = GreedyFrontier(eightPuzzleH2, goal_state)
    #frontier = AStarFrontier(eightPuzzleH2, goal_state)
    if verbose:
        print(init_state)
    plan, num_nodes = graph_search(init_state, goal_state, frontier)
    if verbose:
        print(f'A solution is found after generating {num_nodes} nodes.')
    if verbose:
        for action in plan:
            print(f'- {action}')
    #Test
    #eightPuzzleH1(init_state,goal_state)

# eightPuzzleH2(init_state,goal_state)

#Test
    return len(plan), num_nodes