From 5b19ffc0d13b0c3f9f036868dc4d035042d0aa5b Mon Sep 17 00:00:00 2001 From: Paul Natsuo Kishimoto Date: Tue, 11 Feb 2020 16:49:38 +0100 Subject: [PATCH] Adjust data structure used by Model.initialize_items --- ixmp/model/base.py | 12 ++++++------ ixmp/model/dantzig.py | 26 +++++++++++++------------- ixmp/model/gams.py | 1 - 3 files changed, 19 insertions(+), 20 deletions(-) diff --git a/ixmp/model/base.py b/ixmp/model/base.py index 7ff41bdd7..bb7bcfce3 100644 --- a/ixmp/model/base.py +++ b/ixmp/model/base.py @@ -57,10 +57,10 @@ def initialize_items(cls, scenario, items): ---------- scenario : .Scenario Scenario object to initialize. - items : list of dict - Each entry is one 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 + 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 :meth:`.init_set` etc. See also @@ -79,7 +79,7 @@ def initialize_items(cls, scenario, items): # attempt raises an exception pass - for item_info in items: + for name, item_info in items.items(): # Copy so that pop() below does not modify *items* item_info = item_info.copy() @@ -89,7 +89,7 @@ def initialize_items(cls, scenario, items): try: # Add the item - init_method(**item_info) + init_method(name=name, **item_info) except ValueError: # Item already exists pass diff --git a/ixmp/model/dantzig.py b/ixmp/model/dantzig.py index ffdaeb0c4..9827bc68e 100644 --- a/ixmp/model/dantzig.py +++ b/ixmp/model/dantzig.py @@ -7,26 +7,26 @@ from .gams import GAMSModel -ITEMS = ( +ITEMS = { # Plants - dict(ix_type='set', name='i'), + 'i': dict(ix_type='set'), # Markets - dict(ix_type='set', name='j'), + 'j': dict(ix_type='set'), # Capacity of plant i in cases - dict(ix_type='par', name='a', idx_sets=['i']), + 'a': dict(ix_type='par', idx_sets=['i']), # Demand at market j in cases - dict(ix_type='par', name='b', idx_sets=['j']), + 'b': dict(ix_type='par', idx_sets=['j']), # Distance between plant i and market j - dict(ix_type='par', name='d', idx_sets=['i', 'j']), + 'd': dict(ix_type='par', idx_sets=['i', 'j']), # Transport cost per case per 1000 miles - dict(ix_type='par', name='f', idx_sets=None), + 'f': dict(ix_type='par', idx_sets=None), # Decision variables and equations - dict(ix_type='var', name='x', idx_sets=['i', 'j']), - dict(ix_type='var', name='z', idx_sets=None), - dict(ix_type='equ', name='cost', idx_sets=None), - dict(ix_type='equ', name='demand', idx_sets=['j']), - dict(ix_type='equ', name='supply', idx_sets=['i']), -) + 'x': dict(ix_type='var', idx_sets=['i', 'j']), + 'z': dict(ix_type='var', idx_sets=None), + 'cost': dict(ix_type='equ', idx_sets=None), + 'demand': dict(ix_type='equ', idx_sets=['j']), + 'supply': dict(ix_type='equ', idx_sets=['i']), +} DATA = { 'i': ['seattle', 'san-diego'], diff --git a/ixmp/model/gams.py b/ixmp/model/gams.py index 876b13221..2d7026c5d 100644 --- a/ixmp/model/gams.py +++ b/ixmp/model/gams.py @@ -5,7 +5,6 @@ from ixmp.backend import ItemType -from ixmp.backend.jdbc import JDBCBackend from ixmp.model.base import Model from ixmp.utils import as_str_list