Reusing a model instance in different function calls

Questions on using the GAMS APIs (Python, .NET, etc.)
Post Reply
bfsantoro
User
User
Posts: 2
Joined: 1 week ago

Reusing a model instance in different function calls

Post by bfsantoro » 1 week ago

Dear all,

I am implementing a model predictive controller, which means that I have to solve as fast as possible a series of optimization problems with the same structure but with different data. I have successfully adapted the example of Python API library (transport11.py) to change a parameter in my model and solve it again using the model instance class. However, this is not a perfect solution to my problem because I cannot now in advance what will be the different values that the parameter will take.

I would like to create a function that receives a new value and solves the optimization problem without instantiating the model instance again. The best that I have achieved so far is to load a check point from a previous, offline run. In the online part of the code, I still have to call methods .add_modelinstance() and .instantiate(), which require a significant amount of time. Also, trying to pickle the model instance object was not effective because it produced an error "can't pickle _thread.lock objects".

Do you have any suggestions?

Thank you in advance,

Bruno

User avatar
Clemens
Posts: 42
Joined: 2 years ago

Re: Reusing a model instance in different function calls

Post by Clemens » 6 days ago

Bruno,

You might want to have a look at the transport7.py example: https://www.gams.com/latest/docs/apis/e ... ource.html
This shows how you can use the GamsModelinstance class. More information is available in the documentation: https://www.gams.com/latest/docs/apis/p ... tance.html

Hope that helps,
Clemens

bfsantoro
User
User
Posts: 2
Joined: 1 week ago

Re: Reusing a model instance in different function calls

Post by bfsantoro » 6 days ago

Dear Clemens,

Thank you for your advice. From my understanding of such example, I was able to write the attached piece of code, where instead of just calling ws.add_checkpoint() to add a new checkpoint, I can reuse a previous one.

However, this does not solve my problem because the commands in lines indicated with #1 and #2, related to the creation of a model instance, require a significant amount of time to process. My objective is to avoid to call these commands at every single iteration, instead I would like to only change the modifier (not included in this minimal example) and solve the new optimization problem.
Attachments
semi.txt
(484 Bytes) Downloaded 7 times

User avatar
Clemens
Posts: 42
Joined: 2 years ago

Re: Reusing a model instance in different function calls

Post by Clemens » 5 days ago

Bruno,

You do not have to call those methods multiple times. After you have instantiated your GamsModelinstance you have also defined the symbols that you want to change (GamsModifier). All you have to do is to change the data in your symbol (ySp) and call mi.solve().
Can you can provide a larger example with the actual loop in which you do the solve?

Best,
Clemens

Post Reply