Solved

Calling AIMMS from Python with academic license

  • 3 May 2021
  • 9 replies
  • 205 views

Badge

Hi everyone,

I have been trying to call an AIMMS (academic license) procedure from python although this doesn't seem to work. I got the following code from another thread, which seems to work with a paid license. I also saw on another post that the academic licence might have some limitations in this regard.

 

Is there an API in another language, were I can make Python wrapper around this other language?

or

Is it possible that AIMMS internally allows to run scripts in some language, in which case a script can be built  in such a way that they exchange model parameters/inputs/results with the outside world?

 

Best regards,

icon

Best answer by MarcelRoelofs 28 May 2021, 15:14

@rhserna21 

We've released version 1.2 of the Data Exchange library which offers the capability to expose your model via a REST API, you can find the documentation on https://documentation.aimms.com/dataexchange/rest.html.

To call your model from within your Python code, you can use the Python requests module.

Prior to making the request, you can call the AIMMS launcher with your project from within Python to get the model ready to accept requests if need be (notice that you will have to accept the use conditions once every day, preventing completely automated continuous runs). You can skip this step if you already have opened your AIMMS project manually.

When calling the REST API, you can either use query parameters in the POST URL to pass e.g. the location of a data file to use or other parameters to configure the optimization, or you can pass the data as the request body and use the Data Exchange library to read it into identifiers in your model, and similarly for the model output.

 

View original

9 replies

Userlevel 5
Badge +2

@rhserna21 

 

Are you receiving any specific error message when you try this code ? 

 

  1. What happens when you execute the code you have in command, directly in Windows cmd ? If this executes successfully, you’ll know that it is not an issue with your license.
  2. Did you try subprocess.run instead of subprocess.call ? To my knowledge, subprocess.call does not support arguments.
Badge

Hi Mohan, 

Thanks for your reply, I tried running it in the windows cmd and I get the next error (this line I also ran in a paid license and it worked):


C:\Users\hernandezsernar>"C:\Users\hernandezsernar\AppData\Local\AIMMS\IFA\Aimms\4.79.3.10-x64-VS2017\Bin\AimmsCmd.exe" "C:\Users\hernandezsernar\Documents\COMPETES\Competes_2020.aimms --run-only examplespine"
Error: Unable to open AIMMS with "--as-server --ignore-dialogs --hidden  "C:\Users\hernandezsernar\Documents\COMPETES\Competes_2020.aimms --run-only examplespine"": Starting up Aimms failed. Program initialization error.

 

Userlevel 3
Badge +2

@rhserna21 

You are correct that AimmsCmd does not work with the new licensing scheme for Academic Licenses and the Community License. These new types of licenses deliberately do not allow running headless AIMMS sessions, as would be the case with AimmsCmd. 

However, we will shortly be releasing a new version of the DataExchange library which will allow procedures in AIMMS models to be run through a REST API. This will form the basis for running AIMMS models through a REST API from our cloud platform later this year, but this functionality will also allow you to access interactive AIMMS sessions on your desktop from e.g. Python. Such a setup will also enable debugging such REST calls, e.g. to figure out possible data problems in your input from Python.

Userlevel 3
Badge +2

@rhserna21 

We've released version 1.2 of the Data Exchange library which offers the capability to expose your model via a REST API, you can find the documentation on https://documentation.aimms.com/dataexchange/rest.html.

To call your model from within your Python code, you can use the Python requests module.

Prior to making the request, you can call the AIMMS launcher with your project from within Python to get the model ready to accept requests if need be (notice that you will have to accept the use conditions once every day, preventing completely automated continuous runs). You can skip this step if you already have opened your AIMMS project manually.

When calling the REST API, you can either use query parameters in the POST URL to pass e.g. the location of a data file to use or other parameters to configure the optimization, or you can pass the data as the request body and use the Data Exchange library to read it into identifiers in your model, and similarly for the model output.

 

Badge

Thank you Marcel for your reply!

 

 

Badge

The data exchange library offers and explanation on how to associate a procedure with a dex annotation, by doing this I would be able to expose my procedure under a specific path. My question is where can I insert this annotation in my procedure?. Is there a way to make this more concrete?

e.g. I have this procedure which I would like to call from python

Initialize_Read;

Choose_Model( mt ) := 0;
Choose_Network(nt):= 0;
Choose_Model('Generation+Transmission_Expansion') := 1;
Choose_Network('Transportation_Network'):=1;
PrepareData;


Selectionofyears := '2050' ;
SelectionofyearsDyn := '2050' ;
SelectionOfHours:=SupHours;
SelectionOfSeasons:=SupSeasons;
SelectionOfDays:='1';
LP_MainExecution_GenTransInv;

 

Kind regards!

Userlevel 5
Badge +2

@rhserna21 , procedures have an annotation attribute (under the comments field) where you can specify this. 

 

 

@mohansx, thanks for the clear description!

At what URL is the REST API exposed? I'm running AIMMS on my machine and would like to call it with e.g. a Python process. Through the documentation I understand this has to happen at:

/api/v1/tasks/{service-name}

What would be the base URL? 

Thanks in advance for any help!

Userlevel 3
Badge +2

To start the HTTP service enabling you to connect with the model from Python, you need to call

 dex::api::StartAPIService

from within your model which uses 

 dex::api::ListenerPort

to specify the listen port (default 8080). So, if you didn't make any changes away from the default port, you can then reach the service at the URL

http://localhost:8080/api/v1/tasks/{service-name}

 

Reply


Didn't find what you were looking for? Try searching on our documentation pages:

AIMMS Developer & PRO | AIMMS How-To | AIMMS SC Navigator