Junjie
2024-06-13 11da5829433e788d8f901ee5cd910d5923f0806a
zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/core/utils/NavigateSolution.java
@@ -1,7 +1,11 @@
package com.zy.asrs.wcs.core.utils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zy.asrs.framework.common.SpringUtils;
import com.zy.asrs.framework.exception.CoolException;
import com.zy.asrs.wcs.core.model.NavigateNode;
import com.zy.asrs.wcs.system.entity.Dict;
import com.zy.asrs.wcs.system.service.DictService;
import java.util.ArrayList;
import java.util.List;
@@ -13,7 +17,7 @@
 */
public class NavigateSolution {
    // -1 -> 墙壁, 1 -> 起点  2 -> 终点  3-> 母轨  4->站点
    // -1 -> 墙壁, 0 -> 货位, 1 -> 起点  2 -> 终点  3-> 母轨  4->站点
    int[][] map = {{}};
@@ -31,7 +35,7 @@
        mapData.setLev(lev);
        int[][] data = mapData.getDataFromRedis(mapType, whitePoints, shuttlePoints);
        if (data == null) {
            data = mapData.getData(mapType, whitePoints, shuttlePoints);
            throw new CoolException("地图未载入!");
        }
        this.map = data;
    }
@@ -67,28 +71,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
@@ -97,6 +92,18 @@
    public ArrayList<NavigateNode> extend_current_node(NavigateNode current_node) {
        //默认地图母轨方向x
        String mapDirection = "x";
        DictService dictService = SpringUtils.getBean(DictService.class);
        if (dictService != null) {
            Dict dict = dictService.getOne(new LambdaQueryWrapper<Dict>()
                    .eq(Dict::getFlag, "direction_map")
                    .eq(Dict::getStatus, 1));
            if (dict != null) {
                mapDirection = dict.getValue();
            }
        }
        //获取当前结点的x, y
        int x = current_node.getX();
        int y = current_node.getY();
@@ -129,31 +136,62 @@
//                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] == 3) {
                //母轨才能进行上下移动
                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 (is_valid(x - 1, y))
            {
                NavigateNode node = new NavigateNode(x -1, y);
                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, 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] == 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 (is_valid(x - 1, y))
                {
                    NavigateNode node = new NavigateNode(x -1, y);
                    neighbour_node.add(node);
                }
            }
        }
@@ -161,16 +199,15 @@
    }
    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;
        for (NavigateNode node : Exist) {
            //如果结点出现过,不合法
            if (node.getX() == x && node.getY() == y) {
                return false;
            }
            if (is_exist(new NavigateNode(x, y))) {
                return false;
            }
        NavigateNode navigateNode = new NavigateNode(x, y);
        if (is_exist(navigateNode)) {
            return false;
        }
        //以上情况都没有则合法
        return true;
@@ -198,12 +235,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;
        }
        // 普通拐点
@@ -212,7 +249,7 @@
        拿到父节点和下一节点
        通过判断父节点和下一节点的x数据和y数据都不相同时,则表明当前坐标是一个拐点
         */
        return 2;
        return 3;
    }
    //------------------A*启发函数-end------------------//