示例#1
0
def solve_high_level(gams_folder,sim_folder,output_lst=False):
    """Use higher level apis to run GAMSy"""
    # create GAMS workspace:
    try:
        from gams import GamsWorkspace
        ws = GamsWorkspace(system_directory=str(gams_folder), debug=3)
        shutil.copy(os.path.join(sim_folder, 'UCM_h.gms'), ws.working_directory)
        shutil.copy(os.path.join(sim_folder, 'Inputs.gdx'), ws.working_directory)
        shutil.copy(os.path.join(sim_folder, 'cplex.opt'), ws.working_directory)
        t1 = ws.add_job_from_file('UCM_h.gms')
        opt = ws.add_options()
        # Do not create .lst file
        if not output_lst:
            if sys.platform == 'win32':
                opt.output = 'nul'
            else:
                opt.output = '/dev/null'
        time0 = time.time()
        status = t1.run(opt)
    except Exception as e:
        if 'optCreateD' in str(e):
            logging.error('The GAMS solver can only be run once in the same console. Please open another console')
            sys.exit(1)
        else:
            logging.error('The following error occured when trying to solve the model in gams: ' + str(e))
            sys.exit(1)
    # copy the result file to the simulation environment folder:
    shutil.copy(os.path.join(ws.working_directory, 'Results.gdx'), sim_folder)
    for filename in ['UCM_h.lst','UCM_h.log','debug.gdx']:
        if os.path.isfile(os.path.join(ws.working_directory, filename)):
            shutil.copy(os.path.join(ws.working_directory, filename), sim_folder)
    logging.info('Completed simulation in {0:.2f} seconds'.format(time.time() - time0))
    return status
示例#2
0
def ratmarg_SUT(table_in,EORA=False):

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

    table_in.prep_data()

    load_db_SUT(table_in)

    '''
    RUN SCRIPT WITH DISRUPTION
    '''
    setdir = os.path.join(data_path,'gams_runs')
    ws = GamsWorkspace(setdir)
    ws.get_working_directory()

    gamsfile_in =  os.path.join(setdir,"obtain_marg_value_SUT.gms")
    gamsfile = os.path.join(setdir,"obtain_marg_value_SUT_{}.gms".format(table_in.name))
    copyfile(gamsfile_in, gamsfile)
    str_ctry = ','.join(table_in.countries)
    str_fd = 'FinalD' #','.join(list(table_in.FD_labels['FD'].unique()))


    with open(gamsfile, 'r') as file:
        # read a list of lines into data
        data = file.readlines()

    gdx_file = "%s.gdx" % table_in.name
    data[30] = '$GDXIN '+gdx_file+'\n'

    str_ind = ','.join(table_in.sectors)
    data[40] ='ind(col) list of industries  /'+str_ind+'/\n'

    data[37] = '/'+str_ctry+'/\n'
    data[39] = '/'+str_ctry+'/\n'
    data[43]  = '/'+str_fd+'/\n'

    with open(gamsfile, 'w') as file:
        file.writelines( data )

    gamsfile_run = gamsfile.replace("..\\..\\gams_runs\\", "")
    t1 = ws.add_job_from_file(gamsfile_run)

    t1.run()

    Ratmarg = []
    index_ = []
    for rec in t1.out_db["Ratmarg"]:
        index_.append((rec.keys[0],rec.keys[1]))
        Ratmarg.append(rec.get_value())

    index_ = pd.MultiIndex.from_tuples(index_, names=('CNTRY', 'IND'))
    Ratmarginal = pd.DataFrame(Ratmarg, index=index_).unstack()
    Ratmarginal.columns = Ratmarginal.columns.droplevel()

    Ratmarginal.to_csv(os.path.join(data_path,'input_data','Ratmarg_{}.csv'.format(table_in.name)))

    return Ratmarginal
示例#3
0
def ratmarg_IO(table_in):
    """
    Estimate marginal values of the rationing variable in GAMS. GAMS is required, as the marginal values of a variable are not returned in the free python solvers.

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

    Outputs
        - pandas DataFrame with the marginal values of the rationing variable

    """

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

    table_in.prep_data()

    load_db_IO(table_in)
    """
    RUN SCRIPT WITH DISRUPTION
    """
    setdir = os.path.join(data_path, 'gams_runs')
    ws = GamsWorkspace(setdir)
    ws.get_working_directory()

    gamsfile_in = os.path.join(data_path, "gams_runs", "obtain_marg_value.gms")
    gamsfile = os.path.join(data_path, "gams_runs",
                            "obtain_marg_value_{}.gms".format(table_in.name))
    copyfile(gamsfile_in, gamsfile)
    str_ctry = ','.join(table_in.regions)
    str_fd = ','.join(list(table_in.FD_labels['tfd'].unique()))

    with open(gamsfile, 'r') as file:
        # read a list of lines into data
        data = file.readlines()

    gdx_file = "{}.gdx".format(table_in.name)
    data[26] = '$GDXIN ' + gdx_file + '\n'

    str_ind = ','.join(table_in.sectors)
    data[32] = 'S(col) list of industries  /' + str_ind + '/\n'

    data[34] = '/' + str_ctry + '/\n'
    data[36] = '/' + str_ctry + '/\n'
    data[38] = '/' + str_fd + '/\n'

    with open(gamsfile, 'w') as file:
        file.writelines(data)

    gamsfile_run = gamsfile.replace("..\\..\\gams_runs\\", "")
    print(gamsfile_run)
    t1 = ws.add_job_from_file(gamsfile_run)

    t1.run()

    Ratmarg = []
    index_ = []
    for rec in t1.out_db["Ratmarg"]:
        index_.append((rec.keys[0], rec.keys[1]))
        Ratmarg.append(rec.get_value())

    index_ = pd.MultiIndex.from_tuples(index_, names=('CNTRY', 'IND'))
    Ratmarginal = pd.DataFrame(Ratmarg, index=index_).unstack()
    Ratmarginal.columns = Ratmarginal.columns.droplevel()

    Ratmarginal.to_csv(
        os.path.join(data_path, 'input_data',
                     'Ratmarg_{}.csv'.format(table_in.name)))

    return Ratmarginal
示例#4
0
def solve_GAMS(sim_folder, gams_folder=None, work_dir=None, output_lst=False):
    if not package_exists('gams'):
        logging.warning(
            'Could not import gams. Trying to automatically locate gdxcc folder'
        )
        if not import_local_lib('gams'):
            return False
    if not os.path.exists(gams_folder):
        logging.warn('The provided path for GAMS (' + gams_folder +
                     ') does not exist. Trying to locate...')
        gams_folder = get_gams_path()
        if not os.path.exists(gams_folder):
            logging.error('GAMS path cannot be located. Simulation is stopped')
            return False
    sim_folder = sim_folder.encode()
    gams_folder = gams_folder.encode()

    if is_sim_folder_ok(sim_folder):
        # create GAMS workspace:
        from gams import GamsWorkspace
        try:
            ws = GamsWorkspace(system_directory=gams_folder,
                               working_directory=work_dir,
                               debug=3)
            shutil.copy(os.path.join(sim_folder, 'UCM_h.gms'),
                        ws.working_directory)
            shutil.copy(os.path.join(sim_folder, 'Inputs.gdx'),
                        ws.working_directory)
            t1 = ws.add_job_from_file('UCM_h.gms')
            opt = ws.add_options()
            #Do not create .lst file
            if not output_lst:
                if sys.platform == 'win32':
                    opt.output = 'nul'
                else:
                    opt.output = '/dev/null'
            time0 = time.time()
            t1.run(opt)
        except Exception as e:
            if 'optCreateD' in str(e):
                logging.error(
                    'The GAMS solver can only be run once in the same console. Please open another console'
                )
                sys.exit(1)
            else:
                logging.error(
                    'The following error occured when trying to solve the model in gams: '
                    + str(e))
                sys.exit(1)
        logging.info(
            'Completed simulation in {0:.2f} seconds'.format(time.time() -
                                                             time0))

        # copy the result file to the simulation environment folder:
        shutil.copy(os.path.join(ws.working_directory, 'Results.gdx'),
                    sim_folder)
        for filename in ['UCM_h.lst', 'UCM_h.log', 'debug.gdx']:
            if os.path.isfile(os.path.join(ws.working_directory, 'debug.gdx')):
                shutil.copy(os.path.join(ws.working_directory, 'debug.gdx'),
                            sim_folder)
        if os.path.isfile(os.path.join(ws.working_directory, 'debug.gdx')):
            logging.warn(
                'A debug file was created. There has probably been an optimization error'
            )
        if os.path.isfile('warn.log'):
            shutil.copy('warn.log', os.path.join(sim_folder, 'warn_solve.log'))
    else:
        return False