From 5a9828d9466e0ae7d57c255b6c5f4dfe4999ddcc Mon Sep 17 00:00:00 2001 From: luxiaotao1123 <t1341870251@163.com> Date: 星期一, 28 十月 2024 15:29:41 +0800 Subject: [PATCH] # --- zy-acs-manager/src/main/java/com/zy/acs/manager/core/service/AllocateService.java | 112 +++++++++++++++++++++++++++++++++++++++++++++++++------ 1 files changed, 99 insertions(+), 13 deletions(-) diff --git a/zy-acs-manager/src/main/java/com/zy/acs/manager/core/service/AllocateService.java b/zy-acs-manager/src/main/java/com/zy/acs/manager/core/service/AllocateService.java index 5e7cbd4..8401b2d 100644 --- a/zy-acs-manager/src/main/java/com/zy/acs/manager/core/service/AllocateService.java +++ b/zy-acs-manager/src/main/java/com/zy/acs/manager/core/service/AllocateService.java @@ -17,6 +17,7 @@ import org.springframework.stereotype.Service; import java.util.*; +import java.util.stream.Collectors; /** * Created by vincent on 8/12/2024 @@ -67,12 +68,22 @@ return result; } + /** + * 1. 鍒ゆ柇task鐨勮捣濮嬬偣鍜岀洰鐨勭偣鎵�鍦ㄧ殑宸烽亾鎵胯浇浠诲姟鏁伴噺锛� + * 濡傛灉鏁伴噺宸茬粡杈惧埌璐熻浇锛屽垯鍒ゆ柇璐熻浇浠诲姟鐨凙GV鏄惁杩樻湁绌鸿儗绡擄紝濡傛灉鏈夊垯浼樺厛娲惧彂缁欏畠锛� + * 濡傛灉娌℃湁浜嗭紝閭d箞鍒欓樆濉炰换鍔★紝鐩村埌璇ュ贩閬撻噴鏀� + * 2. 杞绌洪棽灏忚溅锛岀洰鏍囨槸璁╂瘡鍙板皬杞﹂兘鍔ㄨ捣鏉� + * 鍒ゆ柇閫昏緫锛氳儗绡撴暟閲忔渶灏戠殑灏忚溅杞鐨勬椂鍊欙紝浼樺厛绾ф渶楂� + * + * it can break the limit of the number of agv backpack + */ public synchronized Agv execute(Task task) { List<Agv> availableAgvList = getAvailableAgv(); if (Cools.isEmpty(availableAgvList)) { log.warn("No available agv to assign the task[{}]", task.getSeqNum()); return null; } + List<String> availableAgvNos = availableAgvList.stream().map(Agv::getUuid).distinct().collect(Collectors.toList()); Integer maxAgvCountInLane = configService.getVal("maxAgvCountInLane", Integer.class); @@ -80,26 +91,101 @@ Lane originLane = taskService.checkoutOriginLane(task); Lane destinationLane = taskService.checkoutDestinationLane(task); + // allocate about origin - taskService.findTasksByLaneHash(originLane.getHashCode()); + List<String> availableAgvNosByOriLane = new ArrayList<>(availableAgvNos); + if (null != originLane) { + List<String> agvNosByOriLane = findAgvNosByLane(originLane); // the agv list that had tasks in this lane + if (!Cools.isEmpty(agvNosByOriLane) && agvNosByOriLane.size() >= maxAgvCountInLane) { + + availableAgvNosByOriLane = Cools.getIntersection(agvNosByOriLane, availableAgvNos); + } + } + // valid backpack limit + availableAgvNosByOriLane = this.validBackpackLimit(availableAgvNosByOriLane); + // allocate about destination - taskService.findTasksByLaneHash(destinationLane.getHashCode()); + List<String> availableAgvNosByDestLane = new ArrayList<>(availableAgvNos); + if (null != destinationLane) { + List<String> agvNosByDestLane = findAgvNosByLane(destinationLane); + if (!Cools.isEmpty(agvNosByDestLane) && agvNosByDestLane.size() >= maxAgvCountInLane) { - /** - * 1. 鍒ゆ柇task鐨勮捣濮嬬偣鍜岀洰鐨勭偣鎵�鍦ㄧ殑宸烽亾鎵胯浇浠诲姟鏁伴噺锛� - * 濡傛灉鏁伴噺宸茬粡杈惧埌璐熻浇锛屽垯鍒ゆ柇璐熻浇浠诲姟鐨凙GV鏄惁杩樻湁绌鸿儗绡擄紝濡傛灉鏈夊垯浼樺厛娲惧彂缁欏畠锛� - * 濡傛灉娌℃湁浜嗭紝閭d箞鍒欓樆濉炰换鍔★紝鐩村埌璇ュ贩閬撻噴鏀� - * - * 2. 杞绌洪棽灏忚溅锛岀洰鏍囨槸璁╂瘡鍙板皬杞﹂兘鍔ㄨ捣鏉� - * 鍒ゆ柇閫昏緫锛氳儗绡撴暟閲忔渶灏戠殑灏忚溅杞鐨勬椂鍊欙紝浼樺厛绾ф渶楂� - */ + availableAgvNosByDestLane = Cools.getIntersection(agvNosByDestLane, availableAgvNos); + } + } + availableAgvNosByDestLane = this.validBackpackLimit(availableAgvNosByDestLane); + + // valid + if (Cools.isEmpty(availableAgvNosByOriLane)) { + log.warn("No available agv to assign the task origin[{}]", task.getSeqNum()); + return null; + } + if (Cools.isEmpty(availableAgvNosByDestLane)) { + log.warn("No available agv to assign the task destination[{}]", task.getSeqNum()); + return null; + } + + List<String> actualAvailableAgvNos = Cools.getIntersection(availableAgvNosByOriLane, availableAgvNosByDestLane); + if (Cools.isEmpty(actualAvailableAgvNos)) { + log.warn("No available agv to assign the task[{}]", task.getSeqNum()); + return null; + } + + // choose min number of running task + actualAvailableAgvNos.sort(new Comparator<String>() { + @Override + public int compare(String agvNo1, String agvNo2) { + return calcAllocateWeight(agvNo2, task) - calcAllocateWeight(agvNo1, task); + } + }); - task.setOriLaneHash(originLane.getHashCode()); - task.setDestLaneHash(destinationLane.getHashCode()); + if (null != originLane) { + task.setOriLaneHash(originLane.getHashCode()); + } + if (null != destinationLane) { + task.setDestLaneHash(destinationLane.getHashCode()); + } - return null; + return agvService.selectByUuid(actualAvailableAgvNos.stream().findFirst().orElse(null)); + } + + private List<String> findAgvNosByLane(Lane lane) { + if (null == lane) { + return null; + } + List<Task> taskList = taskService.findRunningTasksByLaneHash(lane.getHashCode()); + if (Cools.isEmpty(taskList)) { + return null; + } + return taskList.stream().map(task -> { + return agvService.getById(task.getAgvId()).getUuid(); + }).distinct().collect(Collectors.toList()); + } + + // calculate wight + private int calcAllocateWeight(String agvNo, Task task) { + int weight = 0; + Agv agv = agvService.selectByUuid(agvNo); + List<Task> runningTasks = taskService.findRunningTasksByAgv(agv.getId()); + if (!Cools.isEmpty(runningTasks)) { + weight = weight + runningTasks.size() * 10; + } + + return weight; + } + + private List<String> validBackpackLimit(List<String> agvNoList) { + if (Cools.isEmpty(agvNoList)) { + return new ArrayList<>(); + } + return agvNoList.stream().filter(agvNo -> { + Agv agv = agvService.selectByUuid(agvNo); + AgvModel agvModel = agvModelService.getById(agv.getAgvModel()); + List<Task> runningTasks = taskService.findRunningTasksByAgv(agv.getId()); + return runningTasks.size() < agvModel.getBackpack(); + }).collect(Collectors.toList()); } public synchronized Agv execute(Task task, Map<String, List<Long>> taskAllot, List<Long> taskIds) { -- Gitblit v1.9.1