[Python] minimalistic AnyLogic Cloud closed loop simulation powered via Python API

Discussão em 'Python' iniciado por Stack, Julho 1, 2025.

  1. Stack

    Stack Membro Participativo

    I would like to make a minimalistic AnyLogic Cloud simulation that can be powered via Python API.During the simulation run there is a need to :

    • Run a function on demand
    • change a parameter on demand
    • get output value on demand

    There is no "pause" function in the API, only stop & variations of run. I thought of a Main Loop each time step, that waits for an external signal to proceed.


    # Air Defence Tutorial Simulation
    from anylogiccloudclient.client.cloud_client import CloudClient

    def _intermittant_run(s,dt):
    while s.get_progress()["total"] < 100:
    print("Progress: " + 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 ('=== Model opened ===')
    params = { "P radar 2 range": 300, }
    version = client.get_latest_model_version(MODEL_NAME)
    inputs = client.create_inputs_from_experiment(version, EXPERIMENT_ID)

    print ('=== Changed inputs ===')
    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)
    # It does not wait for the simulation to complete:
    simulation.run()

    dt = 1.033
    progress_thread = threading.Thread(target=_intermittant_run, args=(simulation,dt,))
    # initiates a repeated call of s.get_progress(), which returns information about the progress.
    progress_thread.start()

    print ('=== Running the simulation ... ===')
    polling_period = 1000 # msec
    print(simulation.get_status())

    for step in range(10):
    print('running step ',step)
    inputs.set_input("P radar 2 range", 10*step)
    # simulation.wait_for_completion()
    simulation.wait_for_completion(polling_period)
    print("P radar 2 range is ",inputs.get_input("P radar 2 range"))
    print(simulation.get_status())

    print ('=== Simulation is terminated ===')

    outputs = simulation.get_outputs()



    Thanks in advance,

    Gleb

    Continue reading...

Compartilhe esta Página