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

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

parent b9d3b3e5
......@@ -128,7 +128,7 @@ public class OrderSortService {
}
// 定义阈值:当订单数量小于等于这个值时,遍历所有起始点
int threshold = 5;
int threshold = 10;
if (orders.size() <= threshold) {
// 订单数量较少,遍历所有起始点
......@@ -183,8 +183,7 @@ public class OrderSortService {
}
// 1. 生成多个初始解,选择最优的一个
List<Order> bestInitialSolution = null;
double bestInitialCost = Double.MAX_VALUE;
// 尝试多个随机起始点
Set<Integer> selectedIndices = new HashSet<>();
......@@ -193,16 +192,24 @@ public class OrderSortService {
while (selectedIndices.size() < maxAttempts) {
int randomIndex = random.nextInt(orders.size());
if (selectedIndices.add(randomIndex)) {
List<Order> initialSolution= generateInitialSolution(ProductionDeepCopyUtil.deepCopyList(orders) ,randomIndex);
selectedIndices.add(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());
if (initialCost < bestInitialCost) {
bestInitialCost = initialCost;
bestInitialSolution = initialSolution;
}
// 选择最优初始解
List<Order> bestInitialSolution = null;
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 {
int maxIterations = 50;
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 {
improved = false;
List<Order> bestNeighbor = new ArrayList<>(currentSolution);
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<>(currentSolution);
List<Order> neighbor = new ArrayList<>(finalCurrentSolution);
Collections.swap(neighbor, i, i + 1);
double neighborCost = calculateTotalChangeoverCost2(neighbor);
return new AbstractMap.SimpleEntry<>(neighbor, neighborCost);
})
.collect(Collectors.toList());
// 如果交换后成本更低,更新最佳邻居
if (neighborCost < bestNeighborCost) {
bestNeighbor = neighbor;
bestNeighborCost = neighborCost;
// 找到最佳邻居
for (Map.Entry<List<Order>, Double> entry : neighbors) {
if (entry.getValue() < bestNeighborCost) {
bestNeighbor = entry.getKey();
bestNeighborCost = entry.getValue();
improved = true;
}
}
......@@ -239,13 +284,13 @@ public class OrderSortService {
// 如果找到更优解,更新当前解
if (improved) {
currentSolution = bestNeighbor;
// assignChangeoverPriority(currentSolution);
currentCost = bestNeighborCost;
}
iteration++;
} while (improved && iteration < maxIterations);
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