Commit 3c9b2b71 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/ScheduleOperationService.java
parents fd7c1d51 df2cb5cb
...@@ -197,7 +197,7 @@ public class ResourceGanttController { ...@@ -197,7 +197,7 @@ public class ResourceGanttController {
opids.add(opid); opids.add(opid);
// 3. 执行业务 // 3. 执行业务
Chromosome result = planResultService.Move(sceneId, opids, newStartTime, newMachineId); Chromosome result = planResultService.Move(sceneId, opids, newStartTime, newMachineId,0);
return R.ok("移动成功"); return R.ok("移动成功");
} }
...@@ -219,7 +219,7 @@ public class ResourceGanttController { ...@@ -219,7 +219,7 @@ public class ResourceGanttController {
// 3. 执行业务 // 3. 执行业务
Chromosome result = planResultService.Move(sceneId, opids, newStartTime, newMachineId); Chromosome result = planResultService.Move(sceneId, opids, newStartTime, newMachineId,0);
return R.ok("移动成功"); return R.ok("移动成功");
} }
...@@ -245,7 +245,7 @@ public class ResourceGanttController { ...@@ -245,7 +245,7 @@ public class ResourceGanttController {
opids.add(opid); opids.add(opid);
// 3. 执行业务 // 3. 执行业务
Chromosome result = planResultService.Move(sceneId, opids, newStartTime, 0L); Chromosome result = planResultService.Move(sceneId, opids, newStartTime, 0L,1);
return R.ok("固定开始日期成功"); return R.ok("固定开始日期成功");
} }
......
...@@ -29,7 +29,8 @@ public class GAScheduleResult { ...@@ -29,7 +29,8 @@ public class GAScheduleResult {
private int teardownTime; //后处理 private int teardownTime; //后处理
private double Quantity; // 批次大小(订单可拆分) private double Quantity; // 批次大小(订单可拆分)
private List<ScheduleResultDetail> GeneDetails; // 时间详情 private List<ScheduleResultDetail> GeneDetails; // 时间详情
private int designatedStartTime = -1; // 设计开始时间(默认-1) private int designatedStartTime = -1; // 固定开始时间(默认-1)
private int lockStartTime = 0; // 是否固定开始时间(默认-1)
private Long forcedMachineId =-1L; // 强制分配的设备ID(-1表示无强制) private Long forcedMachineId =-1L; // 强制分配的设备ID(-1表示无强制)
private boolean IsLocked=false; private boolean IsLocked=false;
private double OneTime; // 单件工时 private double OneTime; // 单件工时
......
...@@ -110,18 +110,27 @@ public class GeneticAlgorithm { ...@@ -110,18 +110,27 @@ public class GeneticAlgorithm {
FileHelper.writeLogFile("初始化批量解码-----------开始-------"); FileHelper.writeLogFile("初始化批量解码-----------开始-------");
Chromosomedecode(param,allOperations,globalOpList,population); Chromosomedecode(param,allOperations,globalOpList,population);
FileHelper.writeLogFile("初始化批量解码-----------结束-------"); FileHelper.writeLogFile("初始化批量解码-----------结束-------");
List<List<Chromosome>> fronts = _nsgaIIUtils.parallelFastNonDominatedSort(population); List<List<Chromosome>> combinedFronts = _nsgaIIUtils.parallelFastNonDominatedSort(population);
int ordercount = globalOpList.stream() int ordercount = globalOpList.stream()
.mapToInt(GlobalOperationInfo::getGroupId) .mapToInt(GlobalOperationInfo::getGroupId)
.max() .max()
.orElse(0); .orElse(0);
Chromosome best=GetBest(fronts,"初始"); Chromosome best=GetBest(combinedFronts,"初始");
// best=fronts.get(0).stream() // best=fronts.get(0).stream()
// .max(Comparator.comparingDouble(Chromosome::getFitness)) // .max(Comparator.comparingDouble(Chromosome::getFitness))
// .orElse(null); // .orElse(null);
double bestFitness=best.getFitness(); double bestFitness=best.getFitness();
if(population.size()<param.getTournamentSize())
{
LocalDateTime endtime1=LocalDateTime.now();
FileHelper.writeLogFile(String.format("排产-------总方案数%d----结束---%s----耗时%d----",population.size(),allOperations.get(0).getSceneId(),DateTimeUtil.diffDuration(starttime,endtime1).getSeconds()) );
return best;
}
int Iteration=0; int Iteration=0;
// 步骤2:迭代进化 // 步骤2:迭代进化
FileHelper.writeLogFile("迭代进化-----------开始-------"+param.getMaxIterations()); FileHelper.writeLogFile("迭代进化-----------开始-------"+param.getMaxIterations());
...@@ -156,6 +165,7 @@ public class GeneticAlgorithm { ...@@ -156,6 +165,7 @@ public class GeneticAlgorithm {
List<Chromosome> nextPopulation = new ArrayList<>(); List<Chromosome> nextPopulation = new ArrayList<>();
for (int i = 0; i < selected.size(); i += 2) { for (int i = 0; i < selected.size(); i += 2) {
if (i + 1 >= selected.size()) { if (i + 1 >= selected.size()) {
selected.get(i).setID(UUID.randomUUID().toString());
nextPopulation.add(selected.get(i)); nextPopulation.add(selected.get(i));
break; break;
} }
...@@ -179,6 +189,7 @@ public class GeneticAlgorithm { ...@@ -179,6 +189,7 @@ public class GeneticAlgorithm {
for (Chromosome chromosome : nextPopulation) { for (Chromosome chromosome : nextPopulation) {
if (rnd.nextDouble() < param.getMutationProb()) { if (rnd.nextDouble() < param.getMutationProb()) {
Chromosome chromosome1= ProductionDeepCopyUtil.deepCopy(chromosome,Chromosome.class); Chromosome chromosome1= ProductionDeepCopyUtil.deepCopy(chromosome,Chromosome.class);
chromosome1.setID(UUID.randomUUID().toString());
geneticOps.mutate(chromosome1, globalOpList); geneticOps.mutate(chromosome1, globalOpList);
nextPopulation1.add(chromosome1); nextPopulation1.add(chromosome1);
} }
...@@ -200,12 +211,15 @@ public class GeneticAlgorithm { ...@@ -200,12 +211,15 @@ public class GeneticAlgorithm {
// List<Chromosome> elites = population1.subList(0, param.getElitismCount()); // List<Chromosome> elites = population1.subList(0, param.getElitismCount());
List<Chromosome> newPopulation = new ArrayList<>(); List<Chromosome> newPopulation = new ArrayList<>();
newPopulation.addAll(population); //保留一定数量的上一代
List<Chromosome> populationcopy = _nsgaIIUtils.selectNextPopulation(combinedFronts, param.getPopulationSize()/5);
newPopulation.addAll(populationcopy);
newPopulation.addAll(nextPopulation); newPopulation.addAll(nextPopulation);
newPopulation= chromosomeDistinct1(newPopulation); newPopulation= chromosomeDistinct1(newPopulation);
FileHelper.writeLogFile("非支配排序-----------开始-------"); FileHelper.writeLogFile("非支配排序-----------开始-------");
// 2.7 非支配排序 // 2.7 非支配排序
List<List<Chromosome>> combinedFronts = _nsgaIIUtils.parallelFastNonDominatedSort(newPopulation); combinedFronts = _nsgaIIUtils.parallelFastNonDominatedSort(newPopulation);
FileHelper.writeLogFile("非支配排序-----------结束-------"); FileHelper.writeLogFile("非支配排序-----------结束-------");
// 2.8 选择下一代种群 // 2.8 选择下一代种群
population = _nsgaIIUtils.selectNextPopulation(combinedFronts, param.getPopulationSize()); population = _nsgaIIUtils.selectNextPopulation(combinedFronts, param.getPopulationSize());
...@@ -275,6 +289,14 @@ public class GeneticAlgorithm { ...@@ -275,6 +289,14 @@ public class GeneticAlgorithm {
} }
private List<Chromosome> chromosomeDistinct(List<Chromosome> population) private List<Chromosome> chromosomeDistinct(List<Chromosome> population)
{ {
if(population==null)
{
FileHelper.writeLogFile(String.format("排产-----------方案数量---%d-------",0 ));
return population;
}
// population = population.stream() // population = population.stream()
// .collect(Collectors.toMap( // .collect(Collectors.toMap(
...@@ -290,6 +312,15 @@ public class GeneticAlgorithm { ...@@ -290,6 +312,15 @@ public class GeneticAlgorithm {
} }
private List<Chromosome> chromosomeDistinct1(List<Chromosome> population) private List<Chromosome> chromosomeDistinct1(List<Chromosome> population)
{ {
if(population==null)
{
FileHelper.writeLogFile(String.format("排产-----------方案数量---%d-------",0 ));
return population;
}
// List<Chromosome> population1 = population.stream().filter(t->t.getGeneStr()==null).collect(Collectors.toList());
population = population.stream() population = population.stream()
.collect(Collectors.toMap( .collect(Collectors.toMap(
...@@ -355,6 +386,7 @@ public class GeneticAlgorithm { ...@@ -355,6 +386,7 @@ public class GeneticAlgorithm {
if (chromosome==null){ if (chromosome==null){
System.out.println("chromosome==null"); System.out.println("chromosome==null");
return;
} }
chromosome.setResult(new CopyOnWriteArrayList<>()); chromosome.setResult(new CopyOnWriteArrayList<>());
......
...@@ -366,6 +366,12 @@ if(finishedOrder==null||finishedOrder.size()==0) ...@@ -366,6 +366,12 @@ if(finishedOrder==null||finishedOrder.size()==0)
int prevOperationEndTime, Chromosome chromosome) { int prevOperationEndTime, Chromosome chromosome) {
int processingTimeTotal =(int)(processingTime * operation.getQuantity()); int processingTimeTotal =(int)(processingTime * operation.getQuantity());
if(machine==null||operation.getMachineOptions()==null)
{
return 0;
}
MachineOption machineOption= operation.getMachineOptions().stream() MachineOption machineOption= operation.getMachineOptions().stream()
.filter(t->t.getMachineId()==machine.getId()) .filter(t->t.getMachineId()==machine.getId())
.findFirst().orElse(null); .findFirst().orElse(null);
...@@ -508,8 +514,10 @@ if(finishedOrder==null||finishedOrder.size()==0) ...@@ -508,8 +514,10 @@ if(finishedOrder==null||finishedOrder.size()==0)
result.setPreTime(preTime); result.setPreTime(preTime);
result.setTeardownTime(teardownTime); result.setTeardownTime(teardownTime);
if(existingResult!=null) { if(existingResult!=null) {
result.setDesignatedStartTime(existingResult.getDesignatedStartTime()); if(existingResult.getLockStartTime()==1) {
result.setLockStartTime(existingResult.getLockStartTime());
result.setDesignatedStartTime(existingResult.getDesignatedStartTime());
}
} }
......
...@@ -33,7 +33,7 @@ public class ScheduleOperationService { ...@@ -33,7 +33,7 @@ public class ScheduleOperationService {
* @param newMachineId 新设备ID * @param newMachineId 新设备ID
*/ */
public void moveOperation(Chromosome chromosome, List<Integer> opIds, int newStartTime, public void moveOperation(Chromosome chromosome, List<Integer> opIds, int newStartTime,
Long newMachineId, GlobalParam globalParam) { Long newMachineId, GlobalParam globalParam,int lockStartTime) {
List<Entry> allOperations = chromosome.getAllOperations(); List<Entry> allOperations = chromosome.getAllOperations();
int newStartTime1=newStartTime; int newStartTime1=newStartTime;
Map<Integer, Integer> opTimeMap = chromosome.getResult().stream() Map<Integer, Integer> opTimeMap = chromosome.getResult().stream()
...@@ -82,6 +82,7 @@ Integer newMachineId1=newMachineId.intValue(); ...@@ -82,6 +82,7 @@ Integer newMachineId1=newMachineId.intValue();
} }
// 设置约束 // 设置约束
targetResult.setDesignatedStartTime(newStartTime); targetResult.setDesignatedStartTime(newStartTime);
targetResult.setLockStartTime(lockStartTime);
if(targetOp.getSequence()==1) { if(targetOp.getSequence()==1) {
...@@ -145,167 +146,20 @@ Integer newMachineId1=newMachineId.intValue(); ...@@ -145,167 +146,20 @@ Integer newMachineId1=newMachineId.intValue();
} }
public void UnlockStartTime(Chromosome chromosome, int opId) {
GAScheduleResult targetResults = chromosome.getResult().stream()
public void editMachineOption(Chromosome chromosome,Entry operation, .filter(r ->r.getOperationId() == opId).findFirst()
Long newMachineId, GlobalParam globalParam) {
List<Entry> allOperations = chromosome.getAllOperations();
Map<Integer, Integer> opTimeMap = chromosome.getResult().stream()
.collect(Collectors.toMap(
GAScheduleResult::getOperationId,
r -> r.getStartTime()
));
Integer newMachineId1=newMachineId.intValue();
int opId = operation.getId();
// 获取目标结果和工序
GAScheduleResult targetResult = chromosome.getResult().stream()
.filter(r -> r.getOperationId() == opId)
.findFirst()
.orElseThrow(() -> new NoSuchElementException("Operation not found: " + opId));
Entry targetOp = allOperations.stream()
.filter(o -> o.getId() == opId)
.findFirst()
.orElseThrow(() -> new NoSuchElementException("Operation not found: " + opId));
if(newMachineId1!=0) {
int machineOptionIndex = targetOp.getMachineOptions().stream()
.map(MachineOption::getMachineId)
.collect(Collectors.toList())
.indexOf(newMachineId) + 1;
if (machineOptionIndex == 0) {
throw new NoSuchElementException("Machine not found: " + newMachineId);
}
// 更新设备选择序列
int globalOpIndex = chromosome.getGlobalOpList().stream()
.filter(g -> g.getOp().getId() == opId)
.findFirst()
.map(GlobalOperationInfo::getGlobalOpId)
.orElseThrow(() -> new NoSuchElementException("Global operation not found: " + opId));
chromosome.getMachineSelection().set(globalOpIndex, machineOptionIndex);
targetResult.setForcedMachineId(newMachineId);
}
List<Integer> operationSequencing = allOperations.stream()
.sorted((op1, op2) -> {
int time1 = opTimeMap.getOrDefault(op1.getId(), Integer.MAX_VALUE);
int time2 = opTimeMap.getOrDefault(op2.getId(), Integer.MAX_VALUE);
if (time1 != time2) {
return Integer.compare(time1, time2);
} else {
return Integer.compare(op1.getSequence(), op2.getSequence());
}
})
.map(Entry::getGroupId)
.collect(Collectors.toList());
chromosome.setOperationSequencing(operationSequencing);
// 重新解码
redecode(chromosome, chromosome.getBaseTime(), globalParam);
}
public void editMachine(Chromosome chromosome,Entry operation,
Long newMachineId, GlobalParam globalParam) {
List<Entry> allOperations = chromosome.getAllOperations();
Map<Integer, Integer> opTimeMap = chromosome.getResult().stream()
.collect(Collectors.toMap(
GAScheduleResult::getOperationId,
r -> r.getStartTime()
));
Integer newMachineId1=newMachineId.intValue();
int opId = operation.getId();
// 获取目标结果和工序
GAScheduleResult targetResult = chromosome.getResult().stream()
.filter(r -> r.getOperationId() == opId)
.findFirst()
.orElseThrow(() -> new NoSuchElementException("Operation not found: " + opId));
Entry targetOp = allOperations.stream()
.filter(o -> o.getId() == opId)
.findFirst()
.orElseThrow(() -> new NoSuchElementException("Operation not found: " + opId)); .orElseThrow(() -> new NoSuchElementException("Operation not found: " + opId));
if(newMachineId1!=0) { targetResults.setLockStartTime(0);
int machineOptionIndex = targetOp.getMachineOptions().stream() targetResults.setDesignatedStartTime(0);
.map(MachineOption::getMachineId)
.collect(Collectors.toList())
.indexOf(newMachineId) + 1;
if (machineOptionIndex == 0) {
throw new NoSuchElementException("Machine not found: " + newMachineId);
}
// 更新设备选择序列
int globalOpIndex = chromosome.getGlobalOpList().stream()
.filter(g -> g.getOp().getId() == opId)
.findFirst()
.map(GlobalOperationInfo::getGlobalOpId)
.orElseThrow(() -> new NoSuchElementException("Global operation not found: " + opId));
chromosome.getMachineSelection().set(globalOpIndex, machineOptionIndex);
targetResult.setForcedMachineId(newMachineId);
}
List<Integer> operationSequencing = allOperations.stream()
.sorted((op1, op2) -> {
int time1 = opTimeMap.getOrDefault(op1.getId(), Integer.MAX_VALUE);
int time2 = opTimeMap.getOrDefault(op2.getId(), Integer.MAX_VALUE);
if (time1 != time2) {
return Integer.compare(time1, time2);
} else {
return Integer.compare(op1.getSequence(), op2.getSequence());
}
})
.map(Entry::getGroupId)
.collect(Collectors.toList());
chromosome.setOperationSequencing(operationSequencing);
// 重新解码
redecode(chromosome, chromosome.getBaseTime(), globalParam);
} }
/**
* 增加设备维修保养
* @param chromosome 染色体对象
*/
/**
* 增加设备维修保养
* @param chromosome 染色体对象
*/
public void AddMaintenanceWindow(Chromosome chromosome,Long machineId, MaintenanceWindow maintenanceWindow, public void AddMaintenanceWindow(Chromosome chromosome,Long machineId, MaintenanceWindow maintenanceWindow,
GlobalParam globalParam) { GlobalParam globalParam) {
...@@ -865,12 +719,16 @@ Integer newMachineId1=newMachineId.intValue(); ...@@ -865,12 +719,16 @@ Integer newMachineId1=newMachineId.intValue();
} }
public void LockOperation(Chromosome chromosome,int opId, boolean isLocked, GlobalParam globalParam) public void LockOperation(Chromosome chromosome,int opId, boolean isLocked, GlobalParam globalParam)
{ {
GAScheduleResult targetResult = chromosome.getResult().stream() Entry targetOp = chromosome.getAllOperations().stream()
.filter(r -> r.getOperationId() == opId) .filter(o -> o.getId() == opId)
.findFirst() .findFirst()
.orElseThrow(() -> new NoSuchElementException("Operation not found: " + opId)); .orElseThrow(() -> new NoSuchElementException("Operation not found: " + opId));
targetResult.setIsLocked(isLocked);
List<GAScheduleResult> targetResults = chromosome.getResult().stream()
.filter(r ->r.getGroupId()== targetOp.getGroupId()&&r.getOperationId() <= opId).collect(Collectors.toList());
targetResults.forEach(t->t.setIsLocked(isLocked));
// 若解锁,重新解码受影响工序 // 若解锁,重新解码受影响工序
if (!isLocked) if (!isLocked)
...@@ -1091,6 +949,7 @@ Integer newMachineId1=newMachineId.intValue(); ...@@ -1091,6 +949,7 @@ Integer newMachineId1=newMachineId.intValue();
CopyOnWriteArrayList<GAScheduleResult> Resultlock= chromosome.getResult().stream() CopyOnWriteArrayList<GAScheduleResult> Resultlock= chromosome.getResult().stream()
.filter(o -> o.isIsLocked() == true) .filter(o -> o.isIsLocked() == true)
.collect(Collectors.toCollection(CopyOnWriteArrayList::new)); .collect(Collectors.toCollection(CopyOnWriteArrayList::new));
chromosome.setResult(ProductionDeepCopyUtil.deepCopyList(Resultlock,GAScheduleResult.class)); chromosome.setResult(ProductionDeepCopyUtil.deepCopyList(Resultlock,GAScheduleResult.class));
......
...@@ -530,7 +530,7 @@ order.setDueDate(LocalDateTime.of(2025, 12, 1,0,0,0)); ...@@ -530,7 +530,7 @@ order.setDueDate(LocalDateTime.of(2025, 12, 1,0,0,0));
return chromosome; return chromosome;
} }
public Chromosome Move(String SceneId,List<Integer> opId, LocalDateTime newStartTime, public Chromosome Move(String SceneId,List<Integer> opId, LocalDateTime newStartTime,
Long newMachineId) { Long newMachineId,int lockStartTime) {
GlobalParam globalParam=new GlobalParam(); GlobalParam globalParam=new GlobalParam();
...@@ -540,8 +540,8 @@ order.setDueDate(LocalDateTime.of(2025, 12, 1,0,0,0)); ...@@ -540,8 +540,8 @@ order.setDueDate(LocalDateTime.of(2025, 12, 1,0,0,0));
// WriteScheduleSummary(chromosome); // WriteScheduleSummary(chromosome);
ScheduleOperationService ScheduleOperation=new ScheduleOperationService(); ScheduleOperationService ScheduleOperation=new ScheduleOperationService();
WriteScheduleSummary(chromosome); WriteScheduleSummary(chromosome);
ScheduleOperation.moveOperation(chromosome,opId, (int)ChronoUnit.SECONDS.between(chromosome.getBaseTime(), newStartTime),newMachineId, globalParam); ScheduleOperation.moveOperation(chromosome,opId, (int)ChronoUnit.SECONDS.between(chromosome.getBaseTime(), newStartTime),newMachineId, globalParam, lockStartTime);
WriteScheduleSummary(chromosome); // WriteScheduleSummary(chromosome);
_sceneService.saveChromosomeToFile(chromosome, SceneId); _sceneService.saveChromosomeToFile(chromosome, SceneId);
return chromosome; return chromosome;
......
...@@ -39,7 +39,7 @@ public class PlanResultServiceTest { ...@@ -39,7 +39,7 @@ public class PlanResultServiceTest {
// nsgaiiUtils.Test(); // nsgaiiUtils.Test();
//planResultService.execute2("C5FB5EF2A7334A0A92F826F4937E1008"); //planResultService.execute2("C5FB5EF2A7334A0A92F826F4937E1008");
// planResultService.execute2("726D4C1A712B4B1393175BD44B775B66"); // planResultService.execute2("726D4C1A712B4B1393175BD44B775B66");
planResultService.execute2("E81EE93EFA564CEB8594C61CAEEBCD53"); planResultService.execute2("265F24B6DF3C40E4B17D193B0CC8AAF2");
// 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<>();BCA6FA43FFA444D3952CF8F6E1EA291B // List<Integer> opids=new ArrayList<>();BCA6FA43FFA444D3952CF8F6E1EA291B
// opids.add(1); // opids.add(1);
......
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