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:

  1. Prepare the link between Python and the AIMMS app.

  2. Pass data from Python to the AIMMS app.

  3. Run the solver.

  4. Retrieve the solution.

  5. Write the results back to Excel.

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_assign method efficiently passes the entire DataFrame to 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)