def AStar_H1_solution(estado_inicial, estado_objetivo): time_init = time() busca = AStar.A_Star( list_action_function=listarAcoes, execute_action_function=executarAcao, \ hash_function = funcao_hash, cmp_function = comparar_estados,\ heuristic_function=heuristica_peca_fora_do_lugar) N = len(estado_inicial) estado_inicial = [encoding(estado_inicial), 0] estado_objetivo = [encoding(estado_objetivo), 0] solution = busca.A_Star(estado_inicial, estado_objetivo) solution.E0 = decoding(estado_inicial[0]) solution.Ef = decoding(estado_objetivo[0]) solution.states = [decoding(x[0]) for x in solution.states] solution.duration = time() - time_init solution.deepth = busca.graph.branching_factor() solution.width = busca.graph.deepth_factor() return solution
def AStar_H2_solution(estado_inicial, estado_objetivo): time_init = time() busca = AStar.A_Star( list_action_function=listarAcoes, execute_action_function=executarAcao, \ hash_function = funcao_hash, cmp_function = comparar_estados,\ heuristic_function=heuristica_distancia_de_manhattan) N = len(estado_inicial) estado_inicial = [encoding(estado_inicial), 0] estado_objetivo = [encoding(estado_objetivo), 0] solution = busca.A_Star(estado_inicial, estado_objetivo) solution.E0 = decoding(estado_inicial[0]) solution.Ef = decoding(estado_objetivo[0]) solution.states = [decoding(x[0]) for x in solution.states] solution.duration = time() - time_init solution.deepth = busca.graph.branching_factor() solution.width = busca.graph.deepth_factor() return solution #res = AStar_H1_solution(modelo_quebra_cabeca_Astar.E0, modelo_quebra_cabeca_Astar.Eobj) #print(res.actions)
return None #------------------------------------------------ #--Compara igualdade de estados #----É possivel associar um inteiro a cada estado e #----assim a comparação seria direta #----@Ea: estado 1 #----@Eb: estado 2 #----@Retorna True se Ea == Eb e False caso contrário def cmpEstados(Ea, Eb): return Ea == Eb #------------------------------------------------ def funcaoHash(Ea): return int(ord(Ea[0])) % 30 busca = AStar.A_Star(list_action_function=listarAcoes, execute_action_function=executarAcao, \ hash_function = funcaoHash, cmp_function = cmpEstados) for step in busca.A_Star(E0, Eobj): print(str(step) + ", ", end=' ') print("")