GDX-Python Documentation

GDX-Python Documentation
Release 1.0
Paul Natsuo Kishimoto
April 23, 2014
Contents
1
Installing
3
2
Tutorial
5
3
Reference
7
4
To-Dos
11
5
License
13
Python Module Index
15
i
ii
GDX-Python Documentation, Release 1.0
Contents:
Contents
1
GDX-Python Documentation, Release 1.0
2
Contents
CHAPTER 1
Installing
3
GDX-Python Documentation, Release 1.0
4
Chapter 1. Installing
CHAPTER 2
Tutorial
5
GDX-Python Documentation, Release 1.0
6
Chapter 2. Tutorial
CHAPTER 3
Reference
Note: For those reading the code: Using the Python ‘set’ class makes the code in this module simpler, but unfortunately leads to confusing semantics because the ‘Set’ is also one of the main types of GAMS symbols. To improve
clarity in the comments, capitalized ‘Set’ is used for the latter.
class gdx.GDX
Basic wrapper around the GDX API.
Most methods in the GDX API have similar semantics:
•All method names begin with gdx.
•Most methods return a list where the first element is a return code.
This class hides these details, allowing for simpler code. Methods can be accessed using call():
>>> g = GDX()
>>> g.call(’FileVersion’) # call gdxFileVersion()
Alternately, they can be accessed as “Pythonic” members of GDX objects, where CamelCase is replaced by
lowercase, with underscores separating words.
>>> g.file_version()
# same as above
static _gams_dir()
Locate GAMS on a POSIX system.
The path to the GAMS executable is a required argument of gdxCreateD, the method for connecting to
the GDX API.
Todo
This function relies on the shell utility which, and will probably not work on Windows. Extend.
call(method, *args)
Invoke the GDX API method named gdx*method*.
Optional positional arguments args are passed to the API method. Returns the result of the method call, or
raises an appropriate exception.
class gdx.Symbol(gdxfile=None, index=0, lazy=False)
Abstract base class for GAMS symbols.
Specific GAMS objects, including Set and Parameter, inherit from Symbol. To create a new symbol, use
the factory method create().
7
GDX-Python Documentation, Release 1.0
All symbols have the following attributes:
name
The symbol’s name as declared in GAMS.
dim
The number of dimensions.
records
The number of entries in the symbol’s data table.
description
The description or explanatory text assigned to the symbol in GAMS
static create(type_code, gdxfile, index, lazy)
Create a new GAMS symbol.
The created symbol is of one of the types named in type_str. type_code is one of gdxcc.GMS_DT_EQU,
gdxcc.GMS_DT_PAR, gdxcc.GMS_DT_SET or gdxcc.GMS_DT_VAR. Symbol information and data are
loaded from the symbol at position index of the File instance gdxfile.
class gdx.File(filename=’‘, mode=’r’, lazy=False)
A GDX file.
Load the file at filename into memory. mode must be ‘r’ (writing GDX files is not currently supported). If lazy
is True (the default), then the data for any GAMS parameter is not loaded until the parameter is first accessed.
For large files, this makes loading significantly faster.
Individual GAMS symbols are available in one of three ways:
1.Using get_symbol().
2.Using get_symbol_by_index().
3.As attributes of the File instance. For example, a GAMS parameter ‘myparam’ can be accessed:
>>> f = File(’example.gdx’)
>>> f.myparam.description
’Example GAMS parameter’
get_symbol(name)
Retrieve the Symbol name.
get_symbol_by_index(index)
Retrieve the Symbol stored at the index-th position in the File.
parameters()
Return a list of all Parameter objects in this File.
sets()
Return a list of all Set objects in this File.
class gdx.Symbol(gdxfile=None, index=0, lazy=False)
Abstract base class for GAMS symbols.
Specific GAMS objects, including Set and Parameter, inherit from Symbol. To create a new symbol, use
the factory method create().
All symbols have the following attributes:
static create(type_code, gdxfile, index, lazy)
Create a new GAMS symbol.
8
Chapter 3. Reference
GDX-Python Documentation, Release 1.0
The created symbol is of one of the types named in type_str. type_code is one of gdxcc.GMS_DT_EQU,
gdxcc.GMS_DT_PAR, gdxcc.GMS_DT_SET or gdxcc.GMS_DT_VAR. Symbol information and data are
loaded from the symbol at position index of the File instance gdxfile.
class gdx.Set(gdxfile=None, index=0, lazy=False)
Representation of a GAMS set.
Set elements are accessble via indexing. For example, if ‘myset’ is defined by the following GAMS code:
set myset / abc, def, ghi /;
then it will be accessible as:
>>> myset = File(’example.gdx’).myset
>>> myset[2]
’ghi’
>>> myset[:]
[’abc’, ’def’, ’ghi’]
Note that because Python uses 0-based indexing, indices will be one lower than those used with the GAMS ord()
function.
depth
The ‘depth’ of this GAMS Set.
The depth is the number of levels of inheritance between this set and GDX file’s universal set, *. In the
following GAMS code:
sets
a
/ a1, a2, a3, a4 /
b(a) / a1, a2, a4 /
c(b) / a2, a4/
;
. . . the depth of set a is 1, the depth of set b is 2, and the depth of set c is 3.
When the Set’s domain undefinited, depth is set to sys.maxsize.
idx = None
A pandas.Index or pandas.MultiIndex representing the Set.
class gdx.Parameter(gdxfile=None, index=0, lazy=False)
Representation of a GAMS parameter or variable.
The values are accessible via indexing. For example, if ‘myparam’ is defined by the following GAMS code:
sets
x / x1, x2 /
y / y1, y2 /
;
parameter myparam(x,y)
/ x1.y1 1000, x2.y2 2000 /;
then it will be accessible as:
>>> myparam = File(’example.gdx’).myparam
>>> myparam[’x1’, ’y1’]
1000
>>> myparam[’x2’, ’y2’]
2000
For the moment, passing the incorrect number of indices will not get a subset of data; it will simply fail. Integer
indexing is also not supported.
9
GDX-Python Documentation, Release 1.0
data = None
A pandas.Series containing the parameter data.
10
Chapter 3. Reference
CHAPTER 4
To-Dos
Todo
This function relies on the shell utility which, and will probably not work on Windows. Extend.
(The original entry is located in /var/build/user_builds/pygdx/checkouts/latest/gdx/__init__.py:docstring of
gdx.GDX._gams_dir, line 6.)
11
GDX-Python Documentation, Release 1.0
12
Chapter 4. To-Dos
CHAPTER 5
License
13
GDX-Python Documentation, Release 1.0
14
Chapter 5. License
Python Module Index
g
gdx, 7
15