''' @file This is the 7th model in a series of tutorial examples. Here we show: - How to create a GamsModelInstance from a GamsCheckpoint - How to modify a parameter of a GamsModelInstance using GamsModifier - How to modify a variable of a GamsModelInstance using GamsModifier - Par_h0(i) & Q_swt(w) 的变化体现在 Qtable(i,w)中 - Q_sw(t) is alterable - Qtable(i,w) 和 Q_sw(t)要对应哦 ''' from __future__ import print_function from gams import * import sys import pandas as pd from itertools import product import os import csv def get_model_text(): return ''' Sets i detention reservoirs /1*4/ t long time series /1*72/ w dry time /1*72/ ; alias (t,k); alias (i,j); Parameters FIRST Priority serial number /4/ Ability(t) wwtp max treatment capability ton(i) the time of detention dry out Eneed total v to dry of system ; Ability(t)=1.08; Table Qtable(i,w) Theoretical discharge table 1 2 3 4 5 6 1 0.090 0.090 0.090 0.090 0.090 0.090 2 0.205 0.161 0.116 0.066 3 0.193 0.051 4 0.614 0.515 0.415 0.311 0.201 0.063 + 7 8 1 0.090 0.090 ; Eneed=sum((i,w),Qtable(i,w)); Parameter cod(w); scalar czd; loop(i, cod(w)=Qtable(i,w); czd=0; loop(w, if (cod(w)=0,czd=czd+1;); ); ton(i)=card(w)-czd; ); Parameters Q_sw(t) domestic sewage discharge(10 million m3); Q_sw(t) =0 ; Variables op_val total time; VARIABLES E_re(t) the rest of v Dpai1(t) linear introduced variable Dpai2(t) E_re(t)=Dpai1(t)+Dpai2(t) Qreal(i,t) dry Q OTimeom(i) Opening time of regulating and storage tank ; Integer variables E_re_coff the moment of dry out ; BINARY VARIABLES d(i,t) state of ON or OFF (decesion variables) don(i,t) which time to turn on doff(i,t) which time to turn off V(i,t,w) choose Q from table E_re_c(t) water discharge completion mark bin1(t) linear introduced variable bin2(t) linear introduced variable ; Equations obj; obj.. op_val =e= E_re_coff; Equations con1 con1_1 con2 con3 con4 con5 con6 con7 con8 con9 con10 ; con1(i).. d(i,'1') =e= don(i,'1')-doff(i,'1'); con1_1(i,t-1).. d(i,t)-d(i,t-1) =e= don(i,t)-doff(i,t); con2(i,t).. sum(k$(ord(k)>=max(ord(t)-ton(i)+1,1)and ord(k)<=ord(t)),don(i,k)) =l= d(i,t); con3(i).. sum(t,d(i,t))=e=ton(i); con4(i).. sum(t,don(i,t))=e=1; con5(i).. sum(t,doff(i,t))=e=1; con6(i,t).. sum(w,V(i,t,w))=l=1; con7(i,t).. Qreal(i,t)=e=sum(w,(V(i,t,w)*Qtable(i,w))); con8(i,t).. sum(w,ord(w)*V(i,t,w))=e=sum(k$(ord(k)<=ord(t)),d(i,k))-ton(i)*sum(k$(ord(k)<=ord(t)),doff(i,k)); con9(t).. sum(i,Qreal(i,t))=l=Ability(t)-Q_sw(t); con10(t).. E_re(t)=e=Eneed-sum((i,k)$(ord(k)<=ord(t)),Qreal(i,k)); Equations ton1,ton2,ton3,ton4,ton5,ton6,ton7,ton8; ton1(t).. E_re_c(t)=e=bin1(t); ton2(t).. E_re(t)=e=Dpai1(t)+Dpai2(t); ton3(t).. Dpai1(t)=g=-9999*bin1(t); ton4(t).. Dpai1(t)=l=0.01*bin1(t); ton5(t).. Dpai2(t)=g=0.01*bin2(t); ton6(t).. Dpai2(t)=l=9999*bin2(t); ton7(t).. bin1(t)+bin2(t)=e=1; ton8.. E_re_coff =e= sum(t,ord(t)*(E_re_c(t)-E_re_c(t-1))) ; Equations FON1,FON2; FON1(i).. OTimeom(i)=e=sum(t,ord(t)*don(i,t)); FON2(i)$(ord(i)<>FIRST).. sum(j$(ord(j)=FIRST),OTimeom(j))=l=OTimeom(i); Model DOPF /all/; ''' if __name__ == "__main__": if len(sys.argv) > 1: ws = GamsWorkspace(system_directory = sys.argv[1]) else: ws = GamsWorkspace(debug=DebugLevel.KeepFiles) cp = ws.add_checkpoint() # initialize a GAMSCheckpoint by running a GAMSJob gmodel = ws.add_job_from_string(get_model_text()) gmodel.run(checkpoint=cp,output=sys.stdout) # create a GAMSModelInstance and solve it multiple times with different scalar Qtable & Q_sw mi = cp.add_modelinstance() Qtable = mi.sync_db.add_parameter("Qtable", 2, "Theoretical discharge table") Q_sw = mi.sync_db.add_parameter("Q_sw", 1, "domestic sewage discharge(10 million m3)") opt = ws.add_options() opt.all_model_types = "gurobi" # instantiate the GAMSModelInstance and pass a model definition and GAMSModifier to declare bmult mutable mi.instantiate("DOPF use mip min op_val", [GamsModifier(Q_sw), GamsModifier(Qtable)],opt) # 读取Q_sw,构造成字典类型,可以先把所有的都读进来,行成一个包含多个字典的list,然后遍历 h0_list=[] timeseries = list(range(1, 73, 1)) timeseries = list(str(i) for i in timeseries) data_dir = "F:\PyProject\Predict\随机生成序列2/" files = os.listdir(data_dir) Qs_list = [] for f in files: data_temp = pd.read_csv(data_dir + f, index_col=0) li_temp = list(data_temp.iloc[:, 0].values / 10000) dict_qta = dict(zip(timeseries, li_temp)) Qs_list.append(dict_qta) # print(len(Qs_list)) # 读取理论泄流表,行成一个包含多个dataframe的list,然后遍历,看看是否可以直接赋值 Q_table_list =[] data_dir1 = "F:\PyProject\Predict\理论泄流表/" files1 = os.listdir(data_dir1) for f1 in files1: data_temp1 = pd.read_csv(data_dir1 + f1,index_col=0) Q_table_list.append(data_temp1) print("理论泄流表:",Q_table_list) # 向GAMs模型中更新两个参数 for i in range(len(Qs_list)): result_list = [] list_index1 = [] list_index2 = [] list_q = [] qs = Qs_list[i] qtab = Q_table_list[i] for s in qs: Q_sw.clear() Q_sw.add_record(s).value = qs[s] for rec in mi.sync_db["Q_sw"]: print("x(" + rec.key(0) + "," + "): level=" + str(rec.value)) for j in range(4): for k in range(72): Qtable.clear() Qtable.add_record([str(j),str(k)]).value = qtab.iloc[j][k] for rec in mi.sync_db["Qtable"]: print("x(" + rec.key(0) + "," + rec.key(1) + "): level=" + str(rec.value)) mi.solve() print("Scenario link blocked: " + str(i) + "-" + str(j) + "-" + str(k)) print(" Modelstatus: " + str(mi.model_status)) print(" Solvestatus: " + str(mi.solver_status)) print(" Obj: " + str(mi.sync_db["op_val"].find_record().level)) for rec in mi.sync_db["Qreal"]: print("x(" + rec.key(0) + "," + rec.key(1) + "): level=" + str(rec.level) + " marginal=" + str( rec.marginal)) result_list.append(mi.sync_db["op_val"].find_record().level) for rec in mi.sync_db["Qreal"]: index_q1 = rec.key(0) list_index1.append(index_q1) pdf_index1 = pd.DataFrame(list_index1) index_q2 = rec.key(1) list_index2.append(index_q2) pdf_index2 = pd.DataFrame(list_index2) result_q = rec.level list_q.append(result_q) pdf_q = pd.DataFrame(list_q) result_list1 = pd.concat([pdf_index1,pdf_index2,pdf_q],axis=1) folder_path = 'F:\PyProject\Predict\实际泄流结果/' file_path = folder_path + '/' + str(i) + '.csv' result_list1.to_csv(file_path) print("泄流情况:",result_list1) print(result_list)