You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AnyLogic道路交通库停车场容量不足及多停车场调度问询

AnyLogic Road Traffic Library 多停车场自动选择方案

当然可以实现多停车场按空闲车位自动分配的逻辑,你之前的代码没生效是因为只判断了单个停车场,且没有完成赋值/调用逻辑。下面是具体实现步骤:

1. 统一管理停车场集合

  • 新建一个集合变量,类型设为Collection<ParkingSpace>,命名为parkingLots(可自定义)。
  • 在模型启动事件(比如Main的On startup)中,将所有需要参与分配的停车场加入集合:
    parkingLots.add(parkingLot1);
    parkingLots.add(parkingLot2);
    // 继续添加更多停车场...
    

2. 编写自动选择逻辑

在需要分配停车场的位置(比如车辆到达停车场区域时的代码块),使用以下代码选择第一个有空位的停车场:

// 筛选出所有有空位的停车场,取第一个
ParkingSpace targetLot = findFirst(parkingLots, lot -> lot.nFree() > 0);

if (targetLot != null) {
    // 让车辆驶入选中的停车场
    car.moveTo(targetLot.getEntrance()); // 先移动到停车场入口
    car.park(targetLot); // 执行停车动作
} else {
    // 所有停车场已满的处理逻辑,比如让车辆驶离模型
    car.remove();
}

如果需要优先选择距离当前车辆最近的有空位停车场,可以改用minBy方法:

ParkingSpace targetLot = minBy(parkingLots, lot -> 
    lot.nFree() > 0 ? distance(car, lot.getEntrance()) : Double.MAX_VALUE
);

if (targetLot != null && targetLot.nFree() > 0) {
    car.moveTo(targetLot.getEntrance());
    car.park(targetLot);
} else {
    car.remove();
}

3. 注意事项

  • 确保每个停车场的Entrance(入口点)设置正确,车辆能通过道路网络到达该点。
  • 如果停车场有排队需求,可以在入口前添加Road Queue组件,避免车辆因无车位直接被移除。

内容的提问来源于stack exchange,提问作者Jahidul Hasan Razib

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 11:57:39