From 3c3ec87a6ac907d375f2fb78f0882ab9f30a1533 Mon Sep 17 00:00:00 2001 From: zhangc <zc@123> Date: 星期一, 21 四月 2025 14:26:00 +0800 Subject: [PATCH] 999 --- src/main/java/com/zy/common/utils/NavigateSolution.java | 266 ++++++++++++++++++++++++++++++++++++----------------- 1 files changed, 180 insertions(+), 86 deletions(-) diff --git a/src/main/java/com/zy/common/utils/NavigateSolution.java b/src/main/java/com/zy/common/utils/NavigateSolution.java index 7c71b78..1967be1 100644 --- a/src/main/java/com/zy/common/utils/NavigateSolution.java +++ b/src/main/java/com/zy/common/utils/NavigateSolution.java @@ -1,33 +1,36 @@ package com.zy.common.utils; +import com.alibaba.fastjson.JSON; +import com.baomidou.mybatisplus.mapper.EntityWrapper; +import com.core.common.SpringUtils; +import com.core.exception.CoolException; import com.zy.common.model.NavigateNode; +import com.zy.core.enums.MapNodeType; +import com.zy.core.model.PythonResult; +import com.zy.system.entity.Config; +import com.zy.system.service.ConfigService; -import java.util.ArrayList; -import java.util.List; -import java.util.PriorityQueue; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.*; /** + * 鍥涘悜搴撴牳蹇� * A*绠楁硶瀹炵幇绫� */ public class NavigateSolution { - // -1 -> 澧欏锛� 1 -> 璧风偣 2 -> 缁堢偣 3-> 姣嶈建 4->绔欑偣 + // -1 -> 澧欏锛� 0 -> 璐т綅锛� 1 -> 璧风偣 2 -> 缁堢偣 3-> 姣嶈建 4->杈撻�佺珯鐐� 5->鍏呯數妗� 6->灏忚溅鍙蛋杈撻�佺珯鐐� 66->灏忚溅 67->鎻愬崌鏈� int[][] map = {{}}; - public NavigateSolution() { - //杞藉叆鍦板浘 - NavigateMapData mapData = new NavigateMapData(); - int[][] data = mapData.getData(); - this.map = data; - } - - public NavigateSolution(Integer mapType, Integer lev, List<int[]> whitePoints) { + public NavigateSolution(Integer mapType, Integer lev, List<int[]> whitePoints, List<int[]> shuttlePoints) { //杞藉叆鍦板浘鎸囧畾灞傞珮鍦板浘 - NavigateMapData mapData = new NavigateMapData(lev); - int[][] data = mapData.getDataFromRedis(mapType, whitePoints); + NavigateMapData mapData = SpringUtils.getBean(NavigateMapData.class); + int[][] data = mapData.getDataFromRedis(lev, mapType, whitePoints, shuttlePoints); if (data == null) { - data = mapData.getData(mapType, whitePoints); + throw new CoolException("鍦板浘鏈浇鍏ワ紒"); } this.map = data; } @@ -40,18 +43,77 @@ public PriorityQueue<NavigateNode> Open = new PriorityQueue<NavigateNode>(); //Close琛ㄧ敤鏅�氱殑鏁扮粍 public ArrayList<NavigateNode> Close = new ArrayList<NavigateNode>(); - //Exist琛ㄧ敤鏉ュ瓨鏀惧凡缁忓嚭鐜拌繃鐨勭粨鐐广�� - public ArrayList<NavigateNode> Exist = new ArrayList<NavigateNode>(); + //鐢ㄦ潵瀛樻斁宸茬粡鍑虹幇杩囩殑缁撶偣銆� + Map<String, Integer> bestGMap = new HashMap<>(); - public NavigateNode astarSearch(NavigateNode start, NavigateNode end) { + public String astarSearchPython(NavigateNode start, NavigateNode end, String pythonScriptPath) { + String maps = JSON.toJSONString(map); + String startStr = start.getX() + "-" + start.getY(); + String targetStr = end.getX() + "-" + end.getY(); + + //榛樿鍦板浘姣嶈建鏂瑰悜x + String mapDirection = "x"; + ConfigService configService = SpringUtils.getBean(ConfigService.class); + if (configService != null) { + Config config = configService.selectOne(new EntityWrapper<Config>() + .eq("code", "direction_map") + .eq("status", 1)); + if (config != null) { + mapDirection = config.getValue(); + } + } + + ProcessBuilder processBuilder = new ProcessBuilder("python", pythonScriptPath, maps, startStr, targetStr, mapDirection); + processBuilder.redirectErrorStream(true); + + try { + Process process = processBuilder.start(); + + // 璇诲彇Python鑴氭湰鐨勮緭鍑� + BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); + String line; + StringBuilder builder = new StringBuilder(); + while ((line = reader.readLine()) != null) { + builder.append(line); + } + + // 绛夊緟Python鑴氭湰鎵ц瀹屾垚 + int exitCode = process.waitFor(); + if (exitCode != 0) { + System.out.println("Python script exited with error code: " + exitCode); + return null; + } + reader.close(); + + if (builder.length() <= 0) { + return null; + } + + PythonResult result = JSON.parseObject(builder.toString(), PythonResult.class); + if (result.getCalcResult() != 200) { + return null; + } + + String path = result.getPath(); + return path; + } catch (IOException | InterruptedException e) { + e.printStackTrace(); + } + return null; + } + + public NavigateNode astarSearchJava(NavigateNode start, NavigateNode end) { //鎶婄涓�涓紑濮嬬殑缁撶偣鍔犲叆鍒癘pen琛ㄤ腑 this.Open.add(start); - //鎶婂嚭鐜拌繃鐨勭粨鐐瑰姞鍏ュ埌Exist琛ㄤ腑 - this.Exist.add(start); //涓诲惊鐜� while (Open.size() > 0) { //鍙栦紭鍏堥槦鍒楅《閮ㄥ厓绱犲苟涓旀妸杩欎釜鍏冪礌浠嶰pen琛ㄤ腑鍒犻櫎 NavigateNode current_node = Open.poll(); + + if (current_node.getX() == end.getX() && current_node.getY() == end.getY()) {//鎵惧埌鐩爣缁撶偣灏辫繑鍥� + return current_node; + } + //灏嗚繖涓粨鐐瑰姞鍏ュ埌Close琛ㄤ腑 Close.add(current_node); //瀵瑰綋鍓嶇粨鐐硅繘琛屾墿灞曪紝寰楀埌涓�涓洓鍛ㄧ粨鐐圭殑鏁扮粍 @@ -59,31 +121,20 @@ //瀵硅繖涓粨鐐归亶鍘嗭紝鐪嬫槸鍚︽湁鐩爣缁撶偣鍑虹幇 for (NavigateNode node : neighbour_node) { // G + H + E (瀵瑰惎鍙戝嚱鏁板鍔犲幓鎷愮偣鏂规calcNodeExtraCost) - int gCost = calcNodeCost(current_node, node) * calcNodeExtraCost(current_node, node, end); - if (node.getX() == end.getX() && node.getY() == end.getY()) {//鎵惧埌鐩爣缁撶偣灏辫繑鍥� - //init_node鎿嶄綔鎶婅繖涓偦灞呯粨鐐圭殑鐖惰妭鐐硅缃负褰撳墠缁撶偣 - //骞朵笖璁$畻鍑篏锛� F锛� H绛夊�� - node.init_node(current_node, end); - return node; - } + int gCost = calcNodeExtraCost(current_node, node, end); - //(瀵瑰惎鍙戝嚱鏁板鍔犲幓鎷愮偣鏂规calcNodeExtraCost) - if (is_exist(node)) { - if (gCost < node.getG()) { - node.setFather(current_node); - node.setG(gCost); - node.setF(node.getG() + node.getH()); - } - }else { - //娌″嚭鐜拌繃鐨勭粨鐐瑰姞鍏ュ埌Open琛ㄤ腑骞朵笖璁剧疆鐖惰妭鐐� - //杩涜璁$畻瀵笹, F, H 绛夊�� - node.init_node(current_node, end); - node.setG(gCost); - node.setH(calcNodeCost(node, end)); - node.setF(node.getG() + node.getH()); + //杩涜璁$畻瀵笹, F, H 绛夊�� + node.setG(1 + gCost); + node.init_node(current_node, end); + node.setH(calcNodeCost(node, end)); + node.setF(node.getG() + node.getH()); + + String key = node.getX() + "_" + node.getY(); + Integer recordedG = bestGMap.get(key); + if (recordedG == null || node.getG() <= recordedG) { + bestGMap.put(key, node.getG()); Open.add(node); - Exist.add(node); } } } @@ -93,6 +144,18 @@ public ArrayList<NavigateNode> extend_current_node(NavigateNode current_node) { + //榛樿鍦板浘姣嶈建鏂瑰悜x + String mapDirection = "x"; + ConfigService configService = SpringUtils.getBean(ConfigService.class); + if (configService != null) { + Config config = configService.selectOne(new EntityWrapper<Config>() + .eq("code", "direction_map") + .eq("status", 1)); + if (config != null) { + mapDirection = config.getValue(); + } + } + //鑾峰彇褰撳墠缁撶偣鐨剎, y int x = current_node.getX(); int y = current_node.getY(); @@ -125,31 +188,70 @@ // neighbour_node.add(node); // } // } - if (map[x][y] == 3) { - //姣嶈建鎵嶈兘杩涜宸﹀彸绉诲姩 - if (is_valid(x, y + 1)) - { - NavigateNode node = new NavigateNode(x, y + 1); - neighbour_node.add(node); - } - if (is_valid(x, y - 1)) - { - NavigateNode node = new NavigateNode(x, y - 1); - neighbour_node.add(node); - } - } - if (map[x][y] == 0 || map[x][y] == 3 || map[x][y] == 4 || map[x][y] == 5) { - //瀛愯建鍜屾瘝杞ㄣ�佽緭閫佺嚎銆佸厖鐢垫々鎵嶈兘杩涜涓婁笅绉诲姩 - if (is_valid(x + 1, y)) - { - NavigateNode node = new NavigateNode(x + 1, y); - neighbour_node.add(node); + if (mapDirection.equals("x")) {//姣嶈建x鏂瑰悜 + if (map[x][y] == MapNodeType.MAIN_PATH.id) { + //姣嶈建鎵嶈兘杩涜涓婁笅绉诲姩 + if (is_valid(x + 1, y)) + { + NavigateNode node = new NavigateNode(x + 1, y); + node.setNodeValue(map[x + 1][y]); + neighbour_node.add(node); + } + if (is_valid(x - 1, y)) + { + NavigateNode node = new NavigateNode(x - 1, y); + node.setNodeValue(map[x - 1][y]); + neighbour_node.add(node); + } } - if (is_valid(x - 1, y)) - { - NavigateNode node = new NavigateNode(x -1, y); - neighbour_node.add(node); + + if (map[x][y] == MapNodeType.NORMAL_PATH.id || map[x][y] == MapNodeType.MAIN_PATH.id || map[x][y] == MapNodeType.CONVEYOR_CAR_GO.id || map[x][y] == MapNodeType.CHARGE.id || map[x][y] == MapNodeType.LIFT.id) { + //瀛愯建鍜屾瘝杞ㄣ�佸皬杞﹀彲璧拌緭閫佺嚎銆佸厖鐢垫々銆佹彁鍗囨満鎵嶈兘杩涜宸﹀彸绉诲姩 + if (is_valid(x, y + 1)) + { + NavigateNode node = new NavigateNode(x, y + 1); + node.setNodeValue(map[x][y + 1]); + neighbour_node.add(node); + } + if (is_valid(x, y - 1)) + { + NavigateNode node = new NavigateNode(x, y - 1); + node.setNodeValue(map[x][y - 1]); + neighbour_node.add(node); + } + } + }else if (mapDirection.equals("y")) {//姣嶈建y鏂瑰悜 + if (map[x][y] == MapNodeType.MAIN_PATH.id) { + //姣嶈建鎵嶈兘杩涜宸﹀彸绉诲姩 + if (is_valid(x, y + 1)) + { + NavigateNode node = new NavigateNode(x, y + 1); + node.setNodeValue(map[x][y + 1]); + neighbour_node.add(node); + } + if (is_valid(x, y - 1)) + { + NavigateNode node = new NavigateNode(x, y - 1); + node.setNodeValue(map[x][y - 1]); + neighbour_node.add(node); + } + } + + if (map[x][y] == MapNodeType.NORMAL_PATH.id || map[x][y] == MapNodeType.MAIN_PATH.id || map[x][y] == MapNodeType.CONVEYOR_CAR_GO.id || map[x][y] == MapNodeType.CHARGE.id || map[x][y] == MapNodeType.LIFT.id) { + //瀛愯建鍜屾瘝杞ㄣ�佸皬杞﹀彲璧拌緭閫佺嚎銆佸厖鐢垫々銆佹彁鍗囨満鎵嶈兘杩涜涓婁笅绉诲姩 + if (is_valid(x + 1, y)) + { + NavigateNode node = new NavigateNode(x + 1, y); + node.setNodeValue(map[x + 1][y]); + neighbour_node.add(node); + } + if (is_valid(x - 1, y)) + { + NavigateNode node = new NavigateNode(x - 1, y); + node.setNodeValue(map[x - 1][y]); + neighbour_node.add(node); + } } } @@ -157,29 +259,21 @@ } public boolean is_valid(int x, int y) { - // 濡傛灉缁撶偣鐨勪綅缃皬浜�0锛屽垯涓嶅悎娉� - if (map[x][y] < 0) return false; - for (NavigateNode node : Exist) { - //濡傛灉缁撶偣鍑虹幇杩囷紝涓嶅悎娉� - if (node.getX() == x && node.getY() == y) { - return false; - } - if (is_exist(new NavigateNode(x, y))) { - return false; - } + if (x < 0 || x >= this.map.length + || y < 0 || y >= this.map[0].length) { + return false; } + // 濡傛灉缁撶偣鐨勪綅缃皬浜�0锛屽垯涓嶅悎娉� + if (map[x][y] < 0) { + return false; + } + + if (map[x][y] == MapNodeType.CAR.id) {//鑺傜偣鏄皬杞� + return false; + } + //浠ヤ笂鎯呭喌閮芥病鏈夊垯鍚堟硶 return true; - } - - public boolean is_exist(NavigateNode node) - { - for (NavigateNode exist_node : Exist) { - if (node.getX() == exist_node.getX() && node.getY() == exist_node.getY()) { - return true; - } - } - return false; } //------------------A*鍚彂鍑芥暟------------------// @@ -208,7 +302,7 @@ 鎷垮埌鐖惰妭鐐瑰拰涓嬩竴鑺傜偣 閫氳繃鍒ゆ柇鐖惰妭鐐瑰拰涓嬩竴鑺傜偣鐨剎鏁版嵁鍜寉鏁版嵁閮戒笉鐩稿悓鏃讹紝鍒欒〃鏄庡綋鍓嶅潗鏍囨槸涓�涓嫄鐐� */ - return 2; + return 1; } //------------------A*鍚彂鍑芥暟-end------------------// -- Gitblit v1.9.1