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)
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)
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)
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)
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
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
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 = {}
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
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)
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
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
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 print('ERROR: the set could not be written to the gdx file. Error msg: ' + str(e))