Commit 899a4be3 authored by Tong Li's avatar Tong Li

遗传算法

parent 1173674e
...@@ -135,6 +135,16 @@ public class ResourceGanttController { ...@@ -135,6 +135,16 @@ public class ResourceGanttController {
return scheduleChromosomes; return scheduleChromosomes;
} }
@GetMapping("/Move")
public Chromosome Move() {
// 调用 PlanResultService 获取 ScheduleChromosome 列表
Chromosome scheduleChromosomes = planResultService.Move("B571EF6682DB463AB2977B1055A74112");
// 提取所有场景ID
return scheduleChromosomes;
}
/** /**
* 将 ScheduleChromosome 转换为 ResourceGanttVO 列表 * 将 ScheduleChromosome 转换为 ResourceGanttVO 列表
* @param scheduleChromosome 调度结果 * @param scheduleChromosome 调度结果
......
package com.aps.entity.Algorithm; package com.aps.entity.Algorithm;
import com.aps.entity.Algorithm.IDAndChildID.GroupResult;
import com.aps.entity.basic.Entry; import com.aps.entity.basic.Entry;
import com.aps.entity.basic.Machine; import com.aps.entity.basic.Machine;
import com.aps.entity.basic.Order; import com.aps.entity.basic.Order;
import lombok.Data; import lombok.Data;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import java.util.stream.Collectors; import java.util.stream.Collectors;
...@@ -57,6 +59,7 @@ public class Chromosome { ...@@ -57,6 +59,7 @@ public class Chromosome {
private List<Entry> allOperations; private List<Entry> allOperations;
private List<Order> orders; private List<Order> orders;
private List<Machine> InitMachines; private List<Machine> InitMachines;
private List<GroupResult> OperatRel;
/// <summary> /// <summary>
/// 适应度值 /// 适应度值
/// </summary> /// </summary>
...@@ -72,6 +75,11 @@ public class Chromosome { ...@@ -72,6 +75,11 @@ public class Chromosome {
/// </summary> /// </summary>
private List<GAScheduleResult> Result; private List<GAScheduleResult> Result;
/// <summary>
/// 解码后的调度结果
/// </summary>
private List<GAScheduleResult> ResultOld=new ArrayList<>();
/// <summary> /// <summary>
/// 最早完工时间 /// 最早完工时间
/// </summary> /// </summary>
......
...@@ -286,6 +286,13 @@ public class GeneticDecoder { ...@@ -286,6 +286,13 @@ public class GeneticDecoder {
System.out.println(" 标准模式:安排主处理+" + setupTime + "分钟换型=" + processingTimeTotal + "分钟"); System.out.println(" 标准模式:安排主处理+" + setupTime + "分钟换型=" + processingTimeTotal + "分钟");
} }
GAScheduleResult existingResult = chromosome.getResultOld().stream().filter(r-> r.getOperationId() == operation.Id).findFirst().orElse(null);
if(existingResult!=null)
{
earliestStartTime = Math.max(earliestStartTime,existingResult.getDesignatedStartTime());
}
MachineCalculator machineCalculator=new MachineCalculator(baseTime,machines,machineScheduler); MachineCalculator machineCalculator=new MachineCalculator(baseTime,machines,machineScheduler);
List<ScheduleResultDetail> geneDetails = machineCalculator.getNextAvailableTime(machine, earliestStartTime, -1, List<ScheduleResultDetail> geneDetails = machineCalculator.getNextAvailableTime(machine, earliestStartTime, -1,
processingTimeTotal, chromosome.getResult(), false, true, true); processingTimeTotal, chromosome.getResult(), false, true, true);
...@@ -347,6 +354,9 @@ public class GeneticDecoder { ...@@ -347,6 +354,9 @@ public class GeneticDecoder {
result.setStartTime(startTime); result.setStartTime(startTime);
result.setEndTime(endTime); result.setEndTime(endTime);
result.setTeardownTime(teardownTime); result.setTeardownTime(teardownTime);
if(existingResult!=null) {
result.setDesignatedStartTime(existingResult.getDesignatedStartTime());
}
result.setOneTime(processingTime); result.setOneTime(processingTime);
result.setProcessingTime(processingTimeTotal); result.setProcessingTime(processingTimeTotal);
......
package com.aps.service.Algorithm; package com.aps.service.Algorithm;
import com.aps.common.util.ProductionDeepCopyUtil;
import com.aps.entity.Algorithm.Chromosome; import com.aps.entity.Algorithm.Chromosome;
import com.aps.entity.Algorithm.GAScheduleResult; import com.aps.entity.Algorithm.GAScheduleResult;
import com.aps.entity.Algorithm.GlobalOperationInfo; import com.aps.entity.Algorithm.GlobalOperationInfo;
...@@ -42,6 +43,16 @@ public class ScheduleOperationService { ...@@ -42,6 +43,16 @@ public class ScheduleOperationService {
.findFirst() .findFirst()
.orElseThrow(() -> new NoSuchElementException("Operation not found: " + opId)); .orElseThrow(() -> new NoSuchElementException("Operation not found: " + opId));
int machineOptionIndex = targetOp.getMachineOptions().stream()
.map(MachineOption::getMachineId)
.collect(Collectors.toList())
.indexOf(newMachineId) + 1;
if(machineOptionIndex==0)
{
throw new NoSuchElementException("Machine not found: " + newMachineId);
}
// 设置约束 // 设置约束
targetResult.setDesignatedStartTime(newStartTime); targetResult.setDesignatedStartTime(newStartTime);
targetResult.setForcedMachineId(newMachineId); targetResult.setForcedMachineId(newMachineId);
...@@ -53,10 +64,9 @@ public class ScheduleOperationService { ...@@ -53,10 +64,9 @@ public class ScheduleOperationService {
.map(GlobalOperationInfo::getGlobalOpId) .map(GlobalOperationInfo::getGlobalOpId)
.orElseThrow(() -> new NoSuchElementException("Global operation not found: " + opId)); .orElseThrow(() -> new NoSuchElementException("Global operation not found: " + opId));
int machineOptionIndex = targetOp.getMachineOptions().stream()
.map(MachineOption::getMachineId)
.collect(Collectors.toList())
.indexOf(newMachineId) + 1;
chromosome.getMachineSelection().set(globalOpIndex, machineOptionIndex); chromosome.getMachineSelection().set(globalOpIndex, machineOptionIndex);
...@@ -86,6 +96,9 @@ public class ScheduleOperationService { ...@@ -86,6 +96,9 @@ public class ScheduleOperationService {
MachineSchedulerService machineScheduler = new MachineSchedulerService(baseTime); MachineSchedulerService machineScheduler = new MachineSchedulerService(baseTime);
GeneticDecoder decoder = new GeneticDecoder(globalParam,baseTime, chromosome.getInitMachines(), GeneticDecoder decoder = new GeneticDecoder(globalParam,baseTime, chromosome.getInitMachines(),
chromosome.getOrders(), null, machineScheduler); chromosome.getOrders(), null, machineScheduler);
chromosome.setMachines(chromosome.getInitMachines());
chromosome.setResultOld(ProductionDeepCopyUtil.deepCopyList(chromosome.getResult()));
chromosome.getResult().clear();
decoder.decode(chromosome); decoder.decode(chromosome);
if(chromosome.getFitness()==0) { if(chromosome.getFitness()==0) {
......
...@@ -207,7 +207,7 @@ public class MachineSchedulerService { ...@@ -207,7 +207,7 @@ public class MachineSchedulerService {
&& containsDay(s.getDays(), date.getDayOfWeek())) && containsDay(s.getDays(), date.getDayOfWeek()))
.collect(Collectors.toList()); .collect(Collectors.toList());
if(shifts==null||shifts.size()==0) { if(shifts==null||shifts.size()==0) {
LocalDate sd= LocalDate.of(2020, 10, 1); LocalDate sd= LocalDate.of(2000, 1, 1);
shifts = machine.getShifts().stream() shifts = machine.getShifts().stream()
.filter(s ->sd.compareTo(s.getStartDate().toLocalDate())==0&& s.getDays() != null .filter(s ->sd.compareTo(s.getStartDate().toLocalDate())==0&& s.getDays() != null
&& containsDay(s.getDays(), date.getDayOfWeek())) && containsDay(s.getDays(), date.getDayOfWeek()))
......
...@@ -163,7 +163,7 @@ public class PlanResultService { ...@@ -163,7 +163,7 @@ public class PlanResultService {
} }
} }
ScheduleParams param = new ScheduleParams(); ScheduleParams param = new ScheduleParams();
param.setBaseTime(LocalDateTime.of(2025, 10, 1, 0, 0, 0)); param.setBaseTime(LocalDateTime.of(2025, 11, 1, 0, 0, 0));
param.setPopulationSize(50); param.setPopulationSize(50);
param.setMaxIterations(100); param.setMaxIterations(100);
...@@ -193,6 +193,7 @@ public class PlanResultService { ...@@ -193,6 +193,7 @@ public class PlanResultService {
int id = 1; int id = 1;
for (Order order : orders) { for (Order order : orders) {
order.setDueDate(LocalDateTime.of(2025, 12, 1,0,0,0));
// 假设products是一个List<Product>,根据Product的Id查找对应的产品 // 假设products是一个List<Product>,根据Product的Id查找对应的产品
Product product = products.stream() Product product = products.stream()
.filter(p -> p.getId() == order.getProductId()) .filter(p -> p.getId() == order.getProductId())
...@@ -224,11 +225,11 @@ public class PlanResultService { ...@@ -224,11 +225,11 @@ public class PlanResultService {
// 5. 执行调度算法 // 5. 执行调度算法
GeneticAlgorithm scheduler =new GeneticAlgorithm(globalParam,machines,orders,null,machineScheduler); //new GeneticAlgorithm(products, machines, orders, machineScheduler); GeneticAlgorithm scheduler =new GeneticAlgorithm(globalParam,machines,orders,null,machineScheduler); //new GeneticAlgorithm(products, machines, orders, machineScheduler);
Chromosome Chromosomes =scheduler.Run(param,allOperations); Chromosome Chromosomes =scheduler.Run(param,allOperations);
WriteScheduleSummary(Chromosomes);
ScheduleOperationService ScheduleOperation=new ScheduleOperationService(); ScheduleOperationService ScheduleOperation=new ScheduleOperationService();
LocalDateTime ds= LocalDateTime.of(2025, 12, 7, 23, 59); LocalDateTime ds= LocalDateTime.of(2025, 12, 7, 23, 59);
ScheduleOperation.moveOperation(Chromosomes,3, (int)ChronoUnit.SECONDS.between(param.getBaseTime(), ds),2,param.getBaseTime(), globalParam); ScheduleOperation.moveOperation(Chromosomes,3, (int)ChronoUnit.SECONDS.between(param.getBaseTime(), ds),2,param.getBaseTime(), globalParam);
//Chromosomes.forEach(this::WriteScheduleSummary); WriteScheduleSummary(Chromosomes);
return Chromosomes; return Chromosomes;
...@@ -276,29 +277,46 @@ public class PlanResultService { ...@@ -276,29 +277,46 @@ public class PlanResultService {
orders.add(order); orders.add(order);
} }
List<Entry> entrys= InitEntrys(SceneId,ProdEquipments,ProdLaunchOrders); Map<Integer,Object> list= InitEntrys(SceneId,ProdEquipments,ProdLaunchOrders);
List<Entry> entrys=(List<Entry>)list.get(1);
List<GroupResult> entryRel=(List<GroupResult>)list.get(2);
GlobalParam globalParam=new GlobalParam(); GlobalParam globalParam=new GlobalParam();
// 5. 执行调度算法 // 5. 执行调度算法
GeneticAlgorithm scheduler =new GeneticAlgorithm(globalParam,machines,orders,null,machineScheduler); //new GeneticAlgorithm(products, machines, orders, machineScheduler); GeneticAlgorithm scheduler =new GeneticAlgorithm(globalParam,machines,orders,null,machineScheduler); //new GeneticAlgorithm(products, machines, orders, machineScheduler);
Chromosome Chromosomes =scheduler.Run(param,entrys); Chromosome chromosome =scheduler.Run(param,entrys);
chromosome.setOperatRel(entryRel);
_sceneService.saveChromosomeToFile(chromosome, SceneId);
// Chromosomes.forEach(this::WriteScheduleSummary); // Chromosomes.forEach(this::WriteScheduleSummary);
return Chromosomes; return chromosome;
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException("调度执行失败", e); throw new RuntimeException("调度执行失败", e);
} }
} }
public Chromosome Move(String SceneId) {
ScheduleParams param = new ScheduleParams();
param.setBaseTime(LocalDateTime.of(2025, 11, 1, 0, 0, 0));
GlobalParam globalParam=new GlobalParam();
Chromosome chromosome= _sceneService.loadChromosomeFromFile(SceneId);
WriteScheduleSummary(chromosome);
ScheduleOperationService ScheduleOperation=new ScheduleOperationService();
LocalDateTime ds= LocalDateTime.of(2025, 12, 7, 23, 59);
ScheduleOperation.moveOperation(chromosome,2, (int)ChronoUnit.SECONDS.between(param.getBaseTime(), ds),2,param.getBaseTime(), globalParam);
WriteScheduleSummary(chromosome);
// _sceneService.saveChromosomeToFile(chromosome, SceneId);
return chromosome;
}
public Chromosome schedule(String SceneId) { public Chromosome schedule(String SceneId) {
try { try {
ScheduleParams param = new ScheduleParams(); ScheduleParams param = new ScheduleParams();
param.setBaseTime(LocalDateTime.of(2025, 11, 1, 0, 0, 0)); param.setBaseTime(LocalDateTime.of(2025, 11, 1, 0, 0, 0));
param.setPopulationSize(50); param.setPopulationSize(50);
param.setMaxIterations(100); param.setMaxIterations(100);
...@@ -335,7 +353,10 @@ public class PlanResultService { ...@@ -335,7 +353,10 @@ public class PlanResultService {
orders.add(order); orders.add(order);
} }
List<Entry> entrys= InitEntrys(SceneId,ProdEquipments,ProdLaunchOrders);
Map<Integer,Object> list= InitEntrys(SceneId,ProdEquipments,ProdLaunchOrders);
List<Entry> entrys=(List<Entry>)list.get(1);
List<GroupResult> entryRel=(List<GroupResult>)list.get(2);
GlobalParam globalParam=new GlobalParam(); GlobalParam globalParam=new GlobalParam();
...@@ -344,7 +365,7 @@ public class PlanResultService { ...@@ -344,7 +365,7 @@ public class PlanResultService {
Chromosome chromosomes =scheduler.Run(param,entrys); Chromosome chromosomes =scheduler.Run(param,entrys);
chromosomes.setScenarioID(SceneId); chromosomes.setScenarioID(SceneId);
chromosomes.setBaseTime(param.getBaseTime()); chromosomes.setBaseTime(param.getBaseTime());
chromosomes.setOperatRel(entryRel);
// 保存chromosomes到文件 // 保存chromosomes到文件
_sceneService.saveChromosomeToFile(chromosomes, SceneId); _sceneService.saveChromosomeToFile(chromosomes, SceneId);
...@@ -575,9 +596,9 @@ public class PlanResultService { ...@@ -575,9 +596,9 @@ public class PlanResultService {
} }
private List<Entry> InitEntrys(String SceneId,List<ProdEquipment> ProdEquipments,List<ProdLaunchOrder> ProdLaunchOrders) private Map<Integer, Object> InitEntrys(String SceneId,List<ProdEquipment> ProdEquipments,List<ProdLaunchOrder> ProdLaunchOrders)
{ {
Map<Integer, Object> list=new HashMap<>();
List<ProdProcessExec> ProdProcessExecs= _prodProcessExecService.lambdaQuery() List<ProdProcessExec> ProdProcessExecs= _prodProcessExecService.lambdaQuery()
.eq(ProdProcessExec::getSceneId,SceneId) .eq(ProdProcessExec::getSceneId,SceneId)
.list(); .list();
...@@ -621,14 +642,14 @@ public class PlanResultService { ...@@ -621,14 +642,14 @@ public class PlanResultService {
for (int i = 0; i < results.size(); i++) { for (int i = 0; i < results.size(); i++) {
GroupResult groupResult = results.get(i); GroupResult groupResult = results.get(i);
List<NodeInfo> nodeInfoList = groupResult.getNodeInfoList(); List<NodeInfo> nodeInfoList = groupResult.getNodeInfoList();
System.out.println("分组" + (i + 1) + "顺序:" + nodeInfoList); // System.out.println("分组" + (i + 1) + "顺序:" + nodeInfoList);
for (NodeInfo nodeInfo : nodeInfoList) { for (NodeInfo nodeInfo : nodeInfoList) {
System.out.printf("原始ID:%s → 全局序号:%d,分组内序号:%d,新父ID列表:%s,新子ID列表:%s%n", // System.out.printf("原始ID:%s → 全局序号:%d,分组内序号:%d,新父ID列表:%s,新子ID列表:%s%n",
nodeInfo.getOriginalId(), // nodeInfo.getOriginalId(),
nodeInfo.getGlobalSerial(), // nodeInfo.getGlobalSerial(),
nodeInfo.getGroupSerial(), // nodeInfo.getGroupSerial(),
nodeInfo.getNewParentIds().isEmpty() ? "无" : nodeInfo.getNewParentIds(), // nodeInfo.getNewParentIds().isEmpty() ? "无" : nodeInfo.getNewParentIds(),
nodeInfo.getNewChildIds()); // nodeInfo.getNewChildIds());
Entry entry = new Entry(); Entry entry = new Entry();
...@@ -675,9 +696,10 @@ public class PlanResultService { ...@@ -675,9 +696,10 @@ public class PlanResultService {
System.out.println("------------------------"); System.out.println("------------------------");
} }
list.put(1,entrys);
list.put(2,results);
return list;
return entrys;
} }
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
"id": 1, "id": 1,
"productId": 1, "productId": 1,
"quantity": 50, "quantity": 50,
"dueDate": "2025-10-27T16:21:31.0756427+08:00",
"priority": 1, "priority": 1,
"canSplit": false, "canSplit": false,
"canInterrupt": false "canInterrupt": false
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
"id": 2, "id": 2,
"productId": 1, "productId": 1,
"quantity": 100, "quantity": 100,
"dueDate": "2025-10-18T16:21:31.0756427+08:00",
"priority": 2, "priority": 2,
"canSplit": false, "canSplit": false,
"canInterrupt": false "canInterrupt": false
...@@ -21,7 +21,7 @@ ...@@ -21,7 +21,7 @@
"id": 3, "id": 3,
"productId": 2, "productId": 2,
"quantity": 100, "quantity": 100,
"dueDate": "2025-10-31T16:21:31.0756427+08:00",
"priority": 2, "priority": 2,
"canSplit": false, "canSplit": false,
"canInterrupt": false "canInterrupt": false
......
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
"machineOptions": [ "machineOptions": [
{ {
"machineId": 2, "machineId": 2,
"processingTime": 11, "processingTime": 660,
"setupTime": 0, "setupTime": 0,
"teardownTime": 0, "teardownTime": 0,
"preTime": 0 "preTime": 0
...@@ -25,14 +25,14 @@ ...@@ -25,14 +25,14 @@
"machineOptions": [ "machineOptions": [
{ {
"machineId": 3, "machineId": 3,
"processingTime": 5, "processingTime": 600,
"setupTime": 0, "setupTime": 0,
"teardownTime": 0, "teardownTime": 0,
"preTime": 0 "preTime": 0
}, },
{ {
"machineId": 5, "machineId": 5,
"processingTime": 21, "processingTime": 1320,
"setupTime": 0, "setupTime": 0,
"teardownTime": 0, "teardownTime": 0,
"preTime": 0 "preTime": 0
...@@ -47,7 +47,7 @@ ...@@ -47,7 +47,7 @@
"machineOptions": [ "machineOptions": [
{ {
"machineId": 5, "machineId": 5,
"processingTime": 6, "processingTime": 360,
"setupTime": 0, "setupTime": 0,
"teardownTime": 0, "teardownTime": 0,
"preTime": 0 "preTime": 0
...@@ -62,14 +62,14 @@ ...@@ -62,14 +62,14 @@
"machineOptions": [ "machineOptions": [
{ {
"machineId": 1, "machineId": 1,
"processingTime": 19, "processingTime": 1200,
"setupTime": 0, "setupTime": 0,
"teardownTime": 0, "teardownTime": 0,
"preTime": 0 "preTime": 0
}, },
{ {
"machineId": 4, "machineId": 4,
"processingTime": 20, "processingTime": 1160,
"setupTime": 0, "setupTime": 0,
"teardownTime": 0, "teardownTime": 0,
"preTime": 0 "preTime": 0
...@@ -90,7 +90,7 @@ ...@@ -90,7 +90,7 @@
"machineOptions": [ "machineOptions": [
{ {
"machineId": 1, "machineId": 1,
"processingTime": 5, "processingTime": 300,
"setupTime": 0, "setupTime": 0,
"teardownTime": 0, "teardownTime": 0,
"preTime": 0 "preTime": 0
...@@ -105,7 +105,7 @@ ...@@ -105,7 +105,7 @@
"machineOptions": [ "machineOptions": [
{ {
"machineId": 2, "machineId": 2,
"processingTime": 22, "processingTime": 1440,
"setupTime": 0, "setupTime": 0,
"teardownTime": 0, "teardownTime": 0,
"preTime": 0 "preTime": 0
...@@ -127,7 +127,7 @@ ...@@ -127,7 +127,7 @@
}, },
{ {
"machineId": 5, "machineId": 5,
"processingTime": 28, "processingTime": 1800,
"setupTime": 0, "setupTime": 0,
"teardownTime": 0, "teardownTime": 0,
"preTime": 0 "preTime": 0
......
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