Hello to the World of AIMMSPY from Python-Bridge
This Hello World guide introduces aimmspy from the AIMMS Python-Bridge, focusing on the core “Python-in-the-lead” workflow.
Specifically, the Python script will manage the AIMMS application to:
Open and manage an AIMMS application instance.
Exchange data (read and write) with the AIMMS model.
Execute AIMMS procedures.
Please download the example project to follow along this article.
Prerequisites & Environment Setup
To ensure reproducibility and manage dependencies, this tutorial focuses on using a specific Python version within an isolated virtual environment.
Note
This tutorial focuses on a specific environment setup for reproducibility. Other configurations are possible but are outside the scope of this article.
Install Dependency Tools
If you haven’t already, please install the recommended dependency management tools:
Initialize the Environment
Start a PowerShell or terminal, navigate to the directory containing the Python main.py file, and
execute the following commands:
1pyenv install 3.13
2pyenv local 3.13
3uv init
4uv venv
5.venv\Scripts\activate
Remarks:
Lines 1-2: Set Python 3.13 as the interpreter for the current directory.
Lines 3-4: Initialize the project and create a new virtual environment (
.\venv).Line 5: Activate the newly created virtual environment.
Hint
If you face an error while pyenv install 3.13, use pyenv install --list to see all available python versions.
And, if you don’t find the Python version you’d like to use, please run pyenv update and try again.
Install Dependencies
The documentation mentions that a pip install aimmspy is needed to use the aimmspy library.
In Python applications, it is customary to enumerate such dependencies in a requirements.txt file.
Here we use
aimmspy
pandas
as requirements.txt file; such that
uv pip install -r requirements.txt
will install the dependencies used.
AIMMS Model
The accompanying AIMMS model, hello.aimms, is intentionally simple. It defines an input array (p_A)
and calculates the scalar sum of its elements, storing the result in an output parameter (p_B).
Fig. 56 AIMMS Model Explorer showing input parameter p_A and output parameter p_B.
Fig. 57 AIMMS procedure MainExecution containing a single assignment statement.
Python Script
The Python script uses the aimmspy library to control the AIMMS session.
1# Import necessary classes and functions.
2from aimmspy.project.project import Project, Model
3from aimmspy.utils import find_aimms_path
4
5# Initialize the AIMMS project
6project = Project(
7
8 # Path to the AIMMS Bin folder
9 aimms_path=find_aimms_path("25"),
10
11 # Path to the AIMMS project file (relative to the script).
12 #aimms_project_file=projectfile,
13 aimms_project_file = "..\\AIMMS\\hello.aimms",
14
15 # Optional: Add your licensing URL here.
16)
17aimms_model : Model = project.get_model(__file__)
18
19# Send data to the AIMMS model
20hello_world_dict = { "hello" : 1, "world" : 2 }
21aimms_model.p_a.assign( hello_world_dict )
22
23# Run an AIMMS procedure
24aimms_model.MainExecution()
25
26# Get results back and print.
27hello_world_result = aimms_model.p_b.data()
28print(f"Hello world: sum is {hello_world_result}")
Expected Output
The script can now be executed using:
uv run main.py
When the script is executed, the AIMMS session opens, data is exchanged, and the result is returned to Python:
1C:\Users\ChrisKuip\AppData\Local\AIMMS\IFA\Aimms\25.7.7.4-x64-VS2022\Bin --as-server "..\AIMMS\hello.aimms"
2Hello world: sum is 3.0
Conclusion
You have now established a connection between a Python environment and an AIMMS model using aimmspy. By following this “Hello World” workflow, you have successfully:
Established a communication channel from Python to a running AIMMS session.
Modified AIMMS model data from an external script.
Triggered AIMMS procedures to utilize its optimization and logic capabilities.
With this foundation, you can now use Python as a driver to orchestrate your AIMMS models, allowing you to incorporate powerful optimization solvers into your broader Python-based workflows and applications.
Tip
As a next step, you may want to check out: Orchestrating Contract Allocation AIMMS App from Python.