Table of Contents
- Getting started
- Important Classes of the API
- How to use the API
- How to choose the GAMS system (transport1)
- How to export data to GDX (transport_gdx)
- How to import data from GDX (transport_gdx)
- How to run a GAMSJob from file (transport1)
- How to retrieve a solution from an output database (transport1)
- How to specify the solver using GAMSOptions (transport1)
- How to run a job with a solver option file and capture its log output (transport1)
- How to use include files (transport2)
- How to set a non-default working directory (transport3)
- How to read data from string and export to GDX (transport3)
- How to run a job using data from GDX (transport3)
- How to run a job using implicit database communication (transport3)
- How to define data using Matlab data structures (transport4)
- How to prepare a GAMSDatabase from Matlab data structures (transport4)
- How to initialize a GAMSCheckpoint by running a GAMSJob (transport5)
- How to initialize a GAMSJob from a GAMSCheckpoint (transport5)
- How to create a GAMSModelInstance from a GAMSCheckpoint (transport7)
- How to modify a parameter of a GAMSModelInstance using GAMSModifier (transport7)
- How to modify a variable of a GAMSModelInstance using GAMSModifier (transport7)
- How to create and use a save/restart file (transport11)
The goal of this tutorial is to provide a compact overview of the basic functionality of the GAMS Matlab API. It allows the user to start immediately working with the API by providing a set of small examples based on the well-known transportation problem. These examples introduce several API features step by step.
- Getting started A quick introduction about how to add the API to the Matlab path
- Important Classes of the API Overview of some fundamental classes of the GAMS Matlab API
- How to use the API An extensive set of examples describing how to use API components
Getting started
The object oriented GAMS Matlab API is built on top of the GAMS Java API and provides convenient access to GAMS from within Matlab (2017b or later) and Octave (5.2 or later). Examples using the API are located in apifiles/Matlab/examples
and apifiles/Matlab/examples_octave
for Matlab or Octave, respectively. The examples slightly differ for Octave since it does not support the complete Matlab syntax and functionalities - most importantly to note, enumeration classes (see also the note below). The object oriented API is located in apifiles/Matlab/api
.
For making your Matlab installation aware of the packages provided by GAMS, you need to do the following two steps. First, add the GAMS Java API to the Matlab java class path. This can be done statically or dynamically. The latter is less efficient but more convenient for testing.
- static: Locate the
javaclasspath.txt
and add the path[PathToGAMS]/apifiles/Java/api/GAMSJavaAPI.jar
. You can find the location withcd(prefdir)
in Matlab orwhich javaclasspath
in Octave. Finally, restart Matlab or Octave. - dynamic: Call the function
javaaddpath("[PathToGAMS]/apifiles/Java/api/GAMSJavaAPI.jar")
.
Second, add the GAMS Matlab API to the Matlab path by calling addpath("[PathToGAMS]/apifiles/Matlab/api")
.
- Note
- Octave does not support enumeration classes (yet). In order to use the GAMS enumerations, please use the alternative integer or string value. For example, instead of using
GAMS.DebugLevel.SHOW_LOG
, use2
, see DebugLevel. To tell the GAMS Matlab API to return the alternative values instead of instances of enumeration classes, set the environment variableGAMS_MATLAB_ENUM_ALT
totrue
, i.e.,setenv("GAMS_MATLAB_ENUM_ALT", "true");
.
You can test your setup by creating a GAMSWorkspaceInfo:
- Note
- In Matlab you can import the GAMS package by
import GAMS.*
. Then, you don't need to call the GAMS classes with the precedingGAMS.
.
Important Classes of the API
This section provides a quick overview of some fundamental classes of the GAMS package. Their usage is demonstrated by an extensive set of examples. All GAMS Matlab API classes are contained within one single package GAMS
. It provides objects to interact with the General Algebraic Modeling System (GAMS
). Objects in this package allow convenient exchange of input data and model results (GAMSDatabase) and help to create and run GAMS models (GAMSJob), that can be customized by GAMS options (GAMSOptions). Furthermore, it introduces a way to solve a sequence of closely related models in the most efficient way (GAMSModelInstance).
Other classes are GAMSWorkspace, GAMSOptions and GAMSSymbol.
How to use the API
In the GAMS system directory there are some examples provided that illustrate the usage of the Matlab API. [PathToGAMS]/apifiles/Matlab/examples
contains multiple examples dealing - among others - with the well-known transportation problem. In further course of this tutorial we discuss these examples step by step and introduce new elements of the API in detail.
We recommend to open the aforementioned files to gain a complete overview of the examples. Down below we explain the examples with the help of selected code snippets.
- How to choose the GAMS system (transport1)
- How to export data to GDX (transport_gdx)
- How to import data from GDX (transport_gdx)
- How to run a GAMSJob from file (transport1)
- How to retrieve a solution from an output database (transport1)
- How to specify the solver using GAMSOptions (transport1)
- How to run a job with a solver option file and capture its log output (transport1)
- How to use include files (transport2)
- How to set a non-default working directory (transport3)
- How to read data from string and export to GDX (transport3)
- How to run a job using data from GDX (transport3)
- How to run a job using implicit database communication (transport3)
- How to define data using Matlab data structures (transport4)
- How to prepare a GAMSDatabase from Matlab data structures (transport4)
- How to initialize a GAMSCheckpoint by running a GAMSJob (transport5)
- How to initialize a GAMSJob from a GAMSCheckpoint (transport5)
- How to create a GAMSModelInstance from a GAMSCheckpoint (transport7)
- How to modify a parameter of a GAMSModelInstance using GAMSModifier (transport7)
- How to modify a variable of a GAMSModelInstance using GAMSModifier (transport7)
- How to create and use a save/restart file (transport11)
How to choose the GAMS system (transport1)
By default the GAMS system is determined automatically. In case of having multiple GAMS systems on your machine, the desired system can be specified via an additional argument when the workspace is created. When running the examples, we can provide an additional command line argument in order to define the GAMS system directory that should be used. By executing transport1
with C:/GAMS/win64/43.3 we use the 64-bit version of GAMS 43.3 to run transport1
even if our default GAMS system might be a different one. This is managed by the following code:
- Note
- The API can detect GAMS automatically from the
PATH
environment variable. Please note that this is not theMATLABPATH
. You can inspect thePATH
withgetenv("PATH")
.
How to export data to GDX (transport_gdx)
Although the Object-oriented Matlab API offers much more than exchanging data between Matlab and GDX, a common use case is the export and import of GDX files. The central class for this purpose is GAMSDatabase. We assume that the data to be exported is available in Matlab data structures.
Different type of GAMS symbols are represented using different Matlab data structures. The data for the GAMS sets is represented using a cell of strings (e.g. plants
and markets
). On the other hand, GAMS parameters are represented by a containers.Map (e.g. capacity
and demand
). Note that the representation of the two dimensional parameter distance
uses a dot notation for storing the keys. The choice of data structures can also be different, but the used structures in this example fit well for representing GAMS data with Matlab data structures.
A new GAMSDatabase instance can be created using GAMSWorkspace.addDatabase.
We start adding GAMS sets using the method GAMSDatabase.addSet
which takes the name and the dimension as arguments. The third argument is an optional explanatory text. A for-loop iterates through plants
and adds new records to the recently created GAMSSet instance i
using GAMSSet.addRecord
.
GAMSParameter instances can be added by using the method GAMSDatabase.addParameter
. In this example we use the overloaded method which takes a list of GAMSSet instances instead of the dimension for creating a parameter with domain information.
As soon as all data is prepared in the GAMSDatabase, the method GAMSDatabase.export
can be used to create a GDX file.
How to import data from GDX (transport_gdx)
Data can be imported from a GDX file using GAMSWorkspace.addDatabaseFromGDX
. The method takes a path to a GDX file and creates a GAMSDatabase instance.
Reading the data from the GAMSSet i
into a cell of strings can be done as follows:
i
is retrieved by calling GAMSDatabase.getSet
on gdxdb
. The returned GAMSSet object has an attribute records
with an cell array of GAMSSSetRecords. set. Each record can be asked for its keys.
You can do the same for GAMSParameter. Instead of creating a cell, we want to have the data in the form of a containers.Map. GAMSParameterRecord can not only be asked for its keys, but also for its value. The following code snippet shows how to read the one dimensional parameter a
into a map.
For a key of multi dimensional symbol, we choose a dot based concatenation of keys.
Scalar can be read into a variable of type double by accessing the value of the first and only record.
How to run a GAMSJob from file (transport1)
At first we create our workspace using GAMSWorkspace ws = GAMS.GAMSWorkspace();
. Afterwards, we can create a GAMSJob t1
using the addJobFromGamsLib method and run it.
Apparently you can create a GAMSJob with any other gms file you might have created on your own as long as it is located in the current working directory. Then the GAMSJob t1
can be defined using the GAMSJob.addJobFromFile method.
How to retrieve a solution from an output database (transport1)
The following lines create the solution output and illustrate the usage of the GAMSJob.outDB property to get access to the GAMSDatabase created by the run method. To retrieve the content of variable x
we use the getVariable method and the GAMSVariableRecord class.
How to specify the solver using GAMSOptions (transport1)
The solver can be specified via the GAMSOptions class and the GAMSWorkspace.addOptions method. The GAMSOptions.setAllModelTypes property sets xpress
as default solver for all model types which the solver can handle. Then we run our GAMSJob t1
with the new GAMSOptions.
How to run a job with a solver option file and capture its log output (transport1)
At first we create the file xpress.opt
with content algorithm=barrier
which will be used as solver option file and is stored in the current working directory. Afterward we use a GAMSOptions just like in the preceding example and GAMSOptions.optFile property to 1 to tell the solver to look for a solver option file. We specify the argument output
in order to stream the log of the GAMSJob into the file transport1_xpress.log
. When the output
argument is omitted then the log will be written to standard output.
How to use include files (transport2)
In this example, as in many succeeding, the data text and the model text are separated into two different strings. Note that these strings data
and model
are using GAMS syntax.
At first we write an include file tdata.gms
that contains the data but not the model text:
Afterwards we create a GAMSJob using the GAMSWorkspace.addJobFromString method. GAMSOptions.defines is used like the the 'double dash' GAMS parameters, i.e. it corresponds to --incname=tdata
on the command line where incname is used as name for the include file in the model
string.
The string model contains the following lines to read in the data.
How to set a non-default working directory (transport3)
At first we create a new directory. Once this is done we can use this directory when creating the GAMSWorkspace and make it the working directory.
How to read data from string and export to GDX (transport3)
We read the data from the string data. Note that this contains no model but only data definition in GAMS syntax. By running the corresponding GAMSJob a GAMSDatabase is created that is available via the GAMSJob.outDB property. We can use the GAMSDatabase.export method to write the content of this database to a gdx file tdata.gdx
.
How to run a job using data from GDX (transport3)
This works quite similar to the usage of an include file explained in transport2
- How to use include files (transport2) .
Note that there are some minor changes in the model due to the usage of a gdx instead of an include file.
How to run a job using implicit database communication (transport3)
This example does basically the same as the two preceding examples together. We create two GAMSJobs t3a
and t3b
where the first one contains only the data and the second one contains only the model without data. After running t3a
the corresponding outDB can be read in directly just like a gdx file. Note that the database needs to be passed to the GAMSJob.run method as additional argument.
How to define data using Matlab data structures (transport4)
We use cell arrays and containers.Map to define Matlab data structures that correspond to the sets, parameters and tables used for the data definition in GAMS.
How to prepare a GAMSDatabase from Matlab data structures (transport4)
At first we create an empty GAMSDatabase db
using the GAMSWorkspace.addDatabase method. Afterwards we prepare the database. To add a set to the database we use the GAMSSet class and the GAMSDatabase.addSet method with arguments describing the identifier, dimension and explanatory text. To add the records to the database we iterate over the elements of our Matlab data structure and add them by using the GAMSSet.addRecord method.
For parameters the procedure is pretty much the same. Note that the table that specifies the distances in GAMS can be treated as parameter with dimension 2 and that scalars can be treated as parameter with dimension 0.
The GAMSJob can be run like explained in the preceding example about implicit database communication.
How to initialize a GAMSCheckpoint by running a GAMSJob (transport5)
The following lines of code conduct several operations. While the first line simply creates a GAMSCheckpoint, the second one uses the GAMSWorkspace.addJobFromString method to create a GAMSJob containing the model text and data but no solve statement. Afterwards the run method gets the GAMSCheckpoint as argument. That means the GAMSCheckpoint cp
captures the state of the GAMSJob.
How to initialize a GAMSJob from a GAMSCheckpoint (transport5)
Note that the string returned from function model
contains the entire model and data definition plus an additional demand multiplier and scalars for model and solve status but no solve statement:
In transport5 we create a list with eight different values for this demand multiplier.
For each entry of that list we create a GAMSJob t5
using the GAMSWorkspace.addJobFromString method. Besides another string which resets the demand multiplier bmult
, specifies the solve statement and assigns values to the scalars ms
and ss
we pass the checkpoint cp as additional argument. This results in a GAMSJob combined from the checkpoint plus the content provided by the string. We run the GAMSJob and echo some interesting data from the outDB using the GAMSDatabase.getParameter and GAMSDatabase.getVariable methods and the GAMSSymbol.record attribute plus the GAMSParameterRecord.value and the GAMSVariableRecord.level properties.
- Note
- Some of the demand multipliers cause infeasibility. Nevertheless, GAMS keeps the incumbent objective function value. Therefore the model status and the solve status provide important information for a correct solution interpretation.
How to create a GAMSModelInstance from a GAMSCheckpoint (transport7)
In transport7 the usage of Matlab::GAMS::GAMSModelInstance is demonstrated.
At first checkpoint cp
is created as in the preceding examples. Then we create the GAMSModelInstance mi
using the GAMSCheckpoint.addModelInstance method. Note that the GAMSJob again contains no solve statement and the demand multiplier is already included with default value 1.
How to modify a parameter of a GAMSModelInstance using GAMSModifier (transport7)
A GAMSModelInstance uses a syncDB
to maintain the data. We define bmult
as GAMSParameter using the GAMSParameter method and specify cplex as solver. Afterwards the GAMSModelInstance is instantiated with 3 arguments, the solve statement, GAMSOptions opt
and GAMSModifier bmult
. The GAMSModifier means that bmult
is modifiable while all other parameters, variables and equations of ModelInstance mi
stay unchanged. We use the GAMSParameter.addRecord method and the value property to assign a value to bmult
. That value can be varied afterwards using the GAMSSymbol.record property to reproduce our well-known example with different demand multipliers.
How to modify a variable of a GAMSModelInstance using GAMSModifier (transport7)
We create a GAMSModelInstance just like in the next to last example. We define x
as GAMSVariable and its upper bound as GAMSParameter xup
. At the following instantiate method GAMSModifier has 3 arguments. The first one says that x
is modifiable, the second determines which part of the variable (lower bound, upper bound or level) can be modified and the third specifies the GAMSParameter that holds the new value.
In the following loops we set the upper bound of one link of the network to zero, which means that no transportation between the corresponding plant and market is possible, and solve the modified transportation problem.
How to create and use a save/restart file (transport11)
In transport11 we demonstrate how to create and use a save/restart file. Usually such a file should be supplied by an application provider but in this example we create one for demonstration purpose. Note that the restart is launched from a GAMSCheckpoint. From the main function we call the function create_save_restart
giving it the current workspace settings and the desired file name as arguments.
In function create_save_restart
we create a workspace with the given workspace settings. Then we create a GAMSJob from a string. Note that the string given via basemodel
contains the basic definitions of sets without giving them a content (that is what $onempty
is used for). Afterwards we specify a GAMSOptions to only compile the job but do not execute it. Then we create a checkpoint cp
that is initialized by the following run of the GAMSJob and stored in the file given as argument to the function, in our case tbase
. This becomes possible because the addCheckpoint method accepts identifiers as well as file names as argument.
So what you should keep in mind before we return to further explanations of the main function is, that the file tbase is now in the current working directory and contains a checkpoint. Now in the main function we define some data using Matlab data structures as we already did in transport4 before we create the GAMSWorkspace and a GAMSDatabase.
Afterwards we set up the GAMSDatabase like we already did in transport4. Once this is done we run a GAMSJob using this data plus the checkpoint stored in file tbase.
Note that the string from which we create job t11
is different to the one used to prepare the checkpoint stored in tbase
and is only responsible for reading in the data from the GAMSDatabase correctly. The entire model definition is delivered by the checkpoint cpBase
which is equal to the one we saved in tbase
.