Hi Michael,
Thank you for these initial thoughts.
I am posting a dummy version below. The GAMS model is defined as a CNSModel object. The model is thus first initialised with a workspace, checkpoint and options. Then a model instance using GamsModelIinstance is initialised using initialise_instance() with a parameter “a” that will be changed during the multiple simulations and a variable X that is tracked.
The model is then run from outside through the calculate() method by getting values for parameter “a” (or several) from a different non-GAMS model. calculate() is called for a predefined number of iterations (e.g. 50) and the value of X is cumulated over these successive model runs. Once the max number of iterations is reached, the reset() method is called which calls instance_delete() and then a new instance_initialise().
The process goes on for these iterated simulations for 100k times.
This is where I get a bit stuck - whenever the reset() method is called there is a new folder with .dat files that is created. The process is also slowed down given the IO operations.
Indeed, the advantage of using GamsModelInstance is that the GAMS model instance would not need to be deleted and reinitialised. However, if I simplify the code below and do not use the instance_delete() and instance_initialise() tandem, the RAM resource use is adding up and after 80k runs the process crashes as my memory is running out.
Therefore, I’m looking either at freeing up resources along the way when only one instance initialisation for the whole simulation process is used (ideally), or finding an option to remove the multiple .dat files once they are no longer needed.
Thank you.
Victor
class CNSModel(Model):
def init(self, *args, **kwargs):
self.filename = kwargs.pop(‘model_filename’, None)
self.fullpath = str(os.getcwd()+“/”+self.filename)
self.iteration = 0
self.workspace = GamsWorkspace(str(os.getcwd()), debug=DebugLevel.Off)
self.model = self.workspace.add_job_from_file(self.fullpath)
self.opt = self.workspace.add_options()
self.opt.all_model_types = “ipopt”
self.cp = self.workspace.add_checkpoint(os.path.basename(str(self.name+“checkpoint”)))
running to obtain some base values
self.model.run(self.opt,self.cp)
self.instance_initialise()
def instance_initialise(self):
self.mi = self.cp.add_modelinstance()
self.a = self.mi.sync_db.add_parameter(“A”,0,“parameter A”)
…
modifier_list = [GamsModifier(self.a),etc.]
self.x = self.mi.sync_db.add_variable(“X”,0, VarType.Unknown)
self.mi.instantiate(“CNSMODEL USING CNS”, self.modifier_list,self.opt)
self.iteration = 0
\
default value for a
self.a.add_record().value = 0
initial cumulative value for X
self.cumulative_x = 0
def instance_delete(self):
self.mi.del()
def calculate(self):
self.a.clear()
obtain the value of a - method inherited from superclass Model enabling the connection of the CNSModel to another model generating the value of “a”
self.a.add_record().value = self.obtain_value_a()
same for other modifiers
…
self.mi.solve()
self.cumulative_x += self.mi.sync_db.get_variable(“X”)[()].level
self.iteration += 1
def reset(self):
self.instance_delete()
self.instance_initialise()