Example #1
0
    def read_symbol(self, symbol_name):
        if not (self.lazy_load and self.filename):
            raise RuntimeError(
                """This feature only works if the gdxdict is initialized in 
                               lazy_load mode, and read has already been called."""
            )
        if not symbol_name in self:
            raise RuntimeError(
                "{} is not a symbol in this gdxdict.".format(symbol_name))

        H = gdxx.open(self.gams_dir)
        assert gdxcc.gdxOpenRead(
            H, self.filename)[0], "Couldn't open %s" % filename

        sinfo = self.getinfo(symbol_name)
        set_map = self.set_map
        all_keys = self.all_keys

        self.__read_one_symbol(H, sinfo, all_keys)

        gdxcc.gdxClose(H)
        gdxcc.gdxFree(H)

        guess_domains(self, set_map, all_keys)
        guess_ancestor_domains(self)
        self.set_map = set_map
        self.all_keys = all_keys
Example #2
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 #3
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 #4
0
                continue
                
            logger.debug("Reading {}. ".format(sinfo['name']) + gdxpds.memory_use_str())
            self.__read_one_symbol(H, sinfo, all_keys)
            logger.debug("Read {}. ".format(sinfo['name']) + gdxpds.memory_use_str())

        gdxcc.gdxClose(H)
        gdxcc.gdxFree(H)
        
        logger.debug("Closed the gdx file. " + gdxpds.memory_use_str())

        guess_domains(self, set_map, all_keys)
        guess_ancestor_domains(self)
        
        logger.debug("Finished guessing domains. " + gdxpds.memory_use_str())
        if self.lazy_load:
            self.set_map = set_map
            self.all_keys = all_keys
        
    def read_symbol(self, symbol_name):
        if not (self.lazy_load and self.filename):
            raise RuntimeError("""This feature only works if the gdxdict is initialized in 
                               lazy_load mode, and read has already been called.""")
Example #5
0
    def read(self, filename, gams_dir=None):
        if self.lazy_load:
            self.filename = filename
            self.gams_dir = gams_dir

        H = gdxx.open(gams_dir)
        assert gdxcc.gdxOpenRead(H, filename)[0], "Couldn't open %s" % filename

        logger.debug("Opened the file. " + gdxpds.memory_use_str())

        info = gdxx.file_info(H)
        for k in info:
            if not k in self.file_info:
                self.file_info[k] = info[k]

        logger.debug("Retrieved the file info. " + gdxpds.memory_use_str())

        # read the universal set
        uinfo = gdxx.symbol_info(H, 0)
        for k in uinfo:
            if not k in self.universal_info:
                self.universal_info[k] = uinfo[k]

        logger.debug("Read the universal set. " + gdxpds.memory_use_str())

        ok, records = gdxcc.gdxDataReadStrStart(H, 0)
        for i in range(records):
            ok, elements, values, afdim = gdxcc.gdxDataReadStr(H)
            if not ok: raise gdxx.GDX_error(H, "Error in gdxDataReadStr")
            key = elements[0]
            ret, description, node = gdxcc.gdxGetElemText(
                H, int(values[gdxcc.GMS_VAL_LEVEL]))
            if ret == 0: description = None
            self.add_key(key, description)

        all_keys = {}

        # Read all the 1-D sets
        # Map backwards so we have a map from every set key back to all the sets it's in
        set_map = {}
        for i in range(1, info["symbol_count"] + 1):
            sinfo = gdxx.symbol_info(H, i)
            if sinfo["typename"] == "Set" and sinfo["dims"] == 1:

                self.add_symbol(sinfo)
                symbol_name = sinfo["name"]
                all_keys[symbol_name] = [{}]
                keys = all_keys[symbol_name]
                symbol = self[symbol_name]
                symbol._gdxdim__read_in = True
                ok, records = gdxcc.gdxDataReadStrStart(H, i)
                for i in range(records):
                    ok, elements, values, afdim = gdxcc.gdxDataReadStr(H)
                    if not ok:
                        raise gdxx.GDX_error(H, "Error in gdxDataReadStr")
                    e = elements[0]
                    read_symbol(H, symbol, e, sinfo["typename"], values)
                    if not e in set_map: set_map[e] = {}
                    set_map[e][symbol_name] = True
                    keys[0][e] = True

        # Read all the other symbols
        for i in range(1, info["symbol_count"] + 1):
            sinfo = gdxx.symbol_info(H, i)
            if sinfo["typename"] == "Set" and sinfo["dims"] == 1: continue

            self.add_symbol(sinfo)
            if self.lazy_load and sinfo["dims"] > 0:
                continue

            logger.debug("Reading {}. ".format(sinfo['name']) +
                         gdxpds.memory_use_str())
            self.__read_one_symbol(H, sinfo, all_keys)
            logger.debug("Read {}. ".format(sinfo['name']) +
                         gdxpds.memory_use_str())

        gdxcc.gdxClose(H)
        gdxcc.gdxFree(H)

        logger.debug("Closed the gdx file. " + gdxpds.memory_use_str())

        guess_domains(self, set_map, all_keys)
        guess_ancestor_domains(self)

        logger.debug("Finished guessing domains. " + gdxpds.memory_use_str())
        if self.lazy_load:
            self.set_map = set_map
            self.all_keys = all_keys
Example #6
0
            
        info["type"] = typecode
        info["typename"] = typename


# -- Read a gdx file -----------------------------------------------------------

    def read(self, filename, gams_dir=None):
        if self.lazy_load:
            self.filename = filename
            self.gams_dir = gams_dir
    
        H = gdxx.open(gams_dir)
        assert gdxcc.gdxOpenRead(H, filename)[0], "Couldn't open %s" % filename
        
        logger.debug("Opened the file. " + gdxpds.memory_use_str())

        info = gdxx.file_info(H)
        for k in info:
            if not k in self.file_info:
                self.file_info[k] = info[k]
                
        logger.debug("Retrieved the file info. " + gdxpds.memory_use_str())

        # read the universal set
        uinfo = gdxx.symbol_info(H, 0)
        for k in uinfo:
            if not k in self.universal_info: 
                self.universal_info[k] = uinfo[k]

        logger.debug("Read the universal set. " + gdxpds.memory_use_str())
                
        ok, records = gdxcc.gdxDataReadStrStart(H, 0)        
        for i in range(records):
            ok, elements, values, afdim = gdxcc.gdxDataReadStr(H)
            if not ok: raise gdxx.GDX_error(H, "Error in gdxDataReadStr")
            key = elements[0]
            ret, description, node = gdxcc.gdxGetElemText(H, int(values[gdxcc.GMS_VAL_LEVEL]))
            if ret == 0: description = None
            self.add_key(key, description)
        
        all_keys = {}

        # Read all the 1-D sets
        # Map backwards so we have a map from every set key back to all the sets it's in
        set_map = {}
        for i in range(1, info["symbol_count"]+1):
            sinfo = gdxx.symbol_info(H, i)
            if sinfo["typename"] == "Set" and sinfo["dims"] == 1:

                self.add_symbol(sinfo)
                symbol_name = sinfo["name"]
                all_keys[symbol_name] = [{}]
                keys = all_keys[symbol_name]
                symbol = self[symbol_name]
                symbol._gdxdim__read_in = True
                ok, records = gdxcc.gdxDataReadStrStart(H, i)
                for i in range(records):
                    ok, elements, values, afdim = gdxcc.gdxDataReadStr(H)
                    if not ok: raise gdxx.GDX_error(H, "Error in gdxDataReadStr")
                    e = elements[0]
                    read_symbol(H, symbol, e, sinfo["typename"], values)
                    if not e in set_map: set_map[e] = {}
                    set_map[e][symbol_name] = True
                    keys[0][e] = True
        
        # Read all the other symbols
        for i in range(1, info["symbol_count"]+1):
            sinfo = gdxx.symbol_info(H, i)
            if sinfo["typename"] == "Set" and sinfo["dims"] == 1: continue

            self.add_symbol(sinfo)