| | |
| | | package com.zy.acs.manager.core.service.astart; |
| | | |
| | | import com.zy.acs.common.constant.RedisConstant; |
| | | import com.zy.acs.common.utils.GsonUtils; |
| | | import com.zy.acs.common.utils.RedisSupport; |
| | | import com.zy.acs.framework.common.Cools; |
| | | import com.zy.acs.manager.common.utils.MapDataUtils; |
| | |
| | | import com.zy.acs.manager.core.service.LaneService; |
| | | import com.zy.acs.manager.core.service.astart.domain.DynamicNode; |
| | | import com.zy.acs.manager.core.utils.RouteGenerator; |
| | | import com.zy.acs.manager.manager.entity.Route; |
| | | import com.zy.acs.manager.manager.entity.Segment; |
| | | import com.zy.acs.manager.manager.service.CodeService; |
| | | import com.zy.acs.manager.manager.service.JamService; |
| | | import com.zy.acs.manager.manager.service.RouteService; |
| | | import com.zy.acs.manager.system.service.ConfigService; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import java.util.*; |
| | | import java.util.concurrent.CopyOnWriteArrayList; |
| | | |
| | | /** |
| | | * Created by vincent on 6/12/2024 |
| | | */ |
| | | @Slf4j |
| | | @Service |
| | | public class AStarNavigateService { |
| | | |
| | |
| | | |
| | | public static final int WEIGHT_CALC_FACTOR = 1; |
| | | |
| | | @Autowired |
| | | private CodeService codeService; |
| | | @Autowired |
| | | private RouteService routeService; |
| | | @Autowired |
| | | private MapDataDispatcher mapDataDispatcher; |
| | | @Autowired |
| | |
| | | existNodes.add(start); |
| | | |
| | | int[][] mapMatrix = mapDataDispatcher.getMapMatrix(null, null); |
| | | String[][] codeMatrix = mapDataDispatcher.getCodeMatrix(null); |
| | | DynamicNode[][] dynamicMatrix = mapDataDispatcher.getDynamicMatrix(null); |
| | | String[][] waveMatrix = mapDataDispatcher.getWaveMatrix(null); |
| | | |
| | | while (!openQueue.isEmpty()) { |
| | | // 取优先队列顶部元素并且把这个元素从Open表中删除,取F值最小的节点 |
| | | NavigateNode currentNode = openQueue.poll(); |
| | | |
| | | ArrayList<NavigateNode> neighbourNodes = this.getNeighborNodes(currentNode, mapMatrix, existNodes); |
| | | List<NavigateNode> neighbourNodes = this.getNeighborNodes(currentNode, mapMatrix, existNodes); |
| | | for (NavigateNode node : neighbourNodes) { |
| | | node.setCodeData(codeMatrix[node.getX()][node.getY()]); |
| | | |
| | | boolean isEndNode = node.getX() == end.getX() && node.getY() == end.getY(); |
| | | |
| | | int weight = 0; |
| | |
| | | } |
| | | |
| | | // 单巷道车辆容载数量 |
| | | Lane lane = laneService.search(node.getCodeData()); |
| | | if (null != lane) { |
| | | List<int[]> laneCodeIdxList = laneService.getLaneCodeIdxList(node.getCodeData()); |
| | | if (!Cools.isEmpty(laneCodeIdxList)) { |
| | | Set<String> lanVehicleSet = new HashSet<>(); |
| | | |
| | | List<String> laneCodes = lane.getCodes(); |
| | | for (String laneCodeData : laneCodes) { |
| | | int[] laneCodeMatrixIdx = mapDataDispatcher.getCodeMatrixIdx(null, laneCodeData); |
| | | // scan dynamicMatrix or WaveMatrix |
| | | DynamicNode laneDynamicNode = dynamicMatrix[laneCodeMatrixIdx[0]][laneCodeMatrixIdx[1]]; |
| | | for (int[] codeMatrixIdx : laneCodeIdxList) { |
| | | DynamicNode laneDynamicNode = dynamicMatrix[codeMatrixIdx[0]][codeMatrixIdx[1]]; |
| | | String laneVehicle = laneDynamicNode.getVehicle(); |
| | | assert !laneVehicle.equals(DynamicNodeType.BLOCK.val); |
| | | if (!laneVehicle.equals(DynamicNodeType.ACCESS.val)) { |
| | | if (!laneVehicle.equals(agvNo)) { |
| | | lanVehicleSet.add(laneVehicle); |
| | | // redis.setObject(RedisConstant.AGV_TO_STANDBY_FLAG, laneVehicle, true, 30); |
| | | } |
| | | } |
| | | } |
| | | |
| | | if (lanVehicleSet.size() + 1 > maxAgvCountInLane) { |
| | | continue; |
| | | } |
| | | } |
| | | |
| | | |
| | | //找到目标结点就返回 |
| | | if (isEndNode) { |
| | |
| | | } |
| | | |
| | | // 获取四周节点 |
| | | private ArrayList<NavigateNode> getNeighborNodes(NavigateNode currentNode, int[][] mapMatrix, Set<NavigateNode> existNodes) { |
| | | //获取当前结点的x, y |
| | | private List<NavigateNode> getNeighborNodes(NavigateNode currentNode, int[][] mapMatrix, Set<NavigateNode> existNodes) { |
| | | int x = currentNode.getX(); |
| | | int y = currentNode.getY(); |
| | | //如果当前结点的邻结点合法,就加入到neighbour_node |
| | | ArrayList<NavigateNode> neighbourNodes = new ArrayList<>(); |
| | | |
| | | NavigateNode rightNode = extendNeighborNodes(currentNode, new NavigateNode(x, y + 1), mapMatrix, existNodes, null, null); |
| | | if (is_valid(currentNode, rightNode)) { |
| | | neighbourNodes.add(rightNode); |
| | | } |
| | | List<NavigateNode> neighbourNodes = new CopyOnWriteArrayList<>(); |
| | | |
| | | NavigateNode leftNode = extendNeighborNodes(currentNode, new NavigateNode(x, y - 1), mapMatrix, existNodes, null, null); |
| | | if (is_valid(currentNode, leftNode)) { |
| | | neighbourNodes.add(leftNode); |
| | | } |
| | | List<NavigateNode> possibleNodes = Arrays.asList( |
| | | new NavigateNode(x, y + 1), // right |
| | | new NavigateNode(x, y - 1), // left |
| | | new NavigateNode(x - 1, y), // up |
| | | new NavigateNode(x + 1, y) // down |
| | | ); |
| | | |
| | | NavigateNode topNode = extendNeighborNodes(currentNode, new NavigateNode(x - 1, y), mapMatrix, existNodes, null, null); |
| | | if (is_valid(currentNode, topNode)) { |
| | | neighbourNodes.add(topNode); |
| | | } |
| | | |
| | | NavigateNode bottomNode = extendNeighborNodes(currentNode, new NavigateNode(x + 1, y), mapMatrix, existNodes, null, null); |
| | | if (is_valid(currentNode, bottomNode)) { |
| | | neighbourNodes.add(bottomNode); |
| | | } |
| | | possibleNodes.parallelStream() |
| | | .map(extendNode -> extendNeighborNodes(currentNode, extendNode, mapMatrix, existNodes, null, null)) |
| | | .filter(Objects::nonNull) |
| | | .forEach(neighbourNodes::add); |
| | | |
| | | return neighbourNodes; |
| | | } |
| | | |
| | | private NavigateNode extendNeighborNodes(NavigateNode currentNode, NavigateNode extendNode, int[][] mapMatrix, Set<NavigateNode> existNodes, Integer dx, Integer dy) { |
| | | NavigateNode nextNode = null; |
| | | NavigateNode nextNode; |
| | | |
| | | if (null == dx || null == dy) { |
| | | dx = extendNode.getX() - currentNode.getX(); |
| | |
| | | if (mapMatrix[x][y] == MapNodeType.DISABLE.val) { |
| | | |
| | | return extendNeighborNodes(currentNode, nextNode, mapMatrix, existNodes, dx, dy); |
| | | |
| | | } else { |
| | | |
| | | if (existNodes.contains(nextNode)) { |
| | | return null; |
| | | } |
| | | |
| | | // 节点是否可用 |
| | | if (mapMatrix[x][y] != MapNodeType.ENABLE.val) { |
| | | return null; |
| | | } |
| | | |
| | | String[][] codeMatrix = mapDataDispatcher.getCodeMatrix(null); |
| | | String currentNodeCodeData = codeMatrix[currentNode.getX()][currentNode.getY()]; |
| | | String nextNodeCodeData = codeMatrix[nextNode.getX()][nextNode.getY()]; |
| | | nextNode.setCodeData(nextNodeCodeData); |
| | | |
| | | // 判断通过性 |
| | | String routeKey = RouteGenerator.generateRouteKey(currentNodeCodeData, nextNodeCodeData); |
| | | Object routeVal = redis.getMap(RedisConstant.AGV_MAP_ROUTE_HASH_FLAG, routeKey); |
| | | if (routeVal == null || !(Boolean) routeVal) { |
| | | return null; |
| | | } |
| | | |
| | | return nextNode; |
| | | } |
| | | } |
| | | |
| | | private boolean is_valid(NavigateNode currentNode, NavigateNode node) { |
| | | if (null == node) { |
| | | return false; |
| | | } |
| | | return true; |
| | | } |
| | | assert mapMatrix[x][y] == MapNodeType.ENABLE.val; |
| | | |
| | | private boolean isExist(NavigateNode node, List<NavigateNode> existNodes) { |
| | | for (NavigateNode existNode : existNodes) { |
| | | if (this.isSame(node, existNode)) { |
| | | return true; |
| | | } |
| | | if (existNodes.contains(nextNode)) { |
| | | return null; |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | private boolean isSame(NavigateNode o1, NavigateNode o2) { |
| | | if (Cools.isEmpty(o1, o2)) { |
| | | return false; |
| | | // 判断通过性 |
| | | String routeCdaKey = RouteGenerator.generateRouteCdaKey(new int[]{currentNode.getX(), currentNode.getY()}, new int[]{nextNode.getX(), nextNode.getY()}); |
| | | if (!mapDataDispatcher.validRouteCdaKey(routeCdaKey)) { |
| | | return null; |
| | | } |
| | | return o1.getX() == o2.getX() && o1.getY() == o2.getY(); |
| | | |
| | | return nextNode; |
| | | } |
| | | |
| | | //------------------A*启发函数------------------// |
| | | |
| | | //计算通过现在的结点的位置和最终结点的位置计算H值(曼哈顿法:坐标分别取差值相加) |
| | | private int calcNodeCost(NavigateNode node1, NavigateNode node2) { |
| | | // Code code1 = codeService.selectByData(node1.getCodeData()); |
| | | // Code code2 = codeService.selectByData(node2.getCodeData()); |
| | | // return (int) (Math.abs(code2.getX() - code1.getX()) + Math.abs(code2.getY() - code1.getY())); |
| | | return Math.abs(node2.getX() - node1.getX()) + Math.abs(node2.getY() - node1.getY()); |
| | | } |
| | | |