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
def __init__(self, wdir, DATA, GMS_SETUP, grid_rep, model_horizon): # Import Logger self.logger = logging.getLogger('Log.MarketModel.GamsModel') self.logger.info("Initializing MarketModel..") self.wdir = wdir self.workspace = gams.GamsWorkspace(working_directory=self.wdir.joinpath("gams")) self.gams_db = self.workspace.add_database() self.gams_result_db = self.workspace.add_database() self.model_type = GMS_SETUP["opt"] self.model_horizon = ['t'+ "{0:0>4}".format(x) for x in model_horizon] self.nodes = DATA.nodes self.zones = DATA.zones self.lines = DATA.lines self.plants = DATA.plants self.heatareas = DATA.heatareas self.demand_el = DATA.demand_el self.demand_h = DATA.demand_h self.availability = DATA.availability self.dclines = DATA.dclines self.init_gamsdb(grid_rep) self.check_domain_violations() self.gams_model = self.init_gams_model(GMS_SETUP) self.logger.info("MarketModel initialized!")
def __init__(self, database=None, workspace=None, auto_sort_index=True): if database is None: if workspace is None: workspace = gams.GamsWorkspace() database = workspace.add_database() self.database = database self.auto_sort_index = auto_sort_index self.series = {}
def __init__(self, database=None, workspace=None, auto_sort_index=True, sparse=True): if database is None: if workspace is None: workspace = gams.GamsWorkspace() database = workspace.add_database() self.database = database self.gams2numpy = gams2numpy.Gams2Numpy(database.workspace.system_directory) self.auto_sort_index = auto_sort_index self.sparse = sparse self.series = {}
def write_psse_to_gdx(psse_data, filename): # set up gams env and db ws = gams.GamsWorkspace() ws.__init__(working_directory=os.getcwd()) db = ws.add_database() # write to db write_psse_to_gdx_db(psse_data, db) # export to gdx db.export(filename)
def __init__(self, file_path, workspace=None): if not file_path: import easygui file_path = easygui.fileopenbox("Select reference gdx file", filetypes=["*.gdx"]) if not file_path: raise ValueError("No file path was provided") if not os.path.splitext(file_path)[1]: file_path = file_path + ".gdx" self.abs_path = os.path.abspath(file_path) logger.info(f"Open GDX file from {self.abs_path}.") if workspace is None: workspace = gams.GamsWorkspace() database = workspace.add_database_from_gdx(self.abs_path) super().__init__(database)
def convert_instance(obj, out_gdx_fn): dirs = { 'Darwin': dict(system='/Library/Frameworks/GAMS.framework/Resources/', workdir='/Users/andreschnabel/Desktop'), 'Windows': dict(system='C:\\GAMS\\32', workdir='C:\\Users\\Andre\\Desktop') } ws = gams.GamsWorkspace( system_directory=dirs[platform.system()]['system'], working_directory=dirs[platform.system()]['workdir']) db = ws.add_database() sets = dict(i=['ngoods', 'Gueter'], k=['ncomponents', 'Komponenten'], s=['ndamagepatterns', 'Schadensbilder'], t=['nperiods', 'Perioden']) add_sets_to_database(db, obj, sets) params = dict( ekt=['ik', 'Ankunftszeitpunkt (ganzzahlig) von Teil i,k'], eks=['ik', 'Schadensbild (ganzzahlig) von Teil i,k'], eksreal=['ik', 'Tatsächliches Schadensbild (ganzzahlig) von Teil i,k'], due=['i', 'Liefertermin/Frist'], c=['i', 'Verspaetungskostensatz pro ZE'], rd=['k', 'Remontagedauer in ZE'], rc=['k', 'Reparaturkapazitaet in KE'], hc=['ks', 'Lagerkostensatz pro ZE und ME in Zustand'], d=['ks', 'Reparaturdauern in ZE'], bd=['ks', 'Bestelldauer in ZE'], bc=['ks', 'Bestellkostensatz pro ME in Zustand s von Komponente k']) add_params_to_database(db, obj, params) db.export(out_gdx_fn)
def __init__(self, ws=None, db=None, filepath=None): self.ws = ws if ws is not None else gams.GamsWorkspace() self.db = db self.filepath = filepath
def main(): args = sys.argv con_name = args[1] inl_name = args[2] raw_name = args[3] rop_name = args[4] sol1_name = 'solution1.txt' sol2_name = 'solution2.txt' gdx_name = 'pscopf.gdx' gms_name = 'run_greedy.gms' gams_work_dir = 'src/gams/' print '\nPython/GAMS benchmark solver' print 'MyPython1.py' # read the psse files print 'reading psse files' p = psse.Psse() print 'reading raw file: %s' % raw_name if raw_name is not None: p.raw.read(os.path.normpath(raw_name)) print 'reading rop file: %s' % rop_name if rop_name is not None: p.rop.read(os.path.normpath(rop_name)) print 'reading inl file: %s' % inl_name if inl_name is not None: p.inl.read(os.path.normpath(inl_name)) print 'reading con file: %s' % con_name if con_name is not None: p.con.read(os.path.normpath(con_name)) print "buses: %u" % len(p.raw.buses) print "loads: %u" % len(p.raw.loads) print "fixed_shunts: %u" % len(p.raw.fixed_shunts) print "generators: %u" % len(p.raw.generators) print "nontransformer_branches: %u" % len(p.raw.nontransformer_branches) print "transformers: %u" % len(p.raw.transformers) #print "areas: %u" % len(p.raw.areas) print "switched_shunts: %u" % len(p.raw.switched_shunts) print "generator inl records: %u" % len(p.inl.generator_inl_records) print "generator dispatch records: %u" % len( p.rop.generator_dispatch_records) print "active power dispatch records: %u" % len( p.rop.active_power_dispatch_records) print "piecewise linear cost functions: %u" % len( p.rop.piecewise_linear_cost_functions) print 'contingencies: %u' % len(p.con.contingencies) # write to gdx print 'writing gdx input file: %s' % gdx_name gams_utils.write_psse_to_gdx(p, os.path.normpath(gams_work_dir + gdx_name)) # test gams print 'running gams model: %s' % gms_name ws = gams.GamsWorkspace() ws.__init__(working_directory=(os.path.normpath(gams_work_dir))) job = ws.add_job_from_file(os.path.normpath(gms_name)) opt = gams.GamsOptions(ws) opt.defines['ingdx'] = os.path.normpath(gdx_name) opt.defines['solution1'] = os.path.normpath('../../' + sol1_name) opt.defines['solution2'] = os.path.normpath('../../' + sol2_name) job.run(gams_options=opt, output=sys.stdout)
def solveGAMS(attacked): #if (len(sys.argv) > 1): # ws = gams.GamsWorkspace(system_directory = sys.argv[1]) #else: # ws = gams.GamsWorkspace(debug = 0) ws = gams.GamsWorkspace(debug=0) nodes = [ "s", "A", "a2", "a3", "a4", "B", "b1", "b2", "b3", "b4", "C", "c1", "c2", "c3", "c4", "t" ] edges = [ ("A", "a2"), ("A", "a3"), ("A", "a4"), ("a2", "B"), ("a2", "b1"), ("a2", "b2"), ("a2", "b3"), ("a2", "b4"), ("a3", "A"), ("a3", "C"), ("B", "b1"), ("B", "b2"), ("B", "b3"), ("B", "b4"), ("b3", "C"), ("b4", "A"), ("b4", "a2"), ("b4", "a3"), ("b4", "a4"), ("C", "c1"), ("C", "c2"), ("C", "c3"), ("C", "c4"), ("c1", "B"), ("c1", "b1"), ("c1", "b2"), ("c1", "b3"), ("c1", "b4"), ("c2", "c1"), ("c2", "c3"), ("c3", "c2"), ("c3", "c4"), ("c4", "A"), ("c4", "a2"), ("c4", "a3"), ("c4", "a4"), ("s", "A"), ("s", "B"), ("s", "C"), ("A", "t"), ("a2", "t"), ("a3", "t"), ("a4", "t"), ("B", "t"), ("b1", "t"), ("b2", "t"), ("b3", "t"), ("b4", "t"), ("C", "t"), ("c1", "t"), ("c2", "t"), ("c3", "t"), ("c4", "t") ] capacity = { ("A", "a2", "water"): 4, ("A", "a2", "power"): 4, ("A", "a2", "cyber"): 4, ("A", "a3", "water"): 4, ("A", "a3", "power"): 4, ("A", "a3", "cyber"): 4, ("A", "a4", "water"): 4, ("A", "a4", "power"): 4, ("A", "a4", "cyber"): 4, ("a2", "B", "water"): 2, ("a2", "B", "power"): 0, ("a2", "B", "cyber"): 0, ("a2", "b1", "water"): 2, ("a2", "b1", "power"): 0, ("a2", "b1", "cyber"): 0, ("a2", "b2", "water"): 2, ("a2", "b2", "power"): 0, ("a2", "b2", "cyber"): 0, ("a2", "b3", "water"): 2, ("a2", "b3", "power"): 0, ("a2", "b3", "cyber"): 0, ("a2", "b4", "water"): 2, ("a2", "b4", "power"): 0, ("a2", "b4", "cyber"): 0, ("a3", "A", "water"): 4, ("a3", "A", "power"): 4, ("a3", "A", "cyber"): 4, ("a3", "C", "water"): 2, ("a3", "C", "power"): 0, ("a3", "C", "cyber"): 0, ("B", "b1", "water"): 4, ("B", "b1", "power"): 4, ("B", "b1", "cyber"): 4, ("B", "b2", "water"): 4, ("B", "b2", "power"): 4, ("B", "b2", "cyber"): 4, ("B", "b3", "water"): 4, ("B", "b3", "power"): 4, ("B", "b3", "cyber"): 4, ("B", "b4", "water"): 4, ("B", "b4", "power"): 4, ("B", "b4", "cyber"): 4, ("b3", "C", "water"): 0, ("b3", "C", "power"): 0, ("b3", "C", "cyber"): 4, ("b4", "A", "water"): 0, ("b4", "A", "power"): 0, ("b4", "A", "cyber"): 4, ("b4", "a2", "water"): 0, ("b4", "a2", "power"): 0, ("b4", "a2", "cyber"): 4, ("b4", "a3", "water"): 0, ("b4", "a3", "power"): 0, ("b4", "a3", "cyber"): 4, ("b4", "a4", "water"): 0, ("b4", "a4", "power"): 0, ("b4", "a4", "cyber"): 4, ("C", "c1", "water"): 4, ("C", "c1", "power"): 4, ("C", "c1", "cyber"): 4, ("C", "c2", "water"): 4, ("C", "c2", "power"): 4, ("C", "c2", "cyber"): 4, ("C", "c3", "water"): 4, ("C", "c3", "power"): 4, ("C", "c3", "cyber"): 4, ("C", "c4", "water"): 4, ("C", "c4", "power"): 4, ("C", "c4", "cyber"): 4, ("c1", "B", "water"): 2, ("c1", "B", "power"): 0, ("c1", "B", "cyber"): 0, ("c1", "b1", "water"): 2, ("c1", "b1", "power"): 0, ("c1", "b1", "cyber"): 0, ("c1", "b2", "water"): 2, ("c1", "b2", "power"): 0, ("c1", "b2", "cyber"): 0, ("c1", "b3", "water"): 2, ("c1", "b3", "power"): 0, ("c1", "b3", "cyber"): 0, ("c1", "b4", "water"): 2, ("c1", "b4", "power"): 0, ("c1", "b4", "cyber"): 0, ("c2", "c1", "water"): 4, ("c2", "c1", "power"): 4, ("c2", "c1", "cyber"): 4, ("c2", "c3", "water"): 4, ("c2", "c3", "power"): 4, ("c2", "c3", "cyber"): 4, ("c3", "c2", "water"): 4, ("c3", "c2", "power"): 4, ("c3", "c2", "cyber"): 4, ("c3", "c4", "water"): 4, ("c3", "c4", "power"): 4, ("c3", "c4", "cyber"): 4, ("c4", "A", "water"): 2, ("c4", "A", "power"): 2, ("c4", "A", "cyber"): 2, ("c4", "a2", "water"): 2, ("c4", "a2", "power"): 2, ("c4", "a2", "cyber"): 2, ("c4", "a3", "water"): 2, ("c4", "a3", "power"): 2, ("c4", "a3", "cyber"): 2, ("c4", "a4", "water"): 2, ("c4", "a4", "power"): 2, ("c4", "a4", "cyber"): 2, ("s", "A", "water"): 4, ("s", "A", "power"): 1, ("s", "A", "cyber"): 1, ("s", "B", "water"): 2, ("s", "B", "power"): 1, ("s", "B", "cyber"): 4, ("s", "C", "water"): 1, ("s", "C", "power"): 4, ("s", "C", "cyber"): 2, ("A", "t", "water"): 0, ("A", "t", "power"): 1, ("A", "t", "cyber"): 1, ("a2", "t", "water"): 1, ("a2", "t", "power"): 1, ("a2", "t", "cyber"): 1, ("a3", "t", "water"): 1, ("a3", "t", "power"): 1, ("a3", "t", "cyber"): 1, ("a4", "t", "water"): 1, ("a4", "t", "power"): 1, ("a4", "t", "cyber"): 1, ("B", "t", "water"): 1, ("B", "t", "power"): 1, ("B", "t", "cyber"): 0, ("b1", "t", "water"): 1, ("b1", "t", "power"): 1, ("b1", "t", "cyber"): 1, ("b2", "t", "water"): 1, ("b2", "t", "power"): 1, ("b2", "t", "cyber"): 1, ("b3", "t", "water"): 1, ("b3", "t", "power"): 1, ("b3", "t", "cyber"): 1, ("b4", "t", "water"): 1, ("b4", "t", "power"): 1, ("b4", "t", "cyber"): 1, ("C", "t", "water"): 1, ("C", "t", "power"): 0, ("C", "t", "cyber"): 1, ("c1", "t", "water"): 1, ("c1", "t", "power"): 1, ("c1", "t", "cyber"): 1, ("c2", "t", "water"): 1, ("c2", "t", "power"): 1, ("c2", "t", "cyber"): 1, ("c3", "t", "water"): 1, ("c3", "t", "power"): 1, ("c3", "t", "cyber"): 1, ("c4", "t", "water"): 1, ("c4", "t", "power"): 1, ("c4", "t", "cyber"): 1 } # demand ={"A":(0,1,1), #"a2":(1,1,1), #"a3":(1,1,1), #"a4":(1,1,1), #"B":(1,1,0), #"b1":(1,1,1), #"b2":(1,1,1), #"b3":(1,1,1), #"b4":(1,1,1), #"C" :(1,0,1), #"c1":(1,1,1), #"c2":(1,1,1), #"c3":(1,1,1), #"c4":(1,1,1), #"s":(0,0,0), #"t":(0,0,0)} demand = { ("A", "water"): 0, ("A", "power"): 1, ("A", "cyber"): 1, ("a2", "water"): 1, ("a2", "power"): 1, ("a2", "cyber"): 1, ("a3", "water"): 1, ("a3", "power"): 1, ("a3", "cyber"): 1, ("a4", "water"): 1, ("a4", "power"): 1, ("a4", "cyber"): 1, ("B", "water"): 1, ("B", "power"): 1, ("B", "cyber"): 0, ("b1", "water"): 1, ("b1", "power"): 1, ("b1", "cyber"): 1, ("b2", "water"): 1, ("b2", "power"): 1, ("b2", "cyber"): 1, ("b3", "water"): 1, ("b3", "power"): 1, ("b3", "cyber"): 1, ("b4", "water"): 1, ("b4", "power"): 1, ("b4", "cyber"): 1, ("C", "water"): 1, ("C", "power"): 0, ("C", "cyber"): 1, ("c1", "water"): 1, ("c1", "power"): 1, ("c1", "cyber"): 1, ("c2", "water"): 1, ("c2", "power"): 1, ("c2", "cyber"): 1, ("c3", "water"): 1, ("c3", "power"): 1, ("c3", "cyber"): 1, ("c4", "water"): 1, ("c4", "power"): 1, ("c4", "cyber"): 1, ("s", "water"): 0, ("s", "power"): 0, ("s", "cyber"): 0, ("t", "water"): 0, ("t", "power"): 0, ("t", "cyber"): 0 } supply = { ("A", "water"): 4, ("A", "power"): 1, ("A", "cyber"): 1, ("a2", "water"): 0, ("a2", "power"): 0, ("a2", "cyber"): 0, ("a3", "water"): 0, ("a3", "power"): 0, ("a3", "cyber"): 0, ("a4", "water"): 0, ("a4", "power"): 0, ("a4", "cyber"): 0, ("B", "water"): 2, ("B", "power"): 1, ("B", "cyber"): 4, ("b1", "water"): 0, ("b1", "power"): 0, ("b1", "cyber"): 0, ("b2", "water"): 0, ("b2", "power"): 0, ("b2", "cyber"): 0, ("b3", "water"): 0, ("b3", "power"): 0, ("b3", "cyber"): 0, ("b4", "water"): 0, ("b4", "power"): 0, ("b4", "cyber"): 0, ("C", "water"): 1, ("C", "power"): 4, ("C", "cyber"): 2, ("c1", "water"): 0, ("c1", "power"): 0, ("c1", "cyber"): 0, ("c2", "water"): 0, ("c2", "power"): 0, ("c2", "cyber"): 0, ("c3", "water"): 0, ("c3", "power"): 0, ("c3", "cyber"): 0, ("c4", "water"): 0, ("c4", "power"): 0, ("c4", "cyber"): 0, ("s", "water"): 0, ("s", "power"): 0, ("s", "cyber"): 0, ("t", "water"): 0, ("t", "power"): 0, ("t", "cyber"): 0 } # supply={"A" :(4,1,1), #"a2":(0,0,0), #"a3":(0,0,0), #"a4":(0,0,0), #"B":(2,1,4), #"b1":(0,0,0), #"b2":(0,0,0), #"b3":(0,0,0), #"b4":(0,0,0), #"C":(1,4,2), #"c1":(0,0,0), #"c2":(0,0,0), #"c3":(0,0,0), #"c4":(0,0,0), #"s":(0,0,0), #"t":(0,0,0)} commlist = ["water", "power", "cyber"] attackedNodes = [] db = ws.add_database() i = db.add_set("i", 1, "nodes") k = db.add_set("k", 1, "commodities") for comm in commlist: k.add_record(comm) realNodes = db.add_set("realNodes", 1, "realNodes") VirtualSource = db.add_set("VirtualSource", 1, "VirtualSource") VirtualDest = db.add_set("VirtualDest", 1, "VirtualDest") edgelist = db.add_set("edgelist", 2, "edges") #u = gams.GamsParameter(db, "u", 3, "upper bound on flow across arc (i,j)") #b = gams.GamsParameter(db, "b", 1, "demand vector for node i") #s = gams.GamsParameter(db, "s", 3, "supply vector for node i") #u = db.add_parameter_dc("u", [edges,k], "upper bound on flow across arc (i,j)") b = db.add_parameter_dc("b", [i, k], "demand of node i for commodity k") s = db.add_parameter_dc("s", [i, k], "supply of node i for commodity k") u = db.add_parameter_dc("u", [i, i, k], "capacity of edge for commodity k") #for edge in edgelist: # u.add_record(edge).value = capacity(edge) #only add nodes to graph which have not been attacked (x_i = 0) for j in range(0, len(nodes)): if attacked[j] == 0: i.add_record(nodes[j]) for comm in commlist: b.add_record((nodes[j], comm)).value = demand[(nodes[j], comm)] s.add_record((nodes[j], comm)).value = supply[(nodes[j], comm)] if (nodes[j] == "s"): VirtualSource.add_record(nodes[j]) elif (nodes[j] == "t"): VirtualDest.add_record(nodes[j]) else: realNodes.add_record(nodes[j]) else: attackedNodes.append(nodes[j]) #only add edges which arent connected to attacked nodes for j in range(0, len(edges)): if edges[j][0] in attackedNodes or edges[j][1] in attackedNodes: continue else: edgelist.add_record(edges[j]) u.add_record((edges[j][0], edges[j][1], "water")).value = capacity[(edges[j][0], edges[j][1], "water")] u.add_record((edges[j][0], edges[j][1], "power")).value = capacity[(edges[j][0], edges[j][1], "power")] u.add_record((edges[j][0], edges[j][1], "cyber")).value = capacity[(edges[j][0], edges[j][1], "cyber")] #model = ws.add_job_from_string(get_model_text()) #opt = ws.add_options() #opt.defines["gdxincname"] = db.name #opt.all_model_types = "xpress" #model.run(opt, databases = db) #data = ws.add_job_from_string(get_data_text()) #data.run() #job = gams.GamsJob(ws, source = get_model_text()) #dont know what this stuff does #opt = ws.add_options() #opt.defines["gdxincname"] = db.name #opt.all_model_types = "xpress" #file1= open("/home/absolved/Documents/Bilevel_Evolution/test.gdx", "r+") #file2= open("output.txt", "w+") #base64.decode(file1,file2) #print(file2.read()) db.export("/home/absolved/Documents/Bilevel_Evolution/test.gdx") gdx_string = '' with open("/home/absolved/Documents/Bilevel_Evolution/test.gdx", 'rb') as f: text = f.read() gdx_string = gdx_string + str(base64.b64encode(text)) gdx_string = gdx_string.strip("b'") gams_xml = createGAMS_XML.create_xml(get_model_text(), gdx_string) xmlfile = open("gamstest.xml", "w+") xmlfile.write(gams_xml) xmlfile.close() return neosclient.send_xml()
def __init__(self, ws=None, db=None): self.ws = ws if ws is not None else gams.GamsWorkspace() self.db = db if db is not None else self.ws.add_database()
gamspath = get_gams_path() if gamspath == '': print('ERROR: The GAMS installation folder could not be found') success_path = False else: print('GAMS folder found: ' + gamspath) if success_path and success_lib: print('\n \nTRY TO RUN A SIMPLE GAMS MODEL:') try: ws = gams.GamsWorkspace(system_directory=gamspath,debug=1) ws.gamslib("trnsport") t1 = ws.add_job_from_file("trnsport.gms") t1.run() for rec in t1.out_db["x"]: 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
def __init__(self, wdir, A_matrix, b_vector): # A_matrix = A # b_vector = b # # self = test print("Initializing CBCO Reduction Method..") self.wdir = wdir self.workspace = gams.GamsWorkspace(working_directory=self.wdir) self.gams_db = self.workspace.add_database() if isinstance(A_matrix, list): self.A_matrix = np.array(A_matrix) self.b_vector = np.array(b_vector).reshape(len(b_vector), 1) else: self.A_matrix = A_matrix self.b_vector = b_vector self.row_index = self.gams_db.add_set("row_index", 1, "row_index") self.col_index = self.gams_db.add_set("col_index", 1, "col_index") # self.cbco_index = self.gams_db.add_set("cbco_index", 1, "cbco_index") for col in range(0, np.shape(self.A_matrix)[1]): self.col_index.add_record(str(col)) self.A = self.gams_db.add_parameter_dc( "A", [self.row_index, self.col_index], "A Matrix (LHS)") self.b = self.gams_db.add_parameter_dc("b", [self.row_index], "b vector (RHS)") # self.s = self.gams_db.add_parameter_dc("s", [self.col_index], "additional row for A ") # self.t = self.gams_db.add_parameter("t", 0) # add first element self.row_index_counter = len(self.b_vector) - 1 self.cbco_rows = [x for x in range(0, len(self.b_vector))] ## Full A Matrix, full b vetor, full row_index for row in self.cbco_rows: self.row_index.add_record(str(row)) self.b.add_record(str(row)).value = float(self.b_vector[row]) for row in self.cbco_rows: for col in range(0, np.shape(self.A_matrix)[1]): self.A.add_record([str(row), str(col)]).value = \ float(self.A_matrix[row,col]) self.s = self.gams_db.add_parameter("s", 1, "additional row for A") self.t = self.gams_db.add_parameter("t", 0, "t value") self.cbco_index = self.gams_db.add_parameter("cbco_index", 1, "cbco_index") self.s, self.t, self.cbco_index = self.update_s_t_cbco_index( self.s, self.t, self.cbco_index) self.gams_model, self.model_instance = self.init_gams_model() self.s = self.model_instance.sync_db.add_parameter( "s", 1, "additional row for A") self.t = self.model_instance.sync_db.add_parameter("t", 0, "t value") self.cbco_index = self.model_instance.sync_db.add_parameter( "cbco_index", 1, "cbco_index") print("Reduction Algorithm Init!")
import gams import workdirectory as swd import os import gdxtools as gdxt import plotly_express as px ## Set the dictionaries work_dir, project_dir, temp_dir, data_dir = swd.workdirectory() # get relative data folder PATH = pathlib.Path(__file__).parent DATA_PATH = PATH.joinpath("../data").resolve() ## Get the data ws = gams.GamsWorkspace() gdx_file = os.path.join(temp_dir, 'cgecars.gdx') db = ws.add_database_from_gdx(gdx_file) year_CGE = int(gdxt.get_parameter(db, 'actyearv').iloc[0, 0]) cars = gdxt.get_parameter(db, 'carsupd') ## Reshape data cars.rename(columns={ 'dim1': 'year', 'dim2': 'ryr', 'dim3': 'trst', 'dim4': 'lstkw', 'dim5': 'level' }, inplace=True)
def test(raw_name=None, rop_name=None, inl_name=None, con_name=None, sol1_name=None, sol2_name=None, gdx_name=None, gms_name=None): print '\ntesting Psse' # read the psse files print 'reading psse files' p = psse.Psse() print 'reading raw file: %s' % raw_name if raw_name is not None: p.raw.read(os.path.normpath(raw_name)) print 'reading rop file: %s' % rop_name if rop_name is not None: p.rop.read(os.path.normpath(rop_name)) print 'reading inl file: %s' % inl_name if inl_name is not None: p.inl.read(os.path.normpath(inl_name)) print 'reading con file: %s' % con_name if con_name is not None: p.con.read(os.path.normpath(con_name)) print "buses: %u" % len(p.raw.buses) print "loads: %u" % len(p.raw.loads) print "fixed_shunts: %u" % len(p.raw.fixed_shunts) print "generators: %u" % len(p.raw.generators) print "nontransformer_branches: %u" % len(p.raw.nontransformer_branches) print "transformers: %u" % len(p.raw.transformers) #print "areas: %u" % len(p.raw.areas) print "switched_shunts: %u" % len(p.raw.switched_shunts) print "generator inl records: %u" % len(p.inl.generator_inl_records) print "generator dispatch records: %u" % len( p.rop.generator_dispatch_records) print "active power dispatch records: %u" % len( p.rop.active_power_dispatch_records) print "piecewise linear cost functions: %u" % len( p.rop.piecewise_linear_cost_functions) print 'contingencies: %u' % len(p.con.contingencies) # write to gdx print 'writing gdx file: %s' % gdx_name gams_utils.write_psse_to_gdx(p, os.path.normpath(GAMS_DIR + gdx_name)) # test gams print 'running gams model: %s' % gms_name ws = gams.GamsWorkspace() #ws.__init__(working_directory=os.getcwd()) ws.__init__(working_directory=(os.path.normpath(GAMS_DIR))) job = ws.add_job_from_file(os.path.normpath(gms_name)) opt = gams.GamsOptions(ws) opt.defines['ingdx'] = os.path.normpath(gdx_name) opt.defines['solution1'] = os.path.normpath(sol1_name) opt.defines['solution2'] = os.path.normpath(sol2_name) opt.nlp = 'knitro' complete = (raw_name is not None and rop_name is not None and inl_name is not None and con_name is not None and gdx_name is not None and gms_name is not None) if complete: job.run(gams_options=opt, output=sys.stdout)