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
9ab085a3
Commit
9ab085a3
authored
Mar 09, 2026
by
Tong Li
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
策略最小化换线:增加多线程计算
parent
b9d3b3e5
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
69 additions
and
24 deletions
+69
-24
OrderSortService.java
...main/java/com/aps/service/Algorithm/OrderSortService.java
+69
-24
No files found.
src/main/java/com/aps/service/Algorithm/OrderSortService.java
View file @
9ab085a3
...
@@ -128,7 +128,7 @@ public class OrderSortService {
...
@@ -128,7 +128,7 @@ public class OrderSortService {
}
}
// 定义阈值:当订单数量小于等于这个值时,遍历所有起始点
// 定义阈值:当订单数量小于等于这个值时,遍历所有起始点
int
threshold
=
5
;
int
threshold
=
10
;
if
(
orders
.
size
()
<=
threshold
)
{
if
(
orders
.
size
()
<=
threshold
)
{
// 订单数量较少,遍历所有起始点
// 订单数量较少,遍历所有起始点
...
@@ -183,8 +183,7 @@ public class OrderSortService {
...
@@ -183,8 +183,7 @@ public class OrderSortService {
}
}
// 1. 生成多个初始解,选择最优的一个
// 1. 生成多个初始解,选择最优的一个
List
<
Order
>
bestInitialSolution
=
null
;
double
bestInitialCost
=
Double
.
MAX_VALUE
;
// 尝试多个随机起始点
// 尝试多个随机起始点
Set
<
Integer
>
selectedIndices
=
new
HashSet
<>();
Set
<
Integer
>
selectedIndices
=
new
HashSet
<>();
...
@@ -193,16 +192,24 @@ public class OrderSortService {
...
@@ -193,16 +192,24 @@ public class OrderSortService {
while
(
selectedIndices
.
size
()
<
maxAttempts
)
{
while
(
selectedIndices
.
size
()
<
maxAttempts
)
{
int
randomIndex
=
random
.
nextInt
(
orders
.
size
());
int
randomIndex
=
random
.
nextInt
(
orders
.
size
());
if
(
selectedIndices
.
add
(
randomIndex
))
{
selectedIndices
.
add
(
randomIndex
);
List
<
Order
>
initialSolution
=
generateInitialSolution
(
ProductionDeepCopyUtil
.
deepCopyList
(
orders
)
,
randomIndex
);
}
List
<
Map
.
Entry
<
List
<
Order
>,
Double
>>
initialSolutions
=
selectedIndices
.
parallelStream
()
.
map
(
index
->
{
List
<
Order
>
initialSolution
=
generateInitialSolution
(
ProductionDeepCopyUtil
.
deepCopyList
(
orders
),
index
);
double
initialCost
=
calculateTotalChangeoverCost
(
initialSolution
);
double
initialCost
=
calculateTotalChangeoverCost
(
initialSolution
);
return
new
AbstractMap
.
SimpleEntry
<>(
initialSolution
,
initialCost
);
})
.
collect
(
Collectors
.
toList
());
if
(
initialCost
<
bestInitialCost
)
{
// 选择最优初始解
bestInitialCost
=
initialCost
;
List
<
Order
>
bestInitialSolution
=
null
;
bestInitialSolution
=
initialSolution
;
double
bestInitialCost
=
Double
.
MAX_VALUE
;
}
for
(
Map
.
Entry
<
List
<
Order
>,
Double
>
entry
:
initialSolutions
)
{
if
(
entry
.
getValue
()
<
bestInitialCost
)
{
bestInitialCost
=
entry
.
getValue
();
bestInitialSolution
=
entry
.
getKey
();
}
}
}
}
...
@@ -214,24 +221,62 @@ public class OrderSortService {
...
@@ -214,24 +221,62 @@ public class OrderSortService {
int
maxIterations
=
50
;
int
maxIterations
=
50
;
int
iteration
=
0
;
int
iteration
=
0
;
// do {
// improved = false;
// List<Order> bestNeighbor = new ArrayList<>(currentSolution);
// double bestNeighborCost = currentCost;
//
// // 遍历所有相邻订单对,尝试交换
// for (int i = 0; i < currentSolution.size() - 1; i++) {
// // 交换相邻订单
// List<Order> neighbor = new ArrayList<>(currentSolution);
// Collections.swap(neighbor, i, i + 1);
//
//
// double neighborCost = calculateTotalChangeoverCost2(neighbor);
//
// // 如果交换后成本更低,更新最佳邻居
// if (neighborCost < bestNeighborCost) {
// bestNeighbor = neighbor;
// bestNeighborCost = neighborCost;
// improved = true;
// }
// }
//
// // 如果找到更优解,更新当前解
// if (improved) {
// currentSolution = bestNeighbor;
// // assignChangeoverPriority(currentSolution);
// currentCost = bestNeighborCost;
// }
//
// iteration++;
// } while (improved && iteration < maxIterations);
do
{
do
{
improved
=
false
;
improved
=
false
;
List
<
Order
>
bestNeighbor
=
new
ArrayList
<>(
currentSolution
);
List
<
Order
>
bestNeighbor
=
new
ArrayList
<>(
currentSolution
);
double
bestNeighborCost
=
currentCost
;
double
bestNeighborCost
=
currentCost
;
// 遍历所有相邻订单对,尝试交换
// 并行处理邻域搜索
for
(
int
i
=
0
;
i
<
currentSolution
.
size
()
-
1
;
i
++)
{
List
<
Order
>
finalCurrentSolution
=
currentSolution
;
List
<
Map
.
Entry
<
List
<
Order
>,
Double
>>
neighbors
=
IntStream
.
range
(
0
,
currentSolution
.
size
()
-
1
)
.
parallel
()
.
mapToObj
(
i
->
{
// 交换相邻订单
// 交换相邻订单
List
<
Order
>
neighbor
=
new
ArrayList
<>(
c
urrentSolution
);
List
<
Order
>
neighbor
=
new
ArrayList
<>(
finalC
urrentSolution
);
Collections
.
swap
(
neighbor
,
i
,
i
+
1
);
Collections
.
swap
(
neighbor
,
i
,
i
+
1
);
double
neighborCost
=
calculateTotalChangeoverCost2
(
neighbor
);
double
neighborCost
=
calculateTotalChangeoverCost2
(
neighbor
);
return
new
AbstractMap
.
SimpleEntry
<>(
neighbor
,
neighborCost
);
})
.
collect
(
Collectors
.
toList
());
// 如果交换后成本更低,更新最佳邻居
// 找到最佳邻居
if
(
neighborCost
<
bestNeighborCost
)
{
for
(
Map
.
Entry
<
List
<
Order
>,
Double
>
entry
:
neighbors
)
{
bestNeighbor
=
neighbor
;
if
(
entry
.
getValue
()
<
bestNeighborCost
)
{
bestNeighborCost
=
neighborCost
;
bestNeighbor
=
entry
.
getKey
();
bestNeighborCost
=
entry
.
getValue
();
improved
=
true
;
improved
=
true
;
}
}
}
}
...
@@ -239,13 +284,13 @@ public class OrderSortService {
...
@@ -239,13 +284,13 @@ public class OrderSortService {
// 如果找到更优解,更新当前解
// 如果找到更优解,更新当前解
if
(
improved
)
{
if
(
improved
)
{
currentSolution
=
bestNeighbor
;
currentSolution
=
bestNeighbor
;
// assignChangeoverPriority(currentSolution);
currentCost
=
bestNeighborCost
;
currentCost
=
bestNeighborCost
;
}
}
iteration
++;
iteration
++;
}
while
(
improved
&&
iteration
<
maxIterations
);
}
while
(
improved
&&
iteration
<
maxIterations
);
return
currentSolution
;
return
currentSolution
;
}
}
/**
/**
...
...
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