Python API: reseting the state of a model model instance

Questions on using the GAMS APIs (Python, .NET, etc.)
Post Reply
mabasheer
User
User
Posts: 1
Joined: 3 years ago

Python API: reseting the state of a model model instance

Post by mabasheer »

Hello,

I am using a GAMS model instance of Python API to solve a model many times. I created the model instance (mi) from a checkpoint (cp). I solve the model several times using a loop following the structure below:
---------------------------------------------------------------------------------
def create model instance(self):
self.mi = cp.add_modelinstance()
self.a = mi.sync_db.add_parameter("A",0,"parameter A")
self.b = mi.sync_db.add_parameter("B",0,"parameter B")
self.modifier_list = [GamsModifier(self.a),GamsModifier(self.b)]
self.mi.instantiate("CNSMODEL USING CNS", self.modifier_list,self.opt)

self.create model instance()

for x in range(100):
for y in range(50):
self.a.clear()
self.b.clear()
self.a.add_record().value = y
self.b.add_record().value = x
self.mi.solve()
----------------------------------------------------------------------------------------
The problem is after finishing the first inner loop (i.e., y went through 0 to 49), the model starts giving computational error with the next x value (i.e., when x switches from 0 to 1). I suspect that this error is because the last value of the inner loop (i.e., y=49) is too different from the first value at the start of the second iteration. I solved this problem by recreating the model instance (i.e., calling the "create model instance" function) at the start of each iteration of the outer loop. I anticipate that this resets the model instance to the initial state of the checkpoint. However, it creates many "_gams_py" in my directory. Moreover, it slows down the code significantly because of recreating the model instance.

My question: Is there a way to reset the model instance to the initial state of the checkpoint without recreating the model instance?

Regards,
Mohammed
Post Reply