#
Junjie
1 天以前 96f987b030f4c961a985f07079ba12abd865fdb2
#
2个文件已修改
286 ■■■■■ 已修改文件
src/main/java/com/zy/common/utils/NavigateSolution.java 111 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/zy/common/utils/NavigateUtils.java 175 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/zy/common/utils/NavigateSolution.java
@@ -10,6 +10,8 @@
import com.zy.common.model.NavigateNode;
import com.zy.core.enums.MapNodeType;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.BiFunction;
/**
 * A*算法实现类
@@ -157,6 +159,115 @@
        return null;
    }
    public List<List<NavigateNode>> allSimplePaths(
            List<List<NavigateNode>> map,
            NavigateNode start,
            NavigateNode end,
            int maxDepth,    // 最大步数(边数). 建议:50/100/200;<=0 表示不限制(不建议)
            int maxPaths,    // 最大返回条数. 建议:100/500/2000;<=0 表示不限制(不建议)
            int maxCost      // 最大总代价(含拐点惩罚). <=0 表示不限制
    ) {
        List<List<NavigateNode>> results = new ArrayList<>();
        if (map == null || map.isEmpty() || map.get(0).isEmpty()) return results;
        if (start == null || end == null) return results;
        if (start.getValue() == MapNodeType.DISABLE.id || end.getValue() == MapNodeType.DISABLE.id) return results;
        // visited 用坐标 key,避免 NavigateNode equals/hashCode 不可靠导致重复判断失效
        Set<String> visited = new HashSet<>(map.size() * map.get(0).size() * 2);
        LinkedList<NavigateNode> path = new LinkedList<>();
        String startKey = keyOf(start);
        visited.add(startKey);
        path.add(start);
        AtomicInteger pathCount = new AtomicInteger(0);
        dfsAllSimplePaths(map, start, end,
                visited, path, results,
                0,  // depth
                0,  // cost
                maxDepth, maxPaths, maxCost,
                pathCount
        );
        return results;
    }
    /**
     * DFS + 回溯:枚举所有简单路径(路径中不允许重复节点)
     */
    private void dfsAllSimplePaths(
            List<List<NavigateNode>> map,
            NavigateNode current,
            NavigateNode end,
            Set<String> visited,
            LinkedList<NavigateNode> path,
            List<List<NavigateNode>> results,
            int depth,     // 当前步数(边数)
            int cost,      // 当前总代价(你可以认为是 g)
            int maxDepth,
            int maxPaths,
            int maxCost,
            AtomicInteger pathCount
    ) {
        // 防爆:条数限制
        if (maxPaths > 0 && pathCount.get() >= maxPaths) return;
        // 到达终点:收集路径
        if (current.getX() == end.getX() && current.getY() == end.getY()) {
            results.add(new ArrayList<>(path));
            pathCount.incrementAndGet();
            return;
        }
        // 防爆:深度限制(depth 表示已走的边数)
        if (maxDepth > 0 && depth >= maxDepth) return;
        // 扩展邻居(严格复用你自己的可行走方向规则)
        ArrayList<NavigateNode> neighbors = extend_current_node(map, current);
        if (neighbors == null || neighbors.isEmpty()) return;
        for (NavigateNode next : neighbors) {
            // 防爆:条数限制
            if (maxPaths > 0 && pathCount.get() >= maxPaths) return;
            if (next == null) continue;
            if (next.getValue() == MapNodeType.DISABLE.id) continue;
            String nk = keyOf(next);
            // 简单路径:不允许重复节点
            if (visited.contains(nk)) continue;
            // 你的代价规则:每步 1 + 拐点惩罚
            int stepCost = 1 + calcNodeExtraCost(current, next, end);
            int newCost = cost + stepCost;
            // 防爆:总代价限制
            if (maxCost > 0 && newCost > maxCost) continue;
            // 进入
            visited.add(nk);
            path.addLast(next);
            dfsAllSimplePaths(map, next, end,
                    visited, path, results,
                    depth + 1,
                    newCost,
                    maxDepth, maxPaths, maxCost,
                    pathCount
            );
            // 回溯
            path.removeLast();
            visited.remove(nk);
        }
    }
    private String keyOf(NavigateNode n) {
        return n.getX() + "_" + n.getY();
    }
    public ArrayList<NavigateNode> extend_current_node(List<List<NavigateNode>> map, NavigateNode current_node) {
        //获取当前结点的x, y
        int x = current_node.getX();
src/main/java/com/zy/common/utils/NavigateUtils.java
@@ -2,16 +2,28 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import com.zy.core.News;
import org.springframework.stereotype.Component;
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.common.model.NavigateNode;
import com.zy.asrs.entity.DeviceConfig;
import com.zy.asrs.service.DeviceConfigService;
import com.zy.core.cache.SlaveConnection;
import com.zy.core.enums.SlaveType;
import com.zy.core.model.protocol.StationProtocol;
import com.zy.core.thread.StationThread;
import com.zy.system.entity.Config;
import com.zy.system.service.ConfigService;
@Component
public class NavigateUtils {
@@ -32,31 +44,19 @@
        long startTime = System.currentTimeMillis();
        News.info("[WCS Debug] 站点路径开始计算,startStationId={},endStationId={}", startStationId, endStationId);
        NavigateNode res_node = navigateSolution.astarSearchJava(stationMap, startNode, endNode);
        if (res_node == null) {
        List<List<NavigateNode>> allList = navigateSolution.allSimplePaths(stationMap, startNode, endNode, 120, 500, 300);
        if (allList.isEmpty()) {
            throw new CoolException("未找到该路径");
        }
        News.info("[WCS Debug] 站点路径计算完成,耗时:{}ms", System.currentTimeMillis() - startTime);
        ArrayList<NavigateNode> list = new ArrayList<>();
        // 使用 visited 集合防止父链出现环导致死循环,同时设置安全步数上限
        HashSet<NavigateNode> visited = new HashSet<>();
        int maxSteps = stationMap.size() * stationMap.get(0).size() + 5; // 安全上限
        int steps = 0;
        while (res_node != null && visited.add(res_node) && steps++ < maxSteps) {
            list.add(res_node);
            res_node = res_node.getFather();//迭代操作
        if (allList.size() > 1) {
            System.out.println(JSON.toJSONString(allList));
        }
        if (steps >= maxSteps) {
            throw new CoolException("路径回溯超出安全上限,疑似存在父链循环");
        }
        Collections.reverse(list);
        //将每个节点里面的fatherNode至为null(方便后续计算时父节点过多导致显示的节点太多)
        for (NavigateNode navigateNode : list) {
            //父节点设置为null,不影响计算结果,不影响后续操作。
            //此操作仅为后续排查处理提供视觉方便。
            navigateNode.setFather(null);
        }
        News.info("[WCS Debug] 站点路径权重开始分析,startStationId={},endStationId={}", startStationId, endStationId);
        List<NavigateNode> list = findStationBestPath(allList);
        News.info("[WCS Debug] 站点路径权重分析结束,startStationId={},endStationId={}", startStationId, endStationId);
        //去重
        HashSet<Integer> set = new HashSet<>();
@@ -152,4 +152,139 @@
        return liftStationList;
    }
    public synchronized List<NavigateNode> findStationBestPath(List<List<NavigateNode>> allList) {
        if (allList == null || allList.isEmpty()) {
            return new ArrayList<>();
        }
        Map<Integer, StationProtocol> statusMap = new HashMap<>();
        try {
            DeviceConfigService deviceConfigService = SpringUtils.getBean(DeviceConfigService.class);
            if (deviceConfigService != null) {
                List<DeviceConfig> devpList = deviceConfigService.selectList(new EntityWrapper<DeviceConfig>()
                        .eq("device_type", String.valueOf(SlaveType.Devp)));
                for (DeviceConfig deviceConfig : devpList) {
                    StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, deviceConfig.getDeviceNo());
                    if (stationThread == null) {
                        continue;
                    }
                    Map<Integer, StationProtocol> m = stationThread.getStatusMap();
                    if (m != null && !m.isEmpty()) {
                        statusMap.putAll(m);
                    }
                }
            }
        } catch (Exception ignore) {}
        List<List<NavigateNode>> candidates = new ArrayList<>();
        List<Integer> lens = new ArrayList<>();
        List<Integer> tasksList = new ArrayList<>();
        List<Double> congs = new ArrayList<>();
        for (List<NavigateNode> path : allList) {
            if (path == null || path.isEmpty()) {
                continue;
            }
            int len = path.size();
            int tasks = 0;
            HashSet<Integer> stationIdSet = new HashSet<>();
            for (NavigateNode node : path) {
                JSONObject value = null;
                try {
                    value = JSON.parseObject(node.getNodeValue());
                } catch (Exception ignore) {}
                if (value == null) {
                    continue;
                }
                Integer stationId = value.getInteger("stationId");
                if (stationId == null) {
                    continue;
                }
                if (!stationIdSet.add(stationId)) {
                    continue;
                }
                StationProtocol protocol = statusMap.get(stationId);
                if (protocol != null && protocol.getTaskNo() != null && protocol.getTaskNo() > 0) {
                    tasks++;
                }
            }
            double cong = len <= 0 ? 0.0 : (double) tasks / (double) len;
            candidates.add(path);
            lens.add(len);
            tasksList.add(tasks);
            congs.add(cong);
        }
        if (candidates.isEmpty()) {
            return allList.get(0);
        }
        int minLen = Integer.MAX_VALUE;
        int maxLen = Integer.MIN_VALUE;
        double minCong = Double.MAX_VALUE;
        double maxCong = -Double.MAX_VALUE;
        for (int i = 0; i < candidates.size(); i++) {
            int l = lens.get(i);
            double c = congs.get(i);
            if (l < minLen) minLen = l;
            if (l > maxLen) maxLen = l;
            if (c < minCong) minCong = c;
            if (c > maxCong) maxCong = c;
        }
        //长度权重百分比
        double lenWeightPercent = 50.0;
        //拥堵权重百分比
        double congWeightPercent = 50.0;
        try {
            ConfigService configService = SpringUtils.getBean(ConfigService.class);
            if (configService != null) {
                Config cfgLen = configService.selectOne(new EntityWrapper<Config>().eq("code", "stationPathLenWeightPercent"));
                if (cfgLen != null && cfgLen.getValue() != null) {
                    String v = cfgLen.getValue().trim();
                    if (v.endsWith("%")) v = v.substring(0, v.length() - 1);
                    try { lenWeightPercent = Double.parseDouble(v); } catch (Exception ignore) {}
                }
                Config cfgCong = configService.selectOne(new EntityWrapper<Config>().eq("code", "stationPathCongWeightPercent"));
                if (cfgCong != null && cfgCong.getValue() != null) {
                    String v = cfgCong.getValue().trim();
                    if (v.endsWith("%")) v = v.substring(0, v.length() - 1);
                    try { congWeightPercent = Double.parseDouble(v); } catch (Exception ignore) {}
                }
            }
        } catch (Exception ignore) {}
        double weightSum = lenWeightPercent + congWeightPercent;
        double lenW = weightSum <= 0 ? 0.5 : lenWeightPercent / weightSum;
        double congW = weightSum <= 0 ? 0.5 : congWeightPercent / weightSum;
        List<NavigateNode> best = null;
        double bestCost = Double.MAX_VALUE;
        int bestTasks = Integer.MAX_VALUE;
        int bestLen = Integer.MAX_VALUE;
        for (int i = 0; i < candidates.size(); i++) {
            int l = lens.get(i);
            int t = tasksList.get(i);
            double c = congs.get(i);
            //归一化
            double lenNorm = (maxLen - minLen) <= 0 ? 0.0 : (l - minLen) / (double) (maxLen - minLen);
            double congNorm = (maxCong - minCong) <= 0 ? 0.0 : (c - minCong) / (double) (maxCong - minCong);
            //获取权重
            double cost = lenNorm * lenW + congNorm * congW;
            if (cost < bestCost
                    || (cost == bestCost && t < bestTasks)
                    || (cost == bestCost && t == bestTasks && l < bestLen)) {
                best = candidates.get(i);
                bestCost = cost;
                bestTasks = t;
                bestLen = l;
            }
        }
        if (best == null) {
            return allList.get(0);
        }
        return best;
    }
}