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
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
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
def execute_algorithm(self, type): """ Autor: Carlos Almario Fecha: Marzo 02 2018 Metodo para ejecutar los algoritmos dependiendo del paramtro de entrada :param type: tipo de algorito a ejecutar (int) :return: build_tree, lista con los nodos solución cost, el costo de la solucion """ position_x_mario, position_y_mario = search_mario(self.world) # limpiar lista tree_development self.node = Node() self.node.position_x = position_x_mario self.node.position_y = position_y_mario self.node.node = None self.node.world = self.world self.node.depth = 0 expanded_nodes = 0 # calcular tiempo de ejecución: start_time = t() # algoritmo preferente por amplitud if type == 1: tree_development.append(self.node) expanded_nodes = 0 while True: goal, node_move = preferential_by_amplitude( self.world, self.node) expanded_nodes += 1 if not goal: self.node = node_move else: self.node = node_move build_tree, cost, depth = build_tree_solution(node_move) break # algoritmo por costo uniforme if type == 2: tree_development.append(self.node) while True: goal, node_move = uniform_cost(self.world, self.node) expanded_nodes += 1 if not goal: self.node = node_move else: self.node = node_move build_tree, cost, depth = build_tree_solution(node_move) break # algoritmo preferente por profundidad evitando ciclos if type == 3: tree_development.append(self.node) expanded_nodes = 0 while True: goal, node_move = preferential_by_depth(self.world, self.node) expanded_nodes += 1 if not goal: self.node = node_move else: build_tree, cost, depth = build_tree_solution(node_move) break # algoritmo avara if type == 4: tree_development.append(self.node) while True: goal, node_move = algorithm_avara(self.world, self.node) expanded_nodes += 1 if not goal: self.node = node_move else: self.node = node_move build_tree, cost, depth = build_tree_solution(node_move) break # algoritmo A* if type == 5: tree_development.append(self.node) while True: goal, world, node_move = a_star(self.world, self.node) expanded_nodes += 1 if not goal: self.node = node_move else: self.node = node_move build_tree, cost, depth = build_tree_solution(node_move) break elapsed_time = t() - start_time time = round(elapsed_time, 5) return build_tree, cost, expanded_nodes, depth, str(time)