AnyLogic技术问询:如何将容器分配至对应BDrop节点前的泊位节点
解决方案:关联船舶泊位与容器生成位置
给船舶代理添加泊位标识变量
在船舶的Agent类型中添加一个String类型变量(如assignedBerthId),或者直接存储Node对象(如targetBerthNode)。当船舶完成BerthPool资源抢占并确定分配到BDrop1-BDrop4中的某一节点时,同步赋值对应的专属泊位节点信息:// 示例:在资源抢占的On seize事件或MoveTo设置目的地后执行 if (agent.getDestination() == BDrop1) { agent.assignedBerthId = "Berth1"; // Berth1为BDrop1对应的专属泊位节点名称 } else if (agent.getDestination() == BDrop2) { agent.assignedBerthId = "Berth2"; } // 以此类推处理BDrop3、BDrop4在MoveTo块完成后直接生成容器
放弃使用selectOutput5控制Source位置,改用MoveTo块的On exit事件触发容器生成,并直接指定位置:// 根据船舶的泊位标识找到对应节点 Node berthNode = findFirst(Node.class, n -> n.getName().equals(agent.assignedBerthId)); // 生成容器并设置到目标泊位 Container newContainer = source_container.createAgent(); newContainer.setLocation(berthNode);注:
source_container是你的容器Source块名称,Container是容器的Agent类型。更简洁的节点关联方式
给BDrop1-BDrop4节点添加自定义参数:在节点的属性面板中新增Node类型参数(如correspondingBerth),初始化时手动将每个BDrop节点与对应的专属泊位节点绑定(比如BDrop1的correspondingBerth选择Berth1)。之后在MoveTo的On exit事件中直接调用:Node targetBerth = ((Node)agent.getDestination()).correspondingBerth; Container newContainer = source_container.createAgent(); newContainer.setLocation(targetBerth);排查变量赋值失败的常见原因
- 确保赋值时机正确:必须在船舶明确分配到BDrop节点之后执行赋值逻辑,比如资源抢占成功后、MoveTo设置目的地之后,避免提前赋值导致空值或错误值。
- 加日志验证:在赋值后添加
traceln(agent + " 绑定泊位: " + agent.assignedBerthId);,运行模型时查看控制台输出,确认变量是否正确赋值。
内容的提问来源于stack exchange,提问作者Scar
相关产品推荐
相关产品推荐

