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

AnyLogic仿真暂停与恢复问题:runSimulation()无法恢复仿真

港口仿真暂停后无法恢复的解决方案

我正在进行港口环境仿真,需要临时暂停模型以从代码获取布尔值后继续仿真。代码位于ResourcePool模块的「On Release」事件中,所需信息取自资源代理。测试发现pauseSimulation()可正常暂停仿真,但runSimulation()无法恢复仿真,请问需添加什么代码实现恢复?

相关截图说明

  • 资源代理截图
  • 代码位置截图

尝试的代码

if (unit.timesUsed >= 4) {

    // 暂停仿真
    pauseSimulation();
    machineType = "Balança";
    
    if (unit.GA_balanca() == true) {
        // 尝试恢复仿真
        runSimulation();
    
        if (newData_balanca.before(date())) {
    
            // 更新维护数据
            data_balanca = date();
            newData_balanca = addToDate(data_balanca, HOUR, 2);
    
            // 启动维护任务
            maintenance.startTask(unit);
            unit.qtd_mant ++;
    
            // 记录维护数据
            unit.data_mant = date();
            insertInto(maintenance_data)
            .columns(maintenance_data.machine_type, maintenance_data.when_out, maintenance_data.times_out)
            .values(unit.type, unit.data_mant, unit.qtd_mant)
            .execute();
    
            // 重置使用次数
            unit.timesUsed = 0;
    
            } else {
            }
        } else {
    }

问题核心原因

调用pauseSimulation()后,当前执行的事件线程会立即暂停,后续代码(包括runSimulation())根本没有机会执行,自然无法触发仿真恢复。

解决方案

需要将恢复仿真及后续业务逻辑放到异步执行代码块或延迟调度任务中,避开当前被暂停的线程。

方案1:使用executeAsync()异步执行

if (unit.timesUsed >= 4) {
    // 暂停仿真
    pauseSimulation();
    machineType = "Balança";

    // 在独立线程中执行后续逻辑,不受仿真暂停状态限制
    executeAsync(new Runnable() {
        public void run() {
            if (unit.GA_balanca()) {
                // 恢复仿真
                runSimulation();
                
                // 确保仿真完全恢复后再执行后续业务逻辑
                while (!isSimulationRunning()) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

                if (newData_balanca.before(date())) {
                    // 更新维护数据
                    data_balanca = date();
                    newData_balanca = addToDate(data_balanca, HOUR, 2);

                    // 启动维护任务
                    maintenance.startTask(unit);
                    unit.qtd_mant++;

                    // 记录维护数据
                    unit.data_mant = date();
                    insertInto(maintenance_data)
                        .columns(maintenance_data.machine_type, maintenance_data.when_out, maintenance_data.times_out)
                        .values(unit.type, unit.data_mant, unit.qtd_mant)
                        .execute();

                    // 重置使用次数
                    unit.timesUsed = 0;
                }
            }
        }
    });
}

方案2:使用scheduleAction()延迟调度

if (unit.timesUsed >= 4) {
    pauseSimulation();
    machineType = "Balança";

    // 延迟100ms执行恢复逻辑,利用AnyLogic调度器绕过当前暂停线程
    scheduleAction(0.1, new Runnable() {
        public void run() {
            if (unit.GA_balanca()) {
                runSimulation();
                
                if (newData_balanca.before(date())) {
                    // 更新维护数据
                    data_balanca = date();
                    newData_balanca = addToDate(data_balanca, HOUR, 2);

                    // 启动维护任务
                    maintenance.startTask(unit);
                    unit.qtd_mant++;

                    // 记录维护数据
                    unit.data_mant = date();
                    insertInto(maintenance_data)
                        .columns(maintenance_data.machine_type, maintenance_data.when_out, maintenance_data.times_out)
                        .values(unit.type, unit.data_mant, unit.qtd_mant)
                        .execute();

                    // 重置使用次数
                    unit.timesUsed = 0;
                }
            }
        }
    });
}

关键说明

  • executeAsync()会创建独立线程,该线程不受仿真暂停状态限制,能正常调用runSimulation()触发恢复。
  • scheduleAction()依赖AnyLogic内置调度器,即使仿真暂停,调度器仍会处理延迟任务,从而实现恢复逻辑。
  • 可选的while循环用于确保仿真完全恢复后再执行业务逻辑,避免因仿真未完全启动导致的异常。

内容的提问来源于stack exchange,提问作者Julia Cristina Bremen

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 17:35:12