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
f081f29e
Commit
f081f29e
authored
Jan 12, 2026
by
Tong Li
Browse files
Options
Browse Files
Download
Plain Diff
Merge remote-tracking branch 'origin/tl'
parents
99817cb4
34d7e700
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
12 changed files
with
505 additions
and
99 deletions
+505
-99
ScheduleParams.java
src/main/java/com/aps/entity/Algorithm/ScheduleParams.java
+29
-2
ScheduleResultDetail.java
...n/java/com/aps/entity/Algorithm/ScheduleResultDetail.java
+6
-0
GeneticAlgorithm.java
...main/java/com/aps/service/Algorithm/GeneticAlgorithm.java
+85
-30
GeneticDecoder.java
src/main/java/com/aps/service/Algorithm/GeneticDecoder.java
+1
-0
GeneticOperations.java
...ain/java/com/aps/service/Algorithm/GeneticOperations.java
+79
-6
IdGroupingWithDualSerial.java
...a/com/aps/service/Algorithm/IdGroupingWithDualSerial.java
+1
-1
Initialization.java
src/main/java/com/aps/service/Algorithm/Initialization.java
+1
-1
MachineCalculator.java
...ain/java/com/aps/service/Algorithm/MachineCalculator.java
+289
-50
RoutingDataService.java
...in/java/com/aps/service/Algorithm/RoutingDataService.java
+1
-2
ScheduleOperationService.java
...a/com/aps/service/Algorithm/ScheduleOperationService.java
+2
-2
PlanResultService.java
src/main/java/com/aps/service/plan/PlanResultService.java
+8
-2
PlanResultServiceTest.java
src/test/java/com/aps/demo/PlanResultServiceTest.java
+3
-3
No files found.
src/main/java/com/aps/entity/Algorithm/ScheduleParams.java
View file @
f081f29e
...
@@ -20,8 +20,8 @@ public class ScheduleParams {
...
@@ -20,8 +20,8 @@ public class ScheduleParams {
private
static
final
int
MAX_MAX_ITERATIONS
=
200
;
private
static
final
int
MAX_MAX_ITERATIONS
=
200
;
private
static
final
float
MIN_CROSSOVER_PROB
=
0.6f
;
private
static
final
float
MIN_CROSSOVER_PROB
=
0.6f
;
private
static
final
float
MAX_CROSSOVER_PROB
=
0.9f
;
private
static
final
float
MAX_CROSSOVER_PROB
=
0.9f
;
private
static
final
float
MIN_MUTATION_PROB
=
0.
3
f
;
private
static
final
float
MIN_MUTATION_PROB
=
0.
2
f
;
private
static
final
float
MAX_MUTATION_PROB
=
0.
7
f
;
private
static
final
float
MAX_MUTATION_PROB
=
0.
5
f
;
private
static
final
int
MIN_TOURNAMENT_SIZE
=
3
;
private
static
final
int
MIN_TOURNAMENT_SIZE
=
3
;
private
static
final
int
MAX_TOURNAMENT_SIZE
=
7
;
private
static
final
int
MAX_TOURNAMENT_SIZE
=
7
;
...
@@ -81,6 +81,13 @@ public class ScheduleParams {
...
@@ -81,6 +81,13 @@ public class ScheduleParams {
return
maxIterations
;
return
maxIterations
;
}
}
/// <summary>
/// 交叉概率
/// </summary>
public
float
setCrossoverProb
(
float
crossoverProb
)
{
return
this
.
crossoverProb
=
crossoverProb
;
}
/// <summary>
/// <summary>
/// 交叉概率
/// 交叉概率
/// </summary>
/// </summary>
...
@@ -235,6 +242,26 @@ public class ScheduleParams {
...
@@ -235,6 +242,26 @@ public class ScheduleParams {
}
}
}
}
public
void
fineTuneParams
()
{
// 适应度标准差阈值(可调整):小于0.05视为收敛过慢,大于0.2视为收敛过快
final
float
LOW_STD_THRESHOLD
=
0.05f
;
final
float
HIGH_STD_THRESHOLD
=
0.2f
;
final
float
ADJUST_STEP
=
0.05f
;
// 调整步长
DecimalFormat
df
=
new
DecimalFormat
(
"#.000"
);
// 1. 收敛过慢(适应度方差小):增加变异概率,降低交叉概率
mutationProb
=
Math
.
max
(
MIN_MUTATION_PROB
,
Math
.
min
(
MAX_MUTATION_PROB
,
mutationProb
+
0.05f
));
crossoverProb
=
Math
.
max
(
MIN_CROSSOVER_PROB
,
Math
.
min
(
MAX_CROSSOVER_PROB
,
crossoverProb
-
0.1f
));
}
// 测试示例
// 测试示例
public
void
test
()
{
public
void
test
()
{
ScheduleParams
params
=
new
ScheduleParams
();
ScheduleParams
params
=
new
ScheduleParams
();
...
...
src/main/java/com/aps/entity/Algorithm/ScheduleResultDetail.java
View file @
f081f29e
package
com
.
aps
.
entity
.
Algorithm
;
package
com
.
aps
.
entity
.
Algorithm
;
import
com.aps.entity.basic.TimeSegment
;
import
lombok.Data
;
import
lombok.Data
;
import
java.util.List
;
/**
/**
* 作者:佟礼
* 作者:佟礼
* 时间:2025-11-21
* 时间:2025-11-21
...
@@ -14,6 +17,9 @@ public class ScheduleResultDetail {
...
@@ -14,6 +17,9 @@ public class ScheduleResultDetail {
private
double
OneTime
;
// 单件工时
private
double
OneTime
;
// 单件工时
private
double
Quantity
;
// 时间段
private
double
Quantity
;
// 时间段
private
List
<
TimeSegment
>
usedSegment
;
// Key 的 getter/setter
// Key 的 getter/setter
public
String
getKey
()
{
public
String
getKey
()
{
return
Key
;
return
Key
;
...
...
src/main/java/com/aps/service/Algorithm/GeneticAlgorithm.java
View file @
f081f29e
...
@@ -13,6 +13,9 @@ import org.springframework.beans.factory.annotation.Autowired;
...
@@ -13,6 +13,9 @@ import org.springframework.beans.factory.annotation.Autowired;
import
java.time.LocalDateTime
;
import
java.time.LocalDateTime
;
import
java.util.*
;
import
java.util.*
;
import
java.util.concurrent.CompletableFuture
;
import
java.util.concurrent.ExecutorService
;
import
java.util.concurrent.Executors
;
import
java.util.stream.Collectors
;
import
java.util.stream.Collectors
;
/**
/**
...
@@ -98,7 +101,7 @@ public class GeneticAlgorithm {
...
@@ -98,7 +101,7 @@ public class GeneticAlgorithm {
List
<
Chromosome
>
population
=
initialization
.
generateInitialPopulation
(
param
,
globalOpList
);
List
<
Chromosome
>
population
=
initialization
.
generateInitialPopulation
(
param
,
globalOpList
);
//
population= chromosomeDistinct(population);
population
=
chromosomeDistinct
(
population
);
...
@@ -137,8 +140,14 @@ public class GeneticAlgorithm {
...
@@ -137,8 +140,14 @@ public class GeneticAlgorithm {
}
}
FileHelper
.
writeLogFile
(
"选择操作-----------开始-------"
);
FileHelper
.
writeLogFile
(
"选择操作-----------开始-------"
);
// 选择前移除适应度为 0 或明显劣于当前最优解的个体,减少选择池规模:
double
minValidFitness
=
bestFitness
*
0.5
;
// 保留最优解50%以上的个体
List
<
Chromosome
>
validPopulation
=
population
.
stream
()
.
filter
(
c
->
c
.
getFitness
()
>=
minValidFitness
)
.
collect
(
Collectors
.
toList
());
// 选择操作
// 选择操作
List
<
Chromosome
>
selected
=
geneticOps
.
tournamentSelection
(
p
opulation
);
List
<
Chromosome
>
selected
=
geneticOps
.
tournamentSelection
(
validP
opulation
);
FileHelper
.
writeLogFile
(
"选择操作-----------结束-------"
);
FileHelper
.
writeLogFile
(
"选择操作-----------结束-------"
);
// 交叉操作
// 交叉操作
FileHelper
.
writeLogFile
(
"交叉操作-----------开始-------"
);
FileHelper
.
writeLogFile
(
"交叉操作-----------开始-------"
);
...
@@ -178,7 +187,7 @@ public class GeneticAlgorithm {
...
@@ -178,7 +187,7 @@ public class GeneticAlgorithm {
FileHelper
.
writeLogFile
(
"变异批量解码-----------开始-------"
);
FileHelper
.
writeLogFile
(
"变异批量解码-----------开始-------"
);
nextPopulation
=
chromosomeDistinct
(
nextPopulation
);
Chromosomedecode
(
param
,
allOperations
,
globalOpList
,
nextPopulation
);
Chromosomedecode
(
param
,
allOperations
,
globalOpList
,
nextPopulation
);
FileHelper
.
writeLogFile
(
"变异批量解码-----------结束-------"
);
FileHelper
.
writeLogFile
(
"变异批量解码-----------结束-------"
);
// // 精英保留
// // 精英保留
...
@@ -192,7 +201,7 @@ public class GeneticAlgorithm {
...
@@ -192,7 +201,7 @@ public class GeneticAlgorithm {
List
<
Chromosome
>
newPopulation
=
new
ArrayList
<>();
List
<
Chromosome
>
newPopulation
=
new
ArrayList
<>();
newPopulation
.
addAll
(
population
);
newPopulation
.
addAll
(
population
);
newPopulation
.
addAll
(
nextPopulation
);
newPopulation
.
addAll
(
nextPopulation
);
newPopulation
=
chromosomeDistinct
(
newPopulation
);
newPopulation
=
chromosomeDistinct
1
(
newPopulation
);
FileHelper
.
writeLogFile
(
"非支配排序-----------开始-------"
);
FileHelper
.
writeLogFile
(
"非支配排序-----------开始-------"
);
// 2.7 非支配排序
// 2.7 非支配排序
List
<
List
<
Chromosome
>>
combinedFronts
=
_nsgaIIUtils
.
parallelFastNonDominatedSort
(
newPopulation
);
List
<
List
<
Chromosome
>>
combinedFronts
=
_nsgaIIUtils
.
parallelFastNonDominatedSort
(
newPopulation
);
...
@@ -208,19 +217,26 @@ public class GeneticAlgorithm {
...
@@ -208,19 +217,26 @@ public class GeneticAlgorithm {
best
=
GetBest
(
combinedFronts
,
String
.
valueOf
(
iter
));
best
=
GetBest
(
combinedFronts
,
String
.
valueOf
(
iter
));
if
(
bestFitness
<
best
.
getFitness
())
if
(
bestFitness
<
best
.
getFitness
())
{
{
bestFitness
=
best
.
getFitness
();
bestFitness
=
best
.
getFitness
();
Iteration
=
1
;
Iteration
=
1
;
}
else
{
}
else
{
Iteration
++;
Iteration
++;
if
(
Iteration
>
5
)
{
// 当连续 5 代无最优解提升时,降低交叉概率(如从 0.8 降至 0.5)、提高变异概率(如从 0.1 升至 0.2)
param
.
fineTuneParams
();
}
}
}
if
(
Iteration
>
10
)
if
(
Iteration
>
10
)
{
{
break
;
break
;
}
}
// 当种群适应度标准差小于阈值(如 0.001)时,说明种群已收敛,提前终止迭代
fitnessStd
=
_fitnessCalculator
.
calculateFitnessStd
(
population
);
if
(
fitnessStd
<
0.001
)
{
FileHelper
.
writeLogFile
(
"种群已收敛,提前终止迭代"
);
break
;
}
FileHelper
.
writeLogFile
(
"迭代进化------"
+
iter
+
"-----结束-------"
);
FileHelper
.
writeLogFile
(
"迭代进化------"
+
iter
+
"-----结束-------"
);
}
}
...
@@ -256,21 +272,34 @@ public class GeneticAlgorithm {
...
@@ -256,21 +272,34 @@ public class GeneticAlgorithm {
// WriteKpi(best,"最大");
// WriteKpi(best,"最大");
return
best
;
return
best
;
}
}
private
List
<
Chromosome
>
chromosomeDistinct
(
List
<
Chromosome
>
population
)
private
List
<
Chromosome
>
chromosomeDistinct
(
List
<
Chromosome
>
population
)
{
{
if
(
orders
.
size
()<
10
)
{
population
=
population
.
stream
()
.
collect
(
Collectors
.
toMap
(
Chromosome:
:
getGeneStr
,
// key:去重的字段(GeneStr)
u
->
u
,
// value:Chromosome对象
(
u1
,
u2
)
->
u1
// 重复时保留第一个元素
))
.
values
()
// 获取去重后的
.
stream
()
.
collect
(
Collectors
.
toList
());
return
population
;
}
private
List
<
Chromosome
>
chromosomeDistinct1
(
List
<
Chromosome
>
population
)
{
population
=
population
.
stream
()
population
=
population
.
stream
()
.
collect
(
Collectors
.
toMap
(
.
collect
(
Collectors
.
toMap
(
Chromosome:
:
getGeneStr
,
// key:去重的字段(GeneStr)
Chromosome:
:
getGeneStr
,
// key:去重的字段(GeneStr)
u
->
u
,
// value:Chromosome对象
u
->
u
,
// value:Chromosome对象
(
u1
,
u2
)
->
u1
// 重复时保留第一个元素
(
u1
,
u2
)
->
u1
.
getFitness
()
>
u2
.
getFitness
()
?
u1
:
u2
// 重复时保留第一个元素
))
))
.
values
()
// 获取去重后的
.
values
()
// 获取去重后的
.
stream
()
.
stream
()
.
collect
(
Collectors
.
toList
());
.
collect
(
Collectors
.
toList
());
}
return
population
;
return
population
;
}
}
...
@@ -292,6 +321,7 @@ public class GeneticAlgorithm {
...
@@ -292,6 +321,7 @@ public class GeneticAlgorithm {
FileHelper
.
writeLogFile
(
String
.
format
(
" KPI---%f-------"
,
d
));
FileHelper
.
writeLogFile
(
String
.
format
(
" KPI---%f-------"
,
d
));
}
}
}
}
private
ExecutorService
decodeExecutor
=
Executors
.
newFixedThreadPool
(
Runtime
.
getRuntime
().
availableProcessors
()
*
2
);
private
void
Chromosomedecode
(
ScheduleParams
param
,
List
<
Entry
>
allOperations
,
List
<
GlobalOperationInfo
>
globalOpList
,
List
<
Chromosome
>
population
)
private
void
Chromosomedecode
(
ScheduleParams
param
,
List
<
Entry
>
allOperations
,
List
<
GlobalOperationInfo
>
globalOpList
,
List
<
Chromosome
>
population
)
{
{
...
@@ -299,26 +329,51 @@ public class GeneticAlgorithm {
...
@@ -299,26 +329,51 @@ public class GeneticAlgorithm {
GeneticDecoder
decoder
=
new
GeneticDecoder
(
_GlobalParam
,
param
.
getBaseTime
(),
machines
,
orders
,
materials
,
machineScheduler
,
orderMaterials
);
GeneticDecoder
decoder
=
new
GeneticDecoder
(
_GlobalParam
,
param
.
getBaseTime
(),
machines
,
orders
,
materials
,
machineScheduler
,
orderMaterials
);
if
(
population
!=
null
&&
population
.
size
()>
0
)
{
CompletableFuture
.
allOf
(
population
.
stream
()
population
.
parallelStream
().
forEach
(
chromosome
->
{
.
map
(
chromosome
->
CompletableFuture
.
runAsync
(()
->
decode
(
decoder
,
chromosome
,
param
,
allOperations
,
globalOpList
),
decodeExecutor
))
chromosome
.
setResult
(
new
ArrayList
<>());
.
toArray
(
CompletableFuture
[]::
new
))
.
join
();
// 假设Machine类有拷贝方法,或使用MapStruct等工具进行映射
// chromosome.setInitMachines(ProductionDeepCopyUtil.deepCopyList(machines)); // 简单拷贝,实际可能需要深拷贝
if
(
1
==
2
)
{
chromosome
.
setMachines
(
ProductionDeepCopyUtil
.
deepCopyList
(
machines
));
// 简单拷贝,实际可能需要深拷贝
if
(
population
!=
null
&&
population
.
size
()
>
0
)
{
chromosome
.
setAllOperations
(
allOperations
);
// 简单拷贝,实际可能需要深拷贝
population
.
parallelStream
().
forEach
(
chromosome
->
{
chromosome
.
setGlobalOpList
(
globalOpList
);
// 简单拷贝,实际可能需要深拷贝
chromosome
.
setResult
(
new
ArrayList
<>());
//chromosome.setObjectiveWeights(_objectiveWeights);
chromosome
.
setBaseTime
(
param
.
getBaseTime
());
// _sceneService.saveChromosomeToFile(chromosome, "12345679");
decoder
.
decodeChromosomeWithCache
(
chromosome
);
if
(
chromosome
.
getFitness
()
==
0
)
{
chromosome
.
setFitness
(
_fitnessCalculator
.
calculateFitness
(
chromosome
,
_objectiveWeights
));
}
});
// 假设Machine类有拷贝方法,或使用MapStruct等工具进行映射
// chromosome.setInitMachines(ProductionDeepCopyUtil.deepCopyList(machines)); // 简单拷贝,实际可能需要深拷贝
chromosome
.
setMachines
(
ProductionDeepCopyUtil
.
deepCopyList
(
machines
,
Machine
.
class
));
// 简单拷贝,实际可能需要深拷贝
chromosome
.
setAllOperations
(
allOperations
);
// 简单拷贝,实际可能需要深拷贝
chromosome
.
setGlobalOpList
(
globalOpList
);
// 简单拷贝,实际可能需要深拷贝
//chromosome.setObjectiveWeights(_objectiveWeights);
chromosome
.
setBaseTime
(
param
.
getBaseTime
());
// _sceneService.saveChromosomeToFile(chromosome, "12345679");
decoder
.
decodeChromosomeWithCache
(
chromosome
);
if
(
chromosome
.
getFitness
()
==
0
)
{
chromosome
.
setFitness
(
_fitnessCalculator
.
calculateFitness
(
chromosome
,
_objectiveWeights
));
}
});
}
}
}
private
void
decode
(
GeneticDecoder
decoder
,
Chromosome
chromosome
,
ScheduleParams
param
,
List
<
Entry
>
allOperations
,
List
<
GlobalOperationInfo
>
globalOpList
)
{
chromosome
.
setResult
(
new
ArrayList
<>());
// 假设Machine类有拷贝方法,或使用MapStruct等工具进行映射
// chromosome.setInitMachines(ProductionDeepCopyUtil.deepCopyList(machines)); // 简单拷贝,实际可能需要深拷贝
chromosome
.
setMachines
(
ProductionDeepCopyUtil
.
deepCopyList
(
machines
,
Machine
.
class
));
// 简单拷贝,实际可能需要深拷贝
chromosome
.
setAllOperations
(
allOperations
);
// 简单拷贝,实际可能需要深拷贝
chromosome
.
setGlobalOpList
(
globalOpList
);
// 简单拷贝,实际可能需要深拷贝
//chromosome.setObjectiveWeights(_objectiveWeights);
chromosome
.
setBaseTime
(
param
.
getBaseTime
());
// _sceneService.saveChromosomeToFile(chromosome, "12345679");
decoder
.
decodeChromosomeWithCache
(
chromosome
);
if
(
chromosome
.
getFitness
()
==
0
)
{
chromosome
.
setFitness
(
_fitnessCalculator
.
calculateFitness
(
chromosome
,
_objectiveWeights
));
}
}
}
}
...
...
src/main/java/com/aps/service/Algorithm/GeneticDecoder.java
View file @
f081f29e
...
@@ -374,6 +374,7 @@ if(finishedOrder==null||finishedOrder.size()==0)
...
@@ -374,6 +374,7 @@ if(finishedOrder==null||finishedOrder.size()==0)
int
teardownTime
=
machineOption
.
getTeardownTime
();
int
teardownTime
=
machineOption
.
getTeardownTime
();
int
preTime
=
machineOption
.
getPreTime
();
int
preTime
=
machineOption
.
getPreTime
();
int
setupTime
=
calculateSetupTime
(
chromosome
.
getResult
(),
operation
,
machine
,
machineOption
);
int
setupTime
=
calculateSetupTime
(
chromosome
.
getResult
(),
operation
,
machine
,
machineOption
);
...
...
src/main/java/com/aps/service/Algorithm/GeneticOperations.java
View file @
f081f29e
...
@@ -33,11 +33,80 @@ public class GeneticOperations {
...
@@ -33,11 +33,80 @@ public class GeneticOperations {
* 锦标赛选择
* 锦标赛选择
*/
*/
public
List
<
Chromosome
>
tournamentSelection
(
List
<
Chromosome
>
population
,
int
tournamentSize
)
{
public
List
<
Chromosome
>
tournamentSelection
(
List
<
Chromosome
>
population
,
int
tournamentSize
)
{
// 前置边界校验(避免NPE和无效计算)
if
(
population
==
null
||
population
.
isEmpty
())
{
return
new
ArrayList
<>();
}
int
populationSize
=
population
.
size
();
int
populationSize
=
population
.
size
();
// 预计算需要选中的个体数量,避免动态判断
int
needSelectCount
=
populationSize
-
Math
.
max
(
1
,
Math
.
min
(
tournamentSize
,
populationSize
));
List
<
Chromosome
>
selected
=
new
ArrayList
<>(
needSelectCount
);
// 预初始化容量
// 边界值处理:锦标赛规模不能超过种群规模,且至少为1
int
effectiveTournamentSize
=
Math
.
max
(
1
,
Math
.
min
(
tournamentSize
,
populationSize
));
List
<
Chromosome
>
selectedtemp
=
new
ArrayList
<>(
needSelectCount
);
// 预初始化容量
// 预生成随机索引,减少Random对象创建开销
Random
random
=
this
.
rnd
;
// 固定次数循环,替代动态终止条件
for
(
int
selectIdx
=
0
;
selectIdx
<
needSelectCount
;
selectIdx
++)
{
// 优化1:Fisher-Yates抽样(无重复、无冲突,比HashSet高效)
List
<
Integer
>
indices
=
new
ArrayList
<>(
populationSize
);
for
(
int
i
=
0
;
i
<
populationSize
;
i
++)
{
indices
.
add
(
i
);
}
// 仅打乱前effectiveTournamentSize个索引,减少计算量
for
(
int
i
=
0
;
i
<
effectiveTournamentSize
;
i
++)
{
int
swapIdx
=
i
+
random
.
nextInt
(
populationSize
-
i
);
Collections
.
swap
(
indices
,
i
,
swapIdx
);
}
Chromosome
bestCandidate
=
null
;
for
(
int
i
=
0
;
i
<
effectiveTournamentSize
;
i
++)
{
int
idx
=
indices
.
get
(
i
);
Chromosome
curr
=
population
.
get
(
idx
);
if
(
bestCandidate
!=
null
)
{
int
rankCompare
=
Integer
.
compare
(
curr
.
getRank
(),
bestCandidate
.
getRank
());
if
(
rankCompare
==
0
)
{
int
crowdingCompare
=
Double
.
compare
(
curr
.
getCrowdingDistance
(),
bestCandidate
.
getCrowdingDistance
());
if
(
crowdingCompare
<
0
)
{
bestCandidate
=
curr
;
}
}
else
{
if
(
rankCompare
<
0
)
{
bestCandidate
=
curr
;
}
}
}
else
{
bestCandidate
=
curr
;
}
}
if
(
bestCandidate
!=
null
)
{
selected
.
add
(
bestCandidate
);
}
}
return
selected
;
// ProductionDeepCopyUtil.deepCopyList(selected, Chromosome.class);
}
/**
* 锦标赛选择
*/
public
List
<
Chromosome
>
tournamentSelection1
(
List
<
Chromosome
>
population
,
int
tournamentSize
)
{
int
populationSize
=
population
.
size
();
List
<
Chromosome
>
selected
=
new
ArrayList
<>();
// 预初始化容量
List
<
Chromosome
>
selected
=
new
ArrayList
<>();
// 预初始化容量
// 边界值处理:锦标赛规模不能超过种群规模,且至少为1
// 边界值处理:锦标赛规模不能超过种群规模,且至少为1
int
effectiveTournamentSize
=
Math
.
max
(
1
,
Math
.
min
(
tournamentSize
,
populationSize
));
int
effectiveTournamentSize
=
Math
.
max
(
1
,
Math
.
min
(
tournamentSize
,
populationSize
));
while
(
selected
.
size
()
<
populationSize
-
tournamentSize
)
{
while
(
selected
.
size
()
<
populationSize
-
tournamentSize
)
{
// 优化1:不复制整个种群,直接随机抽取有效锦标赛规模的个体,避免大量内存拷贝和shuffle开销
// 优化1:不复制整个种群,直接随机抽取有效锦标赛规模的个体,避免大量内存拷贝和shuffle开销
...
@@ -46,9 +115,9 @@ public class GeneticOperations {
...
@@ -46,9 +115,9 @@ public class GeneticOperations {
List
<
Chromosome
>
chromosomes
=
population
.
subList
(
0
,
endIndex
);
List
<
Chromosome
>
chromosomes
=
population
.
subList
(
0
,
endIndex
);
chromosomes
.
sort
((
c1
,
c2
)
->
{
chromosomes
.
sort
((
c1
,
c2
)
->
{
int
rankCompare
=
Integer
.
compare
(
c1
.
getRank
(),
c2
.
getRank
());
int
rankCompare
=
Integer
.
compare
(
c1
.
getRank
(),
c2
.
getRank
());
return
rankCompare
!=
0
?
rankCompare
:
Double
.
compare
(
c1
.
getCrowdingDistance
(),
c2
.
getCrowdingDistance
());
return
rankCompare
!=
0
?
rankCompare
:
Double
.
compare
(
c1
.
getCrowdingDistance
(),
c2
.
getCrowdingDistance
());
});
});
Chromosome
bestCandidate
=
chromosomes
.
get
(
0
);
Chromosome
bestCandidate
=
chromosomes
.
get
(
0
);
...
@@ -60,6 +129,7 @@ public class GeneticOperations {
...
@@ -60,6 +129,7 @@ public class GeneticOperations {
return
selected
;
return
selected
;
}
}
// 重载,使用默认锦标赛大小3
// 重载,使用默认锦标赛大小3
public
List
<
Chromosome
>
tournamentSelection
(
List
<
Chromosome
>
population
)
{
public
List
<
Chromosome
>
tournamentSelection
(
List
<
Chromosome
>
population
)
{
return
tournamentSelection
(
population
,
param
.
getTournamentSize
());
return
tournamentSelection
(
population
,
param
.
getTournamentSize
());
...
@@ -259,8 +329,8 @@ public class GeneticOperations {
...
@@ -259,8 +329,8 @@ public class GeneticOperations {
.
findFirst
().
orElse
(
0
);
.
findFirst
().
orElse
(
0
);
List
<
Integer
>
MachineSelections
=
chromosome
.
getMachineSelection
();
int
machineSeq
=
chromosome
.
getMachineSelection
()
.
get
(
pos
);
int
machineSeq
=
MachineSelections
.
get
(
pos
);
// 选择当前所选设备外最短加工时间的机器
// 选择当前所选设备外最短加工时间的机器
...
@@ -274,7 +344,8 @@ public class GeneticOperations {
...
@@ -274,7 +344,8 @@ public class GeneticOperations {
.
orElse
(
currentMachine
);
// 如果没有其他机器,保持当前
.
orElse
(
currentMachine
);
// 如果没有其他机器,保持当前
machineSeq
=
optionalMachines
.
indexOf
(
minLoadMachine
)
+
1
;
machineSeq
=
optionalMachines
.
indexOf
(
minLoadMachine
)
+
1
;
chromosome
.
getMachineSelection
().
set
(
pos
,
machineSeq
);
MachineSelections
.
set
(
pos
,
machineSeq
);
chromosome
.
setMachineSelection
(
MachineSelections
);
i
++;
i
++;
}
}
...
@@ -298,6 +369,7 @@ public class GeneticOperations {
...
@@ -298,6 +369,7 @@ public class GeneticOperations {
// 交换位置
// 交换位置
List
<
Integer
>
os
=
chromosome
.
getOperationSequencing
();
List
<
Integer
>
os
=
chromosome
.
getOperationSequencing
();
Collections
.
swap
(
os
,
idx1
,
idx2
);
Collections
.
swap
(
os
,
idx1
,
idx2
);
chromosome
.
setOperationSequencing
(
os
);
}
else
{
}
else
{
// 反转:仅对高优先级工序集中的子序列反转
// 反转:仅对高优先级工序集中的子序列反转
OperationSequencingWeight
osStart
=
selectHighPriorityIndex
(
chromosome
.
getOperationSequencing
(),
0
);
OperationSequencingWeight
osStart
=
selectHighPriorityIndex
(
chromosome
.
getOperationSequencing
(),
0
);
...
@@ -318,6 +390,7 @@ public class GeneticOperations {
...
@@ -318,6 +390,7 @@ public class GeneticOperations {
int
pos2
=
end
-
i
;
int
pos2
=
end
-
i
;
Collections
.
swap
(
os
,
pos1
,
pos2
);
Collections
.
swap
(
os
,
pos1
,
pos2
);
}
}
chromosome
.
setOperationSequencing
(
os
);
}
}
}
}
...
...
src/main/java/com/aps/service/Algorithm/IdGroupingWithDualSerial.java
View file @
f081f29e
...
@@ -668,7 +668,7 @@ public class IdGroupingWithDualSerial {
...
@@ -668,7 +668,7 @@ public class IdGroupingWithDualSerial {
nodeInfo
.
getNewParentIds
().
isEmpty
()
?
"无"
:
nodeInfo
.
getNewParentIds
(),
nodeInfo
.
getNewParentIds
().
isEmpty
()
?
"无"
:
nodeInfo
.
getNewParentIds
(),
nodeInfo
.
getNewChildIds
());
nodeInfo
.
getNewChildIds
());
}
}
System
.
out
.
println
(
"------------------------"
);
//
System.out.println("------------------------");
}
}
}
}
}
}
src/main/java/com/aps/service/Algorithm/Initialization.java
View file @
f081f29e
...
@@ -65,7 +65,7 @@ int populationSize=param.getPopulationSize();
...
@@ -65,7 +65,7 @@ int populationSize=param.getPopulationSize();
.
forEach
(
i
->
{
.
forEach
(
i
->
{
Chromosome
chromo
=
new
Chromosome
();
// 初始化染色体
Chromosome
chromo
=
new
Chromosome
();
// 初始化染色体
// chromo.setObjectiveWeights(_objectiveWeights);
// chromo.setObjectiveWeights(_objectiveWeights);
chromo
.
setInitMachines
(
ProductionDeepCopyUtil
.
deepCopyList
(
machines
));
//
chromo.setInitMachines(ProductionDeepCopyUtil.deepCopyList(machines));
chromo
.
setOrders
(
orders
);
chromo
.
setOrders
(
orders
);
// 全局选择(GS):按GlobalOpId顺序生成MachineSelection
// 全局选择(GS):按GlobalOpId顺序生成MachineSelection
if
(
i
<
gsCount
)
{
if
(
i
<
gsCount
)
{
...
...
src/main/java/com/aps/service/Algorithm/MachineCalculator.java
View file @
f081f29e
This diff is collapsed.
Click to expand it.
src/main/java/com/aps/service/Algorithm/RoutingDataService.java
View file @
f081f29e
...
@@ -272,9 +272,8 @@ public class RoutingDataService {
...
@@ -272,9 +272,8 @@ public class RoutingDataService {
entrys
.
add
(
entry
);
entrys
.
add
(
entry
);
}
}
// 输出每个节点的详细信息
System
.
out
.
println
(
"------------------------"
);
}
}
list
.
put
(
1
,
entrys
);
list
.
put
(
1
,
entrys
);
list
.
put
(
2
,
results
);
list
.
put
(
2
,
results
);
...
...
src/main/java/com/aps/service/Algorithm/ScheduleOperationService.java
View file @
f081f29e
...
@@ -928,13 +928,13 @@ Integer newMachineId1=newMachineId.intValue();
...
@@ -928,13 +928,13 @@ Integer newMachineId1=newMachineId.intValue();
GeneticDecoder
decoder
=
new
GeneticDecoder
(
globalParam
,
baseTime
,
chromosome
.
getMachines
(),
GeneticDecoder
decoder
=
new
GeneticDecoder
(
globalParam
,
baseTime
,
chromosome
.
getMachines
(),
chromosome
.
getOrders
(),
null
,
machineScheduler
,
chromosome
.
getOrderMaterials
());
chromosome
.
getOrders
(),
null
,
machineScheduler
,
chromosome
.
getOrderMaterials
());
chromosome
.
setResultOld
(
ProductionDeepCopyUtil
.
deepCopyList
(
chromosome
.
getResult
()));
chromosome
.
setResultOld
(
ProductionDeepCopyUtil
.
deepCopyList
(
chromosome
.
getResult
()
,
GAScheduleResult
.
class
));
chromosome
.
getResult
().
clear
();
chromosome
.
getResult
().
clear
();
List
<
GAScheduleResult
>
Resultlock
=
chromosome
.
getResult
().
stream
()
List
<
GAScheduleResult
>
Resultlock
=
chromosome
.
getResult
().
stream
()
.
filter
(
o
->
o
.
isIsLocked
()
==
true
)
.
filter
(
o
->
o
.
isIsLocked
()
==
true
)
.
collect
(
Collectors
.
toList
());
.
collect
(
Collectors
.
toList
());
chromosome
.
setResult
(
ProductionDeepCopyUtil
.
deepCopyList
(
Resultlock
));
chromosome
.
setResult
(
ProductionDeepCopyUtil
.
deepCopyList
(
Resultlock
,
GAScheduleResult
.
class
));
decoder
.
decode
(
chromosome
);
decoder
.
decode
(
chromosome
);
...
...
src/main/java/com/aps/service/plan/PlanResultService.java
View file @
f081f29e
...
@@ -98,7 +98,8 @@ public class PlanResultService {
...
@@ -98,7 +98,8 @@ public class PlanResultService {
@Autowired
@Autowired
private
MaterialInfoMapper
materialInfoMapper
;
private
MaterialInfoMapper
materialInfoMapper
;
@Autowired
private
MaterialPurchaseMapper
materialPurchaseMapper
;
@Autowired
@Autowired
private
StockMapper
stockMapper
;
private
StockMapper
stockMapper
;
...
@@ -1139,14 +1140,19 @@ private GlobalParam InitGlobalParam()
...
@@ -1139,14 +1140,19 @@ private GlobalParam InitGlobalParam()
public
List
<
Material
>
getMaterials
(){
public
List
<
Material
>
getMaterials
(){
List
<
Material
>
materials
=
new
ArrayList
<>();
List
<
Material
>
materials
=
new
ArrayList
<>();
System
.
out
.
println
(
"开始初始化物料数据"
);
LambdaQueryWrapper
<
MaterialInfo
>
MaterialInfoWrapper
=
new
LambdaQueryWrapper
<>();
LambdaQueryWrapper
<
MaterialInfo
>
MaterialInfoWrapper
=
new
LambdaQueryWrapper
<>();
MaterialInfoWrapper
.
eq
(
MaterialInfo:
:
getIsdeleted
,
0
);
MaterialInfoWrapper
.
eq
(
MaterialInfo:
:
getIsdeleted
,
0
);
List
<
MaterialInfo
>
materiallist
=
materialInfoMapper
.
selectList
(
MaterialInfoWrapper
);
List
<
MaterialInfo
>
materiallist
=
materialInfoMapper
.
selectList
(
MaterialInfoWrapper
);
LambdaQueryWrapper
<
MaterialPurchase
>
materialPurchaseWrapper
=
new
LambdaQueryWrapper
<>();
materialPurchaseWrapper
.
eq
(
MaterialPurchase:
:
getIsdeleted
,
0
);
List
<
MaterialPurchase
>
MaterialPurchaselist
=
materialPurchaseMapper
.
selectList
(
materialPurchaseWrapper
);
System
.
out
.
println
(
"开始初始化物料数据"
);
LambdaQueryWrapper
<
Stock
>
StockWrapper
=
new
LambdaQueryWrapper
<>();
LambdaQueryWrapper
<
Stock
>
StockWrapper
=
new
LambdaQueryWrapper
<>();
StockWrapper
.
eq
(
Stock:
:
getIsdeleted
,
0
);
StockWrapper
.
eq
(
Stock:
:
getIsdeleted
,
0
);
...
...
src/test/java/com/aps/demo/PlanResultServiceTest.java
View file @
f081f29e
...
@@ -38,13 +38,13 @@ public class PlanResultServiceTest {
...
@@ -38,13 +38,13 @@ public class PlanResultServiceTest {
// nsgaiiUtils.Test();
// nsgaiiUtils.Test();
//planResultService.execute2("C5FB5EF2A7334A0A92F826F4937E1008");
//planResultService.execute2("C5FB5EF2A7334A0A92F826F4937E1008");
//
planResultService.execute2("726D4C1A712B4B1393175BD44B775B66");
planResultService
.
execute2
(
"726D4C1A712B4B1393175BD44B775B66"
);
planResultService
.
execute2
(
"BCA6FA43FFA444D3952CF8F6E1EA291B"
);
//
planResultService.execute2("BCA6FA43FFA444D3952CF8F6E1EA291B");
// 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<>();
// List<Integer> opids=new ArrayList<>();
// opids.add(1);
// opids.add(1);
// planResultService.Move("B571EF6682DB463AB2977B1055A74112",opids,t,3403L);
// planResultService.Move("B571EF6682DB463AB2977B1055A74112",opids,t,3403L);
//
planResultService.Redecode("12345679");
planResultService
.
Redecode
(
"12345679"
);
// MaintenanceWindow maintenanceWindow=new MaintenanceWindow();
// MaintenanceWindow maintenanceWindow=new MaintenanceWindow();
// maintenanceWindow.setStartTime(LocalDateTime.of(2025, 10, 21, 0, 0, 0));
// maintenanceWindow.setStartTime(LocalDateTime.of(2025, 10, 21, 0, 0, 0));
// maintenanceWindow.setEndTime(LocalDateTime.of(2025, 10, 31, 0, 0, 0));
// maintenanceWindow.setEndTime(LocalDateTime.of(2025, 10, 31, 0, 0, 0));
...
...
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