Junjie
2024-06-05 a4996f162b0ca63113f573e315e3ce4f5dce7ad3
#算法优化
2个文件已修改
62 ■■■■■ 已修改文件
zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/core/model/NavigateNode.java 6 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/core/utils/NavigateSolution.java 56 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/core/model/NavigateNode.java
@@ -3,6 +3,7 @@
import lombok.Data;
import java.io.Serializable;
import java.util.Optional;
/**
 * A*寻路算法Node节点
@@ -15,6 +16,7 @@
    private int x;//坐标x
    private int y;//坐标y
    private int z;//坐标z(高度)
    private Integer lastDistance;//步数
    private int F;//综合花费的步数
    private int G;//已经花费的步数
    private int H;//将要花费的步数
@@ -33,8 +35,8 @@
    public void init_node(NavigateNode father, NavigateNode end) {
        this.Father = father;
        if (this.Father != null) {
            //走过的步数等于父节点走过的步数加一
            this.G = father.G + 1;
            //走过的步数等于父节点走过的步数加最新步数
            this.G = father.G + Optional.ofNullable(this.lastDistance).orElse(0);
        } else { //父节点为空代表它是第一个结点
            this.G = 0;
        }
zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/core/utils/NavigateSolution.java
@@ -70,28 +70,19 @@
                if (node.getX() == end.getX() && node.getY() == end.getY()) {//找到目标结点就返回
                    //init_node操作把这个邻居结点的父节点设置为当前结点
                    //并且计算出G, F, H等值
                    node.setLastDistance(gCost);
                    node.init_node(current_node, end);
                    return node;
                }
                //(对启发函数增加去拐点方案calcNodeExtraCost)
                if (is_exist(node)) {
                    if (gCost < node.getG()) {
                        node.setFather(current_node);
                        node.setG(gCost);
                        node.setF(node.getG() + node.getH());
                    }
                }else {
                    //没出现过的结点加入到Open表中并且设置父节点
                    //进行计算对G, F, H 等值
                    node.init_node(current_node, end);
                    node.setG(gCost);
                    node.setH(calcNodeCost(node, end));
                    node.setF(node.getG() + node.getH());
                //进行计算对G, F, H 等值
                node.setLastDistance(gCost);
                node.init_node(current_node, end);
                node.setH(calcNodeCost(node, end));
                node.setF(node.getG() + node.getH());
                    Open.add(node);
                    Exist.add(node);
                }
                Open.add(node);
                Exist.add(node);
            }
        }
        //如果遍历完所有出现的结点都没有找到最终的结点,返回null
@@ -207,23 +198,18 @@
    }
    public boolean is_valid(int x, int y) {
        try {
            // 如果结点的位置小于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;
                }
            }
            //以上情况都没有则合法
            return true;
        } catch (Exception e) {
        if (x < 0 || x >= this.map.length
                || y < 0 || y >= this.map[0].length) {
            return false;
        }
        // 如果结点的位置小于0,则不合法
        if (map[x][y] < 0) return false;
        NavigateNode navigateNode = new NavigateNode(x, y);
        if (is_exist(navigateNode)) {
            return false;
        }
        //以上情况都没有则合法
        return true;
    }
    public boolean is_exist(NavigateNode node)
@@ -248,12 +234,12 @@
        // 第一个点或直线点
        if (currNode.getFather() == null || nextNode.getX() == currNode.getFather().getX()
                || nextNode.getY() == currNode.getFather().getY()) {
            return 0;
            return 1;
        }
        // 拐向终点的点
        if (nextNode.getX() == endNode.getX() || nextNode.getY() == endNode.getY()) {
            return 1;
            return 2;
        }
        // 普通拐点
@@ -262,7 +248,7 @@
        拿到父节点和下一节点
        通过判断父节点和下一节点的x数据和y数据都不相同时,则表明当前坐标是一个拐点
         */
        return 2;
        return 3;
    }
    //------------------A*启发函数-end------------------//