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

AnyLogic技术问询:如何将OptionList转为AgentType列表以遍历资源?

AnyLogic中遍历OptionList类型资源并导出数据的解决方案

核心思路

你的需求本质是获取OptionList的所有枚举值,并关联到对应的资源Agent类型,进而批量查询该类型下的所有资源实例。以下是两种直接可行的实现方式:


方式一:给OptionList枚举添加AgentType属性(推荐)

这种方式最直观,把资源类型和对应的AgentType直接绑定在枚举里:

  1. 打开你的ResourceType OptionList编辑界面,点击「添加属性」,新增一个类型为AgentType的属性(比如命名为agentType)。
  2. 给每个枚举项(比如MACHINE、WORKSTATION)设置对应的AgentType(选择你模型中定义的资源Agent类型)。
  3. 在模型启动时(比如Main的on startup事件)生成包含所有枚举值的列表:
// 获取ResourceType的所有枚举值并转成List
List<ResourceType> allResourceTypes = Arrays.asList(ResourceType.values());
  1. 遍历导出的代码:
for (ResourceType rt : allResourceTypes) {
    // 通过枚举属性获取对应的资源Agent类型
    AgentType targetAgentType = rt.agentType;
    // 获取该类型下的所有资源实例(假设你的资源Agent类是ResourceAgent)
    List<ResourceAgent> resources = targetAgentType.getAgents();
    
    // 执行Excel导出逻辑
    for (ResourceAgent res : resources) {
        // 示例:获取资源参数并写入Excel
        excelFile.writeCell(res.getName(), row, 0);
        excelFile.writeCell(res.getCapacity(), row, 1);
        excelFile.writeCell(res.getRequiredStaffCount(), row, 2);
        row++;
    }
}

方式二:用Map维护枚举与AgentType的映射

如果不想修改原有OptionList,可以在模型中维护一个映射关系:

  1. 在Main代理中创建一个Map<ResourceType, AgentType>类型的变量(比如命名为resourceTypeMap)。
  2. 在模型启动时初始化这个映射:
resourceTypeMap.put(ResourceType.MACHINE, machineAgentType);
resourceTypeMap.put(ResourceType.WORKSTATION, workstationAgentType);
// 依次添加所有ResourceType枚举项
  1. 遍历导出的代码:
for (Map.Entry<ResourceType, AgentType> entry : resourceTypeMap.entrySet()) {
    ResourceType rt = entry.getKey();
    AgentType targetAgentType = entry.getValue();
    List<ResourceAgent> resources = targetAgentType.getAgents();
    
    // 同样执行Excel导出逻辑
    for (ResourceAgent res : resources) {
        // 写入数据到Excel
    }
}

关键说明

  • ResourceType.values()是Java枚举的原生方法,能直接获取该OptionList的所有枚举值,无需手动创建列表。
  • AgentType.getAgents()方法会返回该类型下的所有Agent实例,对应你手动创建的资源对象。
  • 确保你的资源Agent类(比如ResourceAgent)已经定义了getName()、getCapacity()等获取参数的方法。

内容的提问来源于stack exchange,提问作者name123

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 18:10:27