예제 #1
0
파일: gdxdump.py 프로젝트: vlmeg/py-gdx
def main(argv=None):
    if argv is None:
        argv = sys.argv

    parser = optparse.OptionParser(usage="%prog [options] <input gdx>")
    parser.add_option("-g", "--gams-dir", help="Specify the GAMS installation directory", default=None)

    if True:
        #    try:
        options, args = parser.parse_args(argv)

        if len(args) != 2:
            parser.error("Wrong number of arguments (try python %s --help)" % args[0])

        H = gdxx.open(options.gams_dir)
        dump_GDX_file(H, args[1])
    #
    #    except (optparse.OptionError, TypeError), err:
    #        print >>sys.stderr, err
    #        return 2
    #    except gdxx.GDX_error, err:
    #        print >>sys.stderr, "GDX Error: %s" % err.msg
    #        if err.msg == "Couldn't find the GAMS system directory":
    #            print "  Try specifying where GAMS is with the -g option"
    #        return 2
    #
    return 1
예제 #2
0
파일: gdxdict.py 프로젝트: djhume/py-gdx
    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 i in range(len(self.order)):
            gdxcc.gdxUELRegisterRaw(H, self.order[i])
        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)
예제 #3
0
def main(argv=None):
    if argv is None:
        argv = sys.argv

    parser = optparse.OptionParser(usage = "%prog [options] <input gdx>")
    parser.add_option("-g", "--gams-dir", help="Specify the GAMS installation directory", default=None)

    if True:
#    try:
        options, args = parser.parse_args(argv)

        if len(args) != 2:
            parser.error("Wrong number of arguments (try python %s --help)" % args[0])

        H = gdxx.open(options.gams_dir)
        dump_GDX_file(H, args[1])
#
#    except (optparse.OptionError, TypeError), err:
#        print >>sys.stderr, err
#        return 2
#    except gdxx.GDX_error, err:
#        print >>sys.stderr, "GDX Error: %s" % err.msg
#        if err.msg == "Couldn't find the GAMS system directory":
#            print "  Try specifying where GAMS is with the -g option"
#        return 2
#
    return 1
예제 #4
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 i in range(len(self.order)):
            gdxcc.gdxUELRegisterRaw(H, self.order[i])
        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)
예제 #5
0
파일: gdxdict.py 프로젝트: djhume/py-gdx
    def read(self, filename, gams_dir=None):
        H = gdxx.open(gams_dir)
        assert gdxcc.gdxOpenRead(H, filename)[0], "Couldn't open %s" % filename

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

        # 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]

        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]
                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)
            symbol_name = sinfo["name"]
            all_keys[symbol_name] = []
            keys = all_keys[symbol_name]
            for d in range(sinfo["dims"]): keys.append({})

            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")
                if sinfo["dims"] == 0:
                    read_symbol(H, self, symbol_name, sinfo["typename"], values)
                else:
                    symbol = self[sinfo["name"]]
                    current = symbol
                    for d in range(sinfo["dims"]-1):
                        key = elements[d]
                        keys[d][key] = True
                        if not key in current:
                            current[key] = gdxdim(self)
                        current = current[key]
                    d = sinfo["dims"]-1
                    key = elements[d]
                    keys[d][key] = True
                    read_symbol(H, current, key, sinfo["typename"], values)

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

        guess_domains(self, set_map, all_keys)
        guess_ancestor_domains(self)
예제 #6
0
    def read(self, filename, gams_dir=None):
        H = gdxx.open(gams_dir)
        assert gdxcc.gdxOpenRead(H, filename)[0], "Couldn't open %s" % filename

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

        # 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]

        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]
                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)
            symbol_name = sinfo["name"]
            all_keys[symbol_name] = []
            keys = all_keys[symbol_name]
            for d in range(sinfo["dims"]):
                keys.append({})

            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")
                if sinfo["dims"] == 0:
                    read_symbol(H, self, symbol_name, sinfo["typename"],
                                values)
                else:
                    symbol = self[sinfo["name"]]
                    current = symbol
                    for d in range(sinfo["dims"] - 1):
                        key = elements[d]
                        keys[d][key] = True
                        if not key in current:
                            current[key] = gdxdim(self)
                        current = current[key]
                    d = sinfo["dims"] - 1
                    key = elements[d]
                    keys[d][key] = True
                    read_symbol(H, current, key, sinfo["typename"], values)

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

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