Пример #1
0
import sys
from timeit import default_timer as timer
from gams import GamsWorkspace
from gams.database import GamsDatabase
from gams import GamsVariable

# Read execution parameters
args = sys.argv
assert len(args) > 2, "GAMS path and input path must be specified."
ws = GamsWorkspace(system_directory=args[1])
input_path: str = os.path.abspath(args[2])
assert input_path[-4:] == ".gdx", f"{input_path} is not a .gdx file."
dir_path: str = os.path.split(input_path)[0]

db: GamsDatabase = ws.add_database_from_gdx(input_path)
output: GamsDatabase = ws.add_database()

# Find the dummy variables by searching for symbols starting with 'exo_' or 'endo_'
exo_dummies = [symbol for symbol in db if symbol.name[:4] == "exo_"]
endo_dummies = [symbol for symbol in db if symbol.name[:5] == "endo_"]

endo_exo_strings = []

for dummy in exo_dummies:
    var_name = dummy.name[4:]
    if len(db.get_symbol(var_name)) > 1:
        endo_exo_strings += [
            f"{var_name}.fx{rec.keys} = {var_name}.l{rec.keys};"
            for rec in dummy
        ]
    else:
         
     Carbon_matrix.append(Carbonh)
     OPEX_matrix.append(OPEXh)
     x_limit_bot_opex_matrix.append(x_limit_bot_opex_h)
     x_limit_top_opex_matrix.append(x_limit_top_opex_h)
     x_limit_bot_co2_matrix.append(x_limit_bot_co2_h)
     x_limit_top_co2_matrix.append(x_limit_top_co2_h)
     
 
     
 ############################################
 ### generate GAMS gdx file ###    
 ############################################
 GAMS_model = "Strategic.gms"
 ws = GamsWorkspace()
 db =ws.add_database()
 
 time_set = np.char.mod('%d', year)
 store_set = np.char.mod('%d', Store_id_range[:stores])
 tech_set = np.array(tech_range)
 split_set = np.char.mod('%d', np.arange(split**2))
 
 tech = db.add_set("tech",1,"")
 t = db.add_set("t",1,"")
 s = db.add_set("s",1,"")
 d = db.add_set("d",1,"")
 
 
 for n in tech_set:
     tech.add_record(n)
 for m in time_set:
Пример #3
0
def load_db_SUT(table_in,RoW=None):

    data_path = load_config()['paths']['data']

    '''CREATE GAMS WORKSPACE'''

    ws = GamsWorkspace(os.path.join(data_path,'gams_runs'))

    ''' CREATE INPUT FILES GAMS GDX '''

    db = ws.add_database()

    #set regions
    reg = db.add_set("reg",1,"Regions")
    for r in (table_in.countries):
        reg.add_record(r)

    #set rowcol
    rowcol = db.add_set("rowcol",1,"All rows and columns")
    industries = list(table_in.sectors)
    products = list(table_in.products)
    final_demand = ['FinalD']

    Import_lab  = ['Import']
    Export_lab  = ['Export']
    VA_lab = ['VA']

    rowcol_input = industries + final_demand + Export_lab  + products + VA_lab
    for r in (rowcol_input):
        rowcol.add_record(r)

    #set row
    row = db.add_set("row",1,"All rows")
    row_input = products + VA_lab + Import_lab
    for r in (row_input):
        row.add_record(r)

    #set col
    col = db.add_set("col",1,"All columns")
    col_input = industries + final_demand
    for r in (col_input):
        col.add_record(r)

    #set industries
    industries_ = db.add_set("ind",1,"Industries")
    for r in  industries:
        industries_.add_record(r)

    #set Use table
    use_m = db.add_parameter("REG_USE2013", 4, "Interaction matrix")
    for k, v in table_in.Use.items():
        use_m.add_record(k).value = v

    #set Supply table
    sup_m = db.add_parameter("REG_SUP2013", 4, "Interaction matrix")
    for k, v in table_in.Sup.items():
        sup_m.add_record(k).value = v

    #set export ROW
    exp = db.add_parameter("ExpROW_ini", 3, "Exports to ROW")
    for k, v in table_in.ExpROW.items():
        exp.add_record(k).value = v

    #set export ROW
    imp = db.add_parameter("ImpROW_ini", 3, "Imports from ROW")
    for k, v in table_in.ImpROW.items():
        imp.add_record(k).value = v

    #set ValueA
    val = db.add_parameter("ValueA_ini", 3, "Value Added")
    for k, v in table_in.ValueA.items():
        val.add_record(k).value = v

    # And save to GDX file
    db.export(os.path.join(data_path,"gams_runs","{}.gdx".format(table_in.name)))
Пример #4
0
def load_db_IO(table_in,EORA=False,RoW=None):

    data_path = load_config()['paths']['data']


    '''CREATE GAMS WORKSPACE'''

    ws = GamsWorkspace(os.path.join(data_path,'gams_runs'))

    ''' CREATE INPUT FILES GAMS GDX '''

    db = ws.add_database()

    #set regions
    reg = db.add_set("reg",1,"Regions")
    if EORA is True:
        for r in (table_in.countries+['ROW']):
            reg.add_record(r)
    else:
        for r in (table_in.countries):
            reg.add_record(r)

    #set rowcol
    rowcol = db.add_set("rowcol",1,"All rows and columns")
    if EORA is True:
        industries = list(table_in.sectors)  + ['Total']
        final_demand = list(table_in.FD_labels['FD'].unique())

    else:
        industries = list(table_in.sectors)
        final_demand = list(table_in.FD_labels['tfd'].unique())

    Import_lab  = ['Import']
    Export_lab  = ['Export']
    VA_lab = ['VA']

    rowcol_input = industries + final_demand + VA_lab + Import_lab + Export_lab
    for r in (rowcol_input):
        rowcol.add_record(r)

    #set row
    row = db.add_set("row",1,"All rows")
    row_input = industries + VA_lab + Import_lab
    for r in (row_input):
        row.add_record(r)

    #set col
    col = db.add_set("col",1,"All columns")
    col_input = industries + final_demand
    for r in (col_input):
        col.add_record(r)

    #set industries
    industries_ = db.add_set("S",1,"Industries")
    for r in  industries:
        industries_.add_record(r)

    #set FinalD
    fd_ = GamsParameter(db,"FinDem_ini", 4, "FinDem")
    for k, v in table_in.FinalD.items():
        fd_.add_record(k).value = v

    #set interaction matrix of intermediate demand
    z_m = db.add_parameter("Z_matrix_ini", 4, "Interaction matrix")
    for k, v in table_in.Z_matrix.items():
        z_m.add_record(k).value = v

    #set interaction matrix of intermediate demand
    a_m = db.add_parameter("A_matrix_ini", 4, "A matrix")
    for k, v in table_in.A_matrix.items():
        a_m.add_record(k).value = v

    if EORA is not True:
        #set Export ROW
        exp = db.add_parameter("ExpROW_ini", 3, "Exports to ROW")
        for k, v in table_in.ExpROW.items():
            exp.add_record(k).value = v

    #set ValueA
    val = db.add_parameter("ValueA_ini", 3, "Value Added")
    for k, v in table_in.ValueA.items():
        val.add_record(k).value = v

    # And save to GDX file
    db.export(os.path.join(data_path,"gams_runs","{}.gdx".format(table_in.name)))
Пример #5
0
def load_db_IO(table_in):
    """
    Load the Input-Output data from the **io_basic** Class object and converts it to a GAMS .gdx file.

    Parameters
        - table_in - **io_basic** class object, containing all IO data

    Outputs
        - .gdx file of the IO data
    """

    data_path = 'C:\\Dropbox\\OIA\\Argentina\\Data'  #load_config()['paths']['data']
    """CREATE GAMS WORKSPACE"""

    ws = GamsWorkspace(os.path.join(data_path, 'gams_runs'))
    """ CREATE INPUT FILES GAMS GDX """

    db = ws.add_database()

    # set regions
    reg = db.add_set("reg", 1, "Regions")
    for r in (table_in.regions):
        reg.add_record(r)

    # set rowcol
    rowcol = db.add_set("rowcol", 1, "All rows and columns")

    industries = list(table_in.sectors)
    final_demand = list(table_in.FD_labels['tfd'].unique())

    Import_lab = ['Import']
    Export_lab = ['Export']
    VA_lab = ['VA']

    rowcol_input = industries + final_demand + VA_lab + Import_lab  # + Export_lab
    for r in (rowcol_input):
        rowcol.add_record(r)

    # set row
    row = db.add_set("row", 1, "All rows")
    row_input = industries + VA_lab + Import_lab
    for r in (row_input):
        row.add_record(r)

    # set col
    col = db.add_set("col", 1, "All columns")
    col_input = industries + final_demand
    for r in (col_input):
        col.add_record(r)

    # set industries
    industries_ = db.add_set("S", 1, "Industries")
    for r in industries:
        industries_.add_record(r)

    # set FinalD
    fd_ = GamsParameter(db, "FinDem_ini", 4, "FinDem")
    for k, v in table_in.FinalD.items():
        fd_.add_record(k).value = v

    # set interaction matrix of intermediate demand
    z_m = db.add_parameter("Z_matrix_ini", 4, "Interaction matrix")
    for k, v in table_in.Z_matrix.items():
        z_m.add_record(k).value = v

    # set interaction matrix of intermediate demand
    a_m = db.add_parameter("A_matrix_ini", 4, "A matrix")
    for k, v in table_in.A_matrix.items():
        a_m.add_record(k).value = v

    # set Export ROW
    exp = db.add_parameter("ExpROW_ini", 3, "Exports to ROW")
    for k, v in table_in.ExpROW.items():
        exp.add_record(k).value = v

    # set ValueA
    val = db.add_parameter("ValueA_ini", 3, "Value Added")
    for k, v in table_in.ValueA.items():
        val.add_record(k).value = v

    # And save to GDX file
    db.export(
        os.path.join(data_path, "gams_runs", "{}.gdx".format(table_in.name)))