Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
H
HYH.APSJ
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
佟礼
HYH.APSJ
Commits
e9d075e2
Commit
e9d075e2
authored
Mar 16, 2026
by
Tong Li
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
优化
parent
a0c8a9c1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
117 additions
and
56 deletions
+117
-56
GeneticDecoder.java
src/main/java/com/aps/service/Algorithm/GeneticDecoder.java
+115
-54
PlanResultServiceTest.java
src/test/java/com/aps/demo/PlanResultServiceTest.java
+2
-2
No files found.
src/main/java/com/aps/service/Algorithm/GeneticDecoder.java
View file @
e9d075e2
...
...
@@ -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,73 +77,131 @@ 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 );
// FileHelper.writeLogFile("解码-----------开始-------"+chromosome.getID()+":"+cacheKey );
// _allOperations=chromosome.getAllOperations();
// _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
();
// 确保锁释放
// 2. 尝试从缓存获取(无锁,ConcurrentHashMap 线程安全)
Chromosome
cachedResult
=
decodingCache
.
get
(
cacheKey
);
if
(
cachedResult
!=
null
)
{
// 缓存命中:复用缓存结果
// 赋值给染色体
// 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
;
}
// 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())
}
// 4. 将解码结果存入缓存(无锁,ConcurrentHashMap 线程安全)
decodingCache
.
putIfAbsent
(
cacheKey
,
chromosome
);
// 5. 限制缓存大小(超过 1000 移除最早插入的键)
if
(
decodingCache
.
size
()
>
MAX_CACHE_SIZE
)
{
// 简单清理:移除前 200 个元素
Iterator
<
String
>
keyIterator
=
decodingCache
.
keySet
().
iterator
();
int
removeCount
=
0
;
while
(
keyIterator
.
hasNext
()
&&
removeCount
<
200
)
{
keyIterator
.
next
();
keyIterator
.
remove
();
removeCount
++;
}
}
finally
{
cacheLock
.
unlock
();
}
// FileHelper.writeLogFile("解码-----------结束-------"+chromosome.getID());
// FileHelper.writeLogFile("解码-----------结束-------"+chromosome.getID());
return
chromosome
;
}
...
...
src/test/java/com/aps/demo/PlanResultServiceTest.java
View file @
e9d075e2
...
...
@@ -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
[]{
50
d
,
50
d
});
//
planResultService.SpiltOperation("92BB773E1E2447C99D8176C991D5C9D2",1,new Double[]{50d, 50d});
// planResultService.SpiltOrder("A41D662EE0764D008173C5A0E42B15F6","5f9d5383-b89a-4a4f-8805-2f617c711968",new Double[]{500d, 500d});
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment