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

AnyLogic 8.8.6模型运行失败及代码修改报错咨询

问题描述

原模型在AnyLogic7中创建,为打开模型将其重新保存为8.8.6版本。该版本模型可正常打开但无法运行,推测是新旧版本API差异所致,现咨询如何修改代码。

原代码如下:

for (Pedestrian thisPed : get_Main().pedestrian) {
    // for each pedestrian in model

    double pedX = thisPed.getX() - getX();
    double pedY = thisPed.getY() - getY();
    if (fieldOfView.contains(pedX, pedY)) {
        pedInDanger = true;
        break;
    }
}

将代码中的get_Main().pedestrian修改为main.pedstrain后,编译失败,报错“main cannot be resolved to a variable”,不清楚为何模型无法识别main。

解决方案
  • 先修正拼写错误:你写的pedstrain是笔误,正确应为pedestrian,但这不是编译报错的核心原因。
  • AnyLogic 8调整了API命名规范:
    • AnyLogic7中用get_Main()获取主代理实例,而AnyLogic8中统一改为驼峰式命名的getMain()(移除了下划线),这是新旧版本的核心差异。
    • 直接使用main会报错,因为main不是AnyLogic8中默认的主代理实例变量名,不能直接引用。
  • 修改后的正确代码:
for (Pedestrian thisPed : getMain().pedestrian) {
    // for each pedestrian in model

    double pedX = thisPed.getX() - getX();
    double pedY = thisPed.getY() - getY();
    if (fieldOfView.contains(pedX, pedY)) {
        pedInDanger = true;
        break;
    }
}
  • 额外检查项:
    • 确认主代理中定义的行人集合名称确实是pedestrian,没有拼写错误;
    • 如果代码是在子代理中编写,getMain()方法依然可以正确获取主代理实例,无需额外配置。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 15:27:11