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

如何在AnyLogic中使用非缓存数据库值作为智能体参数值?

AnyLogic仿真实时获取数据库更新值解决方案

当前仿真过程中让智能体参数同步更新后的值

智能体参数绑定数据库初始值后,不会自动监听数据库变化,所以在执行完数据库更新操作后,需要主动将最新值重新赋值给智能体参数。以下是具体实现代码:

if(location_port==1){
    // 执行数据库更新
    update(connection)
            .where(connection.condition.eq(1))
            .set(connection.source_name, "Stephenville Crossing")
            .execute();
    update(connection)      
            .where(connection.condition.eq(2))
            .set(connection.destination_name, "Stephenville Crossing")
            .execute();

    // 重新查询最新值并赋值给智能体参数
    // 示例:假设你有智能体集合agents,或直接获取目标智能体
    for(Agent agent : agents) {
        // 获取更新后的source_name
        String updatedSource = selectFirst(connection.source_name)
                                .where(connection.condition.eq(1))
                                .findFirst();
        agent.sourceParam = updatedSource; // 替换为你的智能体参数名

        // 获取更新后的destination_name
        String updatedDest = selectFirst(connection.destination_name)
                                .where(connection.condition.eq(2))
                                .findFirst();
        agent.destParam = updatedDest; // 替换为你的智能体参数名
    }
}

如果是单个特定智能体,直接跳过循环,查询后赋值即可。

启动时禁用缓存获取最新数据库值

AnyLogic默认缓存数据库查询结果,要在启动时直接获取更新后的非缓存值,有两种实现方式:

方式一:单个查询禁用缓存

在启动时的查询代码中添加setCacheable(false),关闭当前查询的缓存:

// 启动时加载非缓存的source_name
String latestSource = selectFirst(connection.source_name)
                        .where(connection.condition.eq(1))
                        .setCacheable(false) // 禁用此查询的缓存
                        .findFirst();
agent.sourceParam = latestSource;

// 启动时加载非缓存的destination_name
String latestDest = selectFirst(connection.destination_name)
                        .where(connection.condition.eq(2))
                        .setCacheable(false)
                        .findFirst();
agent.destParam = latestDest;

方式二:全局禁用数据库缓存

  1. 打开AnyLogic的数据库连接设置(双击项目中的数据库连接)
  2. 在“Advanced”标签页中,找到“Cache mode”选项
  3. 选择“Disabled”,保存设置后所有查询都会直接从数据库获取最新值

注意:全局禁用缓存可能会影响仿真性能,若数据库操作频繁需谨慎使用。

内容的提问来源于stack exchange,提问作者Jahidul Hasan Razib

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 05:54:53