| | |
| | | package com.zy.acs.manager.core.service; |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.zy.acs.common.constant.RedisConstant; |
| | | import com.zy.acs.common.utils.RedisSupport; |
| | | import com.zy.acs.common.utils.Utils; |
| | | import com.zy.acs.framework.common.Cools; |
| | | import com.zy.acs.manager.common.utils.MapDataUtils; |
| | | import com.zy.acs.manager.core.constant.MapDataConstant; |
| | | import com.zy.acs.manager.core.domain.Lane; |
| | | import com.zy.acs.manager.core.service.astart.*; |
| | | 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.Code; |
| | | import com.zy.acs.manager.manager.entity.Jam; |
| | | import com.zy.acs.manager.manager.entity.Route; |
| | | import com.zy.acs.manager.manager.entity.Segment; |
| | | import com.zy.acs.manager.manager.enums.JamStateType; |
| | | import com.zy.acs.manager.manager.service.*; |
| | |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import java.util.*; |
| | | import java.util.concurrent.CopyOnWriteArrayList; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | |
| | | |
| | | @Autowired |
| | | private CodeService codeService; |
| | | @Autowired |
| | | private RouteService routeService; |
| | | @Autowired |
| | | private MapDataDispatcher mapDataDispatcher; |
| | | @Autowired |
| | |
| | | String breakPoint = avoidPathList.stream().findFirst().orElse(null); |
| | | List<String> blackList = Utils.singletonList(sponsor); |
| | | |
| | | Double avoidDistance = MapDataUtils.getVehicleWaveSafeDistance(agvModelService.getById(agvService.selectByUuid(sponsor).getAgvModel()).getDiameter() |
| | | Double avoidDistance = MapDataUtils.getVehicleWaveSafeDistance(agvModelService.getByAgvNo(sponsor).getDiameter() |
| | | , MapDataConstant.MAX_DISTANCE_BETWEEN_ADJACENT_AGV_FACTOR); |
| | | List<String> avoidPathListWave = mapService.getWaveScopeByCodeList(lev, avoidPathList, avoidDistance); |
| | | |
| | | String[][] codeMatrix = mapDataDispatcher.getCodeMatrix(lev); |
| | | DynamicNode[][] dynamicMatrix = mapDataDispatcher.getDynamicMatrix(lev); |
| | | String[][] waveMatrix = mapDataDispatcher.getWaveMatrix(lev); |
| | | |
| | |
| | | |
| | | List<RetreatNavigateNode> enableNodes = new ArrayList<>(); |
| | | |
| | | ArrayList<RetreatNavigateNode> neighborNodes = this.getNeighborNodes(currentNode, existNodes); |
| | | List<RetreatNavigateNode> neighborNodes = this.getNeighborNodes(currentNode, existNodes, codeMatrix); |
| | | boolean pointOfTurn = neighborNodes.size() >= 2; |
| | | label: for (RetreatNavigateNode node : neighborNodes) { |
| | | if (node.getCodeData().equals(breakPoint)) { continue; } |
| | | Code code = codeService.selectByData(node.getCodeData()); |
| | | |
| | | int weight = 0; |
| | | |
| | |
| | | if (!Cools.isEmpty(blackList) && blackList.contains(otherWave)) { |
| | | continue label; |
| | | } |
| | | if (1 < mapDataDispatcher.queryCodeListFromDynamicNode(lev, otherWave).size()) { |
| | | if (mapService.isWalkingByVehicle(lev, otherWave)) { |
| | | phaseSecond = false; // there is a running way |
| | | continue label; |
| | | } else { |
| | |
| | | |
| | | // lane |
| | | if (pointOfTurn) { |
| | | 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<>(); |
| | | |
| | | for (String laneCodeData : lane.getCodes()) { |
| | | for (int[] codeMatrixIdx : laneCodeIdxList) { |
| | | // overlap with sponsor |
| | | String laneCodeData = codeMatrix[codeMatrixIdx[0]][codeMatrixIdx[1]]; |
| | | if (avoidPathList.contains(laneCodeData)) { |
| | | lanVehicleSet.add(sponsor); |
| | | } |
| | | |
| | | int[] laneCodeMatrixIdx = mapDataDispatcher.getCodeMatrixIdx(null, laneCodeData); |
| | | // scan dynamicMatrix or WaveMatrix |
| | | DynamicNode laneDynamicNode = dynamicMatrix[laneCodeMatrixIdx[0]][laneCodeMatrixIdx[1]]; |
| | | 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) { |
| | | phaseSecond = false; // there is a running way |
| | | continue; |
| | |
| | | weight += WEIGHT_CALC_FACTOR * 2; |
| | | } |
| | | } |
| | | |
| | | } |
| | | |
| | | Code code = codeService.getCacheByData(node.getCodeData()); |
| | | |
| | | // judge whether the node has already been marked as a retreat node? |
| | | // This is a very troublesome matter, if the node be repeatedly mark as a retreat node |
| | |
| | | RetreatNavigateNode currentNode = openQueue.poll(); |
| | | List<RetreatNavigateNode> enableNodes = new ArrayList<>(); |
| | | |
| | | ArrayList<RetreatNavigateNode> neighborNodes = this.getNeighborNodes(currentNode, existNodes); |
| | | List<RetreatNavigateNode> neighborNodes = this.getNeighborNodes(currentNode, existNodes, codeMatrix); |
| | | |
| | | // 第一步:获取有效转弯点 |
| | | if (null == availablePointOfTurn) { |
| | |
| | | |
| | | for (RetreatNavigateNode node : neighborNodes) { |
| | | // lane |
| | | 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<>(); |
| | | |
| | | for (String laneCodeData : lane.getCodes()) { |
| | | 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 (!Cools.isEmpty(blackList) && blackList.contains(otherWave)) { |
| | | continue; |
| | | } |
| | | if (1 < mapDataDispatcher.queryCodeListFromDynamicNode(lev, otherWave).size()) { |
| | | if (mapService.isWalkingByVehicle(lev, otherWave)) { |
| | | |
| | | if (null != availablePointOfTurn && actualLanesOfTurn > 0) { |
| | | actualLanesOfTurn --; |
| | |
| | | } |
| | | |
| | | // 获取四周节点 |
| | | private ArrayList<RetreatNavigateNode> getNeighborNodes(RetreatNavigateNode currentNode, Set<RetreatNavigateNode> existNodes) { |
| | | private List<RetreatNavigateNode> getNeighborNodes(RetreatNavigateNode currentNode, Set<RetreatNavigateNode> existNodes, String[][] codeMatrix) { |
| | | |
| | | int x = currentNode.getX(); |
| | | int y = currentNode.getY(); |
| | | |
| | | ArrayList<RetreatNavigateNode> neighbourNodes = new ArrayList<>(); |
| | | List<RetreatNavigateNode> neighbourNodes = new CopyOnWriteArrayList<>(); |
| | | |
| | | RetreatNavigateNode rightNode = extendNeighborNodes(currentNode, new RetreatNavigateNode(x, y + 1), existNodes, null, null); |
| | | if (null != rightNode) { |
| | | neighbourNodes.add(rightNode); |
| | | } |
| | | List<RetreatNavigateNode> possibleNodes = Arrays.asList( |
| | | new RetreatNavigateNode(x, y + 1), // right |
| | | new RetreatNavigateNode(x, y - 1), // left |
| | | new RetreatNavigateNode(x - 1, y), // up |
| | | new RetreatNavigateNode(x + 1, y) // down |
| | | ); |
| | | |
| | | RetreatNavigateNode leftNode = extendNeighborNodes(currentNode, new RetreatNavigateNode(x, y - 1), existNodes, null, null); |
| | | if (null != leftNode) { |
| | | neighbourNodes.add(leftNode); |
| | | } |
| | | |
| | | RetreatNavigateNode topNode = extendNeighborNodes(currentNode, new RetreatNavigateNode(x - 1, y), existNodes, null, null); |
| | | if (null != topNode) { |
| | | neighbourNodes.add(topNode); |
| | | } |
| | | |
| | | RetreatNavigateNode bottomNode = extendNeighborNodes(currentNode, new RetreatNavigateNode(x + 1, y), existNodes, null, null); |
| | | if (null != bottomNode) { |
| | | neighbourNodes.add(bottomNode); |
| | | } |
| | | possibleNodes.parallelStream() |
| | | .map(extendNode -> extendNeighborNodes(currentNode, extendNode, existNodes, null, null)) |
| | | .filter(Objects::nonNull) |
| | | .peek(node -> node.setCodeData(codeMatrix[node.getX()][node.getY()])) |
| | | .forEach(neighbourNodes::add); |
| | | |
| | | return neighbourNodes; |
| | | } |
| | |
| | | if (mapMatrix[x][y] == MapNodeType.DISABLE.val) { |
| | | |
| | | return extendNeighborNodes(currentNode, nextNode, existNodes, dx, dy); |
| | | } |
| | | |
| | | } else { |
| | | if (existNodes.contains(nextNode)) { |
| | | return null; |
| | | } |
| | | assert mapMatrix[x][y] == MapNodeType.ENABLE.val; |
| | | |
| | | // 节点是否可用 |
| | | if (mapMatrix[x][y] != MapNodeType.ENABLE.val) { |
| | | return null; |
| | | } |
| | | if (existNodes.contains(nextNode)) { |
| | | 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; |
| | | } |
| | | // 判断通过性 |
| | | String routeCdaKey = RouteGenerator.generateRouteCdaKey(new int[]{currentNode.getX(), currentNode.getY()}, new int[]{nextNode.getX(), nextNode.getY()}); |
| | | if (!mapDataDispatcher.validRouteCdaKey(routeCdaKey)) { |
| | | return null; |
| | | } |
| | | |
| | | return nextNode; |
| | | } |
| | | |
| | | private boolean isExist(RetreatNavigateNode node, List<RetreatNavigateNode> existNodes) { |
| | | for (RetreatNavigateNode existNode : existNodes) { |
| | | if (this.isSame(node, existNode)) { |
| | | return true; |
| | | } |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | private boolean isSame(RetreatNavigateNode o1, RetreatNavigateNode o2) { |