From 93d8a38f9fd0746b9ce6ac7541bf2b8b48f7c63c Mon Sep 17 00:00:00 2001 From: luxiaotao1123 <t1341870251@163.com> Date: 星期三, 18 十二月 2024 13:11:24 +0800 Subject: [PATCH] # --- zy-acs-manager/src/main/resources/agv.py | 181 ++++++++++++++++++++++----------------------- 1 files changed, 88 insertions(+), 93 deletions(-) diff --git a/zy-acs-manager/src/main/resources/agv.py b/zy-acs-manager/src/main/resources/agv.py index 2425f67..245bb6e 100644 --- a/zy-acs-manager/src/main/resources/agv.py +++ b/zy-acs-manager/src/main/resources/agv.py @@ -4,11 +4,9 @@ import json import time import redis +from collections import deque radiusLen = None -codeMatrix = None # 鍏ㄥ眬鍙橀噺 -cdaMatrix = None # 鍏ㄥ眬鍙橀噺 -waveMatrix = None # 鍏ㄥ眬鍙橀噺 # 灏嗗瓧绗︿覆杞崲涓烘诞鐐瑰瀷鏁扮粍 def convert_to_float_array(str_array): @@ -16,51 +14,63 @@ return np.array(ast.literal_eval(str_array), dtype=float) return str_array -def getWaveScopeByCode(x, y): - code = codeMatrix[x, y] +def getWaveScopeByCode_iterative(x, y): + """ + 浣跨敤骞垮害浼樺厛鎼滅储锛圔FS锛夋潵浠f浛閫掑綊锛屼互閬垮厤閫掑綊娣卞害杩囧ぇ鐨勯棶棰樸�� + """ includeList = [] existNodes = set() - spreadWaveNode({"x": x, "y": y}, {"x": x, "y": y}, existNodes, includeList) - return includeList + queue = deque() -def spreadWaveNode(originNode, currNode, existNodes, includeList): - x, y = currNode['x'], currNode['y'] - neighbors = [(x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1)] - for neighbor in neighbors: - extendNeighborNodes(originNode, {"x": neighbor[0], "y": neighbor[1]}, existNodes, includeList) - -def extendNeighborNodes(originNode, nextNode, existNodes, includeList): - x, y = nextNode['x'], nextNode['y'] - if (x < 0 or x >= codeMatrix.shape[0] or y < 0 or y >= codeMatrix.shape[1]): - return - - if (x, y) in existNodes: - return - + originNode = {"x": x, "y": y} + currNode = {"x": x, "y": y} + queue.append(currNode) existNodes.add((x, y)) - nextNodeCodeData = codeMatrix[x, y] + while queue: + node = queue.popleft() + node_x, node_y = node['x'], node['y'] + neighbors = [ + (node_x + 1, node_y), + (node_x - 1, node_y), + (node_x, node_y + 1), + (node_x, node_y - 1) + ] - if nextNodeCodeData == 'NONE': - spreadWaveNode(originNode, nextNode, existNodes, includeList) - else: - o1Cda = convert_to_float_array(cdaMatrix[originNode['x'], originNode['y']]) - o2Cda = convert_to_float_array(cdaMatrix[x, y]) + for neighbor in neighbors: + nx, ny = neighbor + # 妫�鏌ヨ竟鐣屾潯浠� + if (nx < 0 or nx >= codeMatrix.shape[0] or ny < 0 or ny >= codeMatrix.shape[1]): + continue + if (nx, ny) in existNodes: + continue - num1 = (o1Cda[0] - o2Cda[0]) ** 2 - num2 = (o1Cda[1] - o2Cda[1]) ** 2 - if num1 + num2 <= radiusLen ** 2: - includeList.append({"x": int(x), "y": int(y), "code": str(codeMatrix[x, y])}) - spreadWaveNode(originNode, nextNode, existNodes, includeList) + existNodes.add((nx, ny)) + neighbor_code = codeMatrix[nx, ny] -# 鎵惧埌鏌愪釜鍊煎搴旂殑 x, y 涓嬫爣 + if neighbor_code == 'NONE': + queue.append({"x": nx, "y": ny}) + else: + o1Cda = convert_to_float_array(cdaMatrix[x, y]) + o2Cda = convert_to_float_array(cdaMatrix[nx, ny]) + + num1 = (o1Cda[0] - o2Cda[0]) ** 2 + num2 = (o1Cda[1] - o2Cda[1]) ** 2 + if num1 + num2 <= radiusLen ** 2: + includeList.append({ + "x": int(nx), + "y": int(ny), + "code": str(codeMatrix[nx, ny]) + }) + queue.append({"x": nx, "y": ny}) + + return includeList + def find_value_in_matrix(value): indices = np.where(codeMatrix == value) return list(zip(indices[0], indices[1])) def initWaveMatrix(): - global codeMatrix, waveMatrix # 澹版槑浣跨敤鍏ㄥ眬鍙橀噺 - lev = 1 waveMatrix = np.empty_like(codeMatrix, dtype=object) for x in range(codeMatrix.shape[0]): @@ -87,15 +97,27 @@ # 灏� dynamicMatrix 杞崲涓� numpy 缁撴瀯鍖栨暟缁� def convert_to_structured_array(dynamicMatrix): # 瀹氫箟缁撴瀯鍖栨暟缁勭殑 dtype - dtype = [('serial', int), ('vehicle', 'U2')] + dtype = [('serial', int), ('vehicle', 'U2'), ('time', int)] + + # 纭繚姣忎釜瀛楀吀鍖呭惈鎵�鏈夊瓧娈� + structured_list = [] + for row in dynamicMatrix: + for d in row: + # 鎻愬彇瀛楁锛岀‘淇� 'time' 瀛樺湪锛屽惁鍒欒缃负榛樿鍊硷紙渚嬪 0.0锛� + serial = d.get('serial', 0) + vehicle = d.get('vehicle', '0') + time_val = d.get('time', 0) + structured_list.append((serial, vehicle, time_val)) + # 灏嗗祵濂楃殑鍒楄〃杞崲涓虹粨鏋勫寲鏁扮粍 - structured_array = np.array([tuple(d.values()) for row in dynamicMatrix for d in row], dtype=dtype) + structured_array = np.array(structured_list, dtype=dtype) # 閲嶅涓哄師濮嬬殑浜岀淮褰㈢姸 return structured_array.reshape(len(dynamicMatrix), -1) # 浣跨敤 numpy 鍔犻�熺殑浠g爜 def process_dynamic_matrix(dynamicMatrix, codeMatrix): - global waveMatrix # 澹版槑浣跨敤鍏ㄥ眬鍙橀噺 + global waveMatrix # 纭繚 waveMatrix 鏄叏灞�鍙橀噺 + # 灏� dynamicMatrix 杞崲涓虹粨鏋勫寲鏁扮粍 dynamicMatrix = convert_to_structured_array(dynamicMatrix) @@ -110,26 +132,21 @@ # 閬嶅巻婊¤冻鏉′欢鐨勫潗鏍� for x, y in zip(x_indices, y_indices): - # print(code) vehicle = dynamicMatrix[x][y]['vehicle'] - includeList = getWaveScopeByCode(x, y) + includeList = getWaveScopeByCode_iterative(x, y) for include in includeList: originWave = waveMatrix[include['x']][include['y']] waveMatrix[include['x']][include['y']] = mergeWave(originWave, vehicle) def main(): - global radiusLen, codeMatrix, cdaMatrix, waveMatrix # 澹版槑浣跨敤鍏ㄥ眬鍙橀噺 + global radiusLen, codeMatrix, cdaMatrix, waveMatrix # 澹版槑涓哄叏灞�鍙橀噺 if len(sys.argv) != 6: - print("鐢ㄦ硶: python agv.py <radiusLen> <redisHost> <redisPwd> <redisPort> <redisIdx>") + print("Usage: python script.py <radiusLen> <redisHost> <redisPwd> <redisPort> <redisIdx>") sys.exit(1) radiusLenStr = sys.argv[1] - try: - radiusLen = float(radiusLenStr) - except ValueError: - print("radiusLen 蹇呴』鏄竴涓诞鐐规暟") - sys.exit(1) + radiusLen = float(radiusLenStr) redisHost = sys.argv[2] redisPwd = sys.argv[3] @@ -139,70 +156,48 @@ startTime = time.perf_counter() # 鍒涘缓涓�涓繛鎺ユ睜 - try: - pool = redis.ConnectionPool(host=redisHost, port=int(redisPort), password=redisPwd, db=int(redisIdx)) - r = redis.Redis(connection_pool=pool, decode_responses=True) - except Exception as e: - print(f"鏃犳硶杩炴帴鍒� Redis: {e}") - sys.exit(1) + pool = redis.ConnectionPool(host=redisHost, port=int(redisPort), password=redisPwd, db=int(redisIdx)) + r = redis.Redis(connection_pool=pool) - try: - codeMatrixStr = r.get('KV.AGV_MAP_ASTAR_CODE_FLAG.1') - if codeMatrixStr is None: - raise ValueError("Redis 涓湭鎵惧埌閿�: KV.AGV_MAP_ASTAR_CODE_FLAG.1") - codeMatrix = np.array(json.loads(codeMatrixStr)) - except Exception as e: - print(f"鑾峰彇 codeMatrix 澶辫触: {e}") + # 鑾峰彇骞跺姞杞� codeMatrix + codeMatrixStr = r.get('KV.AGV_MAP_ASTAR_CODE_FLAG.1') + if codeMatrixStr is None: + print("Error: 'KV.AGV_MAP_ASTAR_CODE_FLAG.1' not found in Redis.") sys.exit(1) + codeMatrix = np.array(json.loads(codeMatrixStr)) - try: - cdaMatrixStr = r.get('KV.AGV_MAP_ASTAR_CDA_FLAG.1') - if cdaMatrixStr is None: - raise ValueError("Redis 涓湭鎵惧埌閿�: KV.AGV_MAP_ASTAR_CDA_FLAG.1") - cdaMatrix = np.array(json.loads(cdaMatrixStr)) - except Exception as e: - print(f"鑾峰彇 cdaMatrix 澶辫触: {e}") + # 鑾峰彇骞跺姞杞� cdaMatrix + cdaMatrixStr = r.get('KV.AGV_MAP_ASTAR_CDA_FLAG.1') + if cdaMatrixStr is None: + print("Error: 'KV.AGV_MAP_ASTAR_CDA_FLAG.1' not found in Redis.") sys.exit(1) + cdaMatrix = np.array(json.loads(cdaMatrixStr)) - try: - dynamicMatrixStr = r.get('KV.AGV_MAP_ASTAR_DYNAMIC_FLAG.1') - if dynamicMatrixStr is None: - raise ValueError("Redis 涓湭鎵惧埌閿�: KV.AGV_MAP_ASTAR_DYNAMIC_FLAG.1") - dynamicMatrix = np.array(json.loads(dynamicMatrixStr)) - except Exception as e: - print(f"鑾峰彇 dynamicMatrix 澶辫触: {e}") + # 鑾峰彇骞跺姞杞� dynamicMatrix + dynamicMatrixStr = r.get('KV.AGV_MAP_ASTAR_DYNAMIC_FLAG.1') + if dynamicMatrixStr is None: + print("Error: 'KV.AGV_MAP_ASTAR_DYNAMIC_FLAG.1' not found in Redis.") sys.exit(1) + dynamicMatrix = np.array(json.loads(dynamicMatrixStr)) # 鍒濆鍖� waveMatrix - try: - waveMatrix = initWaveMatrix() - except Exception as e: - print(f"鍒濆鍖� waveMatrix 澶辫触: {e}") - sys.exit(1) + waveMatrix = initWaveMatrix() # 澶勭悊 dynamicMatrix - try: - process_dynamic_matrix(dynamicMatrix, codeMatrix) - except Exception as e: - print(f"澶勭悊 dynamicMatrix 澶辫触: {e}") - sys.exit(1) + process_dynamic_matrix(dynamicMatrix, codeMatrix) # 灏� numpy.ndarray 杞崲涓哄祵濂楀垪琛� waveMatrixList = waveMatrix.tolist() # 灏嗗祵濂楀垪琛ㄨ浆鎹负 JSON 瀛楃涓� waveMatrixJsonStr = json.dumps(waveMatrixList) - try: - r.set("KV.AGV_MAP_ASTAR_WAVE_FLAG.1", waveMatrixJsonStr) - except Exception as e: - print(f"灏� waveMatrix 鍐欏叆 Redis 澶辫触: {e}") - sys.exit(1) + # 灏嗙粨鏋滀繚瀛樺洖 Redis + r.set("KV.AGV_MAP_ASTAR_WAVE_FLAG.1", waveMatrixJsonStr) end = time.perf_counter() - # print('绋嬪簭杩愯鏃堕棿涓�: %s Seconds' % (end - startTime)) + # 鎵撳嵃绋嬪簭杩愯鏃堕棿 +# print(f"绋嬪簭杩愯鏃堕棿涓�: {end - startTime} Seconds") print("1") - # 濡傛灉闇�瑕侊紝鍙互鎵撳嵃杩愯鏃堕棿 - # print(f"绋嬪簭杩愯鏃堕棿涓�: {end - startTime:.2f} 绉�") if __name__ == "__main__": - main() + main() \ No newline at end of file -- Gitblit v1.9.1