Exemplo n.º 1
0
def astar_search(problem):
    node = Node(problem.initial)
    frontier = []
    explored = set()
    h = problem.manhattanDist(node)
    g = node.path_cost
    f = h + g
    heapq.heappush(frontier, (h, node))
    while frontier:
        node = heapq.heappop(frontier)[1]
        if problem.goal_test(node.state):
            print(node.solution())
            return node
        explored.add(node.state)
        for child in node.expand(problem):
            h = problem.manhattanDist(child)
            g = node.path_cost + 1
            f = g + h
            if child.state not in explored and child not in frontier:
                child.path_cost = g
                heapq.heappush(frontier, (f, child))
            elif child in frontier and child.path_cost > g:
                new_child = child
                new_child.cost = g
                frontier.remove(child)
                heapq.heappush(frontier, (f, new_child))
    return None
Exemplo n.º 2
0
def main():
    global default_cube
    global goal
    scrambled = scrambler(default, 3)
    cube = Rubiks(scrambled, goal)

    startTime = time.time()

    result = astar_search(cube)

    endTime = time.time()

    print('solution is', Node.solution(result))
    print('path is', Node.path(result))
    print('걸린 시간 :', endTime - startTime)
Exemplo n.º 3
0
def best_first_greedy_search(problem):
    node = Node(problem.initial)
    frontier = []
    explored = set()
    h = problem.manhattanDist(node)
    heapq.heappush(frontier, (h, node))
    while frontier:
        node = heapq.heappop(frontier)[1]
        if problem.goal_test(node.state):
            print(node.solution())
            return node
        explored.add(node.state)
        for child in node.expand(problem):
            if child.state not in explored and child not in frontier:
                h = problem.manhattanDist(child)
                heapq.heappush(frontier, (h, child))
    return None
Exemplo n.º 4
0
def main():
    global default_problem
    global goal
    global goal2

    puzzle = EightPuzzle(default_problem, goal)

    startTime = time.time()

    result = depth_limited_search(puzzle, 30)

    #result = breadth_first_tree_search(puzzle)

    endTime = time.time()

    print('solution is ', Node.solution(result))
    print('path is', Node.path(result))

    print('걸린 시간 :', endTime - startTime)
Exemplo n.º 5
0
def printPathAndSolution(result):
    print('path is ...')
    print(Node.path(result))
    print('solution is ...')
    print(Node.solution(result))
Exemplo n.º 6
0
 def output(self, node: Node):
     actions = node.solution()
     print(len(actions))
     for action in actions:
         print(action[0] + " " + action[1])