Anylogic如何对具有相同参数的agent按指定容量分批
解决方案
核心逻辑
- 首先将所有
productsagent按sp参数值做分组,保证相同sp的agent归入同一待分批队列 - 对每个sp对应的agent队列做固定步长切片,步长设置为10,最后不足10的切片保留实际长度
- 可给每个生成的批次绑定sp值和批次序号作为唯一标识,方便后续仿真逻辑调用
代码实现(Python)
假设所有agent已存储在列表all_agents中,每个agent对象包含sp属性:
from collections import defaultdict MAX_BATCH_SIZE = 10 # 按sp参数分组 sp_grouped = defaultdict(list) for agent in all_agents: sp_grouped[agent.sp].append(agent) # 按规则分批 final_batches = [] for sp, agent_list in sp_grouped.items(): # 切片拆分批次 batches = [agent_list[i:i+MAX_BATCH_SIZE] for i in range(0, len(agent_list), MAX_BATCH_SIZE)] # 给批次添加标识信息 for batch_idx, batch in enumerate(batches, start=1): final_batches.append({ "batch_id": f"sp_{sp}_batch_{batch_idx}", "sp_value": sp, "size": len(batch), "agent_list": batch })
适配其他场景
如果你用的是NetLogo、AnyLogic等仿真工具,不需要改动核心逻辑:先通过参数筛选出相同sp的所有agent,再按每10个为单位遍历拆分即可。拿你举的例子验证,sp=1的49个agent拆分后正好得到4个10容量批次、1个9容量批次,完全符合需求。
内容的提问来源于stack exchange,提问作者Mohamed Youssef
相关产品推荐
相关产品推荐

