您好,登錄后才能下訂單哦!
小編給大家分享一下python中A*算法有什么用,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
說明
1、A*算法是靜態路網中解決最短路徑最有效的直接搜索方法。
2、A*算法是啟發式算法,采用最佳優先搜索策略(Best-first),基于評估函數對每個搜索位置的評估結果,猜測最佳優先搜索位置。
A*算法大大降低了低質量的搜索路徑,因此搜索效率高,比傳統的路徑規劃算法更實時、更靈活。但A*算法找到的是相對最優的路徑,而不是絕對最短的路徑,適合大規模、實時性高的問題。
實例
import heapq import copy import re import datetime BLOCK = [] # 給定狀態 GOAL = [] # 目標狀態 # 4個方向 direction = [[0, 1], [0, -1], [1, 0], [-1, 0]] # OPEN表 OPEN = [] # 節點的總數 SUM_NODE_NUM = 0 # 狀態節點 class State(object): def __init__(self, gn=0, hn=0, state=None, hash_value=None, par=None): ''' 初始化 :param gn: gn是初始化到現在的距離 :param hn: 啟發距離 :param state: 節點存儲的狀態 :param hash_value: 哈希值,用于判重 :param par: 父節點指針 ''' self.gn = gn self.hn = hn self.fn = self.gn + self.hn self.child = [] # 孩子節點 self.par = par # 父節點 self.state = state # 局面狀態 self.hash_value = hash_value # 哈希值 def __lt__(self, other): # 用于堆的比較,返回距離最小的 return self.fn < other.fn def __eq__(self, other): # 相等的判斷 return self.hash_value == other.hash_value def __ne__(self, other): # 不等的判斷 return not self.__eq__(other) def manhattan_dis(cur_node, end_node): ''' 計算曼哈頓距離 :param cur_state: 當前狀態 :return: 到目的狀態的曼哈頓距離 ''' cur_state = cur_node.state end_state = end_node.state dist = 0 N = len(cur_state) for i in range(N): for j in range(N): if cur_state[i][j] == end_state[i][j]: continue num = cur_state[i][j] if num == 0: x = N - 1 y = N - 1 else: x = num / N # 理論橫坐標 y = num - N * x - 1 # 理論的縱坐標 dist += (abs(x - i) + abs(y - j)) return dist def test_fn(cur_node, end_node): return 0 def generate_child(cur_node, end_node, hash_set, open_table, dis_fn): ''' 生成子節點函數 :param cur_node: 當前節點 :param end_node: 最終狀態節點 :param hash_set: 哈希表,用于判重 :param open_table: OPEN表 :param dis_fn: 距離函數 :return: None ''' if cur_node == end_node: heapq.heappush(open_table, end_node) return num = len(cur_node.state) for i in range(0, num): for j in range(0, num): if cur_node.state[i][j] != 0: continue for d in direction: # 四個偏移方向 x = i + d[0] y = j + d[1] if x < 0 or x >= num or y < 0 or y >= num: # 越界了 continue # 記錄擴展節點的個數 global SUM_NODE_NUM SUM_NODE_NUM += 1 state = copy.deepcopy(cur_node.state) # 復制父節點的狀態 state[i][j], state[x][y] = state[x][y], state[i][j] # 交換位置 h = hash(str(state)) # 哈希時要先轉換成字符串 if h in hash_set: # 重復了 continue hash_set.add(h) # 加入哈希表 gn = cur_node.gn + 1 # 已經走的距離函數 hn = dis_fn(cur_node, end_node) # 啟發的距離函數 node = State(gn, hn, state, h, cur_node) # 新建節點 cur_node.child.append(node) # 加入到孩子隊列 heapq.heappush(open_table, node) # 加入到堆中 def print_path(node): ''' 輸出路徑 :param node: 最終的節點 :return: None ''' num = node.gn def show_block(block): print("---------------") for b in block: print(b) stack = [] # 模擬棧 while node.par is not None: stack.append(node.state) node = node.par stack.append(node.state) while len(stack) != 0: t = stack.pop() show_block(t) return num def A_start(start, end, distance_fn, generate_child_fn, time_limit=10): ''' A*算法 :param start: 起始狀態 :param end: 終止狀態 :param distance_fn: 距離函數,可以使用自定義的 :param generate_child_fn: 產生孩子節點的函數 :param time_limit: 時間限制,默認10秒 :return: None ''' root = State(0, 0, start, hash(str(BLOCK)), None) # 根節點 end_state = State(0, 0, end, hash(str(GOAL)), None) # 最后的節點 if root == end_state: print("start == end !") OPEN.append(root) heapq.heapify(OPEN) node_hash_set = set() # 存儲節點的哈希值 node_hash_set.add(root.hash_value) start_time = datetime.datetime.now() while len(OPEN) != 0: top = heapq.heappop(OPEN) if top == end_state: # 結束后直接輸出路徑 return print_path(top) # 產生孩子節點,孩子節點加入OPEN表 generate_child_fn(cur_node=top, end_node=end_state, hash_set=node_hash_set, open_table=OPEN, dis_fn=distance_fn) cur_time = datetime.datetime.now() # 超時處理 if (cur_time - start_time).seconds > time_limit: print("Time running out, break !") print("Number of nodes:", SUM_NODE_NUM) return -1 print("No road !") # 沒有路徑 return -1 def read_block(block, line, N): ''' 讀取一行數據作為原始狀態 :param block: 原始狀態 :param line: 一行數據 :param N: 數據的總數 :return: None ''' pattern = re.compile(r'\d+') # 正則表達式提取數據 res = re.findall(pattern, line) t = 0 tmp = [] for i in res: t += 1 tmp.append(int(i)) if t == N: t = 0 block.append(tmp) tmp = [] if __name__ == '__main__': try: file = open("./infile.txt", "r") except IOError: print("can not open file infile.txt !") exit(1) f = open("./infile.txt") NUMBER = int(f.readline()[-2]) n = 1 for i in range(NUMBER): l = [] for j in range(NUMBER): l.append(n) n += 1 GOAL.append(l) GOAL[NUMBER - 1][NUMBER - 1] = 0 for line in f: # 讀取每一行數據 OPEN = [] # 這里別忘了清空 BLOCK = [] read_block(BLOCK, line, NUMBER) SUM_NODE_NUM = 0 start_t = datetime.datetime.now() # 這里添加5秒超時處理,可以根據實際情況選擇啟發函數 length = A_start(BLOCK, GOAL, manhattan_dis, generate_child, time_limit=10) end_t = datetime.datetime.now() if length != -1: print("length =", length) print("time = ", (end_t - start_t).total_seconds(), "s") print("Nodes =", SUM_NODE_NUM)
看完了這篇文章,相信你對“python中A*算法有什么用”有了一定的了解,如果想了解更多相關知識,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。