DHU_3.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. import numpy as np
  2. import pandas as pd
  3. import pymc as pm
  4. import pytensor.tensor as pt
  5. from sklearn.metrics import r2_score,mean_absolute_error,mean_absolute_percentage_error
  6. try:
  7. import plotnine as gg
  8. except:
  9. pass
  10. from .._model._base import BaseModel
  11. from ..components.coil import CoolingCoil2,SteamCoilFs,SteamCoilFs2,SteamCoilFs3
  12. from ..components.wheel import WheelS3
  13. from ..components.mixed import Mixed
  14. def record(name,var):
  15. pm.Deterministic(f'{name}_mu',var)
  16. def observe(name,var,observed,sigma=1):
  17. mu = pm.Deterministic(f'{name}_mu',var)
  18. sigma = pm.HalfNormal(f'{name}_sigma',sigma=sigma)
  19. pm.Normal(name,mu=mu,sigma=sigma,observed=observed)
  20. class DHU_3(BaseModel):
  21. def __init__(self) -> None:
  22. super().__init__()
  23. def fit(
  24. self,
  25. input_data : pd.DataFrame,
  26. observed_data: pd.DataFrame,
  27. rw_FA_val : bool = False,
  28. plot_TVP : bool = True
  29. ):
  30. with pm.Model() as self.MODEL_PYMC:
  31. param_prior = {
  32. 'wheel_1' : WheelS3.prior('wheel_1'),
  33. 'wheel_2' : WheelS3.prior('wheel_2'),
  34. 'coil_2' : CoolingCoil2.prior('coil_2'),
  35. 'coil_3' : CoolingCoil2.prior('coil_3'),
  36. 'steamcoil_1': SteamCoilFs2.prior('steamcoil_1'),
  37. 'steamcoil_2': SteamCoilFs.prior('steamcoil_2'),
  38. 'F_air' : {
  39. 'HzP_X' : pm.HalfNormal('F_air_HzP_X',sigma=1,initval=1),
  40. 'HzP_H' : pm.HalfNormal('F_air_HzP_H',sigma=1,initval=0.1),
  41. 'HzR_B' : pm.HalfNormal('F_air_HzR_B',sigma=1,initval=0.5),
  42. 'X_base': pm.TruncatedNormal('F_air_X_base',mu=0.5,sigma=0.2,lower=0,initval=0.5),
  43. 'H_base': pm.TruncatedNormal('F_air_H_base',mu=0.6,sigma=0.2,lower=0,upper=0.999,initval=0.6),
  44. 'B_base': pm.TruncatedNormal('F_air_B_base',mu=0.2,sigma=0.1,lower=0,initval=0.1),
  45. },
  46. 'mixed_1':{},
  47. 'mixed_2':{}
  48. }
  49. if rw_FA_val:
  50. N = len(input_data)
  51. period = 30
  52. n_segments = int(np.ceil(N/period))
  53. remainder = N % period
  54. repeat = [period] * (n_segments - 1) + ([remainder] if remainder != 0 else [])
  55. rw = pm.GaussianRandomWalk(
  56. 'rw',sigma=0.1,init_dist=pm.Normal.dist(mu=0,sigma=0.3),shape=n_segments)
  57. param_prior['F_air']['val_rw'] = pm.Deterministic('F_air_val_rw',pt.repeat(rw,repeat))
  58. param_prior['F_air']['val_pct'] = pm.Beta('F_air_val_pct',alpha=8,beta=1,initval=0.9)
  59. res = DHU_3.model(
  60. Tin_F = input_data.coil_1_ToutA.values,
  61. Hin_F = input_data.coil_1_HoutA.values,
  62. HzP = input_data.HzP.values,
  63. HzR = input_data.HzR.values,
  64. coil_1_TinW = input_data.coil_1_TinW.values,
  65. coil_2_TinW = input_data.coil_2_TinW.values,
  66. coil_3_TinW = input_data.coil_3_TinW.values,
  67. coil_1_Val = input_data.coil_1_Val.values,
  68. coil_2_Val = input_data.coil_2_Val.values,
  69. coil_3_Val = input_data.coil_3_Val.values,
  70. wheel_1_TinR = input_data.wheel_1_TinR.values,
  71. wheel_2_TinR = input_data.wheel_2_TinR.values,
  72. mixed_1_TinM = input_data.mixed_1_TinM.values,
  73. mixed_2_TinM = input_data.mixed_2_TinM.values,
  74. mixed_1_HinM = input_data.mixed_1_HinM.values,
  75. mixed_2_HinM = input_data.mixed_2_HinM.values,
  76. engine = 'pymc',
  77. param = param_prior
  78. )
  79. observe('mixed_1_ToutA',res['mixed_1']['ToutA'],observed=observed_data.mixed_1_ToutA.values)
  80. observe('mixed_1_DoutA',res['mixed_1']['DoutA'],observed=observed_data.mixed_1_DoutA.values)
  81. observe('wheel_1_ToutC',res['wheel_1']['ToutC'],observed=observed_data.wheel_1_ToutC.values)
  82. observe('coil_2_ToutA',res['coil_2']['ToutA'],observed=observed_data.coil_2_ToutA.values)
  83. observe('coil_2_DoutA',res['coil_2']['DoutA'],observed=observed_data.coil_2_DoutA.values)
  84. observe('wheel_2_ToutP',res['wheel_2']['ToutP'],observed=observed_data.wheel_2_ToutP.values)
  85. observe('wheel_2_DoutP',res['wheel_2']['DoutP'],observed=observed_data.wheel_2_DoutP.values)
  86. observe('wheel_2_ToutR',res['wheel_2']['ToutR'],observed=observed_data.wheel_2_ToutR.values)
  87. observe('steamcoil_1_FP',res['steamcoil_1']['FP'],observed=observed_data.steamcoil_1_FP.values,sigma=1000)
  88. observe('steamcoil_1_Fs',res['steamcoil_1']['Fs'],observed=observed_data.steamcoil_1_Fs.values,sigma=20)
  89. observe('steamcoil_2_Fs',res['steamcoil_2']['Fs'],observed=observed_data.steamcoil_2_Fs.values,sigma=20)
  90. record('wheel_2_ToutC',res['wheel_2']['ToutC'])
  91. record('mixed_2_ToutA',res['mixed_2']['ToutA'])
  92. record('wheel_1_FaP',res['wheel_1']['FP'])
  93. record('wheel_1_FaR',res['wheel_1']['FR'])
  94. record('wheel_1_FaC',res['wheel_1']['FC'])
  95. record('mixed_1_FaA',res['mixed_1']['FA'])
  96. record('mixed_1_FaM',res['mixed_1']['FM'])
  97. record('F_air_S',res['Fa']['Fa_S'])
  98. record('F_air_H',res['Fa']['Fa_H'])
  99. record('F_air_X',res['Fa']['Fa_X'])
  100. self.param_posterior = pm.find_MAP(maxeval=50000,include_transformed=False)
  101. param_posterior_reorder = {'F_air':{}}
  102. for equp_name in param_prior.keys():
  103. param_posterior_reorder.setdefault(equp_name,{})
  104. for param_name,param_value in self.param_posterior.items():
  105. if '__' in param_name:
  106. continue
  107. if param_name == 'F_air_val_rw':
  108. param_value = np.median(param_value[-5:])
  109. if param_name.startswith(equp_name):
  110. param_name_adj = param_name.replace(f'{equp_name}_','')
  111. param_posterior_reorder[equp_name][param_name_adj] = param_value
  112. self.record_model(
  113. model_name = 'DHU',
  114. model = param_posterior_reorder,
  115. train_data = {'x':np.array([1])},
  116. train_metric = {'R2':1,'MAE':1,'MAPE':1}
  117. )
  118. # 样本内预测数据
  119. TVP_data = []
  120. for param_name in self.param_posterior.keys():
  121. if param_name.replace('_mu','') not in observed_data.columns:
  122. continue
  123. TVP_data.append(
  124. pd.DataFrame(
  125. {
  126. 'param_name': param_name.replace('_mu',''),
  127. 'real' : observed_data.loc[:,param_name.replace('_mu','')].values,
  128. 'pred' : self.param_posterior[param_name]
  129. }
  130. )
  131. )
  132. self.TVP_data = pd.concat(TVP_data,axis=0)
  133. group_by_data = self.TVP_data.groupby(['param_name'])[['pred','real']]
  134. self.TVP_metric = (
  135. pd.concat(
  136. [
  137. group_by_data.apply(lambda dt:r2_score(dt.real,dt.pred)),
  138. group_by_data.apply(lambda dt:mean_absolute_error(dt.real,dt.pred)),
  139. group_by_data.apply(lambda dt:mean_absolute_percentage_error(dt.real,dt.pred)),
  140. ],
  141. axis=1
  142. )
  143. .set_axis(['R2','MAE','MAPE'],axis=1)
  144. .sort_values(by='R2',ascending=True)
  145. )
  146. if plot_TVP:
  147. gg.options.figure_size = (10,10)
  148. plot = (
  149. self.TVP_data
  150. .pipe(gg.ggplot)
  151. + gg.aes(x='real',y='pred')
  152. + gg.geom_point()
  153. + gg.facet_wrap(facets='param_name',scales='free')
  154. + gg.geom_abline(intercept=0,slope=1,color='red')
  155. )
  156. plot.show()
  157. return self
  158. def predict(self,input_data:pd.DataFrame) -> dict:
  159. param_posterior = self.model_info['model_DHU']
  160. res = DHU_3.model(
  161. Tin_F = input_data.coil_1_ToutA.values,
  162. Hin_F = input_data.coil_1_HoutA.values,
  163. HzP = input_data.HzP.values,
  164. HzR = input_data.HzR.values,
  165. coil_1_TinW = input_data.coil_1_TinW.values,
  166. coil_2_TinW = input_data.coil_2_TinW.values,
  167. coil_3_TinW = input_data.coil_3_TinW.values,
  168. coil_1_Val = input_data.coil_1_Val.values,
  169. coil_2_Val = input_data.coil_2_Val.values,
  170. coil_3_Val = input_data.coil_3_Val.values,
  171. wheel_1_TinR = input_data.wheel_1_TinR.values,
  172. wheel_2_TinR = input_data.wheel_2_TinR.values,
  173. mixed_1_TinM = input_data.mixed_1_TinM.values,
  174. mixed_2_TinM = input_data.mixed_2_TinM.values,
  175. mixed_1_HinM = input_data.mixed_1_HinM.values,
  176. mixed_2_HinM = input_data.mixed_2_HinM.values,
  177. engine = 'numpy',
  178. param = param_posterior
  179. )
  180. return res
  181. def predict_system(self,input_data:pd.DataFrame) -> pd.DataFrame:
  182. pred_res = self.predict(input_data)
  183. system_output = {}
  184. for equp_name,output_info in pred_res.items():
  185. for output_name,output_value in output_info.items():
  186. system_output[f'{equp_name}_{output_name}'] = output_value
  187. system_output = pd.DataFrame(system_output)
  188. system_output['Fs'] = system_output.steamcoil_1_Fs + system_output.steamcoil_2_Fs
  189. return system_output
  190. @classmethod
  191. def model(
  192. cls,
  193. Tin_F, # 前表冷后温度
  194. Hin_F, # 前表冷后湿度
  195. HzP, # 处理侧风机频率
  196. HzR, # 再生侧风机频率
  197. coil_1_TinW, # 前表冷进水温度
  198. coil_2_TinW, # 中表冷进水温度
  199. coil_3_TinW, # 后表冷进水温度
  200. coil_1_Val, # 前表冷阀门开度
  201. coil_2_Val, # 中表冷阀门开度
  202. coil_3_Val, # 后表冷阀门开度
  203. wheel_1_TinR, # 前转轮再生侧温度
  204. wheel_2_TinR, # 后转轮再生侧温度
  205. mixed_1_TinM, # 回风温度(处理侧)
  206. mixed_1_HinM, # 回风湿度(处理侧)
  207. mixed_2_TinM, # 补风温度(再生侧)
  208. mixed_2_HinM, # 补风湿度(再生侧)
  209. engine,
  210. param
  211. ) -> dict:
  212. # 水的质量流量
  213. coil_2_FW = coil_2_Val / 100
  214. coil_3_FW = coil_3_Val / 100
  215. # 空气的质量流量
  216. F_air_HzP_H = param['F_air']['HzP_H']
  217. F_air_HzP_X = param['F_air']['HzP_X']
  218. F_air_HzP_S = F_air_HzP_H + F_air_HzP_X
  219. F_air_HzR_B = param['F_air']['HzR_B']
  220. F_air_S_base = 1
  221. F_air_X_base = param['F_air']['X_base']
  222. F_air_H_base = param['F_air']['H_base']
  223. F_air_B_base = param['F_air']['B_base']
  224. F_air_val_rw = param['F_air'].get('val_rw',0)
  225. F_air_val_pct = param['F_air'].get('val_pct',0)
  226. F_air_X_base_adj = F_air_X_base + F_air_val_rw
  227. F_air_H_base_adj = F_air_H_base - F_air_val_rw * F_air_val_pct
  228. F_air_B_base_adj = F_air_B_base - F_air_val_rw * (1 - F_air_val_pct)
  229. Fa_S = F_air_S_base + F_air_HzP_S * (HzP / 50)
  230. Fa_H = F_air_H_base_adj + F_air_HzP_H * (HzP / 50)
  231. Fa_X = F_air_X_base_adj + F_air_HzP_X * (HzP / 50)
  232. Fa_B = F_air_B_base_adj + F_air_HzR_B * (HzR / 50)
  233. Fa_P = Fa_B + Fa_X + Fa_H - Fa_S
  234. wheel_1_FaP = Fa_S - Fa_H
  235. wheel_1_FaC = Fa_X - wheel_1_FaP
  236. wheel_1_FaR = Fa_P
  237. wheel_2_FaP = Fa_S
  238. wheel_2_FaC = wheel_1_FaC
  239. wheel_2_FaR = wheel_1_FaC
  240. mixed_1_FaM = Fa_H
  241. mixed_1_FaA = wheel_1_FaP
  242. mixed_2_FaM = Fa_B
  243. mixed_2_FaA = wheel_1_FaC
  244. coil_2_FaA = Fa_S
  245. coil_3_FaA = Fa_S
  246. steamcoil_1_Fa = Fa_P
  247. steamcoil_2_Fa = wheel_1_FaC
  248. # 前转轮
  249. wheel_1_res = WheelS3.model(
  250. TinP = Tin_F,
  251. HinP = Hin_F,
  252. FP = wheel_1_FaP,
  253. TinR = wheel_1_TinR,
  254. HinR = 0,
  255. FR = wheel_1_FaR,
  256. TinC = Tin_F,
  257. HinC = Hin_F,
  258. FC = wheel_1_FaC,
  259. engine = engine,
  260. param = param['wheel_1']
  261. )
  262. # 处理侧混风(回风)
  263. mixed_1_res = Mixed.model(
  264. TinA = wheel_1_res['ToutP'],
  265. HinA = wheel_1_res['HoutP'],
  266. FA = mixed_1_FaA,
  267. TinM = mixed_1_TinM,
  268. HinM = mixed_1_HinM,
  269. FM = mixed_1_FaM,
  270. engine = engine
  271. )
  272. # 中表冷
  273. coil_2_res = CoolingCoil2.model(
  274. TinA = mixed_1_res['ToutA'],
  275. HinA = mixed_1_res['HoutA'],
  276. FA = coil_2_FaA,
  277. TinW = coil_2_TinW,
  278. FW = coil_2_FW,
  279. engine = engine,
  280. param = param['coil_2']
  281. )
  282. # 后转轮
  283. wheel_2_res = WheelS3.model(
  284. TinP = coil_2_res['ToutA'],
  285. HinP = coil_2_res['HoutA'],
  286. FP = wheel_2_FaP,
  287. TinC = wheel_1_res['ToutC'],
  288. HinC = wheel_1_res['HoutC'],
  289. FC = wheel_2_FaC,
  290. TinR = wheel_2_TinR,
  291. HinR = 0,
  292. FR = wheel_2_FaR,
  293. engine = engine,
  294. param = param['wheel_2'],
  295. )
  296. # 后表冷
  297. coil_3_res = CoolingCoil2.model(
  298. TinA = wheel_2_res['ToutP'],
  299. HinA = wheel_2_res['HoutP'],
  300. FA = coil_3_FaA,
  301. TinW = coil_3_TinW,
  302. FW = coil_3_FW,
  303. engine = engine,
  304. param = param['coil_3']
  305. )
  306. # 后转轮湿度修正
  307. wheel_2_res_adj = WheelS3.model(
  308. TinP = coil_2_res['ToutA'],
  309. HinP = coil_2_res['HoutA'],
  310. FP = wheel_2_FaP,
  311. TinC = wheel_1_res['ToutC'],
  312. HinC = wheel_1_res['HoutC'],
  313. FC = wheel_2_FaC,
  314. TinR = wheel_2_TinR,
  315. HinR = wheel_2_res['HoutC'],
  316. FR = wheel_2_FaR,
  317. engine = engine,
  318. param = param['wheel_2'],
  319. )
  320. # 再生侧混风(排风)
  321. mixed_2_res = Mixed.model(
  322. TinA = wheel_2_res_adj['ToutR'],
  323. HinA = wheel_2_res_adj['HoutR'],
  324. FA = mixed_2_FaA,
  325. TinM = mixed_2_TinM,
  326. HinM = mixed_2_HinM,
  327. FM = mixed_2_FaM,
  328. engine = engine
  329. )
  330. # 前转轮湿度修正
  331. wheel_1_res_adj = WheelS3.model(
  332. TinP = Tin_F,
  333. HinP = Hin_F,
  334. FP = wheel_1_FaP,
  335. TinR = wheel_1_TinR,
  336. HinR = mixed_2_res['HoutA'],
  337. FR = wheel_1_FaR,
  338. TinC = Tin_F,
  339. HinC = Hin_F,
  340. FC = wheel_1_FaC,
  341. engine = engine,
  342. param = param['wheel_1']
  343. )
  344. # 前蒸气盘管
  345. steamcoil_1_res = SteamCoilFs2.model(
  346. TinA = mixed_2_res['ToutA'],
  347. ToutA = wheel_1_TinR,
  348. FA = steamcoil_1_Fa,
  349. param = param['steamcoil_1'],
  350. engine = engine
  351. )
  352. # steamcoil_1_res = SteamCoilFs3.model(
  353. # TinA = mixed_2_res['ToutA'],
  354. # ToutA = wheel_1_TinR,
  355. # HinA = mixed_2_res['DoutA'],
  356. # HoutA = mixed_2_res['HoutA'],
  357. # FA = steamcoil_1_Fa,
  358. # param = param['steamcoil_1'],
  359. # engine = engine
  360. # )
  361. # 后蒸气盘管
  362. steamcoil_2_res = SteamCoilFs.model(
  363. TinA = wheel_2_res_adj['ToutC'],
  364. ToutA = wheel_2_TinR,
  365. FA = steamcoil_2_Fa,
  366. param = param['steamcoil_2'],
  367. engine = engine
  368. )
  369. return {
  370. 'coil_2' : coil_2_res,
  371. 'coil_3' : coil_3_res,
  372. 'wheel_1' : wheel_1_res_adj,
  373. 'wheel_2' : wheel_2_res_adj,
  374. 'mixed_1' : mixed_1_res,
  375. 'mixed_2' : mixed_2_res,
  376. 'steamcoil_1': steamcoil_1_res,
  377. 'steamcoil_2': steamcoil_2_res,
  378. 'Fa':{
  379. 'Fa_S': Fa_S,
  380. 'Fa_H': Fa_H,
  381. 'Fa_X': Fa_X,
  382. 'Fa_P': Fa_P,
  383. 'Fa_B': Fa_B,
  384. },
  385. 'summary' : {}
  386. }