From 2bdbfd1ac24d4e9a87daf6fb8b99ec32264d5752 Mon Sep 17 00:00:00 2001 From: luxiaotao1123 <t1341870251@163.com> Date: 星期六, 21 十二月 2024 11:25:27 +0800 Subject: [PATCH] # --- zy-acs-manager/src/main/java/com/zy/acs/manager/core/service/MapService.java | 268 +++++++++++++++++++++++++++-------------------------- 1 files changed, 136 insertions(+), 132 deletions(-) diff --git a/zy-acs-manager/src/main/java/com/zy/acs/manager/core/service/MapService.java b/zy-acs-manager/src/main/java/com/zy/acs/manager/core/service/MapService.java index 107e9a3..5cd7493 100644 --- a/zy-acs-manager/src/main/java/com/zy/acs/manager/core/service/MapService.java +++ b/zy-acs-manager/src/main/java/com/zy/acs/manager/core/service/MapService.java @@ -4,17 +4,20 @@ import com.zy.acs.common.enums.AgvDirectionType; import com.zy.acs.framework.common.Cools; import com.zy.acs.manager.common.utils.MapDataUtils; +import com.zy.acs.manager.core.constant.MapDataConstant; import com.zy.acs.manager.core.service.astart.*; import com.zy.acs.manager.core.service.astart.domain.DynamicNode; import com.zy.acs.manager.core.service.floyd.FloydNavigateService; import com.zy.acs.manager.manager.entity.Code; import com.zy.acs.manager.manager.entity.Loc; +import com.zy.acs.manager.manager.entity.Segment; import com.zy.acs.manager.manager.service.ActionService; import com.zy.acs.manager.manager.service.CodeService; import com.zy.acs.manager.system.service.ConfigService; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang.time.StopWatch; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import java.util.*; @@ -27,6 +30,8 @@ @Component("mapService") public class MapService { + @Value("${floyd.enable}") + private Boolean floydEnable; @Autowired private CodeService codeService; @Autowired @@ -40,15 +45,11 @@ @Autowired private ActionService actionService; - public List<String> checkoutPath(String agvNo, Code startCode, Code endCode, Boolean lock) { - return this.checkoutPath(agvNo, startCode, endCode, lock, null, null); - } - /** - * 瀵诲潃 ===>> A* + * 瀵诲潃 ===>> A Star */ public synchronized List<String> checkoutPath(String agvNo, Code startCode, Code endCode - , Boolean lock, List<String> whiteList, List<String> blackList) { + , Boolean lock, List<String> blackList, Segment segment) { int[] startMapIdx = mapDataDispatcher.getCodeMatrixIdx(null, startCode.getData()); int[] endMapIdx = mapDataDispatcher.getCodeMatrixIdx(null, endCode.getData()); @@ -56,7 +57,9 @@ NavigateNode startNode = new NavigateNode(startMapIdx[0], startMapIdx[1], startCode.getData()); NavigateNode endNode = new NavigateNode(endMapIdx[0], endMapIdx[1], endCode.getData()); - NavigateNode finishNode = aStarNavigateService.execute(agvNo, startNode, endNode, lock, whiteList, blackList); + long startTime = System.currentTimeMillis(); + NavigateNode finishNode = aStarNavigateService.execute(agvNo, startNode, endNode, lock, blackList, segment); +// System.out.println("AStart spend time: " + (System.currentTimeMillis() - startTime)); if (null == finishNode) { log.warn("{} 鍙稟GV妫�绱{}] ===>> [{}]璺緞澶辫触......", agvNo, startCode.getData(), endCode.getData()); @@ -66,7 +69,7 @@ ArrayList<NavigateNode> navigateNodes = new ArrayList<>(); // 娓叉煋 - NavigateNode parentNode = null;//褰撳墠寰幆涓婁竴鑺傜偣锛岀敤浜庢嫄鐐硅绠� + NavigateNode parentNode = null; // 褰撳墠寰幆涓婁竴鑺傜偣锛岀敤浜庢嫄鐐硅绠� while (finishNode != null) { navigateNodes.add(finishNode); @@ -76,20 +79,27 @@ Collections.reverse(navigateNodes); - //灏嗘瘡涓妭鐐归噷闈㈢殑fatherNode鑷充负null(鏂逛究鍚庣画璁$畻鏃剁埗鑺傜偣杩囧瀵艰嚧鏄剧ず鐨勮妭鐐瑰お澶�) - for (NavigateNode navigateNode : navigateNodes) { - //鐖惰妭鐐硅缃负null锛屼笉褰卞搷璁$畻缁撴灉锛屼笉褰卞搷鍚庣画鎿嶄綔銆� - //姝ゆ搷浣滀粎涓哄悗缁帓鏌ュ鐞嗘彁渚涜瑙夋柟渚裤�� - navigateNode.setParent(null); +// for (NavigateNode navigateNode : navigateNodes) { +// navigateNode.setParent(null); +// } + + List<String> navigatePath = navigateNodes.stream().map(NavigateNode::getCodeData).collect(Collectors.toList()); + + // max count of steps + if (navigatePath.size() > MapDataConstant.MAX_STEPS_SINGLE) { + navigatePath = navigatePath.subList(0, MapDataConstant.MAX_STEPS_SINGLE); } - return navigateNodes.stream().map(NavigateNode::getCodeData).collect(Collectors.toList()); + return navigatePath; } /** * 瀵诲潃 ===>> Floyd */ public synchronized List<String> validFeasibility(Code startCode, Code endCode) { + if (!floydEnable) { + return Arrays.asList("00000001", "00000002", "00000003", "00000004", "00000005", "00000006"); + } int startIdx = floydNavigateService.codeIdx(startCode.getId()); int endIdx = floydNavigateService.codeIdx(endCode.getId()); @@ -126,10 +136,9 @@ double deltaX = x1 - x0; double deltaY = y1 - y0; - double angle = Math.atan2(deltaX, deltaY); - int offsetAngle = configService.getVal("mapXoffset", Integer.class); - angle = angle + offsetAngle; - angle = Math.toDegrees(angle); + double angle = -Math.atan2(deltaX, deltaY); + int angleOffsetVal = configService.getVal("mapAngleOffsetVal", Integer.class); + angle = Math.toDegrees(angle) + angleOffsetVal; angle = (angle + 360) % 360; // 灏嗚搴﹁浆鎹负姝e�� return angle; @@ -176,103 +185,6 @@ mapDataDispatcher.modifyDynamicMatrix(lev, pathList, agvNo); } - public synchronized void unlockPath(String agvNo, List<String> nodeList) { - try { - StopWatch stopWatch = new StopWatch(); - stopWatch.start(); - - if (Cools.isEmpty(agvNo, nodeList)) { - return; - } - - List<String> pathList = actionService.getPathListByAgv(agvNo).stream().distinct().collect(Collectors.toList()); - - List<String> inTrace = new ArrayList<>(); - - Set<String> outsideTrace = new HashSet<>(); - - if (Cools.isEmpty(pathList)) { - outsideTrace.addAll(nodeList); - } else { - - int size = nodeList.size(); - for (String code : pathList) { - - - Iterator<String> iterator = nodeList.iterator(); - while (iterator.hasNext()) { - - String next = iterator.next(); - - if (next.equals(code)) { - if (!inTrace.contains(next)) { - inTrace.add(next); - } - - iterator.remove(); - } - } - - } - - - assert inTrace.size() + nodeList.size() <= size; - - if (!Cools.isEmpty(nodeList)) { - outsideTrace.addAll(nodeList); - } - - if (!Cools.isEmpty(inTrace)) { - - String last = inTrace.get(inTrace.size() - 1); - - int idx = pathList.indexOf(last); - inTrace = pathList.subList(0, idx + 1); - } - - - } - - List<String> resetCodeList = new ArrayList<>(); - - Integer lev = null; - - DynamicNode[][] dynamicMatrix = mapDataDispatcher.getDynamicMatrix(lev); - - for (String code : inTrace) { - int[] node = mapDataDispatcher.getCodeMatrixIdx(lev, code); - DynamicNode dynamicNode = dynamicMatrix[node[0]][node[1]]; - - if (dynamicNode.getVehicle().equals(agvNo)) { - resetCodeList.add(code); - } - } - - for (String code : outsideTrace) { - int[] node = mapDataDispatcher.getCodeMatrixIdx(lev, code); - DynamicNode dynamicNode = dynamicMatrix[node[0]][node[1]]; - - if (dynamicNode.getVehicle().equals(agvNo)) { - resetCodeList.add(code); - } - } - - if (!Cools.isEmpty(resetCodeList)) { - - mapDataDispatcher.clearDynamicMatrixByCodeList(lev, resetCodeList); - } - - stopWatch.stop(); - if (stopWatch.getTime() > 50) { - log.info("瑙i攣璺緞鍑芥暟鑺辫垂鏃堕棿涓猴細{}姣......", stopWatch.getTime()); - } - - } catch (Exception e) { - log.error("TrafficService.unlockPath", e); - } - - } - public synchronized void unlockPath(String agvNo, String codeData) { try { StopWatch stopWatch = new StopWatch(); @@ -294,14 +206,20 @@ Integer serial = dynamicNode.getSerial(); + long time = dynamicNode.getTime(); List<String> resetCodeList = new ArrayList<>(); for (int i = 0; i < dynamicMatrix.length; i++) { for (int j = 0; j < dynamicMatrix[i].length; j++) { + + if (i == codeMatrixIdx[0] && j == codeMatrixIdx[1]) { continue; } + DynamicNode node = dynamicMatrix[i][j]; - if (node.getVehicle().equals(agvNo) && node.getSerial() < serial) { - resetCodeList.add(codeMatrix[i][j]); + if (node.getVehicle().equals(agvNo)) { + if (node.getSerial() < serial || node.getTime() != time) { + resetCodeList.add(codeMatrix[i][j]); + } } } } @@ -317,7 +235,7 @@ } } catch (Exception e) { - log.error("TrafficService.unlockPath", e); + log.error("MapService.unlockPath", e); } } @@ -340,8 +258,10 @@ return nodeList.stream().map(NavigateNode::getCodeData).distinct().collect(Collectors.toList()); } + // v1 BFS ------------------------------------------------------------------------------ public List<NavigateNode> getWaveScopeByCode(Integer lev, String code, Double radiusLen) { String[][] codeMatrix = mapDataDispatcher.getCodeMatrix(lev); + String[][] cdaMatrix = mapDataDispatcher.getCdaMatrix(lev); int[] codeMatrixIdx = mapDataDispatcher.getCodeMatrixIdx(lev, code); NavigateNode originNode = new NavigateNode(codeMatrixIdx[0], codeMatrixIdx[1], code); @@ -352,25 +272,25 @@ includeList.add(originNode); existNodes.add(originNode); - this.spreadWaveNode(originNode, originNode, codeMatrix, radiusLen, includeList, existNodes); + this.spreadWaveNode(originNode, originNode, codeMatrix, cdaMatrix, radiusLen, includeList, existNodes); return includeList; } public void spreadWaveNode(NavigateNode originNode, NavigateNode currNode - , String[][] codeMatrix, Double radiusLen + , String[][] codeMatrix, String[][] cdaMatrix, Double radiusLen , List<NavigateNode> includeList, List<NavigateNode> existNodes) { int x = currNode.getX(); int y = currNode.getY(); - this.extendNeighborNodes(originNode, new NavigateNode(x, y + 1), codeMatrix, radiusLen, includeList, existNodes); - this.extendNeighborNodes(originNode, new NavigateNode(x, y - 1), codeMatrix, radiusLen, includeList, existNodes); - this.extendNeighborNodes(originNode, new NavigateNode(x - 1, y), codeMatrix, radiusLen, includeList, existNodes); - this.extendNeighborNodes(originNode, new NavigateNode(x + 1, y), codeMatrix, radiusLen, includeList, existNodes); + this.extendNeighborNodes(originNode, new NavigateNode(x, y + 1), codeMatrix, cdaMatrix, radiusLen, includeList, existNodes); + this.extendNeighborNodes(originNode, new NavigateNode(x, y - 1), codeMatrix, cdaMatrix, radiusLen, includeList, existNodes); + this.extendNeighborNodes(originNode, new NavigateNode(x - 1, y), codeMatrix, cdaMatrix, radiusLen, includeList, existNodes); + this.extendNeighborNodes(originNode, new NavigateNode(x + 1, y), codeMatrix, cdaMatrix, radiusLen, includeList, existNodes); } public void extendNeighborNodes(NavigateNode originNode, NavigateNode nextNode - , String[][] codeMatrix, Double radiusLen + , String[][] codeMatrix, String[][] cdaMatrix, Double radiusLen , List<NavigateNode> includeList, List<NavigateNode> existNodes) { int x = nextNode.getX(); @@ -391,15 +311,10 @@ if (nextNodeCodeData.equals(CodeNodeType.NONE.val)) { - this.spreadWaveNode(originNode, nextNode, codeMatrix, radiusLen, includeList, existNodes); + this.spreadWaveNode(originNode, nextNode, codeMatrix, cdaMatrix, radiusLen, includeList, existNodes); } else { - Integer lev = MapDataDispatcher.MAP_DEFAULT_LEV; - String[][] cdaMatrix = mapDataDispatcher.getCdaMatrix(lev); - -// Code o1 = codeService.selectByData(originNode.getCodeData()); -// Code o2 = codeService.selectByData(nextNodeCodeData); List<Double> o1Cda = MapDataUtils.parseCdaNode(cdaMatrix[originNode.getX()][originNode.getY()]); List<Double> o2Cda = MapDataUtils.parseCdaNode(cdaMatrix[nextNode.getX()][nextNode.getY()]); @@ -408,7 +323,7 @@ nextNode.setCodeData(nextNodeCodeData); includeList.add(nextNode); - this.spreadWaveNode(originNode, nextNode, codeMatrix, radiusLen, includeList, existNodes); + this.spreadWaveNode(originNode, nextNode, codeMatrix, cdaMatrix, radiusLen, includeList, existNodes); } } @@ -430,6 +345,95 @@ return o1.getX() == o2.getX() && o1.getY() == o2.getY(); } + // v2 BFS ------------------------------------------------------------------------------ + public List<NavigateNode> getWaveScopeByCode0(Integer lev, String code, Double radiusLen) { + String[][] codeMatrix = mapDataDispatcher.getCodeMatrix(lev); + + int[] codeMatrixIdx = mapDataDispatcher.getCodeMatrixIdx(lev, code); + NavigateNode originNode = new NavigateNode(codeMatrixIdx[0], codeMatrixIdx[1], code); + + List<NavigateNode> includeList = new ArrayList<>(); + Set<NavigateNode> visited = new HashSet<>(); // Track visited nodes to avoid re-processing + Queue<NavigateNode> queue = new LinkedList<>(); + + includeList.add(originNode); + visited.add(originNode); + queue.offer(originNode); + + while (!queue.isEmpty()) { + NavigateNode currNode = queue.poll(); + this.spreadWaveNode0(originNode, currNode, codeMatrix, radiusLen, includeList, visited, queue); + } + + return includeList; + } + + public void spreadWaveNode0(NavigateNode originNode, NavigateNode currNode, + String[][] codeMatrix, Double radiusLen, + List<NavigateNode> includeList, Set<NavigateNode> visited, Queue<NavigateNode> queue) { + + int x = currNode.getX(); + int y = currNode.getY(); + + // Expand neighbors in all four directions (up, down, left, right) + this.extendNeighborNodes0(originNode, new NavigateNode(x, y + 1), codeMatrix, radiusLen, includeList, visited, queue); + this.extendNeighborNodes0(originNode, new NavigateNode(x, y - 1), codeMatrix, radiusLen, includeList, visited, queue); + this.extendNeighborNodes0(originNode, new NavigateNode(x - 1, y), codeMatrix, radiusLen, includeList, visited, queue); + this.extendNeighborNodes0(originNode, new NavigateNode(x + 1, y), codeMatrix, radiusLen, includeList, visited, queue); + } + + public void extendNeighborNodes0(NavigateNode originNode, NavigateNode nextNode, + String[][] codeMatrix, Double radiusLen, + List<NavigateNode> includeList, Set<NavigateNode> visited, Queue<NavigateNode> queue) { + int x = nextNode.getX(); + int y = nextNode.getY(); + + // Check if the node is out of bounds + if (x < 0 || x >= codeMatrix.length || y < 0 || y >= codeMatrix[0].length) { + return; + } + + // If the node has already been visited, skip it + if (this.isExist0(nextNode, visited)) { + return; + } + + visited.add(nextNode); + + String nextNodeCodeData = codeMatrix[x][y]; + + // If it's a NONE node, we still need to check its surroundings + if (nextNodeCodeData.equals(CodeNodeType.NONE.val)) { + this.spreadWaveNode0(originNode, nextNode, codeMatrix, radiusLen, includeList, visited, queue); + } else { + Integer lev = MapDataDispatcher.MAP_DEFAULT_LEV; + String[][] cdaMatrix = mapDataDispatcher.getCdaMatrix(lev); + + // Check if the distance between nodes is within the radius length + List<Double> o1Cda = MapDataUtils.parseCdaNode(cdaMatrix[originNode.getX()][originNode.getY()]); + List<Double> o2Cda = MapDataUtils.parseCdaNode(cdaMatrix[nextNode.getX()][nextNode.getY()]); + + // Calculate Euclidean distance between the nodes + if (Math.pow(o1Cda.get(0) - o2Cda.get(0), 2) + Math.pow(o1Cda.get(1) - o2Cda.get(1), 2) <= Math.pow(radiusLen, 2)) { + nextNode.setCodeData(nextNodeCodeData); + includeList.add(nextNode); + + // Add the node to the queue to expand its neighbors + queue.offer(nextNode); + } + } + } + + private boolean isExist0(NavigateNode node, Set<NavigateNode> existNodes) { + for (NavigateNode existNode : existNodes) { + if (this.isSame(node, existNode)) { + return true; + } + } + return false; + } + + public Boolean isTurnCorner(String codeData) { if (Cools.isEmpty(codeData)) { return false; -- Gitblit v1.9.1