Commit 028384c3 authored by DESKTOP-VKRD9QF\Administration's avatar DESKTOP-VKRD9QF\Administration

Merge branch 'master' of http://39.100.78.207:1213/tongli/hyh.apsj

# Conflicts:
#	src/main/java/com/aps/service/Algorithm/MachineCalculator.java
parents a9eadadd c4aa3908
...@@ -328,48 +328,52 @@ public class GeneticAlgorithm { ...@@ -328,48 +328,52 @@ public class GeneticAlgorithm {
FileHelper.writeLogFile("解码---------------"+population.size() ); FileHelper.writeLogFile("解码---------------"+population.size() );
GeneticDecoder decoder = new GeneticDecoder(_GlobalParam, param.getBaseTime(), machines, orders, materials, machineScheduler,orderMaterials); GeneticDecoder decoder = new GeneticDecoder(_GlobalParam, param.getBaseTime(), machines, orders, materials, machineScheduler,orderMaterials);
if(1==2) {
CompletableFuture.allOf(population.stream()
.map(chromosome -> CompletableFuture.runAsync(() -> decode(decoder, chromosome, param, allOperations, globalOpList), decodeExecutor))
.toArray(CompletableFuture[]::new))
.join();
}
CompletableFuture.allOf(population.stream() population.forEach(chromosome -> {
.map(chromosome -> CompletableFuture.runAsync(() -> decode(decoder,chromosome,param,allOperations,globalOpList), decodeExecutor)) chromosome.setResult(new ArrayList<>());
.toArray(CompletableFuture[]::new))
.join(); // 假设Machine类有拷贝方法,或使用MapStruct等工具进行映射
chromosome.setMachines(ProductionDeepCopyUtil.deepCopyList(machines,Machine.class)); // 简单拷贝,实际可能需要深拷贝
chromosome.setAllOperations(allOperations); // 简单拷贝,实际可能需要深拷贝
chromosome.setGlobalOpList(globalOpList); // 简单拷贝,实际可能需要深拷贝
//chromosome.setObjectiveWeights(_objectiveWeights);
chromosome.setBaseTime(param.getBaseTime());
// chromosome.setInitMachines(ProductionDeepCopyUtil.deepCopyList(machines,Machine.class)); // 简单拷贝,实际可能需要深拷贝
// _sceneService.saveChromosomeToFile(chromosome, "12345679");
});
if(1==2) {
if (population != null && population.size() > 0) { if (population != null && population.size() > 0) {
population.parallelStream().forEach(chromosome -> { population.parallelStream().forEach(chromosome -> {
chromosome.setResult(new ArrayList<>()); decode(decoder, chromosome, param, allOperations, globalOpList);
// 假设Machine类有拷贝方法,或使用MapStruct等工具进行映射
// chromosome.setInitMachines(ProductionDeepCopyUtil.deepCopyList(machines)); // 简单拷贝,实际可能需要深拷贝
chromosome.setMachines(ProductionDeepCopyUtil.deepCopyList(machines,Machine.class)); // 简单拷贝,实际可能需要深拷贝
chromosome.setAllOperations(allOperations); // 简单拷贝,实际可能需要深拷贝
chromosome.setGlobalOpList(globalOpList); // 简单拷贝,实际可能需要深拷贝
//chromosome.setObjectiveWeights(_objectiveWeights);
chromosome.setBaseTime(param.getBaseTime());
// _sceneService.saveChromosomeToFile(chromosome, "12345679");
decoder.decodeChromosomeWithCache(chromosome);
if (chromosome.getFitness() == 0) {
chromosome.setFitness(_fitnessCalculator.calculateFitness(chromosome, _objectiveWeights));
}
}); });
} }
}
} }
private void decode(GeneticDecoder decoder,Chromosome chromosome,ScheduleParams param, List<Entry> allOperations,List<GlobalOperationInfo> globalOpList) { private void decode(GeneticDecoder decoder,Chromosome chromosome,ScheduleParams param, List<Entry> allOperations,List<GlobalOperationInfo> globalOpList) {
chromosome.setResult(new ArrayList<>()); // chromosome.setResult(new ArrayList<>());
//
// 假设Machine类有拷贝方法,或使用MapStruct等工具进行映射 // // 假设Machine类有拷贝方法,或使用MapStruct等工具进行映射
// chromosome.setInitMachines(ProductionDeepCopyUtil.deepCopyList(machines)); // 简单拷贝,实际可能需要深拷贝 // chromosome.setMachines(ProductionDeepCopyUtil.deepCopyList(machines,Machine.class)); // 简单拷贝,实际可能需要深拷贝
chromosome.setMachines(ProductionDeepCopyUtil.deepCopyList(machines,Machine.class)); // 简单拷贝,实际可能需要深拷贝 //
chromosome.setAllOperations(allOperations); // 简单拷贝,实际可能需要深拷贝 // chromosome.setAllOperations(allOperations); // 简单拷贝,实际可能需要深拷贝
chromosome.setGlobalOpList(globalOpList); // 简单拷贝,实际可能需要深拷贝 // chromosome.setGlobalOpList(globalOpList); // 简单拷贝,实际可能需要深拷贝
//chromosome.setObjectiveWeights(_objectiveWeights); // //chromosome.setObjectiveWeights(_objectiveWeights);
chromosome.setBaseTime(param.getBaseTime()); // chromosome.setBaseTime(param.getBaseTime());
// _sceneService.saveChromosomeToFile(chromosome, "12345679"); // // chromosome.setInitMachines(ProductionDeepCopyUtil.deepCopyList(machines,Machine.class)); // 简单拷贝,实际可能需要深拷贝
// // _sceneService.saveChromosomeToFile(chromosome, "12345679");
decoder.decodeChromosomeWithCache(chromosome); decoder.decodeChromosomeWithCache(chromosome);
if (chromosome.getFitness() == 0) { if (chromosome.getFitness() == 0) {
......
...@@ -65,7 +65,7 @@ public class MachineCalculator { ...@@ -65,7 +65,7 @@ public class MachineCalculator {
Machine machine, int processingTime, LocalDateTime currentTime, Machine machine, int processingTime, LocalDateTime currentTime,
String prevtime, List<GAScheduleResult> existingTasks,double oneTime,double quantity, boolean checkprevtime, boolean islockMachineTime String prevtime, List<GAScheduleResult> existingTasks,double oneTime,double quantity, boolean checkprevtime, boolean islockMachineTime
,boolean isInterrupt) { ,boolean isInterrupt) {
List<GAScheduleResult> machineTasks = new ArrayList<>(existingTasks).stream() List<GAScheduleResult> machineTasks =new ArrayList<>(existingTasks).stream()
.filter(t -> t.getMachineId() == machine.getId()) .filter(t -> t.getMachineId() == machine.getId())
.sorted(Comparator.comparingInt(GAScheduleResult::getStartTime)) .sorted(Comparator.comparingInt(GAScheduleResult::getStartTime))
.collect(Collectors.toList()); .collect(Collectors.toList());
...@@ -185,12 +185,12 @@ public class MachineCalculator { ...@@ -185,12 +185,12 @@ public class MachineCalculator {
List<TimeSegment> timeSegments= findAvailableSegments(machine, currentTime, machineTasks, remainingTime, isInterrupt); List<TimeSegment> timeSegments= findAvailableSegments(machine, currentTime, machineTasks, remainingTime, isInterrupt);
int estimateIndex= (int) Math.ceil(remainingTime / (double) ONE_DAY_MINUTES); int estimateIndex= (int) Math.ceil(remainingTime / (double) ONE_DAY_MINUTES);
List<TimeSegment> timeSegments1=null; List<TimeSegment> timeSegments1=null;
if(estimateIndex>10) // if(estimateIndex>10)
{ // {
timeSegments1= getEnoughSegmentsByEstimateIndex(timeSegments,currentTime,remainingTime); // timeSegments1= getEnoughSegmentsByEstimateIndex(timeSegments,currentTime,remainingTime);
} // }
if(timeSegments1==null) { // if(timeSegments1==null) {
int i = 0; int i = 0;
while (remainingTime > 0) { while (remainingTime > 0) {
TimeSegment shift = timeSegments.get(i); TimeSegment shift = timeSegments.get(i);
...@@ -203,9 +203,9 @@ public class MachineCalculator { ...@@ -203,9 +203,9 @@ public class MachineCalculator {
RemoveMachineAvailable(machine, time,shift); RemoveMachineAvailable(machine, time,shift);
i++; i++;
} }
}else { // }else {
times= CaldScheduleResultDetail(timeSegments1,machine,st,remainingTime,oneTime); // times= CaldScheduleResultDetail(timeSegments1,machine,st,remainingTime,oneTime);
} // }
return times; return times;
} }
...@@ -307,7 +307,7 @@ public class MachineCalculator { ...@@ -307,7 +307,7 @@ public class MachineCalculator {
LocalDateTime current = start; LocalDateTime current = start;
// 预先排序设备可用片段,避免后续遍历混乱 // 预先排序设备可用片段,避免后续遍历混乱
List<TimeSegment> allUseTimeSegment = new ArrayList<>(machine.getAvailability()).stream() List<TimeSegment> allUseTimeSegment = machine.getAvailability().stream()
.filter(slot -> !slot.isUsed() && slot.getType() != SegmentType.MAINTENANCE) .filter(slot -> !slot.isUsed() && slot.getType() != SegmentType.MAINTENANCE)
.sorted(Comparator.comparing(TimeSegment::getStart, (a, b) -> a.compareTo(b))) .sorted(Comparator.comparing(TimeSegment::getStart, (a, b) -> a.compareTo(b)))
.collect(Collectors.toList()); .collect(Collectors.toList());
...@@ -627,7 +627,7 @@ public class MachineCalculator { ...@@ -627,7 +627,7 @@ public class MachineCalculator {
// 2. 已有任务冲突 // 2. 已有任务冲突
if (machineTasks != null && machineTasks.size()>0) { if (machineTasks != null && machineTasks.size()>0) {
// 第一步:转换任务为TimeSegment并过滤重叠冲突 // 第一步:转换任务为TimeSegment并过滤重叠冲突
List<TimeSegment> taskConflicts = new ArrayList<>(machineTasks).stream() List<TimeSegment> taskConflicts = machineTasks.stream()
.map(w -> { .map(w -> {
// 计算任务起始和结束时间:baseTime.AddMinutes(w.StartTime)/w.EndTime // 计算任务起始和结束时间:baseTime.AddMinutes(w.StartTime)/w.EndTime
LocalDateTime taskStart = baseTime.plusSeconds(w.getStartTime()); LocalDateTime taskStart = baseTime.plusSeconds(w.getStartTime());
...@@ -664,10 +664,10 @@ public class MachineCalculator { ...@@ -664,10 +664,10 @@ public class MachineCalculator {
double requiredMinutes) { double requiredMinutes) {
// 遍历所有冲突片段 // 遍历所有冲突片段
for (TimeSegment conflict : new ArrayList<>(conflictSegments)) { for (TimeSegment conflict : conflictSegments) {
LocalDateTime currentTime1=currentTime; LocalDateTime currentTime1=currentTime;
// 过滤冲突前的可用片段( // 过滤冲突前的可用片段(
List<TimeSegment> preConflictSegments = new ArrayList<>(useSegments).stream() List<TimeSegment> preConflictSegments = useSegments.stream()
.filter(slot -> .filter(slot ->
(slot.getStart().compareTo(currentTime1) >= 0 || slot.getEnd().compareTo(currentTime1) > 0) (slot.getStart().compareTo(currentTime1) >= 0 || slot.getEnd().compareTo(currentTime1) > 0)
&& slot.getEnd().compareTo(conflict.getStart()) <= 0 && slot.getEnd().compareTo(conflict.getStart()) <= 0
...@@ -712,7 +712,7 @@ public class MachineCalculator { ...@@ -712,7 +712,7 @@ public class MachineCalculator {
private boolean CheckTask(Machine machine,List<GAScheduleResult> machineTasks,LocalDateTime prevEnd,LocalDateTime shiftStart) { private boolean CheckTask(Machine machine,List<GAScheduleResult> machineTasks,LocalDateTime prevEnd,LocalDateTime shiftStart) {
LocalDateTime finalPrevEnd = prevEnd; LocalDateTime finalPrevEnd = prevEnd;
boolean hasTask = new ArrayList<>(machineTasks).stream() boolean hasTask = machineTasks.stream()
.anyMatch(t -> { .anyMatch(t -> {
LocalDateTime taskStart = baseTime.plusSeconds(t.getStartTime()); LocalDateTime taskStart = baseTime.plusSeconds(t.getStartTime());
return taskStart.isAfter(finalPrevEnd) && taskStart.isBefore(shiftStart); return taskStart.isAfter(finalPrevEnd) && taskStart.isBefore(shiftStart);
...@@ -721,7 +721,7 @@ public class MachineCalculator { ...@@ -721,7 +721,7 @@ public class MachineCalculator {
// 检查班次间维修窗口 // 检查班次间维修窗口
if (machine.getMaintenanceWindows() != null) { if (machine.getMaintenanceWindows() != null) {
LocalDateTime finalPrevEnd1 = prevEnd; LocalDateTime finalPrevEnd1 = prevEnd;
boolean hasMaintenance = new ArrayList<>(machine.getMaintenanceWindows()).stream() boolean hasMaintenance = machine.getMaintenanceWindows().stream()
.anyMatch(w -> w.getStartTime().isAfter(finalPrevEnd1) && w.getStartTime().isBefore(shiftStart)); .anyMatch(w -> w.getStartTime().isAfter(finalPrevEnd1) && w.getStartTime().isBefore(shiftStart));
return hasMaintenance; return hasMaintenance;
...@@ -785,7 +785,7 @@ public class MachineCalculator { ...@@ -785,7 +785,7 @@ public class MachineCalculator {
LocalDateTime currentTime, LocalDateTime startTime, LocalDateTime currentTime, LocalDateTime startTime,
List<GAScheduleResult> existingTasks) { List<GAScheduleResult> existingTasks) {
// 获取设备上已有任务并按开始时间排序 // 获取设备上已有任务并按开始时间排序
List<GAScheduleResult> machineTasks = new ArrayList<>(existingTasks).stream() List<GAScheduleResult> machineTasks = existingTasks.stream()
.filter(t -> t.getMachineId() == machine.getId()) .filter(t -> t.getMachineId() == machine.getId())
.sorted(Comparator.comparing(GAScheduleResult::getStartTime)) .sorted(Comparator.comparing(GAScheduleResult::getStartTime))
.collect(Collectors.toList()); .collect(Collectors.toList());
...@@ -976,26 +976,25 @@ public class MachineCalculator { ...@@ -976,26 +976,25 @@ public class MachineCalculator {
return times; return times;
} }
private void RemoveMachineAvailable(Machine machine, ScheduleResultDetail geneDetails,TimeSegment targetSegment) { private void RemoveMachineAvailable1(Machine machine, ScheduleResultDetail geneDetails,TimeSegment targetSegment) {
// 关键修复2:加锁(若多线程访问),避免并发修改 // 关键修复2:加锁(若多线程访问),避免并发修改
synchronized (machine.getAvailability()) {
List<TimeSegment> availabilitySnapshot = new ArrayList<>(machine.getAvailability());
LocalDateTime geneEndTime = baseTime.plusSeconds(geneDetails.getEndTime());
LocalDateTime geneEndTime = baseTime.plusSeconds(geneDetails.getEndTime());
TimeSegment usedSegment = null;
if (targetSegment.getEnd().isAfter(geneEndTime)) { if (targetSegment.getEnd().isAfter(geneEndTime)) {
TimeSegment usedSegment = new TimeSegment(); usedSegment=new TimeSegment();
usedSegment.setStart(baseTime.plusSeconds(geneDetails.getStartTime())); usedSegment.setStart(baseTime.plusSeconds(geneDetails.getStartTime()));
usedSegment.setEnd(geneEndTime); usedSegment.setEnd(geneEndTime);
usedSegment.setHoliday(false); usedSegment.setHoliday(false);
usedSegment.setKey(UUID.randomUUID().toString()); usedSegment.setKey(UUID.randomUUID().toString());
usedSegment.setType(SegmentType.REGULAR); usedSegment.setType(SegmentType.REGULAR);
usedSegment.setUsed(true); usedSegment.setUsed(true);
availabilitySnapshot.add(usedSegment);
geneDetails.setKey(usedSegment.getKey()); geneDetails.setKey(usedSegment.getKey());
targetSegment.setStart(geneEndTime); targetSegment.setStart(geneEndTime);
...@@ -1004,15 +1003,18 @@ public class MachineCalculator { ...@@ -1004,15 +1003,18 @@ public class MachineCalculator {
} }
if(usedSegment!=null)
{
synchronized (machine.getAvailability()) {
availabilitySnapshot.sort(Comparator.comparing(TimeSegment::getStart)); machine.getAvailability().add(usedSegment);
machine.getAvailability().sort(Comparator.comparing(TimeSegment::getStart));
machine.setAvailability(availabilitySnapshot); }
} }
} }
private void RemoveMachineAvailable1(Machine machine, ScheduleResultDetail geneDetails) { private void RemoveMachineAvailable(Machine machine, ScheduleResultDetail geneDetails,TimeSegment targetSegment1) {
List<TimeSegment> timeSegments = new ArrayList<>(); List<TimeSegment> timeSegments = new ArrayList<>();
List<TimeSegment> availabilitySnapshot = new ArrayList<>(machine.getAvailability()); List<TimeSegment> availabilitySnapshot = new ArrayList<>(machine.getAvailability());
...@@ -1053,9 +1055,9 @@ public class MachineCalculator { ...@@ -1053,9 +1055,9 @@ public class MachineCalculator {
} }
} }
public void AddMachineAvailable(Machine machine, List<ScheduleResultDetail> geneDetails) { public void AddMachineAvailable1(Machine machine, List<ScheduleResultDetail> geneDetails) {
if (geneDetails == null || geneDetails.isEmpty()) return; if (geneDetails == null || geneDetails.isEmpty()) return;
synchronized (machine.getAvailability()) {
List<String> keys= geneDetails.stream(). List<String> keys= geneDetails.stream().
filter(t->t.getUsedSegment()==null||t.getUsedSegment().size()==0) filter(t->t.getUsedSegment()==null||t.getUsedSegment().size()==0)
.map(ScheduleResultDetail::getKey) .map(ScheduleResultDetail::getKey)
...@@ -1074,10 +1076,10 @@ if(keys1!=null&&keys1.size()>0) { ...@@ -1074,10 +1076,10 @@ if(keys1!=null&&keys1.size()>0) {
.forEach(t->t.setUsed(false)); .forEach(t->t.setUsed(false));
}
} }
public void AddMachineAvailable1(Machine machine, List<ScheduleResultDetail> geneDetails) { public void AddMachineAvailable(Machine machine, List<ScheduleResultDetail> geneDetails) {
if (geneDetails == null || geneDetails.isEmpty()) return; if (geneDetails == null || geneDetails.isEmpty()) return;
List<TimeSegment> availabilitySnapshot = new ArrayList<>(machine.getAvailability()); List<TimeSegment> availabilitySnapshot = new ArrayList<>(machine.getAvailability());
......
...@@ -39,9 +39,9 @@ public class PlanResultServiceTest { ...@@ -39,9 +39,9 @@ public class PlanResultServiceTest {
// nsgaiiUtils.Test(); // nsgaiiUtils.Test();
//planResultService.execute2("C5FB5EF2A7334A0A92F826F4937E1008"); //planResultService.execute2("C5FB5EF2A7334A0A92F826F4937E1008");
// planResultService.execute2("726D4C1A712B4B1393175BD44B775B66"); // planResultService.execute2("726D4C1A712B4B1393175BD44B775B66");
planResultService.execute2("BCA6FA43FFA444D3952CF8F6E1EA291B"); planResultService.execute2("E81EE93EFA564CEB8594C61CAEEBCD53");
// LocalDateTime t= LocalDateTime.of(2025, 11, 15, 6, 51, 11); // LocalDateTime t= LocalDateTime.of(2025, 11, 15, 6, 51, 11);
// List<Integer> opids=new ArrayList<>(); // List<Integer> opids=new ArrayList<>();BCA6FA43FFA444D3952CF8F6E1EA291B
// opids.add(1); // opids.add(1);
// planResultService.Move("B571EF6682DB463AB2977B1055A74112",opids,t,3403L); // planResultService.Move("B571EF6682DB463AB2977B1055A74112",opids,t,3403L);
// planResultService.Redecode("12345679"); // planResultService.Redecode("12345679");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment