#
Junjie
3 天以前 8b60bf565eb475c13e39caa305c4415b34d8ec02
src/main/java/com/zy/common/utils/NavigateSolution.java
@@ -1,43 +1,20 @@
package com.zy.common.utils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.core.common.SpringUtils;
import com.core.exception.CoolException;
import com.zy.asrs.entity.BasMap;
import com.zy.asrs.service.BasMapService;
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.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
/**
 * 四向库核心
 * A*算法实现类
 */
public class NavigateSolution {
    // -1 -> 墙壁, 0 -> 货位, 1 -> 起点  2 -> 终点  3-> 母轨  4->输送站点  5->充电桩 6->小车可走输送站点  66->小车  67->提升机
    int[][] map = {{}};
    public NavigateSolution(Integer mapType, Integer lev, List<int[]> whitePoints, List<int[]> shuttlePoints) {
        //载入地图指定层高地图
        NavigateMapData mapData = SpringUtils.getBean(NavigateMapData.class);
        int[][] data = mapData.getDataFromRedis(lev, mapType, whitePoints, shuttlePoints);
        if (data == null) {
            throw new CoolException("地图未载入!");
        }
        this.map = data;
    }
    public NavigateSolution(int[][] data) {
        this.map = data;
    }
    // Open表用优先队列
    public PriorityQueue<NavigateNode> Open = new PriorityQueue<NavigateNode>();
@@ -46,63 +23,96 @@
    //用来存放已经出现过的结点。
    Map<String, Integer> bestGMap = new HashMap<>();
    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();
            }
    public List<List<NavigateNode>> getStationMap(int lev) {
        BasMapService basMapService = SpringUtils.getBean(BasMapService.class);
        BasMap basMap = basMapService.selectOne(new EntityWrapper<BasMap>().eq("lev", lev));
        if (basMap == null) {
            throw new CoolException("地图不存在");
        }
        ProcessBuilder processBuilder = new ProcessBuilder("python", pythonScriptPath, maps, startStr, targetStr, mapDirection);
        processBuilder.redirectErrorStream(true);
        List<List<JSONObject>> dataList = JSON.parseObject(basMap.getData(), List.class);
        try {
            Process process = processBuilder.start();
        List<List<NavigateNode>> navigateNodeList = new ArrayList<>();
        for (int i = 0; i < dataList.size(); i++) {
            List<JSONObject> row = dataList.get(i);
            List<NavigateNode> navigateNodeRow = new ArrayList<>();
            for (int j = 0; j < row.size(); j++) {
                JSONObject map = row.get(j);
                NavigateNode navigateNode = new NavigateNode(i, j);
            // 读取Python脚本的输出
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            StringBuilder builder = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                builder.append(line);
                String nodeType = map.getString("type");
                if(nodeType == null) {
                    navigateNode.setValue(MapNodeType.DISABLE.id);
                }else if(nodeType.equals("devp") || nodeType.equals("merge")){
                    navigateNode.setValue(MapNodeType.NORMAL_PATH.id);
                    JSONObject valueObj = JSON.parseObject(map.getString("value"));
                    List<String> directionList = valueObj.getJSONArray("direction").toJavaList(String.class);
                    navigateNode.setDirectionList(directionList);
                }else {
                    navigateNode.setValue(MapNodeType.DISABLE.id);
                }
                navigateNode.setNodeType(nodeType);
                navigateNode.setNodeValue(map.getString("value"));
                navigateNodeRow.add(navigateNode);
            }
            // 等待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();
            navigateNodeList.add(navigateNodeRow);
        }
        return null;
        return navigateNodeList;
    }
    public NavigateNode astarSearchJava(NavigateNode start, NavigateNode end) {
    public List<List<NavigateNode>> getRgvTrackMap(int lev) {
        BasMapService basMapService = SpringUtils.getBean(BasMapService.class);
        BasMap basMap = basMapService.selectOne(new EntityWrapper<BasMap>().eq("lev", lev));
        if (basMap == null) {
            throw new CoolException("地图不存在");
        }
        List<List<JSONObject>> dataList = JSON.parseObject(basMap.getData(), List.class);
        List<List<NavigateNode>> navigateNodeList = new ArrayList<>();
        for (int i = 0; i < dataList.size(); i++) {
            List<JSONObject> row = dataList.get(i);
            List<NavigateNode> navigateNodeRow = new ArrayList<>();
            for (int j = 0; j < row.size(); j++) {
                JSONObject map = row.get(j);
                NavigateNode navigateNode = new NavigateNode(i, j);
                String nodeType = map.getString("type");
                if(nodeType == null) {
                    navigateNode.setValue(MapNodeType.DISABLE.id);
                }else if(nodeType.equals("rgv")){
                    navigateNode.setValue(MapNodeType.NORMAL_PATH.id);
                    JSONObject valueObj = JSON.parseObject(map.getString("value"));
                    //RGV暂不控制行走方向,默认上下左右都可走
                    List<String> directionList = new ArrayList<>();
                    directionList.add("top");
                    directionList.add("bottom");
                    directionList.add("left");
                    directionList.add("right");
                    navigateNode.setDirectionList(directionList);
                }else {
                    navigateNode.setValue(MapNodeType.DISABLE.id);
                }
                navigateNode.setNodeType(nodeType);
                navigateNode.setNodeValue(map.getString("value"));
                navigateNodeRow.add(navigateNode);
            }
            navigateNodeList.add(navigateNodeRow);
        }
        return navigateNodeList;
    }
    public NavigateNode astarSearchJava(List<List<NavigateNode>> map, NavigateNode start, NavigateNode end) {
        // 清理上次搜索的状态,防止复用实例导致记录残留
        this.Open.clear();
        this.Close.clear();
        this.bestGMap.clear();
        //把第一个开始的结点加入到Open表中
        this.Open.add(start);
        //主循环
@@ -117,9 +127,13 @@
            //将这个结点加入到Close表中
            Close.add(current_node);
            //对当前结点进行扩展,得到一个四周结点的数组
            ArrayList<NavigateNode> neighbour_node = extend_current_node(current_node);
            ArrayList<NavigateNode> neighbour_node = extend_current_node(map, current_node);
            //对这个结点遍历,看是否有目标结点出现
            for (NavigateNode node : neighbour_node) {
                // 已在关闭列表中的节点不再处理,避免父链反复改写形成环
                if (Close.contains(node)) {
                    continue;
                }
                // G + H + E (对启发函数增加去拐点方案calcNodeExtraCost)
                int gCost = calcNodeExtraCost(current_node, node, end);
@@ -131,7 +145,8 @@
                String key = node.getX() + "_" + node.getY();
                Integer recordedG = bestGMap.get(key);
                if (recordedG == null || node.getG() <= recordedG) {
                // 仅当找到更小的代价时才更新与入Open,避免等价代价反复入队导致父链抖动
                if (recordedG == null || node.getG() < recordedG) {
                    bestGMap.put(key, node.getG());
                    Open.add(node);
@@ -142,106 +157,41 @@
        return null;
    }
    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();
            }
        }
    public ArrayList<NavigateNode> extend_current_node(List<List<NavigateNode>> map, NavigateNode current_node) {
        //获取当前结点的x, y
        int x = current_node.getX();
        int y = current_node.getY();
        //如果当前结点的邻结点合法,就加入到neighbour_node
        ArrayList<NavigateNode> neighbour_node = new ArrayList<NavigateNode>();
//        if (map[x][y] == 0 || map[x][y] == 3) {
//            //只有子轨和母轨才能进行左右移动
//            if (is_valid(x, y + 1))
//            {
//                Node node = new Node(x, y + 1);
//                neighbour_node.add(node);
//            }
//            if (is_valid(x, y - 1))
//            {
//                Node node = new Node(x, y - 1);
//                neighbour_node.add(node);
//            }
//        }
//
//        if (map[x][y] == 3) {
//            //只有母轨才能进行上下移动
//            if (is_valid(x + 1, y))
//            {
//                Node node = new Node(x + 1, y);
//                neighbour_node.add(node);
//            }
//            if (is_valid(x - 1, y))
//            {
//                Node node = new Node(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);
                    neighbour_node.add(node);
                }
                if (is_valid(x - 1, y))
                {
                    NavigateNode node = new NavigateNode(x -1, y);
                    neighbour_node.add(node);
                }
            }
        if(current_node.getValue() == MapNodeType.DISABLE.id) {
            return neighbour_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);
                    neighbour_node.add(node);
                }
                if (is_valid(x, y - 1))
                {
                    NavigateNode node = new NavigateNode(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);
                    neighbour_node.add(node);
                }
                if (is_valid(x, y - 1))
                {
                    NavigateNode node = new NavigateNode(x, y - 1);
                    neighbour_node.add(node);
                }
            }
        List<String> directionList = current_node.getDirectionList();
        if(directionList == null || directionList.size() == 0) {
            return neighbour_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);
        for(String direction : directionList) {
            if(direction.equals("top")) {
                NavigateNode node = getValidNavigateNode(map, x - 1, y);
                if(node != null) {
                    neighbour_node.add(node);
                }
                if (is_valid(x - 1, y))
                {
                    NavigateNode node = new NavigateNode(x -1, y);
            }else if(direction.equals("bottom")) {
                NavigateNode node = getValidNavigateNode(map, x + 1, y);
                if(node != null) {
                    neighbour_node.add(node);
                }
            }else if(direction.equals("left")) {
                NavigateNode node = getValidNavigateNode(map, x, y - 1);
                if(node != null) {
                    neighbour_node.add(node);
                }
            }else if(direction.equals("right")) {
                NavigateNode node = getValidNavigateNode(map, x, y + 1);
                if(node != null) {
                    neighbour_node.add(node);
                }
            }
@@ -250,22 +200,48 @@
        return neighbour_node;
    }
    public boolean is_valid(int x, int y) {
        if (x < 0 || x >= this.map.length
                || y < 0 || y >= this.map[0].length) {
            return false;
        }
        // 如果结点的位置小于0,则不合法
        if (map[x][y] < 0) {
            return false;
    public NavigateNode getValidNavigateNode(List<List<NavigateNode>> map, int x, int y) {
        if (x < 0 || x >= map.size()
                || y < 0 || y >= map.get(0).size()) {
            return null;
        }
        if (map[x][y] == MapNodeType.CAR.id) {//节点是小车
            return false;
        NavigateNode node = map.get(x).get(y);
        if(node.getValue() == MapNodeType.DISABLE.id) {
            return null;
        }
        //以上情况都没有则合法
        return true;
        return node;
    }
    public NavigateNode findStationNavigateNode(List<List<NavigateNode>> map, int stationId) {
        for(int x = 0; x < map.size(); x++) {
            for(int y = 0; y < map.get(0).size(); y++) {
                NavigateNode node = map.get(x).get(y);
                if("devp".equals(node.getNodeType())) {
                    JSONObject valueObj = JSON.parseObject(node.getNodeValue());
                    if(valueObj.getInteger("stationId") == stationId) {
                        return node;
                    }
                }
            }
        }
        return null;
    }
    public NavigateNode findTrackSiteNoNavigateNode(List<List<NavigateNode>> map, int trackSiteNo) {
        for(int x = 0; x < map.size(); x++) {
            for(int y = 0; y < map.get(0).size(); y++) {
                NavigateNode node = map.get(x).get(y);
                if("rgv".equals(node.getNodeType())) {
                    JSONObject valueObj = JSON.parseObject(node.getNodeValue());
                    if(valueObj.getInteger("trackSiteNo") == trackSiteNo) {
                        return node;
                    }
                }
            }
        }
        return null;
    }
    //------------------A*启发函数------------------//
@@ -299,4 +275,4 @@
    //------------------A*启发函数-end------------------//
}
}