基于Python API的极简AnyLogic Cloud闭环模拟实现问询
基于Python API驱动的AnyLogic Cloud极简模拟实现
我希望构建一个可通过Python API驱动的极简AnyLogic Cloud模拟,运行期间需要实现以下需求:
- 按需调用函数
- 按需修改参数
- 按需获取输出值
由于该API未提供「暂停」功能,仅支持停止及多种运行模式,我设想通过主循环逐步等待外部信号来推进模拟,相关Python代码示例如下:
# 防空教程模拟 from anylogiccloudclient.client.cloud_client import CloudClient import time import threading def _间歇式运行(s, dt): while s.get_progress()["total"] < 100: print("进度: " + str(s.get_progress()["total"]) + "%") print(s.get_status()) time.sleep(dt) client = CloudClient(ANYLOGIC_API_KEY) # print(client) MODEL = client.get_model_by_name(MODEL_NAME) print ('=== 模型已打开 ===') params = { "P radar 2 range": 300, } version = client.get_latest_model_version(MODEL_NAME) inputs = client.create_inputs_from_experiment(version, EXPERIMENT_ID) print ('=== 已修改输入参数 ===') inputs.set_input("P radar 2 range", 175) inputs.set_input("{STOP_TIME}", 54321) # inputs.set_input("P_SIM_PAUSED", False) ##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ simulation = client.create_simulation(inputs) # 此方法不会等待模拟完成 simulation.run() dt = 1.033 progress_thread = threading.Thread(target=_间歇式运行, args=(simulation, dt,)) # 启动线程,重复调用s.get_progress()获取进度信息 progress_thread.start() print ('=== 模拟运行中... ===') polling_period = 1000 # 毫秒 print(simulation.get_status()) for step in range(10): print('执行步骤 ', step) inputs.set_input("P radar 2 range", 10*step) # simulation.wait_for_completion() simulation.wait_for_completion(polling_period) print("雷达2探测范围为 ", inputs.get_input("P radar 2 range")) print(simulation.get_status()) print ('=== 模拟已终止 ===') outputs = simulation.get_outputs()
内容的提问来源于stack exchange,提问作者user30936851
相关产品推荐
相关产品推荐

