Mathematical models (ixmp.model
)¶
By default, the ix modeling platform is installed with ixmp.model.gams.GAMSModel
, which performs calculations by executing code stored in GAMS files.
However, ix modeling platform is extensible to support other methods of performing calculations or optimization.
Developers wishing to add such capabilities may subclass ixmp.model.base.Model
and implement its methods.
Provided models¶
-
ixmp.model.
MODELS
= {'dantzig': <class 'ixmp.model.dantzig.DantzigModel'>, 'default': <class 'ixmp.model.gams.GAMSModel'>, 'gams': <class 'ixmp.model.gams.GAMSModel'>}¶ Mapping from names to available models. To register additional models, add elements to this variable.
-
ixmp.model.
get_model
(name, **model_options)¶ Return a model for name (or the default) with model_options.
-
class
ixmp.model.gams.
GAMSModel
(name=None, **model_options)¶ General class for ixmp models using GAMS.
GAMSModel solves a Scenario using the following steps:
All Scenario data is written to a model input file in GDX format.
A GAMS program is run to perform calculations, producing output in a GDX file.
Output, or solution, data is read from the GDX file and stored in the Scenario.
When created and
run()
, GAMSModel constructs file paths and other necessary values using format strings. Thedefaults
may be overridden by the keyword arguments to the constructor:- Other Parameters
name (str, optional) – Override the
name
attribute to provide the model_name for format strings.model_file (str, optional) – Path to GAMS file, including ‘.gms’ extension. Default:
'{model_name}.gms'
(in the current directory).case (str, optional) – Run or case identifier to use in GDX file names. Default:
'{scenario.model}_{scenario.name}'
, where scenario is theScenario
object passed torun()
. Formatted using model_name and scenario.in_file (str, optional) – Path to write GDX input file. Default:
'{model_name}_in.gdx'
. Formatted using model_name, scenario, and case.out_file (str, optional) – Path to read GDX output file. Default:
'{model_name}_out.gdx'
. Formatted using model_name, scenario, and case.solve_args (list of str, optional) – Arguments to be passed to GAMS, e.g. to identify the model input and output files. Each formatted using model_file, scenario, case, in_file, and out_file. Default:
'--in="{in_file}"'
'--out="{out_file}"'
gams_args (list of str, optional) – Additional arguments passed directly to GAMS without formatting, e.g. to control solver options or behaviour. See the GAMS Documentation. For example:
'LogOption=4'
prints output to stdout (not console) and the log file.
check_solution (bool, optional) – If
True
, raise an exception if the GAMS solver did not reach optimality. (Only for MESSAGE-scheme Scenarios.)comment (str, optional) – Comment added to Scenario when importing the solution. If omitted, no comment is added.
equ_list (list of str, optional) – Equations to be imported from the out_file. Default: all.
var_list (list of str, optional) – Variables to be imported from the out_file. Default: all.
-
defaults
= {'case': '{scenario.model}_{scenario.scenario}', 'check_solution': True, 'comment': None, 'equ_list': None, 'gams_args': ['LogOption=4'], 'in_file': '{temp_dir}/{model_name}_in.gdx', 'model_file': '{model_name}.gms', 'out_file': '{temp_dir}/{model_name}_out.gdx', 'solve_args': ['--in="{in_file}"', '--out="{out_file}"'], 'use_temp_dir': True, 'var_list': None}¶ Default model options.
-
name
= 'default'¶ Model name.
-
run
(scenario)¶ Execute the model.
-
class
ixmp.model.dantzig.
DantzigModel
(name=None, **model_options)¶ Dantzig’s cannery/transport problem as a
GAMSModel
.Provided for testing
ixmp
code.
Model API¶
-
class
ixmp.model.base.
Model
(name, **kwargs)¶ In the following, the words required, optional, etc. have specific meanings as described in IETF RFC 2119.
Model is an abstract class; this means it MUST be subclassed. It has two REQURIED methods that MUST be overridden by subclasses:
Name of the model.
__init__
(name, **kwargs)Constructor.
initialize
(scenario)Set up scenario with required items.
initialize_items
(scenario, items)Helper for
initialize()
.run
(scenario)Execute the model.
-
abstract
__init__
(name, **kwargs)¶ Constructor.
- Parameters
kwargs –
Model options, passed directly from
Scenario.solve()
.Model subclasses MUST document acceptable option values.
-
classmethod
initialize
(scenario)¶ Set up scenario with required items.
Implementations of
initialize()
:may add sets, set elements, and/or parameter values.
may accept any number of keyword arguments to control behaviour.
must not modify existing parameter data in scenario, either by deleting or overwriting values.
- Parameters
scenario (Scenario) – Object to initialize.
See also
-
classmethod
initialize_items
(scenario, items)¶ Helper for
initialize()
.All of the items are added to scenario. Existing items are not modified. Errors are logged if the description in items conflicts with the index set(s) and/or index name(s) of existing items.
initialize_items may perform one commit. scenario is in the same state (checked in, or checked out) after initialize_items is complete.
- Parameters
scenario (Scenario) – Object to initialize.
items (dict of (str -> dict)) – Each key is the name of an ixmp item (set, parameter, equation, or variable) to initialize. Each dict must have the key ‘ix_type’; one of ‘set’, ‘par’, ‘equ’, or ‘var’; any other entries are keyword arguments to the methods
init_set()
etc.
- Raises
ValueError – if scenario has a solution, i.e.
has_solution()
isTrue
.
See also
-
name
= 'base'¶ Name of the model.
-
abstract