示例#1
0
def propagate_path_improvements(map_array, parent):

    for kid in parent.children:

        if ((parent.g) + b_m.calculate_g(map_array, kid)) < kid.g:

            kid.parent = parent
            kid.g = parent.g + b_m.calculate_g(map_array, kid)
            kid.f = kid.g + kid.h
示例#2
0
def main():
    board = b_m.generate_board(input('board 1-1 to 1-4 or 2-1 to 2-4:'))
    goal_location = b_m.locate(board, 'B')

    closed = {}
    open_d = {}

    existing = {}

    n0 = search_node(G=0, Parent=None)  # initiate n0 node
    n0.location = b_m.locate(board, 'A')
    n0.h = m_h(n0.location, goal_location)
    n0.f = n0.h + n0.g

    existing[
        n0.
        location] = n0  # adds n0 to existing dictionary, with key on node.location
    open_d[
        n0.
        location] = n0  # adds n0 to open dictionary, with key on node.location

    # stores in heap to get priority queue, on format [(f,(x,y))],
    # heapq understands that the f value is the one that needs to sort
    priority_q = [(n0.f, n0.location)]
    heapq.heapify(priority_q)

    n0.status = 'open_d'  # states that n0 is opened
    solution = None

    # AGENDA LOOP
    while (solution is None):

        # check if there are no open nodes
        if (len(open_d.values()) == 0):
            print('fail')
            return 'fail'

        # assign node with lowest f from priority queue to x
        # item that pops is on format:( f , node.location )
        x = open_d[heapq.heappop(priority_q)[1]]
        open_d.pop(x.location)

        # add x to closed dictionary
        closed[x.location] = x
        x.status = 'closed'

        # if manhattan distance is 0, reacehd goal
        if (m_h(x.location, goal_location)) == 0:
            print('success', x.state)
            return b_m.success(board, x, open_d,
                               closed)  # does not check other solution

        # generate all child noes to node x
        succssessors = g_a_s(board, x)

        # iterate childnode S to X
        for S in succssessors:

            # if childnode S already exist, set the node as closed,
            # else add the node to existing dictionary
            if (S in existing.keys()):
                succssessors[S].status = 'closed'
            else:
                existing[S] = succssessors[S]

            # add the child nodes to x.children list
            x.children.append(succssessors[S])

            # check if S is not closed and not open
            if succssessors[S].status is not 'open_d' and succssessors[
                    S].status is not 'closed':

                # attach and eval routine
                a_a_e(board, succssessors[S], x, goal_location)

                # push S onto open dictionary (easy indexing) and priority queue (quick sorting by f)
                open_d[S] = succssessors[S]
                succssessors[S].status = 'open_d'
                heapq.heappush(priority_q,
                               (succssessors[S].f, succssessors[S].location))
                heapq.heapify(priority_q)

            # check if there is a cheaper way to S
            elif (x.g +
                  b_m.calculate_g(board, succssessors[S])) < succssessors[S].g:

                #attach and eval routine
                a_a_e(board, succssessors[S], x, goal_location)

                #check if S is in the closed dictionary
                if S in closed:

                    # propagate path improvements
                    p_p_i(board, succssessors[S])
示例#3
0
def generate_all_successors(board, node):

    new_nodes = {}
    cell_categories = ["w", "m", "f", "g", "r", ".", "B"]

    temp_location = list(node.location)

    if temp_location[1] < (len(board) - 1):  # checks if this is edge of map

        temp_location[1] += 1  # y value +1 (goes up)

        m_v = b_m.get_map_value(board, temp_location)

        if m_v in cell_categories:  #checks if map value is not a wall

            #creates a node
            temp_location = (temp_location[0], temp_location[1])
            new_node = search_node(G=node.g, Parent=node)
            new_node.g += b_m.calculate_g(board, new_node)
            new_node.location = temp_location
            x = new_node.parent

            #creates the nodes "state", what path it takes from start node
            while x != None:

                new_node.state.append(x.location)
                x = x.parent

            new_nodes[temp_location] = new_node

    #repeats previous actions on left, right, down movement

    temp_location = list(node.location)

    if temp_location[0] < (len(board[0]) - 1):

        temp_location[0] += 1

        m_v = b_m.get_map_value(board, temp_location)

        if m_v in cell_categories:

            temp_location = (temp_location[0], temp_location[1])
            new_node = search_node(G=node.g, Parent=node)
            new_node.g += b_m.calculate_g(board, new_node)
            new_node.location = temp_location
            x = new_node.parent

            while x != None:

                new_node.state.append(x.location)
                x = x.parent

            new_nodes[temp_location] = new_node

    temp_location = list(node.location)

    if temp_location[1] > 0:

        temp_location[1] -= 1

        m_v = b_m.get_map_value(board, temp_location)

        if m_v in cell_categories:

            temp_location = (temp_location[0], temp_location[1])
            new_node = search_node(G=node.g, Parent=node)
            new_node.g += b_m.calculate_g(board, new_node)
            new_node.location = temp_location
            x = new_node.parent

            while x != None:

                new_node.state.append(x.location)
                x = x.parent

            new_nodes[temp_location] = new_node

    temp_location = list(node.location)

    if temp_location[0] > 0:

        temp_location[0] -= 1

        m_v = b_m.get_map_value(board, temp_location)

        if m_v in cell_categories:

            temp_location = (temp_location[0], temp_location[1])
            new_node = search_node(G=node.g, Parent=node)
            new_node.g += b_m.calculate_g(board, new_node)
            new_node.location = temp_location
            x = new_node.parent

            while x != None:

                new_node.state.append(x.location)
                x = x.parent

            new_nodes[temp_location] = new_node

    return new_nodes
示例#4
0
def attach_and_eval(map_array, child, parent, goal_location):

    child.parent = parent
    child.g = parent.g + b_m.calculate_g(map_array, child)
    child.h = manhattan_distance(child.location, goal_location)
    child.f = int(child.g) + int(child.h)