Exemple #1
0
def uniform_cost(world, node):
    """
        Autor: Carlos Estifen Almario Galindez
        Fecha: Marzo 28 2017
        Método que contiene la logica del algoritmo por costo uniforme
        :return:
    """
    father_node = node
    is_goal = check_goal(world, node)
    if is_goal:
        return is_goal, node

    possible_movements = read_matriz(world, node)
    father_node.possible_movements = possible_movements

    nodes_visited.append(father_node)

    if possible_movements.get('block_down').get('move'):
        # se crea el nodo hijo
        son_node = Node()

        # se le asigna un padre al nodo hijo
        son_node.node = father_node

        # se hace el moviento hacia arriba del nodo hijo
        x, y = father_node.move_down(position_x=father_node.position_x,
                                     position_y=father_node.position_y)
        son_node.position_x = x
        son_node.position_y = y

        son_node.depth = father_node.depth + 1
        # pregunta si el nodo que se expandio ya fue expandido

        if not son_node.in_list(nodes_visited) and not son_node.in_list(
                tree_development):

            # si el nodo esta parado es una flor.
            if possible_movements.get('block_down').get('flower'):
                son_node.flower = True

            if father_node.flower:
                son_node.flower = True
                son_node.cost = father_node.cost + 1
            else:
                son_node.cost = father_node.cost + possible_movements.get(
                    'block_down').get('cost')

            tree_development.append(son_node)

    if possible_movements.get('block_up').get('move'):
        # se crea el nodo hijo
        son_node = Node()

        # se le asigna un padre al nodo hijo
        son_node.node = father_node

        # se hace el moviento hacia arriba del nodo hijo
        x, y = father_node.move_up(position_x=father_node.position_x,
                                   position_y=father_node.position_y)
        son_node.position_x = x
        son_node.position_y = y
        son_node.depth = father_node.depth + 1

        # pregunta si el nodo que se expandio ya fue expandido
        if not son_node.in_list(nodes_visited) and not son_node.in_list(
                tree_development):

            # si el nodo esta parado es una flor.
            if possible_movements.get('block_up').get('flower'):
                son_node.flower = True

            if father_node.flower:
                son_node.flower = True
                son_node.cost = father_node.cost + 1
            else:
                son_node.cost = father_node.cost + possible_movements.get(
                    'block_up').get('cost')

            tree_development.append(son_node)

    if possible_movements.get('block_right').get('move'):
        # se crea el nodo hijo
        son_node = Node()

        # se le asigna un padre al nodo hijo
        son_node.node = father_node

        # se hace el moviento hacia arriba del nodo hijo
        x, y = father_node.move_right(position_x=father_node.position_x,
                                      position_y=father_node.position_y)
        son_node.position_x = x
        son_node.position_y = y
        son_node.depth = father_node.depth + 1

        # pregunta si el nodo que se expandio ya fue expandido
        if not son_node.in_list(nodes_visited) and not son_node.in_list(
                tree_development):

            # si el nodo esta parado es una flor.
            if possible_movements.get('block_right').get('flower'):
                son_node.flower = True

            if father_node.flower:
                son_node.flower = True
                son_node.cost = father_node.cost + 1
            else:
                son_node.cost = father_node.cost + possible_movements.get(
                    'block_right').get('cost')

            tree_development.append(son_node)

    if possible_movements.get('block_left').get('move'):
        # se crea el nodo hijo
        son_node = Node()

        # se le asigna un padre al nodo hijo
        son_node.node = father_node

        # se hace el moviento hacia arriba del nodo hijo
        x, y = father_node.move_left(position_x=father_node.position_x,
                                     position_y=father_node.position_y)
        son_node.position_x = x
        son_node.position_y = y
        son_node.depth = father_node.depth + 1
        # pregunta si el nodo que se expandio ya fue expandido
        if not son_node.in_list(nodes_visited) and not son_node.in_list(
                tree_development):

            # si el nodo esta parado es una flor.
            if possible_movements.get('block_left').get('flower'):
                son_node.flower = True

            if father_node.flower:
                son_node.flower = True
                son_node.cost = father_node.cost + 1
            else:
                son_node.cost = father_node.cost + possible_movements.get(
                    'block_left').get('cost')

            tree_development.append(son_node)

    # ordena la lista de los nodos meta por el costo menor y elimina el nodo que se va a expandir
    tree_development.sort(key=lambda node: node.cost)
    del tree_development[0]
    next_node = tree_development[0]
    # next_node.depth = father_node.depth + 1

    return is_goal, next_node
Exemple #2
0
def preferential_by_amplitude(world, node):
    """
    Autor: Kevin Cardona
    Fecha: Marzo 6 2017
    Método que contiene la logica del algoritmo por amplitud
    :return:
    """
    father_node = node
    is_goal = check_goal(world, node)
    if is_goal:
        return is_goal, node

    possible_movements = read_matriz(world, node)

    father_node.possible_movements = possible_movements
    nodes_visited.append(father_node)

    if possible_movements.get('block_up').get('move'):
        # se crea el nodo hijo
        son_node = Node()

        # se le asigna un padre al nodo hijo
        son_node.node = father_node

        # se hace el moviento hacia arriba del nodo hijo
        x, y = father_node.move_up(position_x=father_node.position_x,
                                   position_y=father_node.position_y)
        son_node.position_x = x
        son_node.position_y = y
        son_node.depth = father_node.depth + 1

        if not son_node.in_list(nodes_visited) and not son_node.in_list(
                tree_development):
            # si el nodo esta parado es una flor.
            if possible_movements.get('block_up').get('flower'):
                son_node.flower = True

            if father_node.flower:
                son_node.flower = True
                son_node.cost = father_node.cost + 1
            else:
                son_node.cost = father_node.cost + possible_movements.get(
                    'block_up').get('cost')

            tree_development.append(son_node)

    if possible_movements.get('block_right').get('move'):
        # se crea el nodo hijo
        son_node = Node()

        # se le asigna un padre al nodo hijo
        son_node.node = father_node

        # se hace el moviento hacia arriba del nodo hijo
        x, y = father_node.move_right(position_x=father_node.position_x,
                                      position_y=father_node.position_y)
        son_node.position_x = x
        son_node.position_y = y
        son_node.depth = father_node.depth + 1

        if not son_node.in_list(nodes_visited) and not son_node.in_list(
                tree_development):
            # si el nodo esta parado es una flor.
            if possible_movements.get('block_right').get('flower'):
                son_node.flower = True

            if father_node.flower:
                son_node.flower = True
                son_node.cost = father_node.cost + 1
            else:
                son_node.cost = father_node.cost + possible_movements.get(
                    'block_right').get('cost')

            tree_development.append(son_node)

    if possible_movements.get('block_left').get('move'):
        # se crea el nodo hijo
        son_node = Node()

        # se le asigna un padre al nodo hijo
        son_node.node = father_node

        # se hace el moviento hacia arriba del nodo hijo
        x, y = father_node.move_left(position_x=father_node.position_x,
                                     position_y=father_node.position_y)
        son_node.position_x = x
        son_node.position_y = y
        son_node.depth = father_node.depth + 1

        if not son_node.in_list(nodes_visited) and not son_node.in_list(
                tree_development):
            # si el nodo esta parado es una flor.
            if possible_movements.get('block_left').get('flower'):
                son_node.flower = True

            if father_node.flower:
                son_node.flower = True
                son_node.cost = father_node.cost + 1
            else:
                son_node.cost = father_node.cost + possible_movements.get(
                    'block_left').get('cost')

            tree_development.append(son_node)

    if possible_movements.get('block_down').get('move'):
        # se crea el nodo hijo
        son_node = Node()

        # se le asigna un padre al nodo hijo
        son_node.node = father_node

        # se hace el moviento hacia arriba del nodo hijo
        x, y = father_node.move_down(position_x=father_node.position_x,
                                     position_y=father_node.position_y)
        son_node.position_x = x
        son_node.position_y = y
        son_node.depth = father_node.depth + 1

        if not son_node.in_list(nodes_visited) and not son_node.in_list(
                tree_development):
            # si el nodo esta parado es una flor.
            if possible_movements.get('block_down').get('flower'):
                son_node.flower = True

            if father_node.flower:
                son_node.flower = True
                son_node.cost = father_node.cost + 1
            else:
                son_node.cost = father_node.cost + possible_movements.get(
                    'block_down').get('cost')

            tree_development.append(son_node)

    # elimine el primer nodo del array
    del tree_development[0]

    next_node = tree_development[0]

    return is_goal, next_node
Exemple #3
0
def algorithm_avara(world, node):
    """
    Autor: Kevin Cardona
    Fecha: febrero 17 2018
    método que analiza el mundo para saber que hay alrededor del ratón
    :param world: mundo inicial del juego
    :param node: posición del mario en x
    :return: resumen de los posibles moviemientos que puede hacer mario
    """
    father_node = node
    is_goal = check_goal(world, father_node)
    if is_goal:
        return is_goal, node

    possible_movements = read_matriz(world, father_node)

    father_node.possible_movements = possible_movements
    nodes_visited.append(father_node)

    if possible_movements.get('block_up').get('move'):
        # se crea el nodo hijo
        son_node = Node()

        # se le asigna un padre al nodo hijo
        son_node.node = father_node

        # se hace el moviento hacia arriba del nodo hijo
        x, y = father_node.move_up(position_x=father_node.position_x,
                                   position_y=father_node.position_y)
        son_node.position_x = x
        son_node.position_y = y
        son_node.depth = father_node.depth + 1
        # se agrega el valor de la heuristica al Nodo
        heuristic = calculate_heuristic(son_node)

        son_node.heuristic = heuristic

        if not son_node.in_list(nodes_visited) and not son_node.in_list(
                tree_development):
            # si el nodo esta parado es una flor.
            if possible_movements.get('block_up').get('flower'):
                son_node.flower = True

            if father_node.flower:
                son_node.flower = True
                son_node.cost = father_node.cost + 1
            else:
                son_node.cost = father_node.cost + possible_movements.get(
                    'block_up').get('cost')
            tree_development.append(son_node)

    if possible_movements.get('block_right').get('move'):
        # se crea el nodo hijo
        son_node = Node()

        # se le asigna un padre al nodo hijo
        son_node.node = father_node

        # se hace el moviento hacia arriba del nodo hijo
        x, y = father_node.move_right(position_x=father_node.position_x,
                                      position_y=father_node.position_y)
        son_node.position_x = x
        son_node.position_y = y
        son_node.depth = father_node.depth + 1

        # se agrega el valor de la heuristica al Nodo
        heuristic = calculate_heuristic(son_node)
        son_node.heuristic = heuristic
        if not son_node.in_list(nodes_visited) and not son_node.in_list(
                tree_development):
            # si el nodo esta parado es una flor.
            if possible_movements.get('block_right').get('flower'):
                son_node.flower = True

            if father_node.flower:
                son_node.flower = True
                son_node.cost = father_node.cost + 1
            else:
                son_node.cost = father_node.cost + possible_movements.get(
                    'block_right').get('cost')

            tree_development.append(son_node)

    if possible_movements.get('block_left').get('move'):
        # se crea el nodo hijo
        son_node = Node()

        # se le asigna un padre al nodo hijo
        son_node.node = father_node

        # se hace el moviento hacia arriba del nodo hijo
        x, y = father_node.move_left(position_x=father_node.position_x,
                                     position_y=father_node.position_y)
        son_node.position_x = x
        son_node.position_y = y
        son_node.depth = father_node.depth + 1

        # se agrega el valor de la heuristica al Nodo
        heuristic = calculate_heuristic(son_node)
        son_node.heuristic = heuristic

        if not son_node.in_list(nodes_visited) and not son_node.in_list(
                tree_development):
            # si el nodo esta parado es una flor.
            if possible_movements.get('block_left').get('flower'):
                son_node.flower = True

            if father_node.flower:
                son_node.flower = True
                son_node.cost = father_node.cost + 1
            else:
                son_node.cost = father_node.cost + possible_movements.get(
                    'block_left').get('cost')

            tree_development.append(son_node)

    if possible_movements.get('block_down').get('move'):
        # se crea el nodo hijo
        son_node = Node()

        # se le asigna un padre al nodo hijo
        son_node.node = father_node

        # se hace el moviento hacia arriba del nodo hijo
        x, y = father_node.move_down(position_x=father_node.position_x,
                                     position_y=father_node.position_y)
        son_node.position_x = x
        son_node.position_y = y
        son_node.depth = father_node.depth + 1

        # se agrega el valor de la heuristica al Nodo
        heuristic = calculate_heuristic(son_node)
        son_node.heuristic = heuristic
        if not son_node.in_list(nodes_visited) and not son_node.in_list(
                tree_development):
            # si el nodo esta parado es una flor.
            if possible_movements.get('block_down').get('flower'):
                son_node.flower = True

            if father_node.flower:
                son_node.flower = True
                son_node.cost = father_node.cost + 1
            else:
                son_node.cost = father_node.cost + possible_movements.get(
                    'block_down').get('cost')

            tree_development.append(son_node)

    organized = sorted(tree_development, key=lambda node: node.heuristic)
    next_node = organized[0]

    index = tree_development.index(next_node)
    del tree_development[index]

    return is_goal, next_node
Exemple #4
0
def preferential_by_depth(world, node):
    """
    Autor: Kevin Cardona
    Fecha: Abril 4 2017
    Método que contiene la logica del algoritmo por profundidad evitando ciclos
    :return:
    """
    father_node = node
    is_goal = check_goal(world, node)
    if is_goal:
        return is_goal, node

    possible_movements = read_matriz(world, node)

    father_node.possible_movements = possible_movements

    if possible_movements.get('block_up').get('move'):

        # se crea el nodo hijo
        son_node = Node()

        # se le asigna un padre al nodo hijo
        son_node.node = father_node

        # se hace el moviento hacia arriba del nodo hijo
        x, y = father_node.move_up(position_x=father_node.position_x,
                                   position_y=father_node.position_y)
        son_node.position_x = x
        son_node.position_y = y

        son_node.depth = father_node.depth + 1

        # evitar devolverse
        to_be_return = next_position_is_to_be_return(son_node)
        if not to_be_return:

            # evitar el ciclo
            node_found = search_node_in_tree(son_node)
            if not node_found:

                # si el nodo esta parado es una flor.
                if possible_movements.get('block_up').get('flower'):
                    son_node.flower = True

                if father_node.flower:
                    son_node.flower = True
                    son_node.cost = father_node.cost + 1
                else:
                    son_node.cost = father_node.cost + possible_movements.get(
                        'block_up').get('cost')

                tree_development.insert(0, son_node)

    if possible_movements.get('block_right').get('move'):
        # se crea el nodo hijo
        son_node = Node()

        # se le asigna un padre al nodo hijo
        son_node.node = father_node

        # se hace el moviento hacia arriba del nodo hijo
        x, y = father_node.move_right(position_x=father_node.position_x,
                                      position_y=father_node.position_y)
        son_node.position_x = x
        son_node.position_y = y
        son_node.depth = father_node.depth + 1

        # evitar devolverse
        to_be_return = next_position_is_to_be_return(son_node)
        if not to_be_return:

            # evitar el ciclo
            node_found = search_node_in_tree(son_node)
            if not node_found:

                # si el nodo esta parado es una flor.
                if possible_movements.get('block_right').get('flower'):
                    son_node.flower = True

                if father_node.flower:
                    son_node.flower = True
                    son_node.cost = father_node.cost + 1
                else:
                    son_node.cost = father_node.cost + possible_movements.get(
                        'block_right').get('cost')

                tree_development.insert(0, son_node)

    if possible_movements.get('block_left').get('move'):
        # se crea el nodo hijo
        son_node = Node()

        # se le asigna un padre al nodo hijo
        son_node.node = father_node

        # se hace el moviento hacia arriba del nodo hijo
        x, y = father_node.move_left(position_x=father_node.position_x,
                                     position_y=father_node.position_y)
        son_node.position_x = x
        son_node.position_y = y
        son_node.depth = father_node.depth + 1

        # evitar devolverse
        to_be_return = next_position_is_to_be_return(son_node)
        if not to_be_return:

            # evitar el ciclo
            node_found = search_node_in_tree(son_node)
            if not node_found:

                # si el nodo esta parado es una flor.
                if possible_movements.get('block_left').get('flower'):
                    son_node.flower = True

                if father_node.flower:
                    son_node.flower = True
                    son_node.cost = father_node.cost + 1
                else:
                    son_node.cost = father_node.cost + possible_movements.get(
                        'block_left').get('cost')

                tree_development.insert(0, son_node)

    if possible_movements.get('block_down').get('move'):
        # se crea el nodo hijo
        son_node = Node()

        # se le asigna un padre al nodo hijo
        son_node.node = father_node

        # se hace el moviento hacia arriba del nodo hijo
        x, y = father_node.move_down(position_x=father_node.position_x,
                                     position_y=father_node.position_y)
        son_node.position_x = x
        son_node.position_y = y
        son_node.depth = father_node.depth + 1
        # evitar devolverse
        to_be_return = next_position_is_to_be_return(son_node)
        if not to_be_return:

            # evitar el ciclo
            node_found = search_node_in_tree(son_node)
            if not node_found:

                # si el nodo esta parado es una flor.
                if possible_movements.get('block_down').get('flower'):
                    son_node.flower = True

                if father_node.flower:
                    son_node.flower = True
                    son_node.cost = father_node.cost + 1
                else:
                    son_node.cost = father_node.cost + possible_movements.get(
                        'block_down').get('cost')

                tree_development.insert(0, son_node)

    # elimine el nodo padre del array
    index_father_node = tree_development.index(father_node)
    del tree_development[index_father_node]

    next_node = tree_development[0]
    return is_goal, next_node