问题分析与算法设计
原始问题缺陷
固定分区策略在以下场景表现不佳:
- 首层同时出现多组乘客前往不同楼层
- 高层乘客需要快速响应
- 电梯负载不均衡
固定分区策略问题
两组首层乘客:
1. 去15楼 - 分配给高层电梯
2. 去8楼 - 等待中层电梯
➔ 第二组等待时间过长
动态策略解决方案
两组首层乘客:
1. 去15楼 - 分配给最近电梯
2. 去8楼 - 分配给空闲电梯
➔ 两组乘客等待时间均最小化
核心算法
function assignElevator(request) {
let bestElevator = null;
let minCost = Infinity;
for (const elevator of elevators) {
// 计算距离成本
const distance = Math.abs(elevator.floor - request.floor);
// 计算方向成本(顺路加分,反向惩罚)
let directionCost = 0;
if (elevator.direction !== 0) {
if ((elevator.direction === 1 && request.floor < elevator.floor) ||
(elevator.direction === -1 && request.floor > elevator.floor)) {
directionCost = 50; // 高反向惩罚
} else {
directionCost = -10; // 顺路奖励
}
}
// 计算负载成本
const loadCost = elevator.requests.length * 15;
// 首层请求优先级提升
const priorityBoost = request.floor === 1 ? -30 : 0;
// 总成本
const totalCost = distance + directionCost + loadCost + priorityBoost;
if (totalCost < minCost) {
minCost = totalCost;
bestElevator = elevator;
}
}
return bestElevator;
}
空闲位置优化
电梯空闲时动态调整位置:
- 首层有请求时:至少1台电梯常驻首层
- 高峰时段:电梯向高需求区域移动
- 空闲时段:电梯均匀分布在中高层
动态调度模拟
15F 1
14F 1
13F 1
12F 1
11F 1
10F 2
9F 1
8F 1
7F 2
6F 1
5F 1
4F 1
3F 1
2F 2
1F 5
1
2
3
当前请求
1F → 15F
1F → 8F
6.4s
平均等待时间
9.2s
最大等待时间
42%
首层时间节省
96%
电梯利用率