Example #1
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)
Example #2
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)
Example #3
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 = {}
Example #4
0
    def write(self, filename):
        # only write if all symbols loaded
        for symbol in self:
            if not symbol.loaded:
                raise Error(
                    "All symbols must be loaded before this file can be written."
                )

        ret = gdxcc.gdxOpenWrite(self.H, filename, "gdxpds")
        if not ret:
            raise GdxError(
                self.H,
                "Could not open {} for writing. Consider cloning this file (.clone()) before trying to write"
                .format(repr(filename)))
        self._filename = filename

        # set special values
        ret = gdxcc.gdxSetSpecialValues(self.H, self.special_values)
        if ret == 0:
            raise GdxError(self.H, "Unable to set special values")

        # write the universal set
        self.universal_set.write()

        for i, symbol in enumerate(self, start=1):
            try:
                symbol.write(index=i)
            except:
                logger.error("Unable to write {} to {}".format(
                    symbol, filename))
                raise

        gdxcc.gdxClose(self.H)
Example #5
0
        for i in range(records):
            ok, elements, values, afdim = gdxcc.gdxDataReadStr(H)
            if not ok: raise gdxx.GDX_error(H, "Error in gdxDataReadStr")
            if sinfo["dims"] == 0:
                read_symbol(H, self, symbol_name, sinfo["typename"], values)
            else:
                for d in range(sinfo["dims"]-1):
                    key = elements[d]
                    keys[d][key] = True
                    if (len(current_list) < d+2) or (current_list[d+1][0] != key):
                        current_list = current_list[0:d+1]
                        if not key in current_list[d][1]:
                            num_dims_created += 1
                            current_list[d][1][key] = gdxdim(self)
                        current_list = current_list + [(key, current_list[d][1][key])]
                d = sinfo["dims"]-1
                key = elements[d]
                keys[d][key] = True
                read_symbol(H, current_list[d][1], key, sinfo["typename"], values)
        logger.debug("Created {} gdxdims for {} records (ratio = {}). len(current_list) = {}".format(
            num_dims_created, records, float(num_dims_created)/float(records), len(current_list)))

#- Write a GDX file ------------------------------------------------------------

    def write(self, filename, gams_dir=None):
        H = gdxx.open(gams_dir)
        assert gdxcc.gdxOpenWrite(H, filename, "gdxdict.py")[0], "Couldn't open %s" % filename

        # write the universal set
        gdxcc.gdxUELRegisterRawStart(H)
        for key, key_data in self.universal_items():
Example #6
0
    def write(self, filename, gams_dir=None):
        H = gdxx.open(gams_dir)
        assert gdxcc.gdxOpenWrite(
            H, filename, "gdxdict.py")[0], "Couldn't open %s" % filename

        # write the universal set
        gdxcc.gdxUELRegisterRawStart(H)
        for key, key_data in self.universal_items():
            gdxcc.gdxUELRegisterRaw(H, key_data['name'])
        gdxcc.gdxUELRegisterDone(H)

        for k in self:
            symbol = self[k]
            info = self.getinfo(k)
            if info["dims"] == 0:
                if not gdxcc.gdxDataWriteStrStart(
                        H, k, info["description"], 0,
                        get_type_code(info["typename"]), info["userinfo"]):
                    raise gdxx.GDX_error(H, "couldn't start writing data")
                set_symbol(H, self, k, info["typename"], info["userinfo"],
                           values, [])
                gdxcc.gdxDataWriteDone(H)
            else:
                if not gdxcc.gdxDataWriteStrStart(
                        H, k, info["description"], info["dims"],
                        get_type_code(info["typename"]), info["userinfo"]):
                    raise gdxx.GDX_error(H, "couldn't start writing data")
                domain = []
                for d in info["domain"]:
                    domain.append(d["key"])
                if gdxcc.gdxSymbolSetDomain(H, domain) != 1:
                    raise gdxx.GDX_error(
                        H, "couldn't set domain for symbol %s to %s" %
                        (k, domain))
                write_symbol(H, info["typename"], info["userinfo"], symbol, [])
                gdxcc.gdxDataWriteDone(H)

        gdxcc.gdxClose(H)
        gdxcc.gdxFree(H)
Example #7
0
def test_roundtrip_just_special_values(manage_rundir):
    outdir = os.path.join(run_dir,'special_values')
    if not os.path.exists(outdir):
        os.mkdir(outdir)
    # create gdx file containing all special values
    with gdxpds.gdx.GdxFile() as f:
        df = pds.DataFrame([['sv' + str(i+1), f.special_values[i]] for i in range(gdxcc.GMS_SVIDX_MAX-2)],
                           columns=['sv','Value'])
        logger.info("Special values are:\n{}".format(df))

        # save this directly as a GdxSymbol
        filename = os.path.join(outdir,'direct_write_special_values.gdx')
        ret = gdxcc.gdxOpenWrite(f.H,filename,"gdxpds")
        if not ret:
            raise gdxpds.gdx.GdxError(f.H,"Could not open {} for writing. Consider cloning this file (.clone()) before trying to write".format(repr(filename)))
        # set special values
        ret = gdxcc.gdxSetSpecialValues(f.H,f.special_values)
        if ret == 0:
            raise gdxpds.gdx.GdxError(f.H,"Unable to set special values")
        # write the universal set
        f.universal_set.write()
        if not gdxcc.gdxDataWriteStrStart(f.H,
                                          'special_values',
                                          '',
                                          1,
                                          gdxpds.gdx.GamsDataType.Parameter.value,
                                          0):
            raise gdxpds.gdx.GdxError(f.H,"Could not start writing data for symbol special_values")
        # set domain information
        if not gdxcc.gdxSymbolSetDomainX(f.H,1,[df.columns[0]]):
            raise gdxpds.gdx.GdxError(f.H,"Could not set domain information for special_values.")
        values = gdxcc.doubleArray(gdxcc.GMS_VAL_MAX)
        for row in df.itertuples(index=False,name=None):
            dims = [str(x) for x in row[:1]]
            vals = row[1:]
            for col_name, col_ind in gdxpds.gdx.GAMS_VALUE_COLS_MAP[gdxpds.gdx.GamsDataType.Parameter]:
                values[col_ind] = float(vals[col_ind])
            gdxcc.gdxDataWriteStr(f.H,dims,values)
        gdxcc.gdxDataWriteDone(f.H)
        gdxcc.gdxClose(f.H)

    # general test for expected values
    def check_special_values(gdx_file):
        df = gdx_file['special_values'].dataframe
        for i, val in enumerate(df['Value'].values):
            assert gdxpds.gdx.gdx_val_equal(gdx_file.np_to_gdx_svs[i],gdx_file.special_values[i],gdx_file)

    # now roundtrip it gdx-only
    with gdxpds.gdx.GdxFile(lazy_load=False) as f:
        f.read(filename)
        check_special_values(f)
        with f.clone() as g:
            rt_filename = os.path.join(outdir,'roundtripped.gdx')
            g.write(rt_filename)
    with gdxpds.gdx.GdxFile(lazy_load=False) as g:
        g.read(filename)
        check_special_values(g)

    # now roundtrip it through csv
    roundtripped_gdx = roundtrip_one_gdx(filename,'roundtrip_just_special_values')
    with gdxpds.gdx.GdxFile(lazy_load=False) as h:
        h.read(roundtripped_gdx)
        check_special_values(h)
Example #8
0
    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))
        gdxcc.gdxDataWriteDone(gdxHandle)