Skip to content

Improve Your Simulation Projects with Python API for AnyLogic

, , , | October 26, 2020 | By

Integrating Python with AnyLogic for Simulation Modeling

Python + AnyLogic

The Python API for AnyLogic provides a powerful way to take your simulation models to the next level.

Python represents one of the world’s most popular programming languages. According to the analyst firm RedMonk, Python ranks second on the latest ranking of programming language popularity, just below JavaScript. Being a free open-source language, having a large variety of libraries for multiple purposes (e.g. data science, machine learning, artificial intelligence, big data, data visualization, optimization, statistics) and its compatibility with other programs have contributed to its increasing popularity among professionals and academics from multiple fields.

AnyLogic, a leading simulation software for business applications utilized worldwide by over 40% of Fortune 100 companies, has recently integrated a Python API for AnyLogic to work on simulation models stored on your personal AnyLogic Cloud. This new functionality is expected to bring potential benefits to AnyLogic and Python users in the development, configuration and experimentation of their simulation models.

AnyLogic simulation models stored in your personal AnyLogic Cloud account can be configured and run programmatically through a Python API for AnyLogic to obtain experimental results and evaluate multiple scenarios by changing parameters values.

This Python new functionality with AnyLogic represents a great tool for when required to run and experiment with AnyLogic simulation models in a computer that do not has the AnyLogic software installed.

To start using the Python API for AnyLogic, install the AnyLogic cloud client library using pip package installer:

 
Cloud AnyLogic Installation.md
pip install https://cloud.anylogic.com/files/api-8.5.0/clients/anylogiccloudclient-8.5.0-py3-none-any.whl
https://gist.github.com/SimWell/1d2d636c2d2b2efc62a93bf0f9cc0101
 

Minimalistic Simulation Model

The model analyzed in this tutorial (i.e. Service System Demo) can be found in AnyLogic cloud website.

The Service System Demo model consists in a single source, a service module, a check point and one sink. The time agents spend in the system is measured and the goal is to obtain the mean total time agents spend in the system, the server utilization and mean queue size.’

Follow the next steps to run to run the Service System Demo model on AnyLogic Clod website:

  • Type ‘Service Systems Demo’ in AnyLogic Cloud website search bar. The model will be displayed in the screen as follows:
 

AnyLogic Cloud website with 'Service System Demo' typed in search bar

AnyLogic Cloud Website
  • Click on the Service Systems Demo model to open it.
 

Service System Demo model on the AnyLogic Cloud website

Service System Demo Model
  • Click play to run the simulation model and obtain the results.
 

Results from the Service System Demo model

Service System Demo Results

Baseline Results:

According to the baseline model’s results, the mean total time in the system is 4.63, the server utilization 0.83 and the mean queue size 2.54.

Now, let us run the same simulation model using the Python API for AnyLogic by linking it with the AnyLogic model in the cloud. Follow the next steps to run to run the Service System Demo model:

Python Code:

Service System Demo.py
# Load anylogiccloudclient library
from anylogiccloudclient.client.cloud_client import CloudClient

# Creat a CloudClient object, given the API key
client = CloudClient("e05a6efa-ea5f-4adf-b090-ae0ca7d16c20")

# Obtain latest model version of "Service System Demo" model
version = client.get_latest_model_version("Service System Demo")

# Create an Inputs object with the default input values
inputs = client.create_inputs_from_experiment(version, "Baseline")

# Change the "Server Capacity" parameter value
inputs.set_input("Server capacity", 8)

# Creat a simulation object with the inputs
simulation = client.create_simulation(inputs)

# Obtain the simulation outputs
outputs = simulation.get_outputs_and_run_if_absent()

# Print the simulation model outcome values
print("Raw outputs = " + str(outputs.get_raw_outputs()))
print("For Server Capacity = " + str(inputs.get_input("Server capacity")))
print("Mean queue size = " + str(outputs.value("Mean queue size|Mean queue size")))
print("Server utilization = " + str(outputs.value("Utilization|Server utilization")))

https://gist.github.com/SimWell/e84fbcb4346ef39719e8fec7a0983705
 

Results:

Service System Demo Results
Raw outputs = [<anylogiccloudclient.data.model_data.ModelData object at 0x7fef0072fba8>, <anylogiccloudclient.data.model_data.ModelData object at 0x7fef0072f4a8>, <anylogiccloudclient.data.model_data.ModelData object at 0x7fef0072fb70>, <anylogiccloudclient.data.model_data.ModelData object at 0x7fef0072f828>]
For Server Capacity = 8
Mean queue size = 0.9988466025848514
Server utilization = 0.31275860811685163
https://gist.github.com/SimWell/a7344b32a46671b5fabf252ea5e6e648

Comparison with Baseline Model:

By increasing the server capacity from 3 to 8, the mean queue size was reduced from 2.54 to 1 agents. In addition, the mean server utilization was also reduced from 0.83 to 0.31, representing additional idle time that should be further optimized.

What will come next?

Python’s new capabilities with AnyLogic are expected to increase its functionality for simulation projects. While its current applications are still limited, we can expect new functionalities in the near future considering Python’s popularity between programmers and engineers.  For help on the Python API for AnyLogic click here to schedule a call.

For additional information, consult AnyLogic’s Cloud official documentation here and Python codes here.

Interested in obtaining an AnyLogic license? Click here to schedule a call.

About SimWell

At SimWell, we know you're a trailblazing business leader, and it's your job to make confident, informed decisions. The problem is you have a complex operation with infinite variables and interactions that just don't fit on a pivot table. This leaves you utterly underwhelmed by the predictive tools at your disposal, and completely overwhelmed by the massive decisions looming.

We understand the all-consuming nature of big decisions, and the sleepless nights that surely follow. We know the pressure you're feeling and we're here to help. You need REAL answers based on the complexities of your operation. With cutting-edge simulation software from SimWell, you get just that.

We help you accurately predict the outcome of your decisions, giving back years of trial and error, saving you countless costs, and setting you free to be a trailblazing business leader. Press fast-forward on your big decisions with SimWell.

New call-to-action