Commit 9ab085a3 authored by Tong Li's avatar Tong Li

策略最小化换线:增加多线程计算

parent b9d3b3e5
...@@ -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);
return new AbstractMap.SimpleEntry<>(initialSolution, initialCost);
})
.collect(Collectors.toList());
double initialCost = calculateTotalChangeoverCost(initialSolution); // 选择最优初始解
List<Order> bestInitialSolution = null;
double bestInitialCost = Double.MAX_VALUE;
if (initialCost < bestInitialCost) { for (Map.Entry<List<Order>, Double> entry : initialSolutions) {
bestInitialCost = initialCost; if (entry.getValue() < bestInitialCost) {
bestInitialSolution = initialSolution; 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)
List<Order> neighbor = new ArrayList<>(currentSolution); .parallel()
Collections.swap(neighbor, i, i + 1); .mapToObj(i -> {
// 交换相邻订单
List<Order> neighbor = new ArrayList<>(finalCurrentSolution);
double neighborCost = calculateTotalChangeoverCost2(neighbor); Collections.swap(neighbor, i, i + 1);
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;
} }
/** /**
......
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