Loading...
Searching...
No Matches
Matlab.GAMS.GAMSModelInstance Class Reference

Modifying a model instance and solving the resulting problem in the most efficient way. More...

Public Member Functions

GAMSModelInstance copyModelInstance (varargin)
 Copies this ModelInstance to a new ModelInstance.
 
void dispose ()
 Release external resources hold by non-java library.
 
void instantiate (varargin)
 Instantiate the GAMSModelInstance.
 
void interrupt ()
 Send interrupt signal to running GAMSModelInstance.
 
void solve (varargin)
 Solve model instance.
 

Public Attributes

ModelStat modelStatus
 (read only) Status of the model (available after a solve)
 
string name
 (read only) Name of GAMSModelInstance
 
SolveStat solveStatus
 (read only) Solve status of the model (available after a solve)
 
GAMSDatabase syncDB
 (read only) GAMSDatabase used to synchronize modifiable data
 

Detailed Description

Modifying a model instance and solving the resulting problem in the most efficient way.

A GAMSJob 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 instances 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 syncDB (a property of GAMSModelInstance of type GAMSDatabase). One needs to create GAMSModifier 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. GAMSSymbol instances of a GAMSModifier must belong to syncDB. The list of GAMSModifier instances needs to be supplied on the Instantiate call. The use of GAMSParameter instancess that are GAMSModifier instances 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 GAMSModifier, not the data of syncDB, 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 syncDB when Instantiate is called. The GAMSParameter instances that contain the update data in syncDB 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 syncDB. If a record in syncDB is found, this data record will be copied into the model instance. If no corresponding record is found in syncDB there are different choices:

  • the original data record is restored (SymbolUpdateType = BASECASE) which is the default,
  • the default record of a GAMSParameter (which is 0) is used (SymbolUpdateType = ZERO), and
  • no copy takes place and we use the previously copied record value (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 syncDB will contain the primal and dual solution of the model just solved. Moreover, the GAMSParameters that are GAMSModifier are also accessible in syncDB as GAMSVariable 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 modelStatus and solveStatus properties (see GAMSGlobals).

A GAMSModelInstance is connected to external resources and needs to be properly disposed before the Java garbage collector can claim the instance.

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

* ws = GAMS.GAMSWorkspace();
* cp = ws.addCheckpoint();
* 
* job = ws.addJobFromGamsLib('trnsport');
* job.run(cp);
* 
* mi = cp.addModelInstance();
* b = mi.syncDB.addParameter('b', 1, 'demand');
* 
* mi.instantiate('transport us lp min z', GAMS.GAMSModifier(b));
* 
* bmultlist = [0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2, 1.3];
* for i = 1:numel(bmultlist)
* b.clear();
* for rec = job.outDB.getParameter('b').records
* b.addRecord(rec{1}.keys).value = rec{1}.value bmultlist(i);
* mi.solve();
* fprintf('Scenario bmult=%f:\n', bm);
* fprintf('  Modelstatus: %s\n', mi.modelStatus);
* fprintf('  Solvestatus: %s\n', mi.solveStatus);
* fprintf('  Obj: %g\n', mi.syncDB.getVariable('z').findRecord().level);
* end
* 
See also
GAMSCheckpoint, GAMSCheckpoint.addModelInstance, GAMSModelInstanceOpt, GAMSModifier, GAMSSymbol

Member Function Documentation

◆ copyModelInstance()

GAMSModelInstance Matlab.GAMS.GAMSModelInstance.copyModelInstance ( varargin  )

Copies this ModelInstance to a new ModelInstance.

Valid VARARGIN signatures:

  • [ ]
  • string modelInstanceName

Arguments:

  • modelInstanceName: Identifier of GAMSModelInstance (determined automatically if omitted)

Return: Instance of GAMSModelInstance

◆ dispose()

void Matlab.GAMS.GAMSModelInstance.dispose ( )

Release external resources hold by non-java library.

A subsequent call on the object after disposed potentially causes an unexpected error or exception. Call this method either when the object is no longer needed and/or when resource management is a critical issue in the application.

◆ instantiate()

void Matlab.GAMS.GAMSModelInstance.instantiate ( varargin  )

Instantiate the GAMSModelInstance.

Valid VARARGIN signatures:

  • string modelDefinition, GAMSModifier modifier1, ..., GAMSModifier modifierN
  • string modelDefinition, {GAMSModifier modifier1, ..., GAMSModifier modifierN}
  • string modelDefinition, GAMSOptions opt, GAMSModifier modifier1, ..., GAMSModifier modifierN
  • string modelDefinition, GAMSOptions opt, {GAMSModifier modifier1, ..., GAMSModifier modifierN}

Arguments:

  • modelDefinition: Model definition
  • modifier1,...,modifierN: List of GAMSModifier(s)
  • opt: An instance of GAMSOptions
See also
GAMSModifier, GAMSOptions

◆ interrupt()

void Matlab.GAMS.GAMSModelInstance.interrupt ( )

Send interrupt signal to running GAMSModelInstance.

This method is useful for interrupting the long running GAMSModelInstance.

◆ solve()

void Matlab.GAMS.GAMSModelInstance.solve ( varargin  )

Solve model instance.

The Solve method will iterate through all records of modifier symbols in the model instance and try to find update data in syncDB. If a record in syncDB is found, this data record will be copied into the model instance. If no corresponding record is found in syncDB there are different choices:

  • the original data record is restored (updateType = BASECASE) which is the default,
  • the default record of a GAMSParameter (which is 0) is used (updateType = ZERO), and
  • no copy takes place and we use the previously copied record value (updateType = ACCUMULATE). Other symbol types (e.g., updateType = INHERIT) is an invalid update type for solve statement. After the model instance has been updated, the model is passed to the selected solver.

Valid VARARGIN signatures:

  • [ ]
  • GAMSModelInstanceOpt miOpt
  • SymbolUpdateType updateType
  • string output
  • SymbolUpdateType updateType, GAMSModelInstanceOpt miOpt
  • SymbolUpdateType updateType, string output
  • string output, GAMSModelInstanceOpt miOpt
  • SymbolUpdateType updateType, string output, GAMSModelInstanceOpt miOpt

Arguments:

  • updateType: Update type
  • output: File name to store output in or 'cmdout' to print to command line
  • miOpt: GAMSModelInstance option
See also
SymbolUpdateType, GAMSModelInstanceOpt