Commit 7484ece0 authored by Tong Li's avatar Tong Li

变领域优化

parent b17004da
......@@ -595,53 +595,58 @@ public class GeneticOperations {
return indexWeights.get(CommonCalculator.getOsIndex(indexWeights));
}
public void DelOrder(Chromosome chromosome) {
List<Entry> allOperations = chromosome.getAllOperations();
List<GlobalOperationInfo> globalOpList= chromosome.getGlobalOpList();
if(chromosome.getOrders()==null||chromosome.getOrders().size()==0)
return;
List<Order> orders = chromosome.getOrders();
List<Integer> OperationSequencing= chromosome.getOperationSequencing();
log("删除前"+OperationSequencing.size(),true);
log(String.format("半成品订单-删除前,工序: %d,工序序列: %d,设备: %d,设备序列: %d",
allOperations.size(), OperationSequencing.size(),globalOpList.size(),chromosome.getMachineSelection().size()),true);
List<Integer> newoorderids= orders.stream()
.filter(t->t.isNewSfCreate())
.map(Order::getId)
.sorted(Comparator.reverseOrder())
.collect(Collectors.toList());
if(newoorderids!=null&&newoorderids.size()>0) {
List<Entry> allOperations = chromosome.getAllOperations();
List<GlobalOperationInfo> globalOpList = chromosome.getGlobalOpList();
List<Integer> OperationSequencing = chromosome.getOperationSequencing();
log(String.format("半成品订单-删除前,工序: %d,工序序列: %d,设备: %d,设备序列: %d",
allOperations.size(), OperationSequencing.size(), globalOpList.size(), chromosome.getMachineSelection().size()), true);
for (Integer id : newoorderids) {
List<Entry> sourceOps = allOperations.stream()
.filter(o -> o.getGroupId()==id)
.filter(o -> o.getGroupId() == id)
.sorted(Comparator.comparing(Entry::getSequence))
.collect(Collectors.toList());
for (Entry entry : sourceOps) {
OptionalInt index1 = IntStream.range(0, globalOpList.size())
.filter(h -> entry.getId()==globalOpList.get(h).getOp().getId())
.filter(h -> entry.getId() == globalOpList.get(h).getOp().getId())
.findFirst();
globalOpList.remove((int)index1.orElse(0));
globalOpList.remove((int) index1.orElse(0));
chromosome.getMachineSelection().remove((int)index1.orElse(0));
chromosome.getMachineSelection().remove((int) index1.orElse(0));
}
chromosome.getOperatRel().remove(id-1);
chromosome.getOperatRel().remove(id - 1);
}
allOperations.removeIf(t ->newoorderids.contains(t.getGroupId()));
OperationSequencing.removeIf(t ->newoorderids.contains(t));
allOperations.removeIf(t -> newoorderids.contains(t.getGroupId()));
OperationSequencing.removeIf(t -> newoorderids.contains(t));
AtomicInteger globalOpId = new AtomicInteger(0);
globalOpList.forEach(t-> {
globalOpList.forEach(t -> {
t.setGlobalOpId(globalOpId.getAndIncrement());
});
orders.removeIf(t ->newoorderids.contains(t.getId()));
orders.removeIf(t -> newoorderids.contains(t.getId()));
log(String.format("半成品订单-删除后,工序: %d,工序序列: %d,设备: %d,设备序列: %d",
allOperations.size(), OperationSequencing.size(), globalOpList.size(), chromosome.getMachineSelection().size()), true);
}
}
private void log(String message, boolean enableLogging) {
......
......@@ -21,7 +21,7 @@ public class VariableNeighborhoodSearch {
// ==================== 策略选择与轮换参数 ====================
private static final int MAX_CONSECUTIVE_SAME_STRATEGY = 4; // 同一策略最多连续使用次数,避免陷入局部最优
private static final int MAX_NEIGHBORHOODS = 4; // 使用的邻域数量(0-3:插单/交换/并行合并/重排)
private static final int MAX_NEIGHBORHOODS = 3; // 使用的邻域数量(0-3:插单/交换/并行合并/重排)
// ==================== 邻域搜索参数 ====================
private static final double HIGH_PRIORITY_GROUP_PROBABILITY = 0.7; // 优先选择高优先级工单的概率
......@@ -204,7 +204,7 @@ public class VariableNeighborhoodSearch {
neighborhoods.add(new NeighborhoodStructure("SmartBottleneckChange", this::ChangeMachineForBottleneckOpWrapper));
//瓶颈工序在相同设备上移动
neighborhoods.add(new NeighborhoodStructure("moveBottleneckSameMachine", this::moveBottleneckSameMachineWrapper));
neighborhoods.add(new NeighborhoodStructure("moveBottleneckSameMachine", this::moveBottleneckSameMachineWrapper));
//瓶颈工序往前移动
neighborhoods.add(new NeighborhoodStructure("MoveBottleneckForward", this::moveBottleneckForwardWrapper));
......@@ -238,7 +238,7 @@ public class VariableNeighborhoodSearch {
neighborhoods.add(new NeighborhoodStructure("MultiOperationSwap", this::multiOperationSwapWrapper));
// MultiMachineChange - 一次改变多个工序的机器选择
neighborhoods.add(new NeighborhoodStructure("MultiMachineChange", this::multiMachineChangeWrapper));
neighborhoods.add(new NeighborhoodStructure("MultiMachineChange", this::multiMachineChangeWrapper));
// HybridShake - 混合抖动:综合调整
neighborhoods.add(new NeighborhoodStructure("HybridShake", this::hybridShakeWrapper));
......@@ -807,7 +807,7 @@ public class VariableNeighborhoodSearch {
i + 1, op.getGroupId(), op.getOperationId(), op.getMachineId(),
boa.bottleneckScore, boa.waitTime, boa.machineWaitTime,
boa.onCriticalPath ? "是" : "否", boa.delayContribution
));
),false);
}
// 更新关键工序为识别出的瓶颈工序
......@@ -1236,6 +1236,7 @@ public class VariableNeighborhoodSearch {
return result != null ? result : null;
}
private Chromosome ChangeMachineForBottleneckOpWrapper(Chromosome chromosome) {
log("ChangeMachineForBottleneckOpWrapper");
List<GAScheduleResult> results = chromosome.getResult();
Map<Integer, Object> obj = buildPositionToEntryIndex(chromosome);
Map<Integer, Entry> positionToEntryIndex = (Map<Integer, Entry>) obj.get(1);
......@@ -1268,6 +1269,7 @@ public class VariableNeighborhoodSearch {
* 优先选择负载低的设备
*/
private Chromosome generateMachineChangeForSpecificOp(Chromosome chromosome, List<MachineOption> machineOptions, int idx) {
Chromosome neighbor=copyChromosome(chromosome);// ProductionDeepCopyUtil.deepCopy(chromosome, Chromosome.class);
List<Integer> ms = neighbor.getMachineSelection();
if (!ms.isEmpty() && idx >= 0 && idx < ms.size()) {
......@@ -1294,7 +1296,7 @@ public class VariableNeighborhoodSearch {
private Chromosome moveBottleneckForwardWrapper(Chromosome chromosome) {
List<GAScheduleResult> results = chromosome.getResult();
log("moveBottleneckForwardWrapper");
Map<String, Integer> positionIndex = buildPositionIndex(chromosome);
Map<Integer, Object> obj = buildPositionToEntryIndex(chromosome);
......@@ -1320,7 +1322,7 @@ public class VariableNeighborhoodSearch {
private Chromosome moveBottleneckSameMachineWrapper(Chromosome chromosome) {
List<GAScheduleResult> results = chromosome.getResult();
log("moveBottleneckSameMachineWrapper");
Map<String, Integer> positionIndex = buildPositionIndex(chromosome);
Map<Integer, Object> obj = buildPositionToEntryIndex(chromosome);
Map<Integer, Entry> positionToEntryIndex =(Map<Integer, Entry>)obj.get(1);
......@@ -1348,6 +1350,7 @@ public class VariableNeighborhoodSearch {
private Chromosome tryMoveBottleneckOpForward(Chromosome chromosome, List<GAScheduleResult> bottleneckOps,
Map<String, Integer> positionkey,List<List<Integer>> positionByPriority,Map<Integer, Entry> positionIndex,Map<String, Integer> machinePositionIndex,boolean isSameMachine) {
log(String.format("tryMoveBottleneckOpForward: isSameMachine=%b", isSameMachine));
log("tryMoveBottleneckOpForward:positionIndex"+positionIndex.size());
if (bottleneckOps.isEmpty()) {
log("tryMoveBottleneckOpForward: 瓶颈工序为空");
......@@ -1447,7 +1450,7 @@ public class VariableNeighborhoodSearch {
private Chromosome moveNonBottleneckBackwardWrapper(Chromosome chromosome) {
List<GAScheduleResult> results = chromosome.getResult();
log("moveNonBottleneckBackwardWrapper");
Map<String, Integer> positionIndex = buildPositionIndex(chromosome);
Map<Integer, Object> obj = buildPositionToEntryIndex(chromosome);
Map<Integer, Entry> positionToEntryIndex =(Map<Integer, Entry>)obj.get(1);
......@@ -1473,6 +1476,8 @@ public class VariableNeighborhoodSearch {
Map<Integer, Entry> positionToEntryIndex
) {
log(String.format("tryMoveNonBottleneckOpBackward: 瓶颈设备ID=%d", bottleneckMachineId));
log("tryMoveNonBottleneckOpBackward:positionIndex"+positionIndex.size());
log("tryMoveNonBottleneckOpBackward:positionToEntryIndex"+positionToEntryIndex.size());
List<GAScheduleResult> results = chromosome.getResult();
if (results == null) {
......@@ -1553,6 +1558,8 @@ public class VariableNeighborhoodSearch {
* 将工序往后移动
*/
private Chromosome moveOperationBackward(Chromosome chromosome,Map<Integer, Entry> originalPositionIndex, int pos, int steps) {
log("moveOperationBackward:originalPositionIndex"+originalPositionIndex.size());
Chromosome neighbor=copyChromosome(chromosome);// ProductionDeepCopyUtil.deepCopy(chromosome, Chromosome.class);
List<Integer> os = neighbor.getOperationSequencing();
......@@ -1588,6 +1595,8 @@ public class VariableNeighborhoodSearch {
* 将工序往前插入(不交换,直接插入到目标位置)
*/
private Chromosome insertOperationForward(Chromosome chromosome, int pos, int steps, Map<String, Entry> entrys,Map<Integer, Entry> originalPositionIndex) {
log("insertOperationForward:originalPositionIndex"+originalPositionIndex.size());
Chromosome neighbor=copyChromosome(chromosome);// ProductionDeepCopyUtil.deepCopy(chromosome, Chromosome.class);
List<Integer> os = neighbor.getOperationSequencing();
......@@ -1631,8 +1640,10 @@ public class VariableNeighborhoodSearch {
* 随机生成邻域解(兜底策略)
*/
private Chromosome generateRandomNeighbor(Chromosome chromosome,Map<Integer, Entry> positionIndex,List<List<Integer>> positionByPriority) {
int neighborType = rnd.nextInt(5);
int neighborType =4;// rnd.nextInt(5);
log(String.format("随机生成邻域解-"+neighborType));
log("generateRandomNeighbor:positionIndex"+positionIndex.size());
log("generateRandomNeighbor:positionByPriority"+positionByPriority.size());
switch (neighborType) {
case 0:
......@@ -1652,6 +1663,8 @@ public class VariableNeighborhoodSearch {
private Chromosome generateSwapNeighborWrapper(Chromosome chromosome) {
log("generateSwapNeighborWrapper");
Map<Integer, Object> obj = buildPositionToEntryIndex(chromosome);
Map<Integer, Entry> positionToEntryIndex = (Map<Integer, Entry>) obj.get(1);
List<List<Integer>> positionByPriority = (List<List<Integer>>) obj.get(2);
......@@ -1676,6 +1689,7 @@ public class VariableNeighborhoodSearch {
* 生成交换邻域解 - 只在相同优先级的工序之间交换
*/
private Chromosome generateSwapNeighbor(Chromosome chromosome,Map<Integer, Entry> positionIndex,List<List<Integer>> positionByPriority) {
log("generateSwapNeighbor"+positionIndex.size());
Chromosome neighbor=copyChromosome(chromosome);
List<Integer> os = neighbor.getOperationSequencing();
if (os.size() >= 2 && !positionByPriority.isEmpty()) {
......@@ -1717,6 +1731,7 @@ public class VariableNeighborhoodSearch {
return neighbor;
}
private Chromosome generateReverseNeighborWrapper(Chromosome chromosome) {
log("generateReverseNeighborWrapper");
Map<Integer, Object> obj = buildPositionToEntryIndex(chromosome);
Map<Integer, Entry> positionToEntryIndex = (Map<Integer, Entry>) obj.get(1);
List<List<Integer>> positionByPriority = (List<List<Integer>>) obj.get(2);
......@@ -1726,6 +1741,8 @@ public class VariableNeighborhoodSearch {
* 生成反转邻域解 - 只反转相同优先级的连续工序
*/
private Chromosome generateReverseNeighbor(Chromosome chromosome,Map<Integer, Entry> positionIndex,List<List<Integer>> positionByPriority) {
log("generateReverseNeighbor"+positionIndex.size());
Chromosome neighbor = copyChromosome(chromosome); //ProductionDeepCopyUtil.deepCopy(chromosome, Chromosome.class);
List<Integer> os = neighbor.getOperationSequencing();
if (os.size() >= 2&&positionByPriority.size()>0) {
......@@ -1769,25 +1786,26 @@ public class VariableNeighborhoodSearch {
double priority = startOp.getPriority();
boolean allSameGroupId = true;
for (int i = start; i <= end; i++) {
Entry op = positionIndex.get(i);
if (op == null&&op.getGroupId()==startOp.getGroupId() ) {
allSameGroupId = false;
break;
}
}
// for (int i = start; i <= end; i++) {
// Entry op = positionIndex.get(i);
// if (op == null||op.getGroupId()==startOp.getGroupId() ) {
// allSameGroupId = false;
// break;
// }
// }
if (allSameGroupId) {
//if (allSameGroupId) {
java.util.Collections.reverse(os.subList(start, end + 1));
neighbor.setOperationSequencing(os);
break;
}
// }
}
}
return neighbor;
}
private Chromosome generateInsertNeighborWrapper(Chromosome chromosome) {
log("generateInsertNeighborWrapper");
Map<Integer, Object> obj = buildPositionToEntryIndex(chromosome);
Map<Integer, Entry> positionToEntryIndex = (Map<Integer, Entry>) obj.get(1);
List<List<Integer>> positionByPriority = (List<List<Integer>>) obj.get(2);
......@@ -1798,6 +1816,7 @@ public class VariableNeighborhoodSearch {
*/
private Chromosome generateInsertNeighbor(Chromosome chromosome,Map<Integer, Entry> positionIndex,List<List<Integer>> positionByPriority) {
Chromosome neighbor=copyChromosome(chromosome);// ProductionDeepCopyUtil.deepCopy(chromosome, Chromosome.class);
log("generateInsertNeighbor"+positionIndex.size());
List<Integer> os = neighbor.getOperationSequencing();
if (os.size() >= 2&&positionByPriority.size()>0) {
// Map<Integer, Entry> positionIndex = buildPositionToEntryIndex(neighbor);
......@@ -1822,6 +1841,7 @@ public class VariableNeighborhoodSearch {
}
List<Integer> positionos = positionByPriority.get(priorityGroupIndex);
if (positionos.size() < 2) continue;
int idx1InList = rnd.nextInt(positionos.size());
......@@ -1854,6 +1874,7 @@ public class VariableNeighborhoodSearch {
* 生成机器选择邻域解 - 不受优先级限制
*/
private Chromosome generateMachineChangeNeighbor(Chromosome chromosome) {
log("generateMachineChangeNeighbor");
Chromosome neighbor=copyChromosome(chromosome);// ProductionDeepCopyUtil.deepCopy(chromosome, Chromosome.class);
List<Integer> ms = neighbor.getMachineSelection();
if (!ms.isEmpty()) {
......@@ -1884,6 +1905,8 @@ public class VariableNeighborhoodSearch {
}
Chromosome generateSameMachineSwapNeighbor(Chromosome chromosome){
log("generateSameMachineSwapNeighbor");
List<Integer> os = chromosome.getOperationSequencing();
Map<String, Integer> machinePositionIndex = buildEntryMachinePositionIndex(chromosome);
......@@ -1896,8 +1919,8 @@ public class VariableNeighborhoodSearch {
}
private Chromosome generateSameMachineSwapNeighbor(Chromosome chromosome,int idx1,Map<Integer, Entry> positionIndex,Map<String, Integer> machinePositionIndex) {
//有问题
log("generateSameMachineSwapNeighbor"+positionIndex.size());
Chromosome neighbor=copyChromosome(chromosome);// ProductionDeepCopyUtil.deepCopy(chromosome, Chromosome.class);
List<Integer> os = neighbor.getOperationSequencing();
List<Integer> ms = neighbor.getMachineSelection();
......@@ -1935,6 +1958,11 @@ public class VariableNeighborhoodSearch {
if (positions.isEmpty()||positions.size() <=1) return neighbor;
int idx2 = positions.get(rnd.nextInt(positions.size()));
if(idx1>os.size()||idx2>os.size())
{
int i=0;
}
Collections.swap(os, idx1, idx2);
neighbor.setOperationSequencing(os);
}
......@@ -2003,9 +2031,11 @@ public class VariableNeighborhoodSearch {
* 局部搜索
*/
private Chromosome localSearch(Chromosome chromosome, GeneticDecoder decoder, List<Machine> machines) {
geneticOperations.DelOrder(chromosome);
Chromosome best = ProductionDeepCopyUtil.deepCopy(chromosome, Chromosome.class);
Chromosome best =copyChromosome(chromosome);
decode(decoder, best, machines);
Chromosome current = ProductionDeepCopyUtil.deepCopy(best, Chromosome.class);
geneticOperations.DelOrder(current);
writeKpi(best);
// 预定义邻域结构,避免每次循环重复创建
List<NeighborhoodStructure> neighborhoods = defineNeighborhoods();
......@@ -2018,7 +2048,7 @@ public class VariableNeighborhoodSearch {
break;
}
Chromosome neighbor = generateNeighbor(best, neighborhood);
Chromosome neighbor = generateNeighbor(current, neighborhood);
if (neighbor != null) {
neighbors.add(neighbor);
}
......@@ -2123,6 +2153,9 @@ public class VariableNeighborhoodSearch {
private Map<String, Integer> buildPositionIndex(Chromosome chromosome) {
Map<String, Integer> index = new HashMap<>();
List<Integer> os = chromosome.getOperationSequencing();
Map<Integer, Integer> orderProcessCounter = new HashMap<>();
for (int i = 0; i < os.size(); i++) {
......@@ -2170,6 +2203,9 @@ public class VariableNeighborhoodSearch {
Map<Double, Set<Integer>> groupidByPriority = new HashMap<>();
List<Integer> os = chromosome.getOperationSequencing();
log(String.format("buildPositionToEntryIndex--工序: %d,工序序列: %d,设备: %d,设备序列: %d",
chromosome.getAllOperations().size(), os.size(), chromosome.getGlobalOpList().size(), chromosome.getMachineSelection().size()));
Map<Integer, Integer> orderProcessCounter = new HashMap<>();
for (int i = 0; i < os.size(); i++) {
......@@ -2325,6 +2361,7 @@ public class VariableNeighborhoodSearch {
* 瓶颈设备工序激进重排 - Wrapper方法
*/
private Chromosome aggressiveBottleneckReorderWrapper(Chromosome chromosome) {
log("aggressiveBottleneckReorderWrapper");
List<GAScheduleResult> results = chromosome.getResult();
Map<Integer, Object> obj = buildPositionToEntryIndex(chromosome);
Map<Integer, Entry> positionToEntryIndex = (Map<Integer, Entry>) obj.get(1);
......@@ -2444,6 +2481,8 @@ public class VariableNeighborhoodSearch {
* 关键路径工序优化 - Wrapper方法
*/
private Chromosome criticalPathOptimizeWrapper(Chromosome chromosome) {
log("criticalPathOptimizeWrapper");
List<GAScheduleResult> results = chromosome.getResult();
Map<Integer, Object> obj = buildPositionToEntryIndex(chromosome);
Map<Integer, Entry> positionToEntryIndex = (Map<Integer, Entry>) obj.get(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