def getIDSData(goal):
    res = iterativeDeepeningSearch(startState,actionsF,takeActionF, lambda (s): s==goal, 20)
    depth = len(res) - 1 # not counting start state..
    nodes = getNodeCount()
    branchf = ebf(nodes, depth)
    
    return depth, nodes, branchf
    # Test actionsF as shown in class assignment details
    assert(actionsF(state) == ['left', 'right', 'up', 'down'])
    state = [1,0,3, 4,2,5, 6,7,8]
    assert(actionsF(state) == ['left', 'right', 'down'])
    
    # Test takeActionF as shown in class assignment details
    state = [1,0,3, 4,2,5, 6,7,8]
    assert(takeActionF(copy(state), 'left') == [0,1,3, 4,2,5, 6,7,8])
    state = [1,0,3, 4,2,5, 6,7,8]
    assert(takeActionF(copy(state), 'right') == [1,3,0, 4,2,5, 6,7,8])
    state = [1,0,3, 4,2,5, 6,7,8]
    assert(takeActionF(copy(state), 'down') == [1,2,3, 4,0,5, 6,7,8])
    state = [1,2,3, 4,0,5, 6,7,8]
    assert(takeActionF(copy(state), 'up') == [1,0,3, 4,2,5, 6,7,8])
    
    # Setup a test state and goal
    start = [1,2,3, 4,0,5, 6,7,8]
    goal =  [1,2,3, 4,5,8, 6,0,7]
    def goalTestF(state):
        return state == goal

    # Test printing a solution path
    solution = search.iterativeDeepeningSearch(start, actionsF,
                                               takeActionF, goalTestF, 2)
    printResult(start, goal, solution)
    
    # Test printing a solution path
    solution = search.iterativeDeepeningSearch(start, actionsF,
                                               takeActionF, goalTestF, 10)
    printResult(start, goal, solution)
        
    """
      moves: number of random moves to apply

      Creates a random eight puzzle by applying
      a series of 'moves' random moves to a solved
      puzzle.
    """
    puzzle = EightPuzzleState([0,1,2,3,4,5,6,7,8])
    for i in range(moves):
        # Execute a random legal move
        puzzle = puzzle.result(random.sample(puzzle.legalMoves(), 1)[0])
    return puzzle

if __name__ == '__main__':
    puzzle = createRandomEightPuzzle(25)
    print('A random puzzle:')
    print(puzzle)

    problem = EightPuzzleSearchProblem(puzzle)
    path = search.iterativeDeepeningSearch(problem)
    print('IDS found a path of %d moves: %s' % (len(path), str(path)))
    curr = puzzle
    i = 1
    for a in path:
        curr = curr.result(a)
        print('After %d move%s: %s' % (i, ("", "s")[i>1], a))
        print(curr)

        raw_input("Press return for the next state...")   # wait for key stroke
        i += 1
Exemple #4
0
 def __init__(self):
     self.searchFunction = lambda prob: search.iterativeDeepeningSearch(prob)
     self.searchType = FoodSearchProblem
Exemple #5
0
    # Test printState
    state = [0, 1, 2, 3]
    assert (printState(state) == "01\n23")
    state = [1, 0, 2, 3]
    assert (printState(state) == "10\n23")

    # Test actionsF
    assert (actionsF(state) == ['left', 'down'])
    state = [2, 1, 0, 3]
    assert (actionsF(state) == ['right', 'up'])

    # Test takeActionF
    assert (takeActionF(copy(state), 'right') == [2, 1, 3, 0])
    assert (takeActionF(copy(state), 'up') == [0, 1, 2, 3])

    # Setup a test state and goal
    start = [0, 1, 2, 3]
    goal = [1, 3, 2, 0]

    def goalTestF(state):
        return state == goal

    # Test printing a solution path
    solution = search.iterativeDeepeningSearch(start, actionsF, takeActionF,
                                               goalTestF, 1)
    printResult(start, goal, solution)

    # Test printing a solution path
    solution = search.iterativeDeepeningSearch(start, actionsF, takeActionF,
                                               goalTestF, 5)
    printResult(start, goal, solution)
Exemple #6
0
      moves: number of random moves to apply

      Creates a random eight puzzle by applying
      a series of 'moves' random moves to a solved
      puzzle.
    """
    puzzle = EightPuzzleState([0, 1, 2, 3, 4, 5, 6, 7, 8])
    for i in range(moves):
        # Execute a random legal move
        puzzle = puzzle.result(random.sample(puzzle.legalMoves(), 1)[0])
    return puzzle


if __name__ == '__main__':
    puzzle = createRandomEightPuzzle(25)
    print('A random puzzle:')
    print(puzzle)

    problem = EightPuzzleSearchProblem(puzzle)
    path = search.iterativeDeepeningSearch(problem)
    print('IDS found a path of %d moves: %s' % (len(path), str(path)))
    curr = puzzle
    i = 1
    for a in path:
        curr = curr.result(a)
        print('After %d move%s: %s' % (i, ("", "s")[i > 1], a))
        print(curr)

        input("Press return for the next state...")  # wait for key stroke
        i += 1