| | |
| | | package com.algo.service; |
| | | |
| | | import com.algo.config.EnvDataConfig; |
| | | import com.algo.model.*; |
| | | import com.algo.util.JsonUtils; |
| | | import com.algo.util.PathTimeCalculator; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import javax.annotation.PostConstruct; |
| | | import java.util.*; |
| | | import java.util.concurrent.*; |
| | | |
| | | /** |
| | | * 路径规划服务 |
| | | */ |
| | | @Service |
| | | public class PathPlanningService { |
| | | |
| | | /** |
| | | * 路径映射表 |
| | | */ |
| | | private Map<String, Map<String, Integer>> pathMapping; |
| | | |
| | | /** |
| | | * 环境配置 |
| | | */ |
| | | private Map<String, Object> environmentConfig; |
| | | @Autowired |
| | | private EnvDataConfig envDataConfig; |
| | | |
| | | /** |
| | | * 执行中任务提取 |
| | |
| | | * 剩余路径处理 |
| | | */ |
| | | private RemainingPathProcessor remainingPathProcessor; |
| | | |
| | | |
| | | /** |
| | | * 线程池大小 |
| | | * 路径时间计算器 |
| | | */ |
| | | private final int threadPoolSize; |
| | | private PathTimeCalculator timeCalculator; |
| | | |
| | | |
| | | /** |
| | | * 线程池 |
| | | */ |
| | | private final ExecutorService executorService; |
| | | private final ExecutorService executorService = Executors.newFixedThreadPool(Math.max(4, Runtime.getRuntime().availableProcessors())); |
| | | |
| | | /** |
| | | * CTU批处理大小 |
| | | */ |
| | | private final int batchSize = 10; |
| | | |
| | | /** |
| | | * 构造函数 |
| | | * |
| | | * @param pathMapping 路径映射表 |
| | | * @param environmentConfig 环境配置 |
| | | * @param taskDataList 任务数据列表 |
| | | */ |
| | | public PathPlanningService(Map<String, Map<String, Integer>> pathMapping, |
| | | Map<String, Object> environmentConfig, |
| | | List<TaskData> taskDataList) { |
| | | this.pathMapping = pathMapping; |
| | | this.environmentConfig = environmentConfig; |
| | | |
| | | this.threadPoolSize = Math.max(4, Runtime.getRuntime().availableProcessors()); |
| | | this.executorService = Executors.newFixedThreadPool(threadPoolSize); |
| | | |
| | | // 初始化 |
| | | initializeComponents(taskDataList); |
| | | |
| | | System.out.println("路径规划服务初始化完成(线程池大小: " + threadPoolSize + ")"); |
| | | } |
| | | |
| | | /** |
| | | * 初始化各个组件 |
| | | * |
| | | * @param taskDataList 任务数据列表 |
| | | */ |
| | | private void initializeComponents(List<TaskData> taskDataList) { |
| | | // 初始化任务提取器 |
| | | this.taskExtractor = new ExecutingTaskExtractor(pathMapping, taskDataList); |
| | | @PostConstruct |
| | | public void initializeComponents() { |
| | | |
| | | // 初始化路径规划器 |
| | | this.pathPlanner = new AStarPathPlanner(pathMapping); |
| | | this.pathPlanner = new AStarPathPlanner(envDataConfig.getPathMapping()); |
| | | |
| | | // 初始化碰撞检测器 |
| | | this.collisionDetector = new CollisionDetector(pathMapping); |
| | | this.collisionDetector = new CollisionDetector(envDataConfig.getPathMapping()); |
| | | |
| | | // 初始化碰撞解决器 |
| | | this.collisionResolver = new CollisionResolver(collisionDetector); |
| | | |
| | | // 初始化剩余路径处理器 |
| | | this.remainingPathProcessor = new RemainingPathProcessor(pathMapping); |
| | | this.remainingPathProcessor = new RemainingPathProcessor(envDataConfig.getPathMapping()); |
| | | |
| | | // 初始化时间计算器 |
| | | Map<String, double[]> realCoordinateMapping = JsonUtils.loadRealCoordinateMapping("man_code.json"); |
| | | this.timeCalculator = new PathTimeCalculator(envDataConfig.getPathMapping(), realCoordinateMapping); |
| | | |
| | | // 为碰撞解决器设置时间计算器 |
| | | this.collisionResolver.setTimeCalculator(timeCalculator); |
| | | } |
| | | |
| | | /** |
| | |
| | | * @param constraints 路径约束条件 |
| | | * @return 路径规划结果 |
| | | */ |
| | | public PathPlanningResult planAllAgvPaths(List<AGVStatus> agvStatusList, |
| | | public PathPlanningResult planAllAgvPaths(List<TaskData> taskList, List<AGVStatus> agvStatusList, |
| | | boolean includeIdleAgv, |
| | | List<double[]> constraints) { |
| | | // 初始化任务提取器 |
| | | this.taskExtractor = new ExecutingTaskExtractor(envDataConfig.getPathMapping(), taskList); |
| | | |
| | | long startTime = System.currentTimeMillis(); |
| | | |
| | | System.out.println("开始为 " + agvStatusList.size() + " 个CTU规划"); |
| | |
| | | PlannedPath remainingPath = agv.getRemainingPath(); |
| | | List<PathCode> remainingCodes = new ArrayList<>(); |
| | | |
| | | // 从当前位置开始,获取剩余路径 |
| | | // 获取剩余路径 |
| | | List<PathCode> originalCodes = remainingPath.getCodeList(); |
| | | for (int i = agv.getCurrentPathIndex(); i < originalCodes.size(); i++) { |
| | | remainingCodes.add(originalCodes.get(i)); |
| | | PathCode originalCode = originalCodes.get(i); |
| | | PathCode newCode = new PathCode(originalCode.getCode(), originalCode.getDirection()); |
| | | newCode.setActionType(originalCode.getActionType()); |
| | | newCode.setTaskId(originalCode.getTaskId()); |
| | | newCode.setPosType(originalCode.getPosType()); |
| | | newCode.setLev(originalCode.getLev()); |
| | | newCode.setTargetPoint(originalCode.isTargetPoint()); |
| | | remainingCodes.add(newCode); |
| | | } |
| | | |
| | | // 创建新的路径对象 |
| | | PlannedPath processedPath = new PlannedPath(); |
| | | processedPath.setAgvId(agv.getAgvId()); |
| | | processedPath.setCodeList(remainingCodes); |
| | | processedPath.setSegId(agv.getAgvId() + "_REMAINING_" + System.currentTimeMillis()); |
| | | |
| | | if (timeCalculator != null && !remainingCodes.isEmpty()) { |
| | | // 获取AGV的下一个路径点到达时间作为起始时间 |
| | | long startTime = agv.getNextPointArrivalTime(); |
| | | |
| | | CTUPhysicalConfig config = agv.getPhysicalConfig(); |
| | | |
| | | // 估算当前速度(AGV移动中为正常速度;静止为0) |
| | | double initialSpeed = agv.hasRemainingPath() && agv.getRemainingPathLength() > 0 |
| | | ? config.getNormalSpeed() : 0.0; |
| | | |
| | | // 计算时间信息 |
| | | // arrivalTime, departureTime, cumulativeTime |
| | | timeCalculator.calculatePathTiming( |
| | | processedPath, |
| | | startTime, |
| | | config, |
| | | initialSpeed |
| | | ); |
| | | } else { |
| | | System.out.println(" 未能为剩余路径设置时间信息 - AGV: " + agv.getAgvId()); |
| | | } |
| | | |
| | | return processedPath; |
| | | } |
| | |
| | | List<double[]> constraints, |
| | | Map<String, String> occupancyMap, |
| | | AGVStatus agvStatus) { |
| | | // 首先尝试基本路径规划 |
| | | // 尝试基本路径规划 |
| | | PlannedPath basicPath = pathPlanner.planPath(startPos, endPos, constraints); |
| | | if (basicPath == null) { |
| | | return null; |
| | |
| | | basicPath, occupancyMap, agvStatus.getPhysicalConfig() |
| | | ); |
| | | |
| | | // 设置路径的时间信息 |
| | | enhancePathWithTimeInfo(basicPath, safeStartTime, agvStatus.getPhysicalConfig()); |
| | | // 使用统一的时间计算器设置精确的时间信息 |
| | | timeCalculator.calculatePathTiming(basicPath, safeStartTime, agvStatus.getPhysicalConfig(), 0.0); |
| | | |
| | | return basicPath; |
| | | } |
| | |
| | | |
| | | // 如果有方向变化,增加转向时间 |
| | | PathCode nextCode = codeList.get(i + 1); |
| | | if (!pathCode.getDirection().equals(nextCode.getDirection())) { |
| | | double turnTime = config.getTurnTime(pathCode.getDirection(), nextCode.getDirection()); |
| | | currentTime += (long) (turnTime * 1000); |
| | | // 添加null检查 |
| | | if (pathCode.getDirection() != null && nextCode.getDirection() != null) { |
| | | if (!pathCode.getDirection().equals(nextCode.getDirection())) { |
| | | double turnTime = config.getTurnTime(pathCode.getDirection(), nextCode.getDirection()); |
| | | currentTime += (long) (turnTime * 1000); |
| | | } |
| | | } |
| | | } |
| | | } |
| | |
| | | long currentTime = System.currentTimeMillis() / 1000; // 转换为秒 |
| | | |
| | | for (PathCode pathCode : codeList) { |
| | | int[] coord = JsonUtils.getCoordinate(pathCode.getCode(), pathMapping); |
| | | int[] coord = JsonUtils.getCoordinate(pathCode.getCode(), envDataConfig.getPathMapping()); |
| | | if (coord != null) { |
| | | String spaceTimeKey = coord[0] + "," + coord[1] + "," + currentTime; |
| | | occupancyMap.put(spaceTimeKey, agvStatus.getAgvId()); |