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/agv1.py |  159 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 159 insertions(+), 0 deletions(-)

diff --git a/zy-acs-manager/src/main/resources/agv1.py b/zy-acs-manager/src/main/resources/agv1.py
new file mode 100644
index 0000000..b7b8006
--- /dev/null
+++ b/zy-acs-manager/src/main/resources/agv1.py
@@ -0,0 +1,159 @@
+import ast
+import sys
+import numpy as np
+import json
+import time
+import redis
+from collections import deque
+
+radiusLen = None
+
+# 灏嗗瓧绗︿覆杞崲涓烘诞鐐瑰瀷鏁扮粍
+def convert_to_float_array(str_array):
+    if isinstance(str_array, str):
+        return np.array(ast.literal_eval(str_array), dtype=float)
+    return str_array
+
+def getWaveScopeByCode(x, y):
+    code = codeMatrix[x, y]
+    includeList = []
+    existNodes = set()
+    spreadWaveNode({"x": x, "y": y}, {"x": x, "y": y}, existNodes, includeList)
+    return includeList
+
+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
+
+    existNodes.add((x, y))
+
+    nextNodeCodeData = codeMatrix[x, y]
+
+    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])
+
+        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)
+
+# 鎵惧埌鏌愪釜鍊煎搴旂殑 x, y 涓嬫爣
+def find_value_in_matrix(value):
+    indices = np.where(codeMatrix == value)
+    return list(zip(indices[0], indices[1]))
+
+def initWaveMatrix():
+    lev = 1
+    waveMatrix = np.empty_like(codeMatrix, dtype=object)
+
+    for x in range(codeMatrix.shape[0]):
+        for y in range(codeMatrix.shape[1]):
+            if codeMatrix[x][y] == 'NONE':
+                waveMatrix[x][y] = "-"
+            else:
+                waveMatrix[x][y] = '[]'
+
+    return waveMatrix
+
+# 浼樺寲鐗堟湰锛氫娇鐢ㄩ泦鍚堟潵鎻愰珮鎬ц兘
+def mergeWave(originWave, vehicle):
+    # 灏嗗瓧绗︿覆瑙f瀽涓洪泦鍚�
+    set_data = set(ast.literal_eval(originWave))
+    # 濡傛灉 vehicle 涓嶅湪闆嗗悎涓紝鍒欐坊鍔�
+    set_data.add(vehicle)
+    # 杩斿洖搴忓垪鍖栧悗鐨勫瓧绗︿覆
+    return json.dumps(list(set_data))
+
+# 灏� dynamicMatrix 杞崲涓� numpy 缁撴瀯鍖栨暟缁�
+def convert_to_structured_array(dynamicMatrix):
+    # 瀹氫箟缁撴瀯鍖栨暟缁勭殑 dtype
+    dtype = [('serial', int), ('vehicle', 'U2')]
+    # 灏嗗祵濂楃殑鍒楄〃杞崲涓虹粨鏋勫寲鏁扮粍
+    structured_array = np.array([tuple(d.values()) for row in dynamicMatrix for d in row], dtype=dtype)
+    # 閲嶅涓哄師濮嬬殑浜岀淮褰㈢姸
+    return structured_array.reshape(len(dynamicMatrix), -1)
+
+# 浣跨敤 numpy 鍔犻�熺殑浠g爜
+def process_dynamic_matrix(dynamicMatrix, codeMatrix):
+    # 灏� dynamicMatrix 杞崲涓虹粨鏋勫寲鏁扮粍
+    dynamicMatrix = convert_to_structured_array(dynamicMatrix)
+
+    # 鑾峰彇 dynamicMatrix 鐨勫舰鐘�
+    rows, cols = dynamicMatrix.shape
+
+    # 鍒涘缓涓�涓竷灏旀帺鐮侊紝鐢ㄤ簬绛涢�夊嚭 vehicle 涓嶄负 '0' 鍜� '-1' 鐨勫厓绱�
+    mask = (dynamicMatrix['vehicle'] != '0') & (dynamicMatrix['vehicle'] != '-1')
+
+    # 鑾峰彇婊¤冻鏉′欢鐨� x 鍜� y 鍧愭爣
+    x_indices, y_indices = np.where(mask)
+
+    # 閬嶅巻婊¤冻鏉′欢鐨勫潗鏍�
+    for x, y in zip(x_indices, y_indices):
+        # print(code)
+        data = dynamicMatrix[x][y]
+        vehicle = data['vehicle']
+        includeList = getWaveScopeByCode(x,y)
+        for include in includeList:
+            originWave = waveMatrix[include['x']][include['y']]
+            waveMatrix[include['x']][include['y']] = mergeWave(originWave, vehicle)
+
+radiusLenStr = sys.argv[1]
+radiusLen = float(radiusLenStr)
+
+redisHost = sys.argv[2]
+redisPwd = sys.argv[3]
+redisPort = sys.argv[4]
+redisIdx = sys.argv[5]
+
+startTime = time.perf_counter()
+
+# 鍒涘缓涓�涓繛鎺ユ睜
+pool = redis.ConnectionPool(host=redisHost, port=int(redisPort), password=redisPwd, db=int(redisIdx))
+r = redis.Redis(connection_pool=pool)
+
+codeMatrixStr = r.get('KV.AGV_MAP_ASTAR_CODE_FLAG.1')
+codeMatrix = np.array(json.loads(codeMatrixStr))
+
+cdaMatrixStr = r.get('KV.AGV_MAP_ASTAR_CDA_FLAG.1')
+cdaMatrix = np.array(json.loads(cdaMatrixStr))
+
+dynamicMatrixStr = r.get('KV.AGV_MAP_ASTAR_DYNAMIC_FLAG.1')
+dynamicMatrix = np.array(json.loads(dynamicMatrixStr))
+
+waveMatrix = initWaveMatrix()
+
+# # 浣跨敤 numpy 鍔犻�熺殑浠g爜
+process_dynamic_matrix(dynamicMatrix, codeMatrix)
+
+# for x in range(dynamicMatrix.shape[0]):
+#     for y in range(dynamicMatrix.shape[1]):
+#         data = dynamicMatrix[x, y]
+#         vehicle = data['vehicle']
+#         if vehicle != '0' and vehicle != '-1':
+#             getWaveScopeByCode(x, y)
+
+# 灏� numpy.ndarray 杞崲涓哄祵濂楀垪琛�
+waveMatrixList = waveMatrix.tolist()
+# 灏嗗祵濂楀垪琛ㄨ浆鎹负 JSON 瀛楃涓�
+waveMatrixJsonStr = json.dumps(waveMatrixList)
+
+r.set("KV.AGV_MAP_ASTAR_WAVE_FLAG.1",waveMatrixJsonStr)
+
+end = time.perf_counter()
+# print('绋嬪簭杩愯鏃堕棿涓�: %s Seconds' % (end - startTime))
+print("1")
+

--
Gitblit v1.9.1