示例#1
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)
示例#2
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)
示例#3
0
def sarkissian_hw6_2(problem):

    node = Node(problem.initial)

    if problem.goal_test(node.state):
        return node

    # heuristic function = the node's straight line distance to the goal node + the node's path cost
    def h(_node):
        return problem.straight_line_distance(_node.state) + _node.path_cost

    frontier = NodePriorityQueue(h)
    frontier.push(node)
    visited = []

    while len(frontier) > 0:

        # pop a node out of the queue (this will always be the node with the lowest hueristic)
        node = frontier.pop(
        )[1]  # NodePriorityQueue.pop() returns a tuple = (heuristic(node), node)
        visited.append(node.state)

        # if that node is the goal node, return the solution
        if problem.goal_test(node.state):

            print(f"Nodes Visited: {len(visited)}")
            print(f"Distance Traveled: {node.path_cost} km")

            return [
                (n.state.lower(), h(n)) for n in node.path()
            ]  # returns a list of 2-tuples (node.state, heuristic(node))

        # if that node isn't the goal node, add its children to the frontier if they haven't already been visited
        for child in node.expand(problem):
            if child.state not in visited:
                frontier.push(child)

    return "FAILED"
def printPathAndSolution(result):
    print('path is ...')
    print(Node.path(result))
    print('solution is ...')
    print(Node.solution(result))