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

AnyLogic中inject()方法不接受自定义Agent类型问题咨询

Source模块inject方法编译错误原因及解决

问题场景

尝试将population中已有的mp_lkw类型Agent注入Source模块,代码如下:

for (mp_lkw mp : mplkws) 
{
    if (dateToTime(mp.ankunft) <= time()) {
        remove_mplkws(mp);
        source1.inject(mp);
    }
}

编译时触发错误:

Unresolved compilation problem: 
    The method inject(int) in the type Source<mp_lkw> is not applicable for the arguments (mp_lkw)

即使已将Source模块的「New agent」和「Agent type」设置为mp_lkw,仍然提示方法仅接受Integer参数。

原因解析

AnyLogic的Source模块默认提供两种inject方法重载:

  • inject(int n):用于创建并注入n个新的指定类型Agent,这是默认启用的方法
  • inject(Agent agent):用于注入已存在的Agent,但该方法仅在Source模块开启「Inject existing agents」选项后才会被激活

你的问题核心是未开启该选项,导致IDE只能识别到接受int参数的重载,无法匹配传入的mp_lkw对象。

解决方法

  1. 打开source1模块的属性面板
  2. 切换到「Agents」标签页,勾选Inject existing agents选项
  3. 保持原有代码不变,此时source1.inject(mp)即可正常编译运行

注意:勾选该选项后,Source模块的「New agent」配置会失效,模块将不再自动创建新Agent,仅处理注入的已有Agent。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 02:10:39