Along with the release of AnyLogic 8.5, came the new features of AnyLogic Cloud API 8.5.0. Using the API, you can configure and remotely run simulation models in the cloud, as well as create user interfaces for them (here is an example interface). You can find out about these and other features in our previous blog. In this post, the focus is on analyzing the new features and trying out the examples.
You can follow the analysis and reproduce the examples for yourself. Here's what you need to get started with the JavaScript cloud API:
- The key for the API. A trial key is provided bellow and a fully functional key is available to users of commercial versions of AnyLogic Cloud.
- An HTML page that you can edit, or an online service for editing and running HTML and JavaScript code. To connect to the cloud client located on our server, insert this script inside the <head>:
<script src="href="https://cloud.anylogic.com/assets/js-client-8.5.0/cloud-client.js"></script>.
These examples use the Service System Demo model. It describes a maintenance process where you can set the capacity (Server capacity), analyze the average size of the service queue (Mean queue size parameter), and resource loading (Server utilization parameter). The model is located on our test account and you will connect to it via the API. If you’re ready, let's get started.
Running a model without animation
(header links lead to a detailed description)
This example shows how to programmatically find the desired model in the Cloud, set the input data, and run a simple experiment without animation.
To start the experiment, add the code to the HTML and JavaScript page:
How does it work?
As soon as the web page and scripts are loaded, a CloudClient object is created with the API key: e05a6efa-ea5f-4adf-b090-ae0ca7d16c20. It allows you to connect to the host from which the API client is loaded. This step is repeated in each example.
After clicking the button, the runSimulation() function is called. This deactivates the button and launches the following commands: select the latest version of the Service System Demo model, set the value of the Server capacity to 8, and create a simulation object with this input. Next, the getOutputsAndRunIfAbsent() command checks if the experiment has completed and displays data on the page.
When the experiment is completed, the APIs of the Inputs and SingleRunOutputs objects are used to display the input and output data. These are the values for Server capacity, Mean queue size, and Server utilization parameters.
If the experiment completes without errors, the finally block is called and the experiment start button becomes active. If there are errors, a catch block is called that provides an error message to the console.
Setting parameters using the interface and requesting experiment results
All previous experiments along with input and results are stored in the cloud. If the inputs for an experiment run are the same, AnyLogic Cloud recalls the previous results, speeding up the new experiment and saving computing resources. This example shows how this works, and also how to create a widget to configure experiment parameters directly from a web interface.
In the HTML window, a widget is setup to input a value for the Server capacity parameter.
In the JavaScript window, the objects inputs and simulation are set. The Server capacity parameter is passed by inputs and simulation starts the experiment. After this, the simulation.getOutputs() command requests the result. If results are found, they are displayed in the form of values Mean queue size and Server utilization (the then block in the code). If no results are found, or another error occurs, an error window appears (the catch block in the code). You can also set up an alert in the developer console using the code below.
Visualizing experiment progress
To see the progress of a simple experiment, you can make an indicator change color when an experiment is running by using the ModelRun.getProgress() function. This example shows how to do this, and also another way to run the model using the run(), waitForCompletion(), and getOutputs() functions.
When setting the input parameters in JavaScript, the system value {STOP_TIME} must be changed to a larger value (in the example, 10000000) so that the experiment runs longer and the indicator changes color more slowly. The indicator can change color instantly if you run an experiment with the same input values as a previous run because the results will be recalled from the database. Therefore, to check the operation of the indicator, run an experiment with input data that you have not yet used.
The startPolling() function calls the simulation.getProgress() function with an interval of 1 second until the stopPolling() function is called. The stopPolling() function is called when the experiment has ended or an error has occurred, it then checks for 2 seconds that the indicator is full.
Calling the simulation.run() function starts the experiment (if it has not already been started with previous input data) and returns a simulation object, without waiting for the experiment to end. The completion of an experiment is tracked with the simulation.waitForCompletion() function, which returns the same object. Next, calling the simulation.getOutputs() function requests the results. Instead of this sequence, you can use one function simulation.getOutputsAndRunIfAbsent().
Here, we examined only 3 examples of using the cloud service API. Other examples are available in the API documentation.
To test the capabilities of the API on your own model, contact us for the test version of AnyLogic Private Cloud Lite. Please note: the current version of the API is compatible only with AnyLogic 8.5 and subsequent versions of the program.
If you have questions about examples, how the API works, or about installing AnyLogic Private Cloud Lite, please contact our technical support team via the feedback form in AnyLogic.