Commit e9d075e2 authored by Tong Li's avatar Tong Li

优化

parent a0c8a9c1
......@@ -21,6 +21,7 @@ import java.time.Duration;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
......@@ -35,11 +36,13 @@ import java.util.stream.IntStream;
public class GeneticDecoder {
private final Map<String, Chromosome> decodingCache = new HashMap<>();
// private final Map<String, Chromosome> decodingCache = new HashMap<>();
private final ConcurrentHashMap<String, Chromosome> decodingCache = new ConcurrentHashMap<>();
// 缓存大小限制
private static final int MAX_CACHE_SIZE = 1000;
// 线程安全锁:避免多线程下缓存操作冲突(可选,若单线程可移除)
private final ReentrantLock cacheLock = new ReentrantLock();
// private final ReentrantLock cacheLock = new ReentrantLock();
private LocalDateTime baseTime;
private final List<Machine> machines;
private final MachineSchedulerService machineScheduler;
......@@ -74,6 +77,76 @@ public class GeneticDecoder {
// this.orderMaxID = orders.stream().mapToInt(Order::getId).max().orElse(0);
}
// public Chromosome decodeChromosomeWithCache(Chromosome chromosome) {
// String cacheKey = createCacheKey(chromosome);
// // FileHelper.writeLogFile("解码-----------开始-------"+chromosome.getID()+":"+cacheKey );
//
// // _allOperations=chromosome.getAllOperations();
//
//
//
// // 1. 创建缓存键
//
//
// // 2. 尝试从缓存获取(加锁保证线程安全)
// cacheLock.lock();
// try {
// if (decodingCache.containsKey(cacheKey)) {
// // 缓存命中:复用缓存结果
// Chromosome cachedResult = decodingCache.get(cacheKey);
// // 赋值给染色体
// // chromosome.setInitMachines(ProductionDeepCopyUtil.deepCopyList(cachedResult.getInitMachines(),Machine.class));
//
// chromosome.setObjectives(cachedResult.getObjectives());
// chromosome.setMakespan(cachedResult.getMakespan());
// chromosome.setTotalFlowTime(cachedResult.getTotalFlowTime());
// chromosome.setTotalChangeoverTime(cachedResult.getTotalChangeoverTime());
// chromosome.setMachineLoadStd(cachedResult.getMachineLoadStd());
// chromosome.setDelayTime(cachedResult.getDelayTime());
// chromosome.setResult(ProductionDeepCopyUtil.deepCopyList(cachedResult.getResult(),GAScheduleResult.class));
// chromosome.setOrders(ProductionDeepCopyUtil.deepCopyList(cachedResult.getOrders(),Order.class));
// chromosome.setOperatRel(ProductionDeepCopyUtil.deepCopyList(cachedResult.getOperatRel(), GroupResult.class));
// chromosome.setAllOperations(ProductionDeepCopyUtil.deepCopyList(cachedResult.getAllOperations(),Entry.class));
// chromosome.setGlobalOpList(ProductionDeepCopyUtil.deepCopyList(cachedResult.getGlobalOpList(),GlobalOperationInfo.class));
// chromosome.setOperationSequencing(ProductionDeepCopyUtil.deepCopyList(cachedResult.getOperationSequencing()));
// chromosome.setMachineSelection(ProductionDeepCopyUtil.deepCopyList(cachedResult.getMachineSelection()));
//
// // Chromosome chromosomen= ProductionDeepCopyUtil.deepCopy(cachedResult);
// // FileHelper.writeLogFile("解码-----------结束-------"+chromosome.getID());
//
// return chromosome;
// }
// } finally {
// cacheLock.unlock(); // 确保锁释放
// }
// // 3. 缓存未命中:执行解码逻辑
// decode(chromosome);
//
// // 4. 将解码结果存入缓存(加锁保证线程安全)
// cacheLock.lock();
// try {
// // 存储缓存:封装为 CacheValue 对象
// decodingCache.put(
// cacheKey,
// chromosome
// );
//
// // 5. 限制缓存大小(超过 1000 移除最早插入的键)
// if (decodingCache.size() > MAX_CACHE_SIZE) {
//
// Iterator<String> keyIterator = decodingCache.keySet().iterator();
// if (keyIterator.hasNext()) {
// keyIterator.next(); // 获取首个键
// keyIterator.remove(); // 移除首个键(对应 C# _decodingCache.Keys.First())
// }
// }
// } finally {
// cacheLock.unlock();
// }
// // FileHelper.writeLogFile("解码-----------结束-------"+chromosome.getID());
// return chromosome;
// }
public Chromosome decodeChromosomeWithCache(Chromosome chromosome) {
String cacheKey = createCacheKey(chromosome);
// FileHelper.writeLogFile("解码-----------开始-------"+chromosome.getID()+":"+cacheKey );
......@@ -85,12 +158,10 @@ public class GeneticDecoder {
// 1. 创建缓存键
// 2. 尝试从缓存获取(加锁保证线程安全)
cacheLock.lock();
try {
if (decodingCache.containsKey(cacheKey)) {
// 缓存命中:复用缓存结果
// 2. 尝试从缓存获取(无锁,ConcurrentHashMap 线程安全)
Chromosome cachedResult = decodingCache.get(cacheKey);
if (cachedResult != null) {
// 缓存命中:复用缓存结果
// 赋值给染色体
// chromosome.setInitMachines(ProductionDeepCopyUtil.deepCopyList(cachedResult.getInitMachines(),Machine.class));
......@@ -113,32 +184,22 @@ public class GeneticDecoder {
return chromosome;
}
} finally {
cacheLock.unlock(); // 确保锁释放
}
// 3. 缓存未命中:执行解码逻辑
decode(chromosome);
// 4. 将解码结果存入缓存(加锁保证线程安全)
cacheLock.lock();
try {
// 存储缓存:封装为 CacheValue 对象
decodingCache.put(
cacheKey,
chromosome
);
// 4. 将解码结果存入缓存(无锁,ConcurrentHashMap 线程安全)
decodingCache.putIfAbsent(cacheKey, chromosome);
// 5. 限制缓存大小(超过 1000 移除最早插入的键)
if (decodingCache.size() > MAX_CACHE_SIZE) {
// 简单清理:移除前 200 个元素
Iterator<String> keyIterator = decodingCache.keySet().iterator();
if (keyIterator.hasNext()) {
keyIterator.next(); // 获取首个键
keyIterator.remove(); // 移除首个键(对应 C# _decodingCache.Keys.First())
}
int removeCount = 0;
while (keyIterator.hasNext() && removeCount < 200) {
keyIterator.next();
keyIterator.remove();
removeCount++;
}
} finally {
cacheLock.unlock();
}
// FileHelper.writeLogFile("解码-----------结束-------"+chromosome.getID());
return chromosome;
......
......@@ -39,7 +39,7 @@ public class PlanResultServiceTest {
// TestSortService sortService=new TestSortService();
// sortService.test1();
// nsgaiiUtils.Test();
// planResultService.execute2("5475E00B844847ACB6DC20227967BA2F");
planResultService.execute2("5475E00B844847ACB6DC20227967BA2F");
// planResultService.execute2("00E0C5D3E4AD4F36B56C39395906618D");
// planResultService.execute2("92BB773E1E2447C99D8176C991D5C9D2");
......@@ -56,7 +56,7 @@ public class PlanResultServiceTest {
// maintenanceWindow.setEndTime(LocalDateTime.of(2025, 9, 19, 0, 0, 0));
// planResultService.AddMaintenanceWindow("5475E00B844847ACB6DC20227967BA2F",2488l,maintenanceWindow);
// // planResultService.DelOperation("B6AE363FF5044DDF8DECE32D5FE0F7EA",7);
planResultService.SpiltOperation("92BB773E1E2447C99D8176C991D5C9D2",1,new Double[]{50d, 50d});
// planResultService.SpiltOperation("92BB773E1E2447C99D8176C991D5C9D2",1,new Double[]{50d, 50d});
// planResultService.SpiltOrder("A41D662EE0764D008173C5A0E42B15F6","5f9d5383-b89a-4a4f-8805-2f617c711968",new Double[]{500d, 500d});
}
......
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