From 4453cdecbc6e7a925ae76e3223418654f1cf0b18 Mon Sep 17 00:00:00 2001 From: taisheng <taisheng@qq.com> Date: 星期二, 22 四月 2025 16:01:15 +0800 Subject: [PATCH] # --- src/main/java/com/zy/common/utils/NavigateUtils.java | 382 +++++++++++++++++++++++++++++++++++++++++++----------- 1 files changed, 305 insertions(+), 77 deletions(-) diff --git a/src/main/java/com/zy/common/utils/NavigateUtils.java b/src/main/java/com/zy/common/utils/NavigateUtils.java index a0aab42..cb21ea1 100644 --- a/src/main/java/com/zy/common/utils/NavigateUtils.java +++ b/src/main/java/com/zy/common/utils/NavigateUtils.java @@ -1,82 +1,194 @@ package com.zy.common.utils; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONArray; +import com.core.common.SpringUtils; import com.zy.asrs.utils.Utils; import com.zy.common.model.MapNode; import com.zy.common.model.NavigateNode; -import com.zy.core.enums.ShuttleTaskModeType; +import com.zy.common.model.enums.NavigationMapType; +import com.zy.core.News; +import com.zy.core.enums.MapNodeType; +import com.zy.core.model.PythonSimilarityResult; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; +import java.io.BufferedReader; +import java.io.InputStreamReader; import java.util.*; /** * A*绠楁硶浣跨敤宸ュ叿 */ +@Component public class NavigateUtils { - public static List<NavigateNode> calc(String startPoint, String endPoint, Integer mapType) { + @Value("${pythonCalcPath}") + private String pythonCalcPath; + @Value("${pythonCalcSimilarity}") + private String pythonCalcSimilarity; + @Autowired + private NavigateMapData navigateMapData; + + public List<NavigateNode> calc(String startPoint, String endPoint, Integer mapType, List<int[]> shuttlePoints, List<int[]> whites) { + return calcJava(startPoint, endPoint, mapType, shuttlePoints, whites); + } + + public List<NavigateNode> calcJava(String startPoint, String endPoint, Integer mapType, List<int[]> shuttlePoints, List<int[]> whites) { //閫氳繃寮�濮嬬紪鍙峰拰缁撴潫缂栧彿鑾峰彇瀵瑰簲鐨剎y杞村潗鏍� int[] startArr = NavigatePositionConvert.positionToXY(startPoint);//寮�濮嬭妭鐐� int[] endArr = NavigatePositionConvert.positionToXY(endPoint);//缁撴潫鑺傜偣 + ArrayList<int[]> whiteList = new ArrayList<>();//璁剧疆璁$畻鑺傜偣鐨勭櫧鍚嶅崟 + whiteList.add(startArr);//灏嗗紑濮嬭妭鐐硅缃负鐧藉悕鍗曪紝浠ラ槻琚繃婊� + if (whites != null && !whites.isEmpty()) { + whiteList.addAll(whites);//鎵归噺娣诲姞鐧藉悕鍗曡妭鐐� + } + //鑾峰彇褰撳墠鑺傜偣璁$畻鐨勫眰楂橈紝骞惰祴鍊煎埌姣忎竴涓妭鐐逛腑 int lev = Utils.getLev(startPoint); + NavigateSolution solution = new NavigateSolution(mapType, lev, whiteList, shuttlePoints); + int[][] map = solution.map; //鍒濆鍖栧紑濮嬭妭鐐� NavigateNode start = new NavigateNode(startArr[0], startArr[1]); //寮�濮嬭妭鐐规棤鐖惰妭鐐� start.setFather(null); + start.setNodeValue(map[startArr[0]][startArr[1]]); NavigateNode end = new NavigateNode(endArr[0], endArr[1]); - NavigateSolution solution = new NavigateSolution(mapType, lev); - NavigateNode res_node = solution.astarSearch(start, end); + end.setNodeValue(map[endArr[0]][endArr[1]]); + //寮�濮嬭妭鐐癸紝涓嶇撼鍏ョ鐢ㄨ妭鐐瑰唴璁$畻 + + NavigateNode res_node = solution.astarSearchJava(start, end); if (res_node == null) { - System.out.println("鏈壘鍒拌矾寰�"); + News.error("{} dash {} can't find navigate path!", startPoint, endPoint); return null; - } else { - ArrayList<NavigateNode> list = new ArrayList<>(); - - //娓叉煋 - NavigateNode fatherNode = null;//褰撳墠寰幆涓婁竴鑺傜偣锛岀敤浜庢嫄鐐硅绠� - while (res_node != null) { - res_node.setDirection(null); - res_node.setIsInflectionPoint(false); - res_node.setZ(lev);//璁剧疆灞傞珮 - - //瀵绘壘鎷愮偣 - HashMap<String, Object> result = searchInflectionPoint(res_node, fatherNode, res_node.getFather());//鍒嗗埆浼犲叆褰撳墠鑺傜偣銆佺埗鑺傜偣銆佷笅涓�鑺傜偣 - //鍒ゆ柇褰撳墠鑺傜偣鏄惁涓烘嫄鐐� - if (Boolean.parseBoolean(result.get("result").toString())) { - //褰撳墠涓烘嫄鐐� - res_node.setIsInflectionPoint(true); - //鎷愮偣鏂瑰悜 - res_node.setDirection(result.get("direction").toString()); - } - list.add(res_node); - - fatherNode = res_node;//鎶婂綋鍓嶈妭鐐逛繚瀛樻垚涓�涓埗鑺傜偣 - res_node = res_node.getFather();//杩唬鎿嶄綔 - } - - Collections.reverse(list); - - //灏嗘瘡涓妭鐐归噷闈㈢殑fatherNode鑷充负null(鏂逛究鍚庣画璁$畻鏃剁埗鑺傜偣杩囧瀵艰嚧鏄剧ず鐨勮妭鐐瑰お澶�) - for (NavigateNode navigateNode : list) { - //鐖惰妭鐐硅缃负null锛屼笉褰卞搷璁$畻缁撴灉锛屼笉褰卞搷鍚庣画鎿嶄綔銆� - //姝ゆ搷浣滀粎涓哄悗缁帓鏌ュ鐞嗘彁渚涜瑙夋柟渚裤�� - navigateNode.setFather(null); - } - - //璧峰鑺傜偣璁$畻鏂瑰悜 - String direction = calcDirection(list.get(0), list.get(1)); - NavigateNode startNode = list.get(0); - startNode.setDirection(direction); - //鏇存柊鑺傜偣鍒楄〃 - list.set(0, startNode); - return list; } + + ArrayList<NavigateNode> list = new ArrayList<>(); + //娓叉煋 + NavigateNode fatherNode = null;//褰撳墠寰幆涓婁竴鑺傜偣锛岀敤浜庢嫄鐐硅绠� + while (res_node != null) { + res_node.setDirection(null); + res_node.setIsInflectionPoint(false); + res_node.setZ(lev);//璁剧疆灞傞珮 + + //瀵绘壘鎷愮偣 + HashMap<String, Object> result = searchInflectionPoint(res_node, fatherNode, res_node.getFather());//鍒嗗埆浼犲叆褰撳墠鑺傜偣銆佺埗鑺傜偣銆佷笅涓�鑺傜偣 + //鍒ゆ柇褰撳墠鑺傜偣鏄惁涓烘嫄鐐� + if (Boolean.parseBoolean(result.get("result").toString())) { + //褰撳墠涓烘嫄鐐� + res_node.setIsInflectionPoint(true); + //鎷愮偣鏂瑰悜 + res_node.setDirection(result.get("direction").toString()); + } + list.add(res_node); + + fatherNode = res_node;//鎶婂綋鍓嶈妭鐐逛繚瀛樻垚涓�涓埗鑺傜偣 + res_node = res_node.getFather();//杩唬鎿嶄綔 + } + + Collections.reverse(list); + + //灏嗘瘡涓妭鐐归噷闈㈢殑fatherNode鑷充负null(鏂逛究鍚庣画璁$畻鏃剁埗鑺傜偣杩囧瀵艰嚧鏄剧ず鐨勮妭鐐瑰お澶�) + for (NavigateNode navigateNode : list) { + //鐖惰妭鐐硅缃负null锛屼笉褰卞搷璁$畻缁撴灉锛屼笉褰卞搷鍚庣画鎿嶄綔銆� + //姝ゆ搷浣滀粎涓哄悗缁帓鏌ュ鐞嗘彁渚涜瑙夋柟渚裤�� + navigateNode.setFather(null); + } + + //璧峰鑺傜偣璁$畻鏂瑰悜 + String direction = calcDirection(list.get(0), list.get(1)); + NavigateNode startNode = list.get(0); + startNode.setDirection(direction); + //鏇存柊鑺傜偣鍒楄〃 + list.set(0, startNode); + return list; + } + + public List<NavigateNode> calcPython(String startPoint, String endPoint, Integer mapType, List<int[]> shuttlePoints, List<int[]> whites) { + //閫氳繃寮�濮嬬紪鍙峰拰缁撴潫缂栧彿鑾峰彇瀵瑰簲鐨剎y杞村潗鏍� + int[] startArr = NavigatePositionConvert.positionToXY(startPoint);//寮�濮嬭妭鐐� + int[] endArr = NavigatePositionConvert.positionToXY(endPoint);//缁撴潫鑺傜偣 + + ArrayList<int[]> whiteList = new ArrayList<>();//璁剧疆璁$畻鑺傜偣鐨勭櫧鍚嶅崟 + whiteList.add(startArr);//灏嗗紑濮嬭妭鐐硅缃负鐧藉悕鍗曪紝浠ラ槻琚繃婊� + if (whites != null && !whites.isEmpty()) { + whiteList.addAll(whites);//鎵归噺娣诲姞鐧藉悕鍗曡妭鐐� + } + + //鑾峰彇褰撳墠鑺傜偣璁$畻鐨勫眰楂橈紝骞惰祴鍊煎埌姣忎竴涓妭鐐逛腑 + int lev = Utils.getLev(startPoint); + NavigateSolution solution = new NavigateSolution(mapType, lev, whiteList, shuttlePoints); + int[][] map = solution.map; + + //鍒濆鍖栧紑濮嬭妭鐐� + NavigateNode start = new NavigateNode(startArr[0], startArr[1]); + start.setNodeValue(map[startArr[0]][startArr[1]]); + //寮�濮嬭妭鐐规棤鐖惰妭鐐� + start.setFather(null); + + NavigateNode end = new NavigateNode(endArr[0], endArr[1]); + end.setNodeValue(map[endArr[0]][endArr[1]]); + //寮�濮嬭妭鐐癸紝涓嶇撼鍏ョ鐢ㄨ妭鐐瑰唴璁$畻 + + String pathStr = solution.astarSearchPython(start, end, pythonCalcPath); + if (pathStr == null) { + News.error("{} dash {} can't find navigate path!", startPoint, endPoint); + return null; + } + + List<NavigateNode> list = new ArrayList<>(); + + NavigateNode fatherNode = null;//褰撳墠寰幆涓婁竴鑺傜偣锛岀敤浜庢嫄鐐硅绠� + JSONArray rows = JSON.parseArray(pathStr); + + for (int i = 0; i < rows.size(); i++) { + Object currentObj = rows.get(i); + + NavigateNode nextNode = null; + if ((i + 1) < rows.size()) { + Object object = rows.get(i + 1); + nextNode = pythonObjTransferNode(object, lev); + } + + NavigateNode node = pythonObjTransferNode(currentObj, lev); + + //瀵绘壘鎷愮偣 + HashMap<String, Object> result = searchInflectionPoint(node, fatherNode, nextNode);//鍒嗗埆浼犲叆褰撳墠鑺傜偣銆佺埗鑺傜偣銆佷笅涓�鑺傜偣 + //鍒ゆ柇褰撳墠鑺傜偣鏄惁涓烘嫄鐐� + if (Boolean.parseBoolean(result.get("result").toString())) { + //褰撳墠涓烘嫄鐐� + node.setIsInflectionPoint(true); + //鎷愮偣鏂瑰悜 + node.setDirection(result.get("direction").toString()); + } + + list.add(node); + + fatherNode = node;//鎶婂綋鍓嶈妭鐐逛繚瀛樻垚涓�涓埗鑺傜偣 + } + + //灏嗘瘡涓妭鐐归噷闈㈢殑fatherNode鑷充负null(鏂逛究鍚庣画璁$畻鏃剁埗鑺傜偣杩囧瀵艰嚧鏄剧ず鐨勮妭鐐瑰お澶�) + for (NavigateNode navigateNode : list) { + //鐖惰妭鐐硅缃负null锛屼笉褰卞搷璁$畻缁撴灉锛屼笉褰卞搷鍚庣画鎿嶄綔銆� + //姝ゆ搷浣滀粎涓哄悗缁帓鏌ュ鐞嗘彁渚涜瑙夋柟渚裤�� + navigateNode.setFather(null); + } + + //璧峰鑺傜偣璁$畻鏂瑰悜 + String direction = calcDirection(list.get(0), list.get(1)); + NavigateNode startNode = list.get(0); + startNode.setDirection(direction); + //鏇存柊鑺傜偣鍒楄〃 + list.set(0, startNode); + return list; } //鍒ゆ柇褰撳墠鑺傜偣鍒颁笅涓�涓妭鐐规槸鍚︿负鎷愮偣 - public static HashMap<String,Object> searchInflectionPoint(NavigateNode currentNode, NavigateNode fatherNode, NavigateNode nextNode) { + public HashMap<String,Object> searchInflectionPoint(NavigateNode currentNode, NavigateNode fatherNode, NavigateNode nextNode) { HashMap<String, Object> map = new HashMap<>(); map.put("result", false);//鏄惁涓烘嫄鐐癸紝true锛氭嫄鐐癸紝false锛氱洿绾� // 绗竴涓偣鎴栫洿绾跨偣 @@ -95,7 +207,7 @@ /** * 璁$畻鏂瑰悜 */ - public static String calcDirection(NavigateNode currentNode, NavigateNode fatherNode) { + public String calcDirection(NavigateNode currentNode, NavigateNode fatherNode) { //鎷愮偣鏂瑰悜 String direction = ""; // 鏅�氭嫄鐐� @@ -128,30 +240,30 @@ } /** + * 鍔犺浆寮妭鐐� * 鑾峰彇鍒嗘璺緞锛屾瘡褰撴湁涓�涓嫄鐐瑰垯杩涜涓�娆″垎娈碉紝鏈�缁堣繑鍥炴�诲垎娈垫暟鎹� */ - public static ArrayList<ArrayList<NavigateNode>> getSectionPath(List<NavigateNode> mapList) { + public ArrayList<ArrayList<NavigateNode>> getSectionPath(List<NavigateNode> mapList) { ArrayList<ArrayList<NavigateNode>> list = new ArrayList<>(); + ArrayList<NavigateNode> data = new ArrayList<>(); String direction = mapList.get(0).getDirection();//琛岃蛋鏂瑰悜 - for (int i = 0; i < mapList.size(); i++) { - NavigateNode mapNode = mapList.get(i); - boolean isInflectionPoint = mapNode.getIsInflectionPoint(); - data.add(mapNode); - if (isInflectionPoint) { - //鎷愮偣 + for (NavigateNode navigateNode : mapList) { + data.add(navigateNode); + //鎷愮偣 + if (navigateNode.getIsInflectionPoint()) { //鍒嗗壊鏁版嵁 list.add(data);//娣诲姞鏌愪竴娈垫暟鎹� - direction = mapNode.getDirection();//鏇存柊琛岃蛋鏂瑰悜 + direction = navigateNode.getDirection();//鏇存柊琛岃蛋鏂瑰悜 data = new ArrayList<>(); - data.add(mapNode);//灏嗘嫄鐐圭殑缁堢偣锛屾洿鏂版垚涓嬩竴娈靛懡浠ょ殑璧风偣鍧愭爣 - }else { + data.add(navigateNode);//灏嗘嫄鐐圭殑缁堢偣锛屾洿鏂版垚涓嬩竴娈靛懡浠ょ殑璧风偣鍧愭爣 + } else { //鐩磋绾胯矾 - mapNode.setDirection(direction);//璁剧疆琛岃蛋鏂瑰悜 + navigateNode.setDirection(direction);//璁剧疆琛岃蛋鏂瑰悜 } - Integer distance = getXToNextDistance(mapNode);//鑾峰彇褰撳墠鐐瑰埌涓嬩竴鐐圭殑琛岃蛋璺濈 - mapNode.setMoveDistance(distance); + Integer distance = getXToNextDistance(navigateNode);//鑾峰彇褰撳墠鐐瑰埌涓嬩竴鐐圭殑琛岃蛋璺濈 + navigateNode.setMoveDistance(distance); } //灏嗘渶鍚庝竴娈垫暟鎹坊鍔犺繘鍏� @@ -161,9 +273,9 @@ } //鑾峰彇浠巟鐐瑰埌涓嬩竴鐐圭殑琛岃蛋璺濈 - public static Integer getXToNextDistance(NavigateNode xNode) { - NavigateMapData mapData = new NavigateMapData(); - List<List<MapNode>> lists = mapData.getJsonData(1); + public Integer getXToNextDistance(NavigateNode xNode) { + NavigateMapData mapData = SpringUtils.getBean(NavigateMapData.class); + List<List<MapNode>> lists = mapData.getJsonData(xNode.getZ(), NavigationMapType.NONE.id, null, null); if (lists != null) { MapNode mapNode = lists.get(xNode.getX()).get(xNode.getY()); if (mapNode != null) { @@ -184,9 +296,22 @@ } /** + * 鏍规嵁鍘熷鑺傜偣缁撴灉锛岃绠楁�昏璧拌窛绂� + */ + public Integer getOriginPathAllDistance(List<NavigateNode> path) { + ArrayList<ArrayList<NavigateNode>> sectionPath = getSectionPath(path); + Integer allDistance = 0; + for (ArrayList<NavigateNode> navigateNodes : sectionPath) { + Integer distance = getCurrentPathAllDistance(navigateNodes); + allDistance += distance; + } + return allDistance; + } + + /** * 鑾峰彇褰撳墠璺緞鎬昏璧拌窛绂� */ - public static Integer getCurrentPathAllDistance(List<NavigateNode> path) { + public Integer getCurrentPathAllDistance(List<NavigateNode> path) { if (path.size() == 1) { //璺緞鍙湁涓�鏉℃暟鎹紝鍒欑洿鎺ヨ繑鍥炶璧拌窛绂� return path.get(0).getMoveDistance(); @@ -200,19 +325,122 @@ return allDistance; } - public static void main(String[] args) { - //璁$畻璺緞 - List<NavigateNode> calc = calc("1000901", "1800201", ShuttleTaskModeType.PAK_OUT.id); - System.out.println(calc); - System.out.println("------------------------"); -// List<NavigateNode> calc = calc("0501401", "0201801", "out"); - //灏嗚矾寰勫垎鍓叉垚澶氭 - ArrayList<ArrayList<NavigateNode>> data = getSectionPath(calc); - for (ArrayList<NavigateNode> list : data) { - Integer currentPathAllDistance = getCurrentPathAllDistance(list);//璁$畻褰撳墠璺緞鎬昏窛绂� - System.out.println(currentPathAllDistance); - System.out.println(list); + /** + * 鑾峰彇涓棿鐐瑰埌鐩爣鐐硅璧拌窛绂� + */ + public Integer getMiddleToDistDistance(List<NavigateNode> path, NavigateNode middlePath) { + //鏈�鍚庝竴鏉¤妭鐐逛笉璁$畻杩涜璧拌窛绂� + NavigateNode lastPath = path.get(path.size() - 1); + //鎬昏窛绂� + int allDistance = 0; + boolean flag = false; + for (NavigateNode navigateNode : path) { + if (!flag && navigateNode.equals(middlePath)) { + flag = true; + } + + if (navigateNode.equals(lastPath)) { + continue;//鏈�鍚庝竴鏉¤妭鐐逛笉璁$畻杩涜璧拌窛绂� + } + + if (flag) { + allDistance += navigateNode.getMoveDistance(); + } } + return allDistance; + } + + /** + * 妫�娴嬭矾寰勬槸鍚﹀彲鐢�(鍙蛋) + */ + public boolean checkPathIsAvailable(List<NavigateNode> path, Integer shuttleNo, Integer lev) { + int[][] map = navigateMapData.getDataFromRedis(lev, NavigationMapType.DFX.id, null, Utils.getShuttlePoints(shuttleNo, lev)); + for (NavigateNode node : path) { + int value = map[node.getX()][node.getY()]; + if (value != MapNodeType.NORMAL_PATH.id && value != MapNodeType.MAIN_PATH.id && value != MapNodeType.CHARGE.id && value != MapNodeType.CONVEYOR_CAR_GO.id) {//姣嶈建閬�3銆佸瓙杞ㄩ亾0銆佸厖鐢垫々5銆佸皬杞﹀彲璧拌緭閫佺珯 + return false; + } + } + return true; + } + + private NavigateNode pythonObjTransferNode(Object obj, Integer z) { + List<Integer> pathData = JSON.parseArray(obj.toString(), Integer.class); + Integer x = pathData.get(0); + Integer y = pathData.get(1); + + NavigateNode node = new NavigateNode(x, y); + node.setDirection(null); + node.setIsInflectionPoint(false); + node.setZ(z); + return node; + } + + public Double similarityPath(List<NavigateNode> firstPath, List<NavigateNode> secondPath) { + try { + List<int[]> first = new ArrayList<>(); + for (NavigateNode node : firstPath) { + first.add(new int[]{node.getX(), node.getY()}); + } + + List<int[]> second = new ArrayList<>(); + for (NavigateNode node : secondPath) { + second.add(new int[]{node.getX(), node.getY()}); + } + + ProcessBuilder processBuilder = new ProcessBuilder("python", pythonCalcSimilarity, JSON.toJSONString(first), JSON.toJSONString(second)); + processBuilder.redirectErrorStream(true); + + 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; + } + + PythonSimilarityResult result = JSON.parseObject(builder.toString(), PythonSimilarityResult.class); + if (result.getCalcResult() != 200) { + return 0D; + } + + Double similarity = result.getSimilarity(); + return similarity; + } catch (Exception e) { + e.printStackTrace(); + } + return 0D; + } + + public static void main(String[] args) { +// //璁$畻璺緞 +// List<NavigateNode> calc = calc("1000901", "1800201", NavigationMapType.NONE.id, null); +// System.out.println(calc); +// System.out.println("------------------------"); +//// List<NavigateNode> calc = calc("0501401", "0201801", "out"); +// //灏嗚矾寰勫垎鍓叉垚澶氭 +// ArrayList<ArrayList<NavigateNode>> data = getSectionPath(calc); +// for (ArrayList<NavigateNode> list : data) { +// Integer currentPathAllDistance = getCurrentPathAllDistance(list);//璁$畻褰撳墠璺緞鎬昏窛绂� +// System.out.println(currentPathAllDistance); +// System.out.println(list); +// } + + System.loadLibrary("example"); } -- Gitblit v1.9.1