Commit 9389ef11 authored by Tong Li's avatar Tong Li

遗传算法-优化编译逻辑

parent c925515f
......@@ -76,7 +76,7 @@ public class GeneticAlgorithm {
// 预生成全局工序列表(所有初始化方法共享同一顺序)
List<GlobalOperationInfo> globalOpList = initialization.generateGlobalOpList();
FileHelper.writeLogFile("初始化种群-----------开始-------");
FileHelper.writeLogFile("初始化种群-----------开始-------"+allOperations.get(0).getSceneId());
// 步骤1:初始化种群
List<Chromosome> population = initialization.generateInitialPopulation(param, globalOpList);
......@@ -99,8 +99,9 @@ public class GeneticAlgorithm {
.sorted((c1, c2) -> Double.compare(c2.getWeightedObjective(), c1.getWeightedObjective()))
.findFirst()
.orElse(null);
FileHelper.writeLogFile(String.format("KPI---%f-----初始-----%s----",best.getWeightedObjective(),best.getID()));
double bestFitness=0;
double bestFitness=best.getWeightedObjective();
int Iteration=0;
// 步骤2:迭代进化
FileHelper.writeLogFile("迭代进化-----------开始-------"+param.getMaxIterations());
......@@ -185,15 +186,19 @@ public class GeneticAlgorithm {
.sorted((c1, c2) -> Double.compare(c2.getWeightedObjective(), c1.getWeightedObjective()))
.findFirst()
.orElse(null);
FileHelper.writeLogFile(String.format("KPI---%f-----"+iter+"-----%s----",best.getWeightedObjective(),best.getID()));
if(bestFitness<best.getWeightedObjective())
{
bestFitness=best.getWeightedObjective();
Iteration=1;
}else {
Iteration++;
}
if(Iteration>10)
if(Iteration>5)
{
break;
}
......@@ -209,7 +214,7 @@ public class GeneticAlgorithm {
LocalDateTime endtime=LocalDateTime.now();
FileHelper.writeLogFile(String.format("排产-----------结束-------耗时%d----",DateTimeUtil.diffDuration(starttime,endtime).getSeconds()) );
FileHelper.writeLogFile(String.format("排产-----------结束---%s----耗时%d----",allOperations.get(0).getSceneId(),DateTimeUtil.diffDuration(starttime,endtime).getSeconds()) );
// 步骤3:返回最优解
return best;
......
......@@ -339,10 +339,7 @@ if(finishedOrder==null||finishedOrder.size()==0)
.filter(m -> m.getId() == machineId)
.findFirst()
.orElse(null);
if(machine==null)
{
int i=1+2;
}
// int changeoverTime =0; //(lastDiscreteParameter.isEmpty() ||
// lastDiscreteParameter.equals(currentOp.getDiscreteParameter())) ? 0 : 0;
......@@ -374,8 +371,8 @@ if(finishedOrder==null||finishedOrder.size()==0)
int preTime = machineOption.getPreTime();
int setupTime = calculateSetupTime(chromosome.getResult(), operation, machine, machineOption);
// FileHelper.writeLogFile (" 处理时间: " + processingTime + ", 后处理: " + teardownTime +
// ", 前处理: " + preTime + ", 换型: " + setupTime+ ", 数量: " + operation.getQuantity());
FileHelper.writeLogFile (" 处理时间: " + processingTime + ", 后处理: " + teardownTime +
", 前处理: " + preTime + ", 换型: " + setupTime+ ", 数量: " + operation.getQuantity());
// 确定任务的最早开始时间(基于前一道工序的完整结束时间,包含后处理)
......
......@@ -8,6 +8,7 @@ import com.aps.entity.basic.MachineOption;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
/**
* 作者:佟礼
......@@ -240,18 +241,29 @@ public class GeneticOperations {
private void mutateMachineSelection(Chromosome chromosome, List<GlobalOperationInfo> globalOpList, double baseMutationProb) {
// 计算变异位置数量
int r = Math.max(1, (int) (chromosome.getMachineSelection().size() * baseMutationProb));
List<Entry> entrys= allOperations.stream().filter(t->t.getMachineOptions().size()>1).collect(Collectors.toList());
if(entrys==null||entrys.size()==0)
{
return;
}
Integer count=entrys.size();
int r = Math.max(1, (int) (count * baseMutationProb));
int i = 0;
while (i < r) {
int pos = rnd.nextInt(chromosome.getMachineSelection().size());
int pos1 = rnd.nextInt(count);
// 获取对应工序
GlobalOperationInfo opInfo = globalOpList.get(pos);
Entry operation = opInfo.getOp();
Entry operation= entrys.get(pos1);
int pos = IntStream.range(0, globalOpList.size())
.filter(h -> operation.getId()==globalOpList.get(h).getOp().getId())
.findFirst().orElse(0);
int machineSeq = chromosome.getMachineSelection().get(pos);
if (operation.getMachineOptions().size() == 1) {
continue;
} else {
// 选择当前所选设备外最短加工时间的机器
List<MachineOption> optionalMachines = operation.getMachineOptions();
// 找到当前选中的机器
......@@ -265,7 +277,7 @@ public class GeneticOperations {
machineSeq = optionalMachines.indexOf(minLoadMachine) + 1;
chromosome.getMachineSelection().set(pos, machineSeq);
i++;
}
}
}
......
......@@ -40,6 +40,9 @@ public class NSGAIIUtils {
*/
public List<List<Chromosome>> parallelFastNonDominatedSort(List<Chromosome> population) {
int popSize = population.size();
// 步骤1:预处理 - 计算归一化目标值和加权目标值
preprocessObjectives(population);
if (popSize <= taskCount * 10) { // 小规模种群直接串行
return fastNonDominatedSort(population);
}
......
......@@ -766,20 +766,17 @@ order.setDueDate(LocalDateTime.of(2025, 12, 1,0,0,0));
// List<Machine> machines = _routingDataService.InitCalendarToAllMachines(SceneId, ProdEquipments, machineScheduler, IsUseCalendar);
List<Machine> machines= (List<Machine>)GlobalCacheUtil.get("machines");
if(machines!=null)
{
if(machines==null||machines.size()==0) {
return machines;
}
machines= (List<Machine>)redisUtils.get("machines");
if(machines!=null)
{
machines = (List<Machine>) redisUtils.get("machines");
if (machines == null||machines.size()==0) {
return machines;
machines = InitCalendarToAllMachines();
}
}
machines=InitCalendarToAllMachines();
FileHelper.writeLogFile("初始化设备日历-----------结束-------");
GlobalCacheUtil.put("machines",machines, 10, TimeUnit.MINUTES);
......@@ -1040,22 +1037,18 @@ private GlobalParam InitGlobalParam()
// .eq(MaterialInfo::getIsdeleted,0)
// .list();
List<Material> cmaterials= (List<Material>)GlobalCacheUtil.get("material");
if(cmaterials!=null)
{
materials= (List<Material>)GlobalCacheUtil.get("material");
if(materials==null||materials.size()==0) {
return cmaterials;
}
cmaterials= (List<Material>)redisUtils.get("material");
if(cmaterials!=null)
{
return cmaterials;
materials = (List<Material>) redisUtils.get("material");
if (materials == null||materials.size()==0) {
}
materials=getMaterials();
materials = getMaterials();
}
}
GlobalCacheUtil.put("material",materials, 10, TimeUnit.MINUTES);
FileHelper.writeLogFile("初始化物料-----------结束-------");
......
......@@ -28,10 +28,11 @@ public class PlanResultServiceTest {
public void testExecute() {
// planResultService.getMaterials();
// RangeSubtractUtil.test();
planResultService.testSceneChromsome("qwerty");
// planResultService.testSceneChromsome("qwerty");
// Chromosome chromosome= planResultService.moveChromosome("qwerty",3);
// planResultService.execute2("8835EF6E1C8A491D99B16DE59160ED64");
planResultService.execute2("8835EF6E1C8A491D99B16DE59160ED64");
// planResultService.execute2("BE037838EF074B07B87D7DE763107398");
// planResultService.execute2("31EC5BAF7F6B41DFB79AB031D81C53C0");
// LocalDateTime t= LocalDateTime.of(2025, 11, 15, 6, 51, 11);
// List<Integer> opids=new ArrayList<>();
// 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