| | |
| | | package com.algo.service; |
| | | |
| | | import com.algo.config.EnvDataConfig; |
| | | import com.algo.model.*; |
| | | import com.algo.util.JsonUtils; |
| | | 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 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()); |
| | | } |
| | | |
| | | /** |
| | |
| | | * @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规划"); |
| | |
| | | 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()); |