Orchestrating Contract Allocation AIMMS App from Python
This guide shows you how to use the AIMMS Python-Bridge to control an AIMMS application from a Python script. This approach is ideal for running an AIMMS model as a “headless” optimization service within a larger data pipeline.
Please refer to the Contract Allocation example to follow along with this article. This article illustrates the process, which consists of the following steps:
Prepare the link between Python and the AIMMS app.
Pass data from Python to the AIMMS app.
Run the solver.
Retrieve the solution.
Write the results back to Excel.
Prepare Link Between Python and AIMMS
First, ensure you have the aimmspy library installed. You can install it using pip:
pip install aimmspy
To use the aimmspy library, import it as follows:
1from aimmspy.project.project import DataReturnTypes, Project, AimmsException, AimmsPyException, Model
2from aimmspy.utils import find_aimms_path
Next, initialize the Project and get a reference to your AIMMS model. This sets up the communication link.
1# Initialize the AIMMS project connection.
2project = Project(
3 # Path to the AIMMS Bin folder (required for API connection).
4 aimms_path=find_aimms_path("25"),
5
6 # Path to the AIMMS project file to be opened.
7 aimms_project_file=os.path.join(projectroot, 'AIMMS-project', 'ContractAllocation.aimms'),
8
9 # The name of the AIMMS set containing all identifiers exposed to Python.
10 exposed_identifier_set_name="AllIdentifiers",
11
12 # Default data type for multi-dimensional data retrieval (Pandas DataFrame is preferred).
13 data_type_preference=DataReturnTypes.PANDAS,
14
15 # license_url: Fill license URL if using a network license.
16)
17
18# Get a reference to the active AIMMS model instance for data transfer and execution.
19aimms_model : Model = project.get_model(__file__)
Remarks:
Line 4:
aimms_pathis the path to the AIMMS installation. Thefind_aimms_pathutility helps to locate your AIMMS installation.Line 7:
aimms_project_fileis the path to your AIMMS project file. In this example it happens to be relative to the Python script:. ├── AIMMS-project │ ├── MainProject │ ├── .... │ └── ContractAllocation.aimms │ └── python-bridge ├── main.py └── requirements.txtLine 10:
exposed_identifier_set_namecontrols which model identifiers the Python script can access.AllIdentifiersprovides full access.Line 13:
data_type_preferencespecifies the default data structure for data exchange. We usepandasas it also provides seamless integration with Excel files. Pythonpolarsalso provide seamless integration with Excel files and might have been used as well.
Passing Input Data
Importing Producers Data
This example reads data from a sheet called Producers and
renames the columns to match the corresponding AIMMS identifiers.
1# 1. Load and Assign Producer Data (i_producer, capacities)
2datainput_pd_producer = pd.read_excel(datainput, sheet_name='Producers')
3
4# Rename columns to match exact AIMMS identifiers for seamless assignment.
5datainput_pd_producer.rename(columns={
6 'Producers' : 'i_producer',
7 'Available Capacity' : 'p_availableCapacity',
8 'Minimal Delivery' : 'p_minimalDelivery'
9 }, inplace=True)
10
11# Assign data to the corresponding AIMMS identifiers.
12aimms_model.multi_assign(datainput_pd_producer)
Remarks:
Line 3: Reading the data using the Panda’s read_excel method.
Line 6-14: Usually, the column names of tables are not the same as the identifier names in an AIMMS model. In this example too. Therefor, Panda’s rename is used.
Line 17: The
multi_assignmethod efficiently passes the entireDataFrameto the AIMMS model in a single, concise statement.
Note that the data for the other sheets are read in and passed to AIMMS similarly.
Running the Optimization Inside the AIMMS App
After loading all data, you can execute the optimization logic within your AIMMS model by calling the corresponding procedure.
1# 4. Execute the AIMMS Optimization
2# Calls the main procedure in AIMMS to solve the optimization problem.
3aimms_model.MainExecution()
This single line tells the AIMMS model to run its MainExecution procedure,
which typically contains the solver calls and other logic.
Retrieving the Solution and Writing to Excel
Once the solver finishes, you can retrieve the results from the AIMMS model and write them to a new Excel workbook.
Retrieving Contract Allocation
The multi_data method retrieves data from multiple AIMMS identifiers at once.
1# Get the total contract fulfillment results.
2df_contract_allocation = aimms_model.multi_data(["i_contractExport","p_totalGeneration"])
The rename function then prepares the data for export.
1# Rename columns for user-friendly export to Excel.
2df_contract_allocation.rename(columns={
3 'i_contractExport' : 'Contract',
4 'p_totalGeneration' : 'Total Generation'
5 }, inplace=True)
Finally, use the ExcelWriter from pandas to save all your result DataFrames into a single Excel file,
with each DataFrame on its own sheet.
1# Use ExcelWriter to write multiple DataFrames to separate sheets in a single file.
2with pd.ExcelWriter(excel_file_path, engine='openpyxl') as writer:
3 df_producer_allocation.to_excel(writer, sheet_name='Allocation per Producer', index=False)
4 df_contract_allocation.to_excel(writer, sheet_name='Contract Allocation', index=False)
See also