main2.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import time
  2. from pprint import pprint
  3. import numpy as np
  4. import pandas as pd
  5. from .model.DHU_2 import DHU_2
  6. from ._opt.algorithm.sim_config import simulate_config as sim_opt_config
  7. from ._opt.algorithm.main import main as main_opt
  8. from ._opt.boundary.sim_config import simulate_config as sim_config_bound
  9. from ._opt.boundary.main import main as main_bound
  10. from ._opt.algorithm.model.model import SystemModel
  11. def main(*args,config=None):
  12. args = [_.reset_index(drop=True) for _ in args]
  13. var_cur = {
  14. 'coil_1_ToutA': args[0],
  15. 'coil_1_HoutA': args[1],
  16. 'HzP' : args[2],
  17. 'HzR' : args[3],
  18. 'coil_1_TinW' : args[4],
  19. 'coil_2_TinW' : args[5],
  20. 'coil_3_TinW' : args[6],
  21. 'coil_1_Val' : args[7],
  22. 'coil_2_Val' : args[8],
  23. 'coil_3_Val' : args[9],
  24. 'wheel_1_TinR': args[10],
  25. 'wheel_2_TinR': args[11],
  26. 'mixed_1_TinM': args[12],
  27. 'mixed_1_HinM': args[13],
  28. 'mixed_2_TinM': args[14],
  29. 'mixed_2_HinM': args[15],
  30. }
  31. var_cur = {k:v.set_axis([k],axis=1) for k,v in var_cur.items()}
  32. var_opt_name = ['coil_2_Val','wheel_1_TinR','wheel_2_TinR']
  33. var_sys_name = [_ for _ in var_cur.keys() if _ not in var_opt_name]
  34. var_opt_cur = {k:v for k,v in var_cur.items() if k in var_opt_name}
  35. var_sys_cur = {k:v for k,v in var_cur.items() if k in var_sys_name}
  36. var_opt_var = {
  37. 'coil_2_Val': main_bound(
  38. None,None,None,
  39. var_opt_cur['coil_2_Val'],
  40. config = sim_config_bound(
  41. opt_var = ['coil_2_Val'],
  42. syn_opt = False,
  43. var_type = True,
  44. lb_static = 100,
  45. ub_static = 100,
  46. var_precis = 1
  47. )
  48. ),
  49. 'wheel_1_TinR': main_bound(
  50. None,None,None,
  51. var_opt_cur['wheel_1_TinR'],
  52. config = sim_config_bound(
  53. opt_var = ['wheel_1_TinR'],
  54. syn_opt = False,
  55. var_type = True,
  56. lb_static = 50,
  57. ub_static = 120,
  58. var_precis = 1
  59. )
  60. ),
  61. 'wheel_2_TinR': main_bound(
  62. None,None,None,
  63. var_opt_cur['wheel_2_TinR'],
  64. config = sim_config_bound(
  65. opt_var = ['wheel_2_TinR'],
  66. syn_opt = False,
  67. var_type = True,
  68. lb_static = 50,
  69. ub_static = 120,
  70. var_precis = 1
  71. )
  72. ),
  73. }
  74. # 获取优化结果
  75. opt_config = sim_opt_config(
  76. target = 'Fs',
  77. dir_min = True,
  78. var_opt = var_opt_name,
  79. var_sys = var_sys_name,
  80. diag_model = False,
  81. algorithm = 'soea_DE_best_1_L_templet',
  82. NIND = 5000,
  83. MAXGEN = 200,
  84. constrains = [
  85. 'coil_3_DoutA-[coil_3_DoutA]<0',
  86. # 'steamcoil_1_Fs-220<0',
  87. # 'steamcoil_2_Fs-100<0',
  88. # '40-steamcoil_1_Fs<0',
  89. # '30-steamcoil_2_Fs<0',
  90. ],
  91. allow_neg_opt = False,
  92. )
  93. system = AirSystem()
  94. opt_output = main_opt(
  95. *list(var_opt_var.values()),
  96. *list(var_sys_cur.values()),
  97. system_model = system,
  98. config = opt_config
  99. )
  100. sys_output = system.predict(
  101. pd.concat([*opt_output[3:],*list(var_sys_cur.values())],axis=1),
  102. )
  103. show_data = None
  104. # show_data = get_show_data(opt_res=opt_output[2])
  105. # print(show_data)
  106. return [
  107. sys_output.loc[:,['coil_2_ToutA']],
  108. opt_output[3:][1],
  109. opt_output[3:][2],
  110. show_data
  111. ]
  112. class AirSystem(SystemModel):
  113. def __init__(self):
  114. super().__init__()
  115. # self.model = DHU_2().load_from_platform()
  116. path = r'C:\Users\zhangshenhao\Documents\WorkSpace\zsh_project\004_溧阳宁德\除湿机模型\model.pkl'
  117. self.model = DHU_2().load(path)
  118. def predict(self,data:pd.DataFrame) -> pd.DataFrame:
  119. time_start = time.time()
  120. self.PENALTY = {}
  121. self.index = data.index
  122. sys_out = self.model.predict_system(data)
  123. time_end = time.time()
  124. past_time = round(time_end-time_start,2)
  125. self.PAST_TIME.append(past_time)
  126. print(f'第{len(self.PAST_TIME)}次调用系统模型,本次调用时长为:{past_time}秒 \n')
  127. return sys_out
  128. def get_show_data(opt_res:pd.DataFrame) -> pd.DataFrame:
  129. var_names = {
  130. 'coil_3_DoutA' : '送风露点(℃)',
  131. 'coil_2_Q' : '中表冷冷量(kW)',
  132. 'coil_3_Q' : '后表冷冷量(kW)',
  133. 'steamcoil_1_Q' : '前再生热量(kW)',
  134. 'steamcoil_2_Q' : '后再生热量(kW)',
  135. 'wheel_1_E_diff' : '前转轮焓升(kJ/kg)',
  136. 'wheel_2_E_diff' : '后转轮焓升(kJ/kg)',
  137. 'summary_cost_cooling': '耗冷费用折算(元/h)',
  138. 'summary_cost_heating': '耗热费用折算(元/h)',
  139. 'summary_cost_total' : '冷热费用合计(元/h)',
  140. }
  141. res = (
  142. opt_res
  143. .loc[list(var_names.keys()),['Before','After']]
  144. .assign(指标=list(var_names.values()))
  145. .rename(columns={'Before':'优化前状态','After':'优化后状态'})
  146. .loc[:,['指标','优化前状态','优化后状态']]
  147. )
  148. return res