def ida_search_function(node,g,threshold,method): global md_step global hu_step if method == "MD": f_value = g + heuristic.manhattan_distance_node(node) md_step += 1 else: f_value = g + heuristic.hungarian_method(node) hu_step += 1 if (f_value >threshold): return f_value if (board.check_goal(node)): print("IDA* Goal achieved:") if method == "MD": print("Steps: {}" .format(md_step)) else: print("Steps: {}" .format(hu_step)) board.draw_board(node) return -1 min = float("inf") frontier = board.gen_frontier(node) for i in range(1, len(frontier), 2): tempvalue = ida_search_function(frontier[i], g+1, threshold, method) if (tempvalue == -1): return -1 if (tempvalue < min): min = tempvalue return min
def rbfs_search_function(f_node, f_limit, depth, method): global rbfs_md_step global rbfs_hu_step if board.check_goal(f_node.n): print("RBFS Goal Achieved:") if method == "MD": print("Steps: {}".format(rbfs_md_step)) else: print("Steps: {}".format(rbfs_hu_step)) board.draw_board(f_node.n) return "SUCCESS", f_node.f frontier = board.gen_frontier(f_node.n) if len(frontier) == 0: return "FAILURE", infinity successors = [] for i in range(1, len(frontier), 2): if method == "MD": rbfs_md_step += 1 successors.append(F_node(max((depth + heuristic.manhattan_distance_node(frontier[i])), f_node.f), frontier[i], depth)) else: rbfs_hu_step += 1 successors.append(F_node(max((depth + heuristic.hungarian_method(frontier[i])), f_node.f), frontier[i], depth)) while True: successors.sort() best_node = successors[0] if best_node.f > f_limit: return "FAILURE", best_node.f if len(successors) == 1: alternative = successors[0] else: alternative = successors[1] result, best_node.f = rbfs_search_function(best_node, min(f_limit, alternative.f),depth+1,method) if result != "FAILURE": return result, best_node.f
def ida_run(evaluation_board,method): if method == "MD": ida_threshold = heuristic.manhattan_distance_node(evaluation_board) else: ida_threshold = heuristic.hungarian_method(evaluation_board) while True: search_result = ida_search_function(evaluation_board,0,ida_threshold,method) if (search_result == -1): return "GOAL" ida_threshold = search_result