Commit fc778193 authored by Tong Li's avatar Tong Li

遗传算法-移动多工单,删除订单

parent bb843694
This diff is collapsed.
...@@ -12,6 +12,7 @@ import java.time.format.DateTimeFormatter; ...@@ -12,6 +12,7 @@ import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException; import java.time.format.DateTimeParseException;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors;
@Slf4j @Slf4j
@UtilityClass @UtilityClass
...@@ -241,4 +242,25 @@ public class ParamValidator { ...@@ -241,4 +242,25 @@ public class ParamValidator {
}) })
.toArray(Double[]::new); .toArray(Double[]::new);
} }
public static List<Integer> convertToIntArray(List<?> sourceList, String fieldName) {
requireNotEmpty(sourceList, fieldName);
return sourceList.stream()
.map(item -> {
if (item instanceof Number) {
return ((Integer) item);
} else if (item instanceof String) {
try {
return Integer.parseInt(((String) item).trim());
} catch (NumberFormatException e) {
throw new IllegalArgumentException(fieldName + "包含无效的数字: " + item);
}
} else {
throw new IllegalArgumentException(fieldName + "包含不支持的数据类型: " +
(item != null ? item.getClass().getSimpleName() : "null"));
}
})
.collect(Collectors.toList());
}
} }
\ No newline at end of file
...@@ -144,15 +144,17 @@ public class ResourceGanttController { ...@@ -144,15 +144,17 @@ public class ResourceGanttController {
// 1. 提取参数 // 1. 提取参数
String sceneId = ParamValidator.getString(params, "sceneId", "场景ID"); String sceneId = ParamValidator.getString(params, "sceneId", "场景ID");
Integer opid = ParamValidator.getInteger(params, "id", "操作ID"); List<?> opid = ParamValidator.getList(params, "id", "操作ID");
LocalDateTime newStartTime = ParamValidator.getDateTime(params, "newStartTime", "新的开始时间"); LocalDateTime newStartTime = ParamValidator.getDateTime(params, "newStartTime", "新的开始时间");
Long newMachineId = ParamValidator.getLong(params, "newMachineId", "新机器ID"); Long newMachineId = ParamValidator.getLong(params, "newMachineId", "新机器ID");
// 2. 验证场景 // 2. 验证场景
ParamValidator.validateSceneExists(sceneService, sceneId); ParamValidator.validateSceneExists(sceneService, sceneId);
List<Integer> opids = ParamValidator.convertToIntArray(opid, "操作IDS");
// 3. 执行业务 // 3. 执行业务
Chromosome result = planResultService.Move(sceneId, opid, newStartTime, newMachineId); Chromosome result = planResultService.Move(sceneId, opids, newStartTime, newMachineId);
return R.ok(result); return R.ok(result);
} }
......
...@@ -66,7 +66,7 @@ public class GeneticAlgorithm { ...@@ -66,7 +66,7 @@ public class GeneticAlgorithm {
} }
System.out.println("开始"); System.out.println("开始");
Initialization initialization = new Initialization(_GlobalParam,allOperations,orders); Initialization initialization = new Initialization(_GlobalParam,allOperations,orders,machines);
GeneticOperations geneticOps = new GeneticOperations(_GlobalParam,allOperations,param); GeneticOperations geneticOps = new GeneticOperations(_GlobalParam,allOperations,param);
...@@ -183,8 +183,7 @@ public class GeneticAlgorithm { ...@@ -183,8 +183,7 @@ public class GeneticAlgorithm {
} }
} }
best.setBaseTime(param.getBaseTime()); best.setBaseTime(param.getBaseTime());
best.setInitMachines(ProductionDeepCopyUtil.deepCopyList(machines));
best.setOrders(orders);
best.setOrderMaterials(orderMaterials); best.setOrderMaterials(orderMaterials);
best.setOperatRel(_entryRel); best.setOperatRel(_entryRel);
// 步骤3:返回最优解 // 步骤3:返回最优解
......
...@@ -78,7 +78,7 @@ public class GeneticDecoder { ...@@ -78,7 +78,7 @@ public class GeneticDecoder {
chromosome.setTotalChangeoverTime(cachedResult.getTotalChangeoverTime()); chromosome.setTotalChangeoverTime(cachedResult.getTotalChangeoverTime());
chromosome.setMachineLoadStd(cachedResult.getMachineLoadStd()); chromosome.setMachineLoadStd(cachedResult.getMachineLoadStd());
chromosome.setDelayTime(cachedResult.getDelayTime()); chromosome.setDelayTime(cachedResult.getDelayTime());
chromosome.setResult(ProductionDeepCopyUtil.deepCopy(cachedResult.getResult())); chromosome.setResult(cachedResult.getResult());
// Chromosome chromosomen= ProductionDeepCopyUtil.deepCopy(cachedResult); // Chromosome chromosomen= ProductionDeepCopyUtil.deepCopy(cachedResult);
return chromosome; return chromosome;
......
...@@ -136,11 +136,13 @@ public class GeneticOperations { ...@@ -136,11 +136,13 @@ public class GeneticOperations {
Chromosome child1 = new Chromosome(); Chromosome child1 = new Chromosome();
child1.setOrders(parent1.getOrders()); child1.setOrders(parent1.getOrders());
child1.setMachines(parent1.getMachines());
child1.setMachineSelection(child1Ms); child1.setMachineSelection(child1Ms);
child1.setOperationSequencing(child1Os); child1.setOperationSequencing(child1Os);
Chromosome child2 = new Chromosome(); Chromosome child2 = new Chromosome();
child2.setOrders(parent1.getOrders()); child2.setOrders(parent1.getOrders());
child2.setMachines(parent1.getMachines());
child2.setMachineSelection(child2Ms); child2.setMachineSelection(child2Ms);
child2.setOperationSequencing(child2Os); child2.setOperationSequencing(child2Os);
......
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.GlobalOperationInfo; import com.aps.entity.Algorithm.GlobalOperationInfo;
import com.aps.entity.Algorithm.ScheduleParams; import com.aps.entity.Algorithm.ScheduleParams;
import com.aps.entity.basic.Entry; import com.aps.entity.basic.*;
import com.aps.entity.basic.GlobalParam;
import com.aps.entity.basic.MachineOption;
import com.aps.entity.basic.Order;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
...@@ -22,10 +20,13 @@ public class Initialization { ...@@ -22,10 +20,13 @@ public class Initialization {
private static List<Order> orders; private static List<Order> orders;
public Initialization(GlobalParam globalParam,List<Entry> allOperations,List<Order> _orders) { private static List<Machine> machines;
public Initialization(GlobalParam globalParam,List<Entry> allOperations,List<Order> _orders,List<Machine> _machines) {
Initialization.allOperations = allOperations; Initialization.allOperations = allOperations;
_globalParam= globalParam; _globalParam= globalParam;
orders=_orders; orders=_orders;
machines=_machines;
} }
/** /**
* 预生成全局工序列表(按“订单0→订单1→…+订单内工序1→2→…”排序,分配GlobalOpId) * 预生成全局工序列表(按“订单0→订单1→…+订单内工序1→2→…”排序,分配GlobalOpId)
...@@ -59,7 +60,7 @@ int populationSize=param.getPopulationSize(); ...@@ -59,7 +60,7 @@ int populationSize=param.getPopulationSize();
.parallel() // 开启并行 .parallel() // 开启并行
.forEach(i -> { .forEach(i -> {
Chromosome chromo = new Chromosome(); // 初始化染色体 Chromosome chromo = new Chromosome(); // 初始化染色体
chromo.setInitMachines(ProductionDeepCopyUtil.deepCopyList(machines));
chromo.setOrders(orders); chromo.setOrders(orders);
// 全局选择(GS):按GlobalOpId顺序生成MachineSelection // 全局选择(GS):按GlobalOpId顺序生成MachineSelection
if (i < gsCount) { if (i < gsCount) {
......
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.basic.Machine; import com.aps.entity.basic.Machine;
...@@ -128,7 +129,7 @@ public class KpiCalculator { ...@@ -128,7 +129,7 @@ public class KpiCalculator {
private void calculateMachine() { private void calculateMachine() {
// 按设备ID分组工序列表(对应C# GroupBy + ToList) // 按设备ID分组工序列表(对应C# GroupBy + ToList)
List<GAScheduleResult> list= chromosome.getResult(); List<GAScheduleResult> list= chromosome.getResult() ;
Map<Long, List<GAScheduleResult>> machineTasks = list.stream() Map<Long, List<GAScheduleResult>> machineTasks = list.stream()
.collect(Collectors.groupingBy(GAScheduleResult::getMachineId)); .collect(Collectors.groupingBy(GAScheduleResult::getMachineId));
......
...@@ -33,9 +33,19 @@ public class ScheduleOperationService { ...@@ -33,9 +33,19 @@ public class ScheduleOperationService {
* @param newStartTime 新开始时间 * @param newStartTime 新开始时间
* @param newMachineId 新设备ID * @param newMachineId 新设备ID
*/ */
public void moveOperation(Chromosome chromosome, int opId, int newStartTime, public void moveOperation(Chromosome chromosome, List<Integer> opIds, int newStartTime,
Long newMachineId, GlobalParam globalParam) { Long newMachineId, GlobalParam globalParam) {
List<Entry> allOperations = chromosome.getAllOperations(); List<Entry> allOperations = chromosome.getAllOperations();
Map<Integer, Integer> opTimeMap = chromosome.getResult().stream()
.collect(Collectors.toMap(
GAScheduleResult::getOperationId,
r -> r.getStartTime()
));
for (Integer opId:opIds) {
// 获取目标结果和工序 // 获取目标结果和工序
GAScheduleResult targetResult = chromosome.getResult().stream() GAScheduleResult targetResult = chromosome.getResult().stream()
.filter(r -> r.getOperationId() == opId) .filter(r -> r.getOperationId() == opId)
...@@ -69,13 +79,31 @@ public class ScheduleOperationService { ...@@ -69,13 +79,31 @@ public class ScheduleOperationService {
chromosome.getMachineSelection().set(globalOpIndex, machineOptionIndex); chromosome.getMachineSelection().set(globalOpIndex, machineOptionIndex);
if(targetOp.getSequence()==1) {
opTimeMap.put(opId, newStartTime);
}else {
Entry targetOp1 = allOperations.stream()
.filter(o -> o.getGroupId() == targetOp.getGroupId()
&&o.getSequence()==targetOp.getSequence()-1)
.findFirst()
.orElseThrow(() -> new NoSuchElementException("Operation not found: " + opId));
GAScheduleResult targetResult1 = chromosome.getResult().stream()
.filter(r -> r.getOperationId() == targetOp1.getId())
.findFirst()
.orElseThrow(() -> new NoSuchElementException("Operation not found: " + opId));
if(targetResult1.getStartTime()<newStartTime)
{
opTimeMap.put(opId, newStartTime);
}else {
opTimeMap.put(opId, targetResult1.getStartTime()+1);
}
}
newStartTime=newStartTime+(int)targetResult.getProcessingTime();
}
// 生成新的工序顺序 // 生成新的工序顺序
Map<Integer, Integer> opTimeMap = chromosome.getResult().stream()
.collect(Collectors.toMap(
GAScheduleResult::getOperationId,
r -> r.getOperationId() == opId ? newStartTime : r.getStartTime()
));
List<Integer> operationSequencing = allOperations.stream() List<Integer> operationSequencing = allOperations.stream()
.sorted((op1, op2) -> { .sorted((op1, op2) -> {
...@@ -96,7 +124,6 @@ public class ScheduleOperationService { ...@@ -96,7 +124,6 @@ public class ScheduleOperationService {
} }
public void SpiltOperation(Chromosome chromosome, int opId,Double[] splitCounts, GlobalParam globalParam) public void SpiltOperation(Chromosome chromosome, int opId,Double[] splitCounts, GlobalParam globalParam)
{ {
List<Entry> allOperations = chromosome.getAllOperations(); List<Entry> allOperations = chromosome.getAllOperations();
...@@ -693,9 +720,10 @@ public class ScheduleOperationService { ...@@ -693,9 +720,10 @@ public class ScheduleOperationService {
public void redecode(Chromosome chromosome,LocalDateTime baseTime, GlobalParam globalParam) public void redecode(Chromosome chromosome,LocalDateTime baseTime, GlobalParam globalParam)
{ {
MachineSchedulerService machineScheduler = new MachineSchedulerService(baseTime); MachineSchedulerService machineScheduler = new MachineSchedulerService(baseTime);
chromosome.setMachines(chromosome.getInitMachines());
GeneticDecoder decoder = new GeneticDecoder(globalParam,baseTime, chromosome.getInitMachines(), GeneticDecoder decoder = new GeneticDecoder(globalParam,baseTime, chromosome.getInitMachines(),
chromosome.getOrders(), null, machineScheduler,chromosome.getOrderMaterials()); chromosome.getOrders(), null, machineScheduler,chromosome.getOrderMaterials());
chromosome.setMachines(chromosome.getInitMachines());
chromosome.setResultOld(ProductionDeepCopyUtil.deepCopyList(chromosome.getResult())); chromosome.setResultOld(ProductionDeepCopyUtil.deepCopyList(chromosome.getResult()));
chromosome.getResult().clear(); chromosome.getResult().clear();
......
...@@ -301,6 +301,7 @@ order.setDueDate(LocalDateTime.of(2025, 12, 1,0,0,0)); ...@@ -301,6 +301,7 @@ order.setDueDate(LocalDateTime.of(2025, 12, 1,0,0,0));
public Chromosome execute2(String SceneId) { public Chromosome execute2(String SceneId) {
try { try {
SceneId="6AF8001449FC4D20A3C9992EC24CBF05"; SceneId="6AF8001449FC4D20A3C9992EC24CBF05";
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));
...@@ -325,7 +326,7 @@ order.setDueDate(LocalDateTime.of(2025, 12, 1,0,0,0)); ...@@ -325,7 +326,7 @@ order.setDueDate(LocalDateTime.of(2025, 12, 1,0,0,0));
// 3. 构建订单-工序数据 // 3. 构建订单-工序数据
List<Order> orders=InitOrder(ProdLaunchOrders); List<Order> orders=InitOrder(ProdLaunchOrders);
List<Material> Materials= InitMaterial(); List<Material> Materials= null;//InitMaterial();
Map<Integer,Object> list= InitEntrys(SceneId,ProdEquipments,orders); Map<Integer,Object> list= InitEntrys(SceneId,ProdEquipments,orders);
List<Entry> entrys=(List<Entry>)list.get(1); List<Entry> entrys=(List<Entry>)list.get(1);
...@@ -404,7 +405,7 @@ order.setDueDate(LocalDateTime.of(2025, 12, 1,0,0,0)); ...@@ -404,7 +405,7 @@ order.setDueDate(LocalDateTime.of(2025, 12, 1,0,0,0));
return chromosome; return chromosome;
} }
public Chromosome Move(String SceneId,int opId, LocalDateTime newStartTime, public Chromosome Move(String SceneId,List<Integer> opId, LocalDateTime newStartTime,
Long newMachineId) { Long newMachineId) {
...@@ -604,7 +605,7 @@ order.setDueDate(LocalDateTime.of(2025, 12, 1,0,0,0)); ...@@ -604,7 +605,7 @@ order.setDueDate(LocalDateTime.of(2025, 12, 1,0,0,0));
public void WriteScheduleSummary(Chromosome schedule) { public void WriteScheduleSummary(Chromosome schedule) {
// 写入日志 // 写入日志
FileHelper.writeLogFile(String.format("\n=== Schedule Summary === %f", schedule.getFitness())); FileHelper.writeLogFile("\n=== Schedule Summary === ");
FileHelper.writeLogFile(String.format("Operation: %s", schedule.getOperationStr())); FileHelper.writeLogFile(String.format("Operation: %s", schedule.getOperationStr()));
FileHelper.writeLogFile(String.format("Makespan: %f minutes", schedule.getMakespan())); FileHelper.writeLogFile(String.format("Makespan: %f minutes", schedule.getMakespan()));
FileHelper.writeLogFile(String.format("Total Tardiness: %f hours", schedule.getDelayTime())); FileHelper.writeLogFile(String.format("Total Tardiness: %f hours", schedule.getDelayTime()));
......
...@@ -26,9 +26,14 @@ public class PlanResultServiceTest { ...@@ -26,9 +26,14 @@ public class PlanResultServiceTest {
@Test @Test
public void testExecute() { public void testExecute() {
LocalDateTime t= LocalDateTime.of(2025, 10, 31, 6, 51, 11); // planResultService.execute2("");
planResultService.Move("86ED02C9BAB54BB6B8F413938A3F2869",1,t,3402L); LocalDateTime t= LocalDateTime.of(2025, 11, 15, 6, 51, 11);
List<Integer> opids=new ArrayList<>();
opids.add(1);
planResultService.Move("B571EF6682DB463AB2977B1055A74112",opids,t,3403L);
// planResultService.Move("B571EF6682DB463AB2977B1055A74112",2,t,3243L);
} }
@Test @Test
......
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