Skip to main content

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,

@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.

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.

 


@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.


@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.

 


Thank you Marcel for your reply!

 

 


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!


@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!


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}

 


@jimhommes , were you successful in calling AIMMS from python using the REST API ? If possible, could you share the code and the steps you followed. I am not able to get it done.

Thanks in advance !

 


Hey @premkrishnan612!

There a few topics on how to use python, did you already read these? 

 

If it still did not work for you, can you send us an email at support@aimms.com with your project for us to understand what is the problem? 

 

Let me know if it helped!


Hi, Thank you for the response, sorry for the delayed reply. I am not a very tech savy person and am still figuring my way around. I had already gone through these topics. I was trying to access the url which leads to the procedure and got a 403 forbidden and then only realised that a key would be needed. How do we get the key for the RESTful API. I am currently using a community edition.  


Hello @premkrishnan612 

I strongly recommend reading the following section of the documentation to dive a little deeper into the workings of the Rest API capabilities:

https://documentation.aimms.com/dataexchange/rest.html#providing-rest-apis

On the 403 error:

POST request to the URL will either return the status code 403 Forbidden if the service name cannot be found.

 

I made a simple example, to share with you:

  1. I created a procedure named pr_stopStartApi that stops and then starts again the API service (I did not change anything else, so the port is 8080)

Procedure pr_startRestApi {
    Body: {
        dex::api::StopAPIService;
        dex::api::StartAPIService;
    }
}

  1. I created a procedure named pr_updateName that has the dex::ServiceName newName, hence is my available service/task.

Procedure pr_updateName {
    Body: {
        sp_requestParameters(dex::api::reqpar) := dex::api::RequestParameter(dex::api::reqpar);
        if "newName" in dex::api::RequestPars then 
            sp_name := sp_requestParameters('newName');
        endif;
    }
    dex::ServiceName: newName;
}

 

Note: the procedure: read all parameters in a request into a string parameter, checks if the newName parameter was sent in the request, if so, updates the string parameter sp_name with that new String.

  1. Add a declaration with the used string parameters

DeclarationSection __unnamed {
    StringParameter sp_requestParameters {
        IndexDomain: dex::api::reqpar;
    }
    StringParameter sp_name;
}
 

Note: sp_requestParameters is indexed on dex::api::reqpar. This is necessary to capture all request parameters passed to the service.

Here is print of the project:

 

My test was succesful:

  1. Ran pr_stopStartApi
  2. Sent a post request - http://localhost:8080/api/v1/tasks/newName?newName=AIMMS
  3. After which my sp_name string parameter was correctly updated with the string AIMMS

 

Hope this helps.

 

 


Hi @luispinto , Sorry for the delay in responding. Thank you so much for the example, its much clearer now and I was able to adapt it to the application, I was working on.


Reply


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

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