Loading...
Searching...
No Matches
gams.control.execution.GamsModelInstance Class Reference

Public Member Functions

def __init__ (self, checkpoint=None, modelinstance_name=None, source=None)
 Constructor.
 
def copy_modelinstance (self, modelinstance_name=None)
 Copies this ModelInstance to a new ModelInstance.
 
def __del__ (self)
 Use this to explicitly free unmanaged resources.
 
def instantiate (self, model_definition, modifiers=[], options=None)
 Instantiate the GamsModelInstance.
 
def solve (self, update_type=SymbolUpdateType.BaseCase, output=None, mi_opt=None)
 Solve model instance.
 
def interrupt (self)
 Send interrupt signal to running GamsModelInstance.
 

Properties

property model_status = property(get_model_status)
 Status of the model.
 
property solver_status = property(get_solver_status)
 Solve status of the model.
 
property checkpoint = property(get_checkpoint)
 Retrieve GamsCheckpoint.
 
property name = property(get_name)
 Retrieve name of GamsModelInstance.
 
property sync_db = property(get_sync_db)
 Retrieve GamsDatabase used to synchronize modifiable data.
 

Detailed Description

The GamsJob class is the standard way of dealing with a GAMS model and the corresponding solution provided by a solver. The GAMS language provides programming flow that allows to solve models in a loop and do other sophisticated tasks, like building decomposition algorithms.

In rare cases, the GAMS model generation time dominates the solver solution time and GAMS itself becomes the bottleneck in an optimization application. For a model instance which is a single mathematical model generated by a GAMS solve statement, the GamsModelInstance class provides a controlled way of modifying a model instance and solving the resulting problem in the most efficient way, by communicating only the changes of the model to the solver and doing a hot start (in case of a continuous model like LP) without the use of disk IO.

The GamsModelInstance requires a GamsCheckpoint that contains the model definition. Significant parts of the GAMS solve need to be provided for the instantiation of the GamsModelInstance. The modification of the model instance is done through data in sync_db (a property of GamsModelInstance of type GamsDatabase). One needs to create GamsModifiers which contain the information on how to modify the GamsModelInstance. Such a GamsModifier consists either of a GamsParameter or of a triple with the GamsVariable or GamsEquation to be updated, the modification action (e.g. Upper, Lower or Fixed for updating bounds of a variable, or Primal/Dual for updating the level/marginal of a variable or equation mainly used for starting non-linear models from different starting points), and a GamsParameter that holds the data for modification. GamsSymbols of a GamsModifier must belong to sync_db. The list of GamsModifiers needs to be supplied on the instantiate call. The use of GamsParameters that are GamsModifiers is restricted in the GAMS model source. For example, the parameter cannot be used inside $(). Such parameters become endogenous to the model and will be treated by the GAMS compiler as such. Moreover, the rim of the model instance is fixed: No addition of variables and equations is possible.

The instantiate call will only query the symbol information of the GamsModifiers, not the data of sync_db, e.g. to retrieve the dimension of the modifiers. That's why the modifier symbols have to exist (but don't have to have data) in sync_db when instantiate is called. The GamsParameters that contain the update data in sync_db can be filled at any time before executing the solve method. The solve method uses this data to update the model instance. The solve method will iterate through all records of modifier symbols in the model instance and try to find update data in sync_db. If a record in sync_db is found, this data record will be copied into the model instance. If no corresponding record is found in SyncDB there are different choices: 1) the original data record is restored (update_type=SymbolUpdateType.BaseCase) which is the default, 2) the default record of a GamsParameter (which is 0) is used (update_type=SymbolUpdateType.Zero, and 3) no copy takes place and we use the previously copied record value (update_type=SymbolUpdateType.Accumulate). After the model instance has been updated, the model is passed to the selected solver.

After the completion of the Solve method, the sync_db will contain the primal and dual solution of the model just solved. Moreover, the GamsParameters that are GamsModifiers are also accessible in sync_db as GamsVariables with the name of the GamsParameter plus "_var". The Marginal of this GamsVariable can provide sensitivity information about the parameter setting. The status of the solve is accessible through the model_status and solver_status properties.

In general, file operations in GAMS Python API take place in the working_directory defined in the GamsWorkspace. Exceptions to this rule are files read or created due to solver specific options in the solve routine of GamsModelInstance. These files are written to (or read from) the current directory, meaning the directory where the application gets executed.

Example on how to create a GAMSModelInstance from a GAMSCheckpoint that was generated by the Run method of GAMSJob.

ws = GamsWorkspace()
cp = ws.add_checkpoint()
ws.gamslib("trnsport")
job = ws.add_job_from_file("trnsport.gms")
job.run(checkpoint=cp)
mi = cp.add_modelinstance()
b = mi.sync_db.add_parameter("b", 1, "demand")
mi.instantiate("transport us lp min z", GamsModifier(b))
bmult = [ 0.7, 0.9, 1.1, 1.3 ]
for bm in bmult:
b.clear()
for rec in job.out_db.get_parameter("b"):
b.add_record(rec.keys).value = rec.value * bm
mi.solve()
print("Scenario bmult=" + str(bm) + ":")
print(" Modelstatus: " + str(mi.model_status))
print(" Solvestatus: " + str(mi.solver_status))
print(" Obj: " + str(mi.sync_db.get_variable("z")[()].level))

Constructor & Destructor Documentation

◆ __init__()

def gams.control.execution.GamsModelInstance.__init__ (   self,
  checkpoint = None,
  modelinstance_name = None,
  source = None 
)

Constructor.

Parameters
checkpointGamsCheckpoint
modelinstance_nameIdentifier of GamsModelInstance (determined automatically if omitted)
sourcemodel instance to be copied

Member Function Documentation

◆ copy_modelinstance()

def gams.control.execution.GamsModelInstance.copy_modelinstance (   self,
  modelinstance_name = None 
)

Copies this ModelInstance to a new ModelInstance.

Parameters
modelinstance_nameIdentifier of GamsModelInstance (determined automatically if omitted)
Returns
Reference to new ModelInstance

◆ instantiate()

def gams.control.execution.GamsModelInstance.instantiate (   self,
  model_definition,
  modifiers = [],
  options = None 
)

Instantiate the GamsModelInstance.

Parameters
model_definitionModel definition
modifiersList of GamsModifiers
optionsGamsOptions

◆ solve()

def gams.control.execution.GamsModelInstance.solve (   self,
  update_type = SymbolUpdateType.BaseCase,
  output = None,
  mi_opt = None 
)

Solve model instance.

Parameters
update_typeUpdate type
outputUsed to capture GAMS log, (e.g. sys.stdout or an object created by the build-in function open())
mi_optGamsModelInstance options

Property Documentation

◆ model_status

property gams.control.execution.GamsModelInstance.model_status = property(get_model_status)
static

Status of the model.

(available after a solve)

◆ solver_status

property gams.control.execution.GamsModelInstance.solver_status = property(get_solver_status)
static

Solve status of the model.

(available after a solve)