如何在AnyLogic中判断不同Agent参数相等并更新对应参数
解决AnyLogic中跨Agent参数匹配与更新问题
具体实现步骤
1. 在Delay块中编写匹配逻辑
在Delay块的On enter或On exit事件(根据业务需求选择:进入Delay时统计选On enter,停留完成后统计选On exit)中添加以下代码:
// 从Location群体中匹配当前Shipment的destination对应的Location Agent Location targetLocation = findFirst(你的Location群体名称, loc -> loc.name.equals(agent.destination)); // 若找到匹配的Location,更新shpNbr参数 if(targetLocation != null){ targetLocation.shpNbr += 1; }
2. 关键细节说明
你的Location群体名称:替换成你在Main窗口中创建的Location Agent群体(Population)的实际变量名,比如你命名为locations就写locations。agent:在流程块的事件中,agent默认指代当前流经该块的Shipment Agent,直接用它访问destination参数即可。- 参数类型一致性:确保Shipment的
destination参数和Location的name参数都是String类型,否则equals比较会失效。 - 空指针防护:
if(targetLocation != null)的判断是为了避免当Shipment的destination没有对应Location时出现报错。
内容的提问来源于stack exchange,提问作者Behnaz
相关产品推荐
相关产品推荐

