| | |
| | | <artifactId>junit</artifactId> |
| | | </dependency> |
| | | |
| | | |
| | | <dependency> |
| | | <groupId>org.projectlombok</groupId> |
| | | <artifactId>lombok</artifactId> |
| | | <version>1.18.20</version> |
| | | <scope>provided</scope> |
| | | </dependency> |
| | | </dependencies> |
| | | |
| | | </project> |
| | |
| | | import com.algo.service.PathPlanningService; |
| | | import com.algo.service.TaskAllocationService; |
| | | import com.algo.util.AgvTaskUtils; |
| | | import com.algo.util.JsonUtils; |
| | | import org.junit.Test; |
| | | import org.junit.runner.RunWith; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.boot.test.context.SpringBootTest; |
| | | import org.springframework.test.context.junit4.SpringRunner; |
| | | |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | @SpringBootTest |
| | | @RunWith(SpringRunner.class) |
| | | public class AlgorithmMain { |
| | | |
| | | |
| | | @Autowired |
| | | private TaskAllocationService taskAllocationService; |
| | | |
| | | @Autowired |
| | | private PathPlanningService pathPlanningService; |
| | | |
| | | |
| | | /** |
| | |
| | | */ |
| | | @Test |
| | | public void taskAllocationService() { |
| | | Map<String, Map<String, Integer>> pathMapping = JsonUtils.loadPathMapping("path_mapping.json"); |
| | | System.out.println(pathMapping); |
| | | Map<String, Object> environment = JsonUtils.loadEnvironment("environment.json"); |
| | | System.out.println(environment); |
| | | |
| | | TaskAllocationService taskAllocationService = new TaskAllocationService(pathMapping, environment); |
| | | |
| | | List<AGVStatus> agvStatusList = AgvTaskUtils.loadAgvStatus("ctu_agv_status.json"); |
| | | System.out.println(agvStatusList); |
| | |
| | | */ |
| | | @Test |
| | | public void pathPlanningService() { |
| | | Map<String, Map<String, Integer>> pathMapping = JsonUtils.loadPathMapping("path_mapping.json"); |
| | | System.out.println(pathMapping); |
| | | Map<String, Object> environment = JsonUtils.loadEnvironment("environment.json"); |
| | | System.out.println(environment); |
| | | |
| | | List<TaskData> taskList = AgvTaskUtils.loadTaskList("ctu_task_data.json"); |
| | | System.out.println(taskList); |
| | | |
| | | PathPlanningService pathPlanningService = new PathPlanningService(pathMapping, environment, taskList); |
| | | List<AGVStatus> agvStatusList = AgvTaskUtils.loadAgvStatus("ctu_agv_status.json"); |
| | | System.out.println(agvStatusList); |
| | | PathPlanningService.PathPlanningResult planningResult = pathPlanningService.planAllAgvPaths(agvStatusList, true, null); |
| | | PathPlanningService.PathPlanningResult planningResult = pathPlanningService.planAllAgvPaths(taskList, agvStatusList, true, null); |
| | | System.out.println(planningResult); |
| | | } |
| | | |
New file |
| | |
| | | //package com.algo.config; |
| | | // |
| | | //import com.algo.expose.BaseDataService; |
| | | //import com.algo.expose.impl.BaseDataServiceImpl; |
| | | //import org.springframework.context.annotation.Bean; |
| | | //import org.springframework.context.annotation.Configuration; |
| | | // |
| | | //@Configuration |
| | | //public class BaseDataConfig { |
| | | // |
| | | // |
| | | // @Bean |
| | | // public BaseDataService baseDataService() { |
| | | // return new BaseDataServiceImpl(); |
| | | // } |
| | | // |
| | | // |
| | | //} |
New file |
| | |
| | | package com.algo.config; |
| | | |
| | | import com.algo.expose.BaseDataService; |
| | | import lombok.Data; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import javax.annotation.PostConstruct; |
| | | import java.util.Map; |
| | | |
| | | @Component |
| | | @Data |
| | | public class EnvDataConfig { |
| | | |
| | | @Autowired |
| | | private BaseDataService baseDataService; |
| | | |
| | | |
| | | private Map<String, Map<String, Integer>> pathMapping; |
| | | |
| | | |
| | | private Map<String, Object> environmentConfig; |
| | | |
| | | /** |
| | | * 初始化 |
| | | */ |
| | | @PostConstruct |
| | | public void init() { |
| | | setEnvironmentConfig(baseDataService.loadEnvironment()); |
| | | setPathMapping(baseDataService.loadPathMapping()); |
| | | } |
| | | } |
| | |
| | | * |
| | | * @return |
| | | */ |
| | | PathPlanningService.PathPlanningResult planAllAgvPaths(List<AGVStatus> agvStatusList, boolean flag, List<double[]> constraints); |
| | | PathPlanningService.PathPlanningResult planAllAgvPaths(List<TaskData> taskList, List<AGVStatus> agvStatusList, boolean flag, List<double[]> constraints); |
| | | |
| | | } |
New file |
| | |
| | | package com.algo.expose; |
| | | |
| | | import com.algo.model.AGVStatus; |
| | | import com.algo.model.TaskData; |
| | | |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * 基础 |
| | | */ |
| | | public interface BaseDataService { |
| | | /** |
| | | * 加载路径映射 |
| | | * |
| | | * @return |
| | | */ |
| | | Map<String, Map<String, Integer>> loadPathMapping(); |
| | | |
| | | /** |
| | | * 加载环境参数 |
| | | * |
| | | * @return |
| | | */ |
| | | Map<String, Object> loadEnvironment(); |
| | | |
| | | /** |
| | | * 加载agv状态 |
| | | * |
| | | * @return |
| | | */ |
| | | List<AGVStatus> loadAgvStatus(); |
| | | |
| | | /** |
| | | * 加载任务列表 |
| | | * |
| | | * @return |
| | | */ |
| | | List<TaskData> loadTaskList(); |
| | | } |
| | |
| | | import com.algo.model.TaskAssignment; |
| | | import com.algo.model.TaskData; |
| | | import com.algo.service.PathPlanningService; |
| | | import com.algo.service.TaskAllocationService; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import java.util.Collections; |
| | | import java.util.List; |
| | | |
| | | @Service |
| | | public class AlgoSupportImpl implements AlgoSupport { |
| | | |
| | | |
| | | @Autowired |
| | | private TaskAllocationService taskAllocationService; |
| | | |
| | | @Autowired |
| | | private PathPlanningService pathPlanningService; |
| | | |
| | | |
| | | @Override |
| | | public List<TaskAssignment> allocateTasks(List<AGVStatus> agvStatusList, List<TaskData> taskList) { |
| | | return Collections.emptyList(); |
| | | return taskAllocationService.allocateTasks(agvStatusList, taskList); |
| | | } |
| | | |
| | | @Override |
| | | public PathPlanningService.PathPlanningResult planAllAgvPaths(List<AGVStatus> agvStatusList, boolean flag, List<double[]> constraints) { |
| | | return null; |
| | | public PathPlanningService.PathPlanningResult planAllAgvPaths(List<TaskData> taskList, List<AGVStatus> agvStatusList, boolean flag, List<double[]> constraints) { |
| | | return pathPlanningService.planAllAgvPaths(taskList, agvStatusList, true, null); |
| | | } |
| | | } |
New file |
| | |
| | | package com.algo.expose.impl; |
| | | |
| | | import com.algo.expose.BaseDataService; |
| | | import com.algo.model.AGVStatus; |
| | | import com.algo.model.TaskData; |
| | | import com.algo.util.AgvTaskUtils; |
| | | import com.algo.util.JsonUtils; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * 基础 |
| | | */ |
| | | @Service |
| | | public class BaseDataServiceImpl implements BaseDataService { |
| | | |
| | | |
| | | @Override |
| | | public Map<String, Map<String, Integer>> loadPathMapping() { |
| | | return JsonUtils.loadPathMapping("path_mapping.json"); |
| | | } |
| | | |
| | | @Override |
| | | public Map<String, Object> loadEnvironment() { |
| | | return JsonUtils.loadEnvironment("environment.json"); |
| | | } |
| | | |
| | | @Override |
| | | public List<AGVStatus> loadAgvStatus() { |
| | | return AgvTaskUtils.loadAgvStatus("ctu_agv_status.json"); |
| | | } |
| | | |
| | | @Override |
| | | public List<TaskData> loadTaskList() { |
| | | return AgvTaskUtils.loadTaskList("ctu_task_data.json"); |
| | | } |
| | | } |
| | |
| | | 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()); |
| | |
| | | package com.algo.service; |
| | | |
| | | import com.algo.config.EnvDataConfig; |
| | | import com.algo.model.AGVStatus; |
| | | import com.algo.model.BackpackData; |
| | | import com.algo.model.TaskAssignment; |
| | | import com.algo.model.TaskData; |
| | | import com.algo.util.JsonUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import java.util.*; |
| | |
| | | /** |
| | | * 任务分配服务类 |
| | | */ |
| | | @Service |
| | | public class TaskAllocationService { |
| | | |
| | | private Map<String, Map<String, Integer>> pathMapping; |
| | | private Map<String, Object> environmentConfig; |
| | | @Autowired |
| | | private EnvDataConfig envDataConfig; |
| | | |
| | | /** |
| | | * 构造函数 |
| | | * |
| | | * @param pathMapping 路径映射信息 |
| | | * @param environmentConfig 环境配置信息 |
| | | */ |
| | | public TaskAllocationService(Map<String, Map<String, Integer>> pathMapping, |
| | | Map<String, Object> environmentConfig) { |
| | | this.pathMapping = pathMapping; |
| | | this.environmentConfig = environmentConfig; |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * 执行任务分配 |
| | |
| | | System.out.println("开始任务分配,AGV数量: " + agvStatusList.size() + ", 任务数量: " + taskList.size()); |
| | | |
| | | // 输出环境信息 |
| | | System.out.println("环境信息:宽度=" + environmentConfig.get("width") + |
| | | ", 高度=" + environmentConfig.get("height") + |
| | | ", 工作站数量=" + environmentConfig.get("stationCount")); |
| | | System.out.println("环境信息:宽度=" + envDataConfig.getEnvironmentConfig().get("width") + |
| | | ", 高度=" + envDataConfig.getEnvironmentConfig().get("height") + |
| | | ", 工作站数量=" + envDataConfig.getEnvironmentConfig().get("stationCount")); |
| | | |
| | | List<TaskAssignment> assignments = new ArrayList<>(); |
| | | |
| | |
| | | System.out.println("处理起点 " + startLocation + " 的任务,数量: " + locationTasks.size()); |
| | | |
| | | // 检查是否为工作站 |
| | | if (JsonUtils.isStation(startLocation, environmentConfig)) { |
| | | Map<String, Object> stationInfo = JsonUtils.getStationInfo(startLocation, environmentConfig); |
| | | if (JsonUtils.isStation(startLocation, envDataConfig.getEnvironmentConfig())) { |
| | | Map<String, Object> stationInfo = JsonUtils.getStationInfo(startLocation, envDataConfig.getEnvironmentConfig()); |
| | | if (stationInfo != null) { |
| | | Integer capacity = (Integer) stationInfo.get("capacity"); |
| | | System.out.println("工作站 " + startLocation + " 容量: " + capacity); |
| | |
| | | AGVStatus bestAgv = null; |
| | | double minDistance = Double.MAX_VALUE; |
| | | |
| | | int[] targetCoord = JsonUtils.getCoordinate(targetLocation, pathMapping); |
| | | int[] targetCoord = JsonUtils.getCoordinate(targetLocation, envDataConfig.getPathMapping()); |
| | | if (targetCoord == null) { |
| | | System.out.println("无法获取目标位置 " + targetLocation + " 的坐标"); |
| | | return availableAgvs.isEmpty() ? null : availableAgvs.get(0); |
| | |
| | | continue; |
| | | } |
| | | |
| | | int[] agvCoord = JsonUtils.getCoordinate(agv.getPosition(), pathMapping); |
| | | int[] agvCoord = JsonUtils.getCoordinate(agv.getPosition(), envDataConfig.getPathMapping()); |
| | | if (agvCoord != null) { |
| | | double distance = JsonUtils.calculateManhattanDistance(agvCoord, targetCoord); |
| | | |
| | |
| | | org.springframework.boot.autoconfigure.EnableAutoConfiguration= |
| | | org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.algo.service.PathPlanningService |
| | | |