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
681b20a8
Commit
681b20a8
authored
Nov 21, 2025
by
Tong Li
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
遗传算法
parent
15b3d7d4
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
433 additions
and
49 deletions
+433
-49
Chromosome.java
src/main/java/com/aps/entity/Algorithm/Chromosome.java
+10
-10
GlobalOperationInfo.java
...in/java/com/aps/entity/Algorithm/GlobalOperationInfo.java
+5
-5
OperationSequencingWeight.java
...a/com/aps/entity/Algorithm/OperationSequencingWeight.java
+3
-3
ScheduleParams.java
src/main/java/com/aps/entity/Algorithm/ScheduleParams.java
+11
-11
ScheduleResult.java
src/main/java/com/aps/entity/Algorithm/ScheduleResult.java
+11
-11
ScheduleResultDetail.java
...n/java/com/aps/entity/Algorithm/ScheduleResultDetail.java
+3
-3
MaterialRequirement.java
src/main/java/com/aps/entity/basic/MaterialRequirement.java
+3
-3
MaterialSupply.java
src/main/java/com/aps/entity/basic/MaterialSupply.java
+3
-3
CommonCalculator.java
...main/java/com/aps/service/Algorithm/CommonCalculator.java
+72
-0
Initialization.java
src/main/java/com/aps/service/Algorithm/Initialization.java
+312
-0
No files found.
src/main/java/com/aps/entity/Algorithm/Chromosome.java
View file @
681b20a8
...
...
@@ -15,52 +15,52 @@ public class Chromosome {
/// <summary>
/// 机器选择部分(可选机器集中的顺序号)
/// </summary>
p
ublic
List
<
Integer
>
MachineSelection
;
p
rivate
List
<
Integer
>
MachineSelection
;
/// <summary>
/// 工序排序部分(工件/订单ID)
/// </summary>
p
ublic
List
<
Integer
>
P
perationSequencing
;
p
rivate
List
<
Integer
>
O
perationSequencing
;
/// <summary>
/// 适应度值
/// </summary>
p
ublic
double
Fitness
;
p
rivate
double
Fitness
;
/// <summary>
/// 机器
/// </summary>
p
ublic
List
<
Machine
>
Machines
;
p
rivate
List
<
Machine
>
Machines
;
/// <summary>
/// 解码后的调度结果
/// </summary>
p
ublic
List
<
ScheduleResult
>
Result
;
p
rivate
List
<
ScheduleResult
>
Result
;
/// <summary>
/// 最早完工时间
/// </summary>
p
ublic
double
Makespan
;
p
rivate
double
Makespan
;
/// <summary>
/// 总流程时间
/// </summary>
p
ublic
double
TotalFlowTime
;
p
rivate
double
TotalFlowTime
;
/// <summary>
/// 总换型时间
/// </summary>
p
ublic
double
TotalChangeoverTime
;
p
rivate
double
TotalChangeoverTime
;
/// <summary>
/// 机器负载标准差(越小越均衡)
/// </summary>
p
ublic
double
MachineLoadStd
;
p
rivate
double
MachineLoadStd
;
/// <summary>
/// 交付期延迟时间
/// </summary>
p
ublic
double
DelayTime
;
p
rivate
double
DelayTime
;
}
src/main/java/com/aps/entity/Algorithm/GlobalOperationInfo.java
View file @
681b20a8
...
...
@@ -7,24 +7,24 @@ import lombok.Data;
* 时间:2025-11-21
*/
@Data
public
class
GlobalOperationInfo
{
public
class
GlobalOperationInfo
{
/**
* 全局唯一工序ID(0开始递增)
*/
p
ublic
int
GlobalOpId
;
p
rivate
int
GlobalOpId
;
/**
* 所属组ID
*/
p
ublic
int
GroupId
;
p
rivate
int
GroupId
;
/**
* 订单内工序顺序号
*/
p
ublic
int
Sequence
;
p
rivate
int
Sequence
;
/**
* 对应的工序实例
*/
p
ublic
Entry
Op
;
p
rivate
Entry
Op
;
}
src/main/java/com/aps/entity/Algorithm/OperationSequencingWeight.java
View file @
681b20a8
...
...
@@ -11,13 +11,13 @@ public class OperationSequencingWeight {
/**
* 工序基因的序号
*/
p
ublic
int
Index
;
p
rivate
int
Index
;
/**
* 工序基因的值
*/
p
ublic
int
OsValue
;
p
rivate
int
OsValue
;
/**
* 权重
*/
p
ublic
int
Weight
;
p
rivate
int
Weight
;
}
src/main/java/com/aps/entity/Algorithm/ScheduleParams.java
View file @
681b20a8
...
...
@@ -10,39 +10,39 @@ import java.util.Date;
* 时间:2025-11-21
*/
@Data
public
class
ScheduleParams
{
p
ublic
Date
BaseTime
=
new
Date
();
// 当前基准时间
public
class
ScheduleParams
{
p
rivate
Date
BaseTime
=
new
Date
();
// 当前基准时间
/// <summary>
/// 设备总数
/// </summary>
p
ublic
int
MachineCount
;
// 设备总数
p
rivate
int
MachineCount
;
// 设备总数
/// <summary>
/// 交叉概率
/// </summary>
p
ublic
double
CrossoverProb
=
0.8
;
p
rivate
double
CrossoverProb
=
0.8
;
/// <summary>
/// 变异概率
/// </summary>
p
ublic
double
MutationProb
=
0.1
;
// 变异概率
p
rivate
double
MutationProb
=
0.1
;
// 变异概率
/// <summary>
/// 种群规模
/// </summary>
p
ublic
int
PopulationSize
=
50
;
// 种群规模
p
rivate
int
PopulationSize
=
50
;
// 种群规模
/// <summary>
/// 最大迭代次数
/// </summary>
p
ublic
int
MaxIterations
=
100
;
// 最大迭代次数
p
rivate
int
MaxIterations
=
100
;
// 最大迭代次数
/// <summary>
/// 精英保留计数
/// </summary>
p
ublic
int
ElitismCount
=
5
;
p
rivate
int
ElitismCount
=
5
;
...
...
@@ -50,17 +50,17 @@ public class ScheduleParams {
/// <summary>
/// 全局选择占比
/// </summary>
p
ublic
double
GsRatio
=
0.4
;
// 全局选择占比
p
rivate
double
GsRatio
=
0.4
;
// 全局选择占比
/// <summary>
/// 局部选择占比
/// </summary>
p
ublic
double
LsRatio
=
0.4
;
// 局部选择占比
p
rivate
double
LsRatio
=
0.4
;
// 局部选择占比
/// <summary>
/// 随机选择占比
/// </summary>
p
ublic
double
RsRatio
=
0.2
;
// 随机选择占比
p
rivate
double
RsRatio
=
0.2
;
// 随机选择占比
}
src/main/java/com/aps/entity/Algorithm/ScheduleResult.java
View file @
681b20a8
...
...
@@ -10,15 +10,15 @@ import java.util.List;
*/
@Data
public
class
ScheduleResult
{
p
ublic
int
GroupId
;
p
ublic
int
ProductId
;
p
ublic
int
OperationId
;
p
ublic
int
MachineId
;
p
ublic
int
StartTime
;
// 相对开始时间(分钟)
p
ublic
int
EndTime
;
// 相对结束时间(分钟)
p
ublic
int
Quantity
;
// 批次大小(订单可拆分)
p
ublic
List
<
ScheduleResultDetail
>
GeneDetails
;
// 时间详情
p
ublic
int
OneTime
;
// 单件工时
p
ublic
int
ProcessingTime
;
// 绝对处理时间(分钟)
p
ublic
int
ChangeoverTime
;
p
rivate
int
GroupId
;
p
rivate
int
ProductId
;
p
rivate
int
OperationId
;
p
rivate
int
MachineId
;
p
rivate
int
StartTime
;
// 相对开始时间(分钟)
p
rivate
int
EndTime
;
// 相对结束时间(分钟)
p
rivate
int
Quantity
;
// 批次大小(订单可拆分)
p
rivate
List
<
ScheduleResultDetail
>
GeneDetails
;
// 时间详情
p
rivate
int
OneTime
;
// 单件工时
p
rivate
int
ProcessingTime
;
// 绝对处理时间(分钟)
p
rivate
int
ChangeoverTime
;
}
src/main/java/com/aps/entity/Algorithm/ScheduleResultDetail.java
View file @
681b20a8
...
...
@@ -8,7 +8,7 @@ import lombok.Data;
*/
@Data
public
class
ScheduleResultDetail
{
p
ublic
String
Key
;
p
ublic
int
StartTime
;
// 相对开始时间(分钟)
p
ublic
int
EndTime
;
// 相对结束时间(分钟)
p
rivate
String
Key
;
p
rivate
int
StartTime
;
// 相对开始时间(分钟)
p
rivate
int
EndTime
;
// 相对结束时间(分钟)
}
src/main/java/com/aps/entity/basic/MaterialRequirement.java
View file @
681b20a8
...
...
@@ -17,17 +17,17 @@ public class MaterialRequirement {
/**
* 物料ID
*/
p
ublic
int
MaterialId
;
p
rivate
int
MaterialId
;
/**
* 物料类型
*/
p
ublic
MaterialType
MaterialType
;
p
rivate
MaterialType
MaterialType
;
/**
* 该工序需要的物料数量
*/
p
ublic
BigDecimal
RequiredQuantity
;
p
rivate
BigDecimal
RequiredQuantity
;
@Override
public
String
toString
()
{
...
...
src/main/java/com/aps/entity/basic/MaterialSupply.java
View file @
681b20a8
...
...
@@ -17,17 +17,17 @@ public class MaterialSupply {
/**
* 物料ID
*/
p
ublic
int
MaterialId
;
p
rivate
int
MaterialId
;
/**
* 数量
*/
p
ublic
double
Quantity
;
p
rivate
double
Quantity
;
/**
* 预计到货时间
*/
p
ublic
LocalDateTime
ArrivalTime
;
p
rivate
LocalDateTime
ArrivalTime
;
@Override
public
String
toString
()
{
...
...
src/main/java/com/aps/service/Algorithm/CommonCalculator.java
0 → 100644
View file @
681b20a8
package
com
.
aps
.
service
.
Algorithm
;
import
com.aps.entity.Algorithm.OperationSequencingWeight
;
import
com.aps.entity.basic.Entry
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.Random
;
/**
* 作者:佟礼
* 时间:2025-11-21
*/
public
class
CommonCalculator
{
public
static
List
<
OperationSequencingWeight
>
getOsw
(
List
<
Integer
>
os
,
List
<
Entry
>
allOperations
)
{
// 计算最大优先级并加1
int
maxPriority
=
allOperations
.
stream
()
.
mapToInt
(
Entry:
:
getPriority
)
.
max
()
.
orElse
(
0
)
+
1
;
// 为每个工序分配权重
List
<
OperationSequencingWeight
>
weightedOs
=
new
ArrayList
<>();
for
(
int
idx
=
0
;
idx
<
os
.
size
();
idx
++)
{
int
groupId
=
os
.
get
(
idx
);
// 查找对应groupId的第一个操作
Entry
entry
=
allOperations
.
stream
()
.
filter
(
o
->
o
.
getGroupId
()
==
groupId
)
.
findFirst
()
.
orElse
(
null
);
OperationSequencingWeight
osw
=
new
OperationSequencingWeight
();
osw
.
setIndex
(
idx
);
osw
.
setOsValue
(
groupId
);
osw
.
setWeight
(
maxPriority
-
entry
.
getPriority
());
weightedOs
.
add
(
osw
);
}
return
weightedOs
;
}
public
static
int
getOsIndex
(
List
<
OperationSequencingWeight
>
indexWeights
)
{
Random
rnd
=
new
Random
();
int
totalWeight
=
indexWeights
.
stream
()
.
mapToInt
(
OperationSequencingWeight:
:
getWeight
)
.
sum
();
int
randomWeight
=
rnd
.
nextInt
(
totalWeight
);
int
cumulativeWeight
=
0
;
for
(
OperationSequencingWeight
w
:
indexWeights
)
{
cumulativeWeight
+=
w
.
getWeight
();
if
(
cumulativeWeight
>=
randomWeight
)
{
return
w
.
getIndex
();
}
}
return
0
;
}
public
static
int
getOsIndex2
(
List
<
OperationSequencingWeight
>
weightedOs
)
{
Random
rnd
=
new
Random
();
int
totalWeight
=
weightedOs
.
stream
()
.
mapToInt
(
OperationSequencingWeight:
:
getWeight
)
.
sum
();
int
randomWeight
=
rnd
.
nextInt
(
totalWeight
);
int
cumulativeWeight
=
0
;
int
selectedIndex
=
-
1
;
for
(
int
i
=
0
;
i
<
weightedOs
.
size
();
i
++)
{
cumulativeWeight
+=
weightedOs
.
get
(
i
).
getWeight
();
if
(
cumulativeWeight
>=
randomWeight
)
{
selectedIndex
=
i
;
break
;
}
}
return
0
;
}
}
src/main/java/com/aps/service/Algorithm/Initialization.java
0 → 100644
View file @
681b20a8
package
com
.
aps
.
service
.
Algorithm
;
import
com.aps.entity.Algorithm.Chromosome
;
import
com.aps.entity.Algorithm.GlobalOperationInfo
;
import
com.aps.entity.Algorithm.ScheduleParams
;
import
com.aps.entity.basic.Entry
;
import
com.aps.entity.basic.GlobalParam
;
import
com.aps.entity.basic.MachineOption
;
import
java.util.*
;
import
java.util.stream.Collectors
;
import
java.util.stream.IntStream
;
/**
* 作者:佟礼
* 时间:2025-11-21
*/
public
class
Initialization
{
private
static
List
<
Entry
>
allOperations
;
public
Initialization
(
List
<
Entry
>
allOperations
)
{
Initialization
.
allOperations
=
allOperations
;
}
/**
* 预生成全局工序列表(按“订单0→订单1→…+订单内工序1→2→…”排序,分配GlobalOpId)
*/
public
List
<
GlobalOperationInfo
>
generateGlobalOpList
()
{
List
<
GlobalOperationInfo
>
globalOpList
=
new
ArrayList
<>();
int
globalOpId
=
0
;
for
(
Entry
op
:
allOperations
)
{
GlobalOperationInfo
info
=
new
GlobalOperationInfo
();
info
.
setGlobalOpId
(
globalOpId
);
info
.
setGroupId
(
op
.
getGroupId
());
info
.
setSequence
(
op
.
getSequence
());
info
.
setOp
(
op
);
globalOpList
.
add
(
info
);
globalOpId
++;
}
return
globalOpList
;
}
/**
* 生成初始种群
*/
public
List
<
Chromosome
>
generateInitialPopulation
(
ScheduleParams
param
,
List
<
GlobalOperationInfo
>
globalOpList
)
{
List
<
Chromosome
>
population
=
new
ArrayList
<>();
int
gsCount
=
(
int
)
(
param
.
getPopulationSize
()
*
param
.
getGsRatio
());
int
lsCount
=
(
int
)
(
param
.
getPopulationSize
()
*
param
.
getLsRatio
());
int
rsCount
=
param
.
getPopulationSize
()
-
gsCount
-
lsCount
;
// 全局选择(GS):按GlobalOpId顺序生成MachineSelection
for
(
int
i
=
0
;
i
<
gsCount
;
i
++)
{
population
.
add
(
generateGSChromosome
(
globalOpList
,
param
.
getMachineCount
()));
}
// 局部选择(LS):按GlobalOpId顺序生成MachineSelection(仅负载计算范围不同)
for
(
int
i
=
0
;
i
<
lsCount
;
i
++)
{
population
.
add
(
generateLSChromosome
(
globalOpList
,
param
.
getMachineCount
()));
}
// 随机选择(RS):按GlobalOpId顺序生成MachineSelection(仅机器选择随机)
for
(
int
i
=
0
;
i
<
rsCount
;
i
++)
{
population
.
add
(
generateRSChromosome
(
globalOpList
,
param
.
getMachineCount
()));
}
return
population
;
}
/**
* 全局选择(GS)生成染色体(按GlobalOpId顺序生成MachineSelection)
*/
private
Chromosome
generateGSChromosome
(
List
<
GlobalOperationInfo
>
globalOpList
,
int
machineCount
)
{
Chromosome
chromosome
=
new
Chromosome
();
Map
<
Integer
,
Integer
>
machineLoad
=
new
HashMap
<>();
// int[] machineLoad = new int[machineCount + 1]; // 设备负载(1-based,设备ID从1开始)
List
<
Integer
>
ms
=
new
ArrayList
<>();
// MachineSelection(顺序=GlobalOpId顺序)
List
<
Integer
>
os
=
new
ArrayList
<>();
// OperationSequencing
List
<
Integer
>
osp
=
new
ArrayList
<>();
// 相同优先级OperationSequencing
int
prevPriority
=
-
1
;
Random
rnd
=
new
Random
();
// 步骤1:按GlobalOpId顺序为每道工序选择机器
for
(
GlobalOperationInfo
globalOp
:
globalOpList
)
{
Entry
op
=
globalOp
.
getOp
();
int
groupId
=
globalOp
.
getGroupId
();
if
(
prevPriority
!=
op
.
getPriority
())
{
prevPriority
=
op
.
getPriority
();
if
(!
osp
.
isEmpty
())
{
// 打乱工序排序部分
Collections
.
shuffle
(
osp
);
os
.
addAll
(
osp
);
osp
.
clear
();
}
}
// 选择“设备当前负载+工序加工时间”最小的机器(全局负载均衡)
List
<
MachineOption
>
optionalMachines
=
op
.
getMachineOptions
();
MachineOption
minLoadMachine
=
optionalMachines
.
stream
()
.
min
(
Comparator
.
comparingInt
(
m
->
machineLoad
.
getOrDefault
(
m
.
getMachineId
(),
0
)
+
m
.
getProcessingTime
()))
.
orElseThrow
(()
->
new
NoSuchElementException
(
"MachineOption not found for machine: "
));
OptionalInt
index
=
IntStream
.
range
(
0
,
optionalMachines
.
size
())
.
filter
(
i
->
minLoadMachine
.
getMachineId
()==
optionalMachines
.
get
(
i
).
getMachineId
())
.
findFirst
();
// 计算该机器在“可选机器列表”中的顺序号(1-based)
int
machineSeq
=
index
.
orElse
(
0
)
;
//findIndex(optionalMachines, minLoadMachine.getMachineId()) + 1;
ms
.
add
(
machineSeq
);
// 更新设备负载
machineLoad
.
put
(
minLoadMachine
.
getMachineId
(),
machineLoad
.
getOrDefault
(
minLoadMachine
.
getMachineId
(),
0
)
+
minLoadMachine
.
getProcessingTime
());
osp
.
add
(
groupId
);
}
if
(!
osp
.
isEmpty
())
{
Collections
.
shuffle
(
osp
,
rnd
);
os
.
addAll
(
osp
);
osp
.
clear
();
}
// 步骤2:打乱工序排序部分(OS)
chromosome
.
setOperationSequencing
(
shuffleWithPriority
(
os
));
chromosome
.
setMachineSelection
(
ms
);
return
chromosome
;
}
/**
* 局部选择(LS)生成染色体(按GlobalOpId顺序,每个订单重新初始化负载)
*/
private
Chromosome
generateLSChromosome
(
List
<
GlobalOperationInfo
>
globalOpList
,
int
machineCount
)
{
Chromosome
chromosome
=
new
Chromosome
();
List
<
Integer
>
ms
=
new
ArrayList
<>();
List
<
Integer
>
os
=
new
ArrayList
<>();
Random
rnd
=
new
Random
();
int
currentOrderId
=
-
1
;
Map
<
Integer
,
Integer
>
machineLoad
=
new
HashMap
<>();
List
<
Integer
>
osp
=
new
ArrayList
<>();
int
prevPriority
=
-
1
;
for
(
GlobalOperationInfo
globalOp
:
globalOpList
)
{
Entry
op
=
globalOp
.
getOp
();
int
groupId
=
globalOp
.
getGroupId
();
if
(
prevPriority
!=
op
.
getPriority
())
{
prevPriority
=
op
.
getPriority
();
if
(!
osp
.
isEmpty
())
{
Collections
.
shuffle
(
osp
,
rnd
);
os
.
addAll
(
osp
);
osp
.
clear
();
}
}
// 若切换到新订单,重置设备负载
if
(
groupId
!=
currentOrderId
)
{
machineLoad
.
clear
();
currentOrderId
=
groupId
;
}
// 选择“当前订单内设备负载+加工时间”最小的机器
List
<
MachineOption
>
optionalMachines
=
op
.
getMachineOptions
();
MachineOption
minLoadMachine
=
optionalMachines
.
stream
()
.
min
(
Comparator
.
comparingInt
(
m
->
machineLoad
.
getOrDefault
(
m
.
getMachineId
(),
0
)
+
m
.
getProcessingTime
()))
.
orElseThrow
(()
->
new
NoSuchElementException
(
"MachineOption not found for machine: "
));
OptionalInt
index
=
IntStream
.
range
(
0
,
optionalMachines
.
size
())
.
filter
(
i
->
minLoadMachine
.
getMachineId
()==
optionalMachines
.
get
(
i
).
getMachineId
())
.
findFirst
();
int
machineSeq
=
index
.
orElse
(
0
)
+
1
;
ms
.
add
(
machineSeq
);
// 更新设备负载
machineLoad
.
put
(
minLoadMachine
.
getMachineId
(),
machineLoad
.
getOrDefault
(
minLoadMachine
.
getMachineId
(),
0
)
+
minLoadMachine
.
getProcessingTime
());
osp
.
add
(
groupId
);
}
if
(!
osp
.
isEmpty
())
{
Collections
.
shuffle
(
osp
,
rnd
);
os
.
addAll
(
osp
);
osp
.
clear
();
}
chromosome
.
setOperationSequencing
(
shuffleWithPriority
(
os
));
chromosome
.
setMachineSelection
(
ms
);
return
chromosome
;
}
/**
* 随机选择(RS)生成染色体(按GlobalOpId顺序,随机选择机器)
*/
private
Chromosome
generateRSChromosome
(
List
<
GlobalOperationInfo
>
globalOpList
,
int
machineCount
)
{
Chromosome
chromosome
=
new
Chromosome
();
List
<
Integer
>
ms
=
new
ArrayList
<>();
List
<
Integer
>
os
=
new
ArrayList
<>();
Random
rnd
=
new
Random
();
List
<
Integer
>
osp
=
new
ArrayList
<>();
int
prevPriority
=
-
1
;
for
(
GlobalOperationInfo
globalOp
:
globalOpList
)
{
Entry
op
=
globalOp
.
getOp
();
int
groupId
=
globalOp
.
getGroupId
();
if
(
prevPriority
!=
op
.
getPriority
())
{
prevPriority
=
op
.
getPriority
();
if
(!
osp
.
isEmpty
())
{
Collections
.
shuffle
(
osp
,
rnd
);
os
.
addAll
(
osp
);
osp
.
clear
();
}
}
// 随机选择一道工序的可选机器(1-based顺序号)
List
<
MachineOption
>
optionalMachines
=
op
.
getMachineOptions
();
int
randomSeq
=
rnd
.
nextInt
(
optionalMachines
.
size
())
+
1
;
ms
.
add
(
randomSeq
);
osp
.
add
(
groupId
);
}
if
(!
osp
.
isEmpty
())
{
Collections
.
shuffle
(
osp
,
rnd
);
os
.
addAll
(
osp
);
osp
.
clear
();
}
chromosome
.
setOperationSequencing
(
shuffleWithPriority
(
os
));
chromosome
.
setMachineSelection
(
ms
);
return
chromosome
;
}
/**
* 按优先级加权打乱工序排序(高优先级订单的工序更可能靠前)
*/
private
List
<
Integer
>
shuffleWithPriority
(
List
<
Integer
>
os
)
{
if
(!
GlobalParam
.
IsBreakPriority
)
{
return
new
ArrayList
<>(
os
);
}
Random
rnd
=
new
Random
();
int
maxPriority
=
allOperations
.
stream
()
.
mapToInt
(
Entry:
:
getPriority
)
.
max
()
.
orElse
(
0
)
+
1
;
// 为每个工序分配权重:低优先级订单的工序权重更高
List
<
WeightedGroup
>
weightedOs
=
os
.
stream
()
.
map
(
groupId
->
{
Entry
entry
=
allOperations
.
stream
()
.
filter
(
o
->
o
.
getGroupId
()
==
groupId
)
.
findFirst
().
orElse
(
null
);
return
new
WeightedGroup
(
groupId
,
maxPriority
-
entry
.
getPriority
());
})
.
collect
(
Collectors
.
toList
());
// 按权重随机排序(权重越高,被选中的概率越大)
List
<
Integer
>
shuffled
=
new
ArrayList
<>();
while
(!
weightedOs
.
isEmpty
())
{
int
totalWeight
=
weightedOs
.
stream
().
mapToInt
(
WeightedGroup:
:
getWeight
).
sum
();
int
randomWeight
=
rnd
.
nextInt
(
totalWeight
);
int
cumulativeWeight
=
0
;
int
selectedIndex
=
-
1
;
for
(
int
i
=
0
;
i
<
weightedOs
.
size
();
i
++)
{
cumulativeWeight
+=
weightedOs
.
get
(
i
).
getWeight
();
if
(
cumulativeWeight
>=
randomWeight
)
{
selectedIndex
=
i
;
break
;
}
}
shuffled
.
add
(
weightedOs
.
get
(
selectedIndex
).
getGroupId
());
weightedOs
.
remove
(
selectedIndex
);
}
return
shuffled
;
}
/**
* 辅助类:用于权重排序
*/
private
static
class
WeightedGroup
{
private
final
int
groupId
;
private
final
int
weight
;
public
WeightedGroup
(
int
groupId
,
int
weight
)
{
this
.
groupId
=
groupId
;
this
.
weight
=
weight
;
}
public
int
getGroupId
()
{
return
groupId
;
}
public
int
getWeight
()
{
return
weight
;
}
}
}
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