Commit b779f0fc authored by Tong Li's avatar Tong Li

Merge remote-tracking branch 'origin/tl'

# Conflicts:
#	result/chromosome_result_B571EF6682DB463AB2977B1055A74112.json
#	schedule_log.txt
#	src/main/java/com/aps/service/Algorithm/GeneticDecoder.java
parents a5a1c4a7 fc778193
......@@ -12,6 +12,7 @@ import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Slf4j
@UtilityClass
......@@ -262,4 +263,25 @@ public class ParamValidator {
})
.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 {
// 1. 提取参数
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", "新的开始时间");
Long newMachineId = ParamValidator.getLong(params, "newMachineId", "新机器ID");
// 2. 验证场景
ParamValidator.validateSceneExists(sceneService, sceneId);
List<Integer> opids = ParamValidator.convertToIntArray(opid, "操作IDS");
// 3. 执行业务
Chromosome result = planResultService.Move(sceneId, opid, newStartTime, newMachineId);
Chromosome result = planResultService.Move(sceneId, opids, newStartTime, newMachineId);
return R.ok("移动成功");
}
......
......@@ -66,7 +66,7 @@ public class GeneticAlgorithm {
}
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);
......@@ -183,8 +183,7 @@ public class GeneticAlgorithm {
}
}
best.setBaseTime(param.getBaseTime());
best.setInitMachines(ProductionDeepCopyUtil.deepCopyList(machines));
best.setOrders(orders);
best.setOrderMaterials(orderMaterials);
best.setOperatRel(_entryRel);
// 步骤3:返回最优解
......
......@@ -41,14 +41,8 @@ public class GeneticDecoder {
private List<OrderMaterialRequirement> orderMaterials;
// 添加静态实例引用
private static DiscreteParameterMatrixService staticDiscreteParameterMatrixService;
// 设置静态服务实例的方法
public static void setDiscreteParameterMatrixService(DiscreteParameterMatrixService service) {
staticDiscreteParameterMatrixService = service;
}
private DiscreteParameterMatrixService discreteParameterMatrixService;
public GeneticDecoder(GlobalParam globalParam,LocalDateTime baseTime, List<Machine> machines, List<Order> orders,
List<Material> materials, MachineSchedulerService machineScheduler, List<OrderMaterialRequirement> _orderMaterials) {
this.baseTime = baseTime;
......@@ -84,7 +78,7 @@ public class GeneticDecoder {
chromosome.setTotalChangeoverTime(cachedResult.getTotalChangeoverTime());
chromosome.setMachineLoadStd(cachedResult.getMachineLoadStd());
chromosome.setDelayTime(cachedResult.getDelayTime());
chromosome.setResult(ProductionDeepCopyUtil.deepCopyList(cachedResult.getResult(), GAScheduleResult.class));
chromosome.setResult(cachedResult.getResult());
// Chromosome chromosomen= ProductionDeepCopyUtil.deepCopy(cachedResult);
return chromosome;
......@@ -364,7 +358,8 @@ public class GeneticDecoder {
int preTime = machineOption.getPreTime();
int setupTime = calculateSetupTime(chromosome.getResult(), operation, machine, machineOption);
System.out.println(" 处理时间: " + processingTime + ", 后处理: " + teardownTime +
", 前处理: " + preTime + ", 换型: " + setupTime);
// 确定任务的最早开始时间(基于前一道工序的完整结束时间,包含后处理)
......@@ -404,16 +399,21 @@ public class GeneticDecoder {
earliestStartTime = Math.max(earliestStartTime, machineAvailableTime);
}
System.out.println(" 最终最早开始时间: " + earliestStartTime +
" (前序工序结束含后处理: " + prevOperationEndTime + ", 设备可用: " +
(lastGeneOnMachine != null ? lastGeneOnMachine.getEndTime() : 0) +
", 换型: " + setupTime + ")");
// 根据换型模式调整处理时间
// int processingTimeForScheduling;
if (_globalParam.is_smoothSetup()) {
// 平滑模式:只需要安排主处理时间
// processingTimeForScheduling = processingTimeTotal;
System.out.println(" 平滑模式:安排主处理时间 " + processingTime + " 分钟");
} else {
// 标准模式:需要安排主处理时间+换型时间
processingTimeTotal = processingTimeTotal + setupTime;
System.out.println(" 标准模式:安排主处理+" + setupTime + "分钟换型=" + processingTimeTotal + "分钟");
}
GAScheduleResult existingResult = chromosome.getResultOld().stream().filter(r-> r.getOperationId() == operation.Id).findFirst().orElse(null);
......@@ -807,6 +807,7 @@ public class GeneticDecoder {
.orElse(null);
if (lastGeneOnMachine == null) {
System.out.println("设备 " + machine.getId() + " 上无历史任务,换型时间为0");
return 0;
}
Entry prev= _allOperations.stream().
......@@ -818,12 +819,13 @@ public class GeneticDecoder {
//离散参数
// prev.getDiscreteParameter()
setupTime = (prev.getProductId() != operation.getProductId())
? (int) discreteParameterMatrixService.getDiscreteParameterMatrixValue(prev, operation)
: 0;
if (staticDiscreteParameterMatrixService != null) {
setupTime = (int) staticDiscreteParameterMatrixService.getDiscreteParameterMatrixValue(prev, operation);
} else {
// 添加兜底处理,避免空指针异常
setupTime = 0;
if (setupTime > 0) {
System.out.println("设备 " + machine.getId() + " 需要换型,因为产品从 " + prev.getProductId() + " 变更为 " + operation.getProductId());
}
}
......
......@@ -136,11 +136,13 @@ public class GeneticOperations {
Chromosome child1 = new Chromosome();
child1.setOrders(parent1.getOrders());
child1.setMachines(parent1.getMachines());
child1.setMachineSelection(child1Ms);
child1.setOperationSequencing(child1Os);
Chromosome child2 = new Chromosome();
child2.setOrders(parent1.getOrders());
child2.setMachines(parent1.getMachines());
child2.setMachineSelection(child2Ms);
child2.setOperationSequencing(child2Os);
......
package com.aps.service.Algorithm;
import com.aps.common.util.ProductionDeepCopyUtil;
import com.aps.entity.Algorithm.Chromosome;
import com.aps.entity.Algorithm.GlobalOperationInfo;
import com.aps.entity.Algorithm.ScheduleParams;
import com.aps.entity.basic.Entry;
import com.aps.entity.basic.GlobalParam;
import com.aps.entity.basic.MachineOption;
import com.aps.entity.basic.Order;
import com.aps.entity.basic.*;
import java.util.*;
import java.util.stream.Collectors;
......@@ -22,10 +20,13 @@ public class Initialization {
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;
_globalParam= globalParam;
orders=_orders;
machines=_machines;
}
/**
* 预生成全局工序列表(按“订单0→订单1→…+订单内工序1→2→…”排序,分配GlobalOpId)
......@@ -59,7 +60,7 @@ int populationSize=param.getPopulationSize();
.parallel() // 开启并行
.forEach(i -> {
Chromosome chromo = new Chromosome(); // 初始化染色体
chromo.setInitMachines(ProductionDeepCopyUtil.deepCopyList(machines));
chromo.setOrders(orders);
// 全局选择(GS):按GlobalOpId顺序生成MachineSelection
if (i < gsCount) {
......
package com.aps.service.Algorithm;
import com.aps.common.util.ProductionDeepCopyUtil;
import com.aps.entity.Algorithm.Chromosome;
import com.aps.entity.Algorithm.GAScheduleResult;
import com.aps.entity.basic.Machine;
......@@ -128,7 +129,7 @@ public class KpiCalculator {
private void calculateMachine() {
// 按设备ID分组工序列表(对应C# GroupBy + ToList)
List<GAScheduleResult> list= chromosome.getResult();
List<GAScheduleResult> list= chromosome.getResult() ;
Map<Long, List<GAScheduleResult>> machineTasks = list.stream()
.collect(Collectors.groupingBy(GAScheduleResult::getMachineId));
......
......@@ -33,9 +33,19 @@ public class ScheduleOperationService {
* @param newStartTime 新开始时间
* @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) {
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()
.filter(r -> r.getOperationId() == opId)
......@@ -69,13 +79,31 @@ public class ScheduleOperationService {
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()
.sorted((op1, op2) -> {
......@@ -96,7 +124,6 @@ public class ScheduleOperationService {
}
public void SpiltOperation(Chromosome chromosome, int opId,Double[] splitCounts, GlobalParam globalParam)
{
List<Entry> allOperations = chromosome.getAllOperations();
......@@ -693,9 +720,10 @@ public class ScheduleOperationService {
public void redecode(Chromosome chromosome,LocalDateTime baseTime, GlobalParam globalParam)
{
MachineSchedulerService machineScheduler = new MachineSchedulerService(baseTime);
chromosome.setMachines(chromosome.getInitMachines());
GeneticDecoder decoder = new GeneticDecoder(globalParam,baseTime, chromosome.getInitMachines(),
chromosome.getOrders(), null, machineScheduler,chromosome.getOrderMaterials());
chromosome.setMachines(chromosome.getInitMachines());
chromosome.setResultOld(ProductionDeepCopyUtil.deepCopyList(chromosome.getResult()));
chromosome.getResult().clear();
......
......@@ -301,6 +301,7 @@ order.setDueDate(LocalDateTime.of(2025, 12, 1,0,0,0));
public Chromosome execute2(String SceneId) {
try {
SceneId="6AF8001449FC4D20A3C9992EC24CBF05";
ScheduleParams param = new ScheduleParams();
param.setBaseTime(LocalDateTime.of(2025, 11, 1, 0, 0, 0));
......@@ -325,7 +326,7 @@ order.setDueDate(LocalDateTime.of(2025, 12, 1,0,0,0));
// 3. 构建订单-工序数据
List<Order> orders=InitOrder(ProdLaunchOrders);
List<Material> Materials= InitMaterial();
List<Material> Materials= null;//InitMaterial();
Map<Integer,Object> list= InitEntrys(SceneId,ProdEquipments,orders);
List<Entry> entrys=(List<Entry>)list.get(1);
......@@ -404,7 +405,7 @@ order.setDueDate(LocalDateTime.of(2025, 12, 1,0,0,0));
return chromosome;
}
public Chromosome Move(String SceneId,int opId, LocalDateTime newStartTime,
public Chromosome Move(String SceneId,List<Integer> opId, LocalDateTime newStartTime,
Long newMachineId) {
......@@ -604,7 +605,7 @@ order.setDueDate(LocalDateTime.of(2025, 12, 1,0,0,0));
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("Makespan: %f minutes", schedule.getMakespan()));
FileHelper.writeLogFile(String.format("Total Tardiness: %f hours", schedule.getDelayTime()));
......
......@@ -26,9 +26,14 @@ public class PlanResultServiceTest {
@Test
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
......
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