Пример #1
0
    def __init__(self, args, connection=None):
        import gdxcc
        self.gdxcc = gdxcc
        self.gdx_handle = gdxcc.new_gdxHandle_tp()
        rc = gdxcc.gdxCreate(self.gdx_handle, gdxcc.GMS_SSSIZE)
        log.info("=============================>" + str(rc))
        if rc[0] == 0:
            raise HydraPluginError('Could not find GAMS installation.')
        self.symbol_count = 0
        self.element_count = 0
        self.gdx_variables = dict()
        self.gams_units = dict()
        self.gdx_ts_vars = dict()
        self.network_id = args.network_id
        self.scenario_id = args.scenario_id
        self.network = None
        self.res_scenario = None
        self.attrs = dict()
        self.time_axis = dict()
        self.gms_data = []
        self.connection = connection

        if self.connection is None:
            self.connect(args)

        attrslist = self.connection.call('get_all_attributes', {})
        for attr in attrslist:
            self.attrs.update({attr.id: attr.name})
Пример #2
0
def get_df(file_name, param_name):
    ws = gams.GamsWorkspace()

    gdxFile = os.path.join(os.getcwd(), file_name)

    gdxHandle = gdxcc.new_gdxHandle_tp()
    rc =  gdxcc.gdxCreate(gdxHandle, gdxcc.GMS_SSSIZE)
    assert rc[0],rc[1]
    assert gdxcc.gdxOpenRead(gdxHandle, gdxFile)[0]
    nrUels = gdxcc.gdxUMUelInfo(gdxHandle)[1]
    uelMap = []

    for i in range(nrUels+1):
        uelMap.append(gdxcc.gdxUMUelGet(gdxHandle, i)[1])
    ret, symNr = gdxcc.gdxFindSymbol(gdxHandle, param_name)
    assert ret, param_name + " parameter not found"
    ret, nrRecs = gdxcc.gdxDataReadRawStart(gdxHandle, symNr)
    assert ret, "Error in gdxDataReadRawStart: " + gdxcc.gdxErrorStr(gdxHandle, gdxcc.gdxGetLastError(gdxHandle))[1]

    ls = []
    for i in range(nrRecs):
        ret = gdxcc.gdxDataReadRaw(gdxHandle)
        sets = [uelMap[x] for x in ret[1]]
        val = ret[2][gdxcc.GMS_VAL_LEVEL]
        if val == 5e300:
            val = 0
        ls.append(sets+[val])

    assert not gdxcc.gdxClose(gdxHandle)
    assert gdxcc.gdxFree(gdxHandle)
    df = pd.DataFrame(ls)
    return df
Пример #3
0
def write_variables(gams_dir, gdx_out, list_vars):
    """
    This function performs the following:
    * Use the gdxcc library to create a gdxHandle instance
    * Check that the gams path is well defined
    * Call the 'insert_symbols' function to write all sets and parameters to gdxHandle

    :param gams_dir:        (Relative) path to the gams directory
    :param gdx_out:         (Relative) path to the gdx file to be written
    :param list_vars:       List with the sets and parameters to be written
    """
    if not os.path.isdir(gams_dir):
        gams_dir = get_gams_path()
    if not os.path.isdir(gams_dir):
        logging.critical(
            'GDXCC: Could not find the specified gams directory: ' + gams_dir)
        sys.exit(1)
    gams_dir = gams_dir.encode()

    gdxHandle = gdxcc.new_gdxHandle_tp()
    gdxcc.gdxCreateD(gdxHandle, gams_dir, gdxcc.GMS_SSSIZE)
    gdxcc.gdxOpenWrite(gdxHandle, gdx_out, "")

    [sets, parameters] = list_vars
    _insert_symbols(gdxHandle, sets, parameters)

    gdxcc.gdxClose(gdxHandle)

    logging.info('Data Successfully written to ' + gdx_out)
Пример #4
0
def load_specials(gams_dir_finder):
    """
    Load special values

    Needs to be called after gdxcc is loaded. Populates the module attributes
    SPECIAL_VALUES, GDX_TO_NP_SVS, and NP_TO_GDX_SVS.

    Parameters
    ----------
    gams_dir_finder : :class:`gdxpds.tools.GamsDirFinder`
    """
    global SPECIAL_VALUES
    global GDX_TO_NP_SVS
    global NP_TO_GDX_SVS

    H = gdxcc.new_gdxHandle_tp()
    rc = gdxcc.gdxCreateD(H, gams_dir_finder.gams_dir, gdxcc.GMS_SSSIZE)
    if not rc:
        raise Exception(rc[1])
    # get special values
    special_values = gdxcc.doubleArray(gdxcc.GMS_SVIDX_MAX)
    gdxcc.gdxGetSpecialValues(H, special_values)

    SPECIAL_VALUES = []
    GDX_TO_NP_SVS = {}
    NP_TO_GDX_SVS = {}
    for i in range(gdxcc.GMS_SVIDX_MAX):
        if i >= len(NUMPY_SPECIAL_VALUES):
            break
        SPECIAL_VALUES.append(special_values[i])
        gdx_val = special_values[i]
        GDX_TO_NP_SVS[gdx_val] = NUMPY_SPECIAL_VALUES[i]
        NP_TO_GDX_SVS[NUMPY_SPECIAL_VALUES[i]] = gdx_val

    gdxcc.gdxFree(H)
Пример #5
0
def write_variables(config, gdx_out, list_vars):
    """
    This function performs the following:
    * Use the gdxcc library to create a gdxHandle instance
    * Check that the gams path is well defined
    * Call the 'insert_symbols' function to write all sets and parameters to gdxHandle

    :param config:          Main config dictionary
    :param gdx_out:         (Relative) path to the gdx file to be written
    :param list_vars:       List with the sets and parameters to be written
    """
    gams_dir = get_gams_path(gams_dir=config['GAMS_folder'].encode())
    if not gams_dir:  # couldn't locate
        logging.critical(
            'GDXCC: Could not find the specified gams directory: ' + gams_dir)
        sys.exit(1)
    gams_dir = force_str(gams_dir)
    config['GAMS_folder'] = gams_dir  # updating the config dictionary
    gdx_out = force_str(gdx_out)

    gdxHandle = gdxcc.new_gdxHandle_tp()
    gdxcc.gdxCreateD(gdxHandle, gams_dir,
                     gdxcc.GMS_SSSIZE)  #it accepts only str type
    gdxcc.gdxOpenWrite(gdxHandle, gdx_out, "")

    [sets, parameters] = list_vars
    _insert_symbols(gdxHandle, sets, parameters)

    gdxcc.gdxClose(gdxHandle)

    logging.info('Data Successfully written to ' + gdx_out)
Пример #6
0
 def __init__(self, filename=None,gamsdir=None):
     assert os.access(filename, os.R_OK) != None, 'Gdx file "{}" not found or readable!'.format(filename)
     self.internal_filename = os.path.abspath(filename)
     self.gdx_handle = gdxcc.new_gdxHandle_tp()
     rc = gdxcc.gdxCreateD(self.gdx_handle, _gamsdir, gdxcc.GMS_SSSIZE)
     assert rc[0], rc[1]
     assert gdxcc.gdxOpenRead(self.gdx_handle, self.internal_filename)[0], 'Unable to read "{}"!'.format(filename)
     for symb in self.get_symbols_list():
         setattr(self, symb.name.lower(), symb)
Пример #7
0
def open(system_dir=None):
    H = gdxcc.new_gdxHandle_tp()
    if system_dir == None:
        system_dir = paths[sys.platform]
        if type(system_dir) != str:
            system_dir = system_dir()
    if not system_dir:
        raise GDX_error(None, "Couldn't find the GAMS system directory")
    rc = gdxcc.gdxCreateD(H, system_dir, gdxcc.GMS_SSSIZE)
    if not rc[0]: raise GDX_error(H, rc[1])
    return H
Пример #8
0
def open(system_dir=None):
    H = gdxcc.new_gdxHandle_tp()
    if system_dir == None:
        system_dir = paths[sys.platform]
        if type(system_dir) != str:
            system_dir = system_dir()
    if not system_dir:
        raise GDX_error(None, "Couldn't find the GAMS system directory")
    rc = gdxcc.gdxCreateD(H, system_dir, gdxcc.GMS_SSSIZE)
    if not rc[0]:
        raise GDX_error(H, rc[1])
    return H
Пример #9
0
def load_gdxcc(gams_dir=None):
    if 'pandas' in sys.modules:
        logger.warn("Especially on Linux, gdxpds should be imported before " + \
                    "pandas to avoid a library conflict. Also make sure your " + \
                    "GAMS directory is listed in LD_LIBRARY_PATH.")
    import gdxcc
    from gdxpds.tools import GamsDirFinder
    finder = GamsDirFinder(gams_dir=gams_dir)
    H = gdxcc.new_gdxHandle_tp()
    rc = gdxcc.gdxCreateD(H, finder.gams_dir, gdxcc.GMS_SSSIZE)
    gdxcc.gdxFree(H)
    return
Пример #10
0
    def __init__(self, filename: str, mode: str = 'r', gams_dir: str = None):
        """Constructor for GdxFile
        
        Args:
            filename: str
            mode: File open mode: 'r' for reading, 'w' for writing, 'w+' for appending (replaces existing symbol)
            gams_dir (optional): Location of GAMS installation directory

        Raises:
            RuntimeError: Unable to load gdx library, invalid mode
            FileNotFoundError: Input file not found
            ValueError: Unsupported mode
            OSError: Unable to read/write file
            Exception: Other errors
        """

        self._h = gdxcc.new_gdxHandle_tp()  # Create a gdx handle
        if gams_dir is None:
            ret, err = gdxcc.gdxCreate(self._h, gdxcc.GMS_SSSIZE)
        else:
            ret, err = gdxcc.gdxCreateD(self._h, gams_dir, gdxcc.GMS_SSSIZE)
        if ret == 0:
            raise RuntimeError(err)

        creator = 'Python {}'.format(sys.version)

        self.filename = os.path.abspath(filename)
        if mode == 'r':
            ret, errno = gdxcc.gdxOpenRead(self._h, self.filename)
        elif mode == 'w':
            ret, errno = gdxcc.gdxOpenWrite(self._h, self.filename, creator)
        elif mode == 'w+' or mode == 'a':
            ret, errno = gdxcc.gdxOpenAppend(self._h, self.filename, creator)
            # Fallback to creating a new file if not found
            if ret == 0 and errno == -100041:
                ret, errno = gdxcc.gdxOpenWrite(self._h, self.filename,
                                                creator)
        else:
            raise ValueError("Unsupported mode '{}'.".format(mode))
        self._mode = mode

        # Error checking
        if ret == 0:
            if errno == 2:
                raise FileNotFoundError(self.filename)
            else:
                raise OSError(gdxcc.gdxErrorStr(self._h, errno)[1])

        # Set up unique element map
        self._UEL_map = {}
Пример #11
0
def load_gdxcc(gams_dir=None):
    """
    Method to initialize GAMS, especially to load required libraries that can 
    sometimes conflict with other packages.

    Parameters
    ----------
    gams_dir : None or str
        if not None, directory containing the GAMS executable
    """
    if 'pandas' in sys.modules:
        logger.warn("Especially on Linux, gdxpds should be imported before " + \
                    "pandas to avoid a library conflict. Also make sure your " + \
                    "GAMS directory is listed in LD_LIBRARY_PATH.")
    import gdxcc
    from gdxpds.tools import GamsDirFinder
    finder = GamsDirFinder(gams_dir=gams_dir)
    H = gdxcc.new_gdxHandle_tp()
    rc = gdxcc.gdxCreateD(H, finder.gams_dir, gdxcc.GMS_SSSIZE)
    gdxcc.gdxFree(H)
    return
Пример #12
0
 def __init__(self, filename=None,gamsdir=None):
     global __gdxpy_mode__
     # Check filename
     if filename == None:
        raise Exception('No GDX provided')
     self.internal_filename = filename
     if not os.access(filename, os.R_OK):
         raise Exception('GDX "%s" not found or readable' % filename)
     # Identify access mode (through gdxcc API or shell)
     if __gdxpy_mode__ == GDX_MODE_API:
         try:
             self.gdxHandle = gdxcc.new_gdxHandle_tp()
             rc = gdxcc.gdxCreateD(self.gdxHandle, __gdxpy_gamsdir__, gdxcc.GMS_SSSIZE)
             assert rc[0],rc[1]
             assert gdxcc.gdxOpenRead(self.gdxHandle, self.internal_filename)[0]
         except Exception as e:
             print_traceback(e)
             print("GDX API NOT WORKING: FALLING BACK TO GDX SHELL MODE")
             __gdxpy_mode__ = GDX_MODE_SHELL
     # Access symbols as class members
     #for symb in self.get_symbols_list():
     for symb in self.get_symbols_list():
         setattr(self, symb.name.lower(), symb)
Пример #13
0
def gdx_to_list(gams_dir, filename, varname='all', verbose=False):
    """
    This function loads the gdx with the results of the simulation
    All results are stored in an unordered list

    :param gams_dir:    Gams working directory
    :param filename:    Path to the gdx file to be read
    :param varname:     In case online one variable is needed, specify it name (otherwise specify 'all')
    :returns:        Dictionary with all the collected values (within lists)
    """



    from gdxcc import gdxSymbolInfo, gdxCreateD, gdxOpenRead, GMS_SSSIZE, gdxDataReadDone, new_gdxHandle_tp, \
        gdxDataReadStr, gdxFindSymbol, gdxErrorStr, gdxDataReadStrStart, gdxGetLastError
    out = {}
    tgdx = tm.time()
    gdxHandle = new_gdxHandle_tp()
    gdxCreateD(gdxHandle, gams_dir, GMS_SSSIZE)

    # make sure the file path is properly formatted:
    filename = filename.replace('/', os.path.sep).replace(
        '\\\\', os.path.sep).replace('\\', os.path.sep)
    filename = str(filename)  # removing possible unicode formatting

    if not os.path.isfile(filename):
        logging.critical('Gdx file "' + filename + '" does not exist')
        sys.exit(1)

    gdxOpenRead(gdxHandle, filename)

    if varname == 'all':
        # go through all the symbols one by one and add their data to the dict
        symNr = 0
        SymbolInfo = gdxSymbolInfo(gdxHandle, 0)
        while SymbolInfo[0] > 0:
            ret, nrRecs = gdxDataReadStrStart(gdxHandle, symNr)
            assert ret, "Error in gdx data string" + gdxErrorStr(
                gdxHandle, gdxGetLastError(gdxHandle))[1]

            res = []
            for i in range(nrRecs):
                ret, elements, values, afdim = gdxDataReadStr(gdxHandle)
                res.append(elements + [values[0]])
            out[SymbolInfo[1]] = res
            symNr += 1
            SymbolInfo = gdxSymbolInfo(gdxHandle, symNr)
    else:
        # find the number of the required symbol:
        ret, symNr = gdxFindSymbol(gdxHandle, varname)
        assert ret, "Symbol not found"

        ret, nrRecs = gdxDataReadStrStart(gdxHandle, symNr)
        assert ret, "Error in gdx data string" + gdxErrorStr(
            gdxHandle, gdxGetLastError(gdxHandle))[1]

        res = []
        for i in range(nrRecs):
            ret, elements, values, afdim = gdxDataReadStr(gdxHandle)
            res.append(elements + [values[0]])
        out[varname] = res

    gdxDataReadDone(gdxHandle)
    if verbose:
        logging.info("Loading gdx file " + filename +
                     " took {}s".format(tm.time() - tgdx))
    return out
Пример #14
0
            print ("x(" + rec.keys[0] + "," + rec.keys[1] + "): level=" + str(rec.level) + " marginal=" + str(rec.marginal))
        print('The optimization seems to have run properly')
    except Exception as e:
        print('ERROR while trying to run the optimization: ' + str(e))
        success_sim = False







if success_gdxcc and success_path:
    print('\n \nTRY TO GENERATE GDX FILE')
    try:
        gdxHandle = gdxcc.new_gdxHandle_tp()
        gdxcc.gdxCreateD(gdxHandle, gamspath, gdxcc.GMS_SSSIZE)
        gdxcc.gdxOpenWrite(gdxHandle, 'test.gdx', "")
    
        # Write a set:
        dims = 1
        setname = 'set_test'
        keys = ['aa']
        gdxcc.gdxDataWriteStrStart(gdxHandle, setname, "", dims, gdxcc.GMS_DT_SET, 0)
        gdxValues = gdxcc.doubleArray(5)
        gdxValues[gdxcc.GMS_VAL_LEVEL] = 0.0  # 0.0 == Y (explanatory text of set in gdx)
    
        try:
            success = gdxcc.gdxDataWriteStr(gdxHandle, keys, gdxValues)
        except Exception as e:
            success = False
Пример #15
0
 def __init__(self):
     """Constructor."""
     self._handle = gdxcc.new_gdxHandle_tp()
     self.error_count = 0
     self.call("CreateD", str(_gams_dir()), gdxcc.GMS_SSSIZE)
Пример #16
0
 def _create_gdx_object(self):
     H = gdxcc.new_gdxHandle_tp()
     rc = gdxcc.gdxCreateD(H, self.gams_dir, gdxcc.GMS_SSSIZE)
     if not rc:
         raise GdxError(H, rc[1])
     return H
Пример #17
0
 def __init__(self):
     """Constructor."""
     self._handle = gdxcc.new_gdxHandle_tp()
     self.error_count = 0
     self.call('CreateD', str(_gams_dir()), gdxcc.GMS_SSSIZE)