import os from datetime import datetime,timedelta from pathlib import Path from pprint import pprint import pandas as pd from ...model.DHU.DHU_AB import DHU_AB from .config_reader import ConfigReader from ...tools.data_loader import DataLoader NOW = datetime.now().replace(second=0,microsecond=0) def optimize(*inputs,config=None): config = {} if config is None else config if '__LOCAL' in config.keys(): config_reader_path = config['__LOCAL'] data_URL = config['__URL'] else: config_reader_path = '/mnt/workflow_data' data_URL = 'http://basedataportal-svc:8080/data/getpointsdata' config_reader = ConfigReader(path=f'{config_reader_path}/DHU_AB配置.xlsx') ALL_RESULT = { 'EXCEPTION':{ 'Mod' : {}, 'Data_Room': {}, 'Data_ATD' : {}, 'Opt' : {}, 'Push' : {} }, 'STATUS':{ 'Mode_Steady': [], 'Mode_Low' : [], 'Mode_stop' : [] } } for each_eaup_name in config_reader.all_equp_names: # 加载模型 # 加载数据 # 运行判断 # 稳态判断:房间露点设定值与反馈值是否接近 # 模型调整 # 模型判断:模型精度是否满足要求 # 模式判断: # 1. 基于当前露点优化模式:基于房间露点设定值减偏差 # 2. 快速提升送风露点模式:约束送风露点保持不变 equp_type = config_reader.get_equp_info(each_eaup_name,key='设备类型',info_type='str') # 加载模型 try: if config_reader.get_app_info( each_eaup_name, app_type = '实时优化', key = '使用临时模型', info_type = 'bool' ): # 从文件夹中获取临时模型 MODEL = DHU_AB.load(path=f'{config_reader_path}/model/{each_eaup_name}.pkl') else: MODEL = DHU_AB.load_from_platform( source = 'id', model_id = config_reader.get_equp_info( equp_name = each_eaup_name, key = '模型编号', info_type = 'str' ) ) except Exception as e: ALL_RESULT['EXCEPTION']['Mod'][each_eaup_name] = e continue # 加载房间数据 try: room_steady_len = config_reader.get_app_info(each_eaup_name,'实时优化','房间稳态判断时长','float') room_data_loader = DataLoader( path = f'{config_reader_path}/data/optimize/data_cur/', start_time = NOW - timedelta(minutes=room_steady_len), end_time = NOW ) room_data_loader.download_equp_data( equp_name = each_eaup_name, point = config_reader.get_equp_point(each_eaup_name,equp_class=['C']), url = data_URL, clean_cache = True ) room_data = room_data_loader.get_equp_data(each_eaup_name) room_Dew_SP_adj = config_reader.get_app_info(each_eaup_name,'实时优化','房间露点设定值偏差','float') room_Dew_SP = room_data.room_DSP.mean() + room_Dew_SP_adj room_Dew_PV = room_data.room_DPV.mean() room_Dew_diff_dwlim = config_reader.get_app_info(each_eaup_name,'实时优化','房间露点过低阈值','float') # 模式判断 is_room_Dew_steady = (room_Dew_PV < room_Dew_SP + 0.5) or (room_Dew_PV > room_Dew_SP - 0.5) is_room_Dew_low = room_Dew_PV - room_Dew_SP < room_Dew_diff_dwlim if is_room_Dew_steady: DewOut_constrain = 'coil_3_DoutA-[coil_3_DoutA]<0' ALL_RESULT['STATUS']['Mode_Steady'].append(each_eaup_name) elif is_room_Dew_low: DewOut_constrain = f'coil_3_DoutA-{room_Dew_SP-4}<0' #TODO 这个4度比较粗糙,待改进 ALL_RESULT['STATUS']['Mode_Low'].append(each_eaup_name) else: ALL_RESULT['STATUS']['Mode_stop'].append(each_eaup_name) continue except Exception as e: ALL_RESULT['STATUS']['Data_Room'][each_eaup_name] = e continue # 加载除湿机数据 try: dhu_steady_len = config_reader.get_app_info(each_eaup_name,'实时优化','除湿机工况均值时长','float') data_input_point = config_reader.get_equp_point(each_eaup_name,equp_class=['A','B']) data_cur = ( DataLoader( path = f'{config_reader_path}/data/optimize/data_cur/', start_time = NOW - timedelta(minutes=dhu_steady_len), end_time = NOW ) .download_equp_data( equp_name = each_eaup_name, point = data_input_point, url = data_URL, clean_cache = True ) .get_equp_data( equp_name = each_eaup_name, ) .mean(axis=0) .to_frame().T ) except Exception as e: ALL_RESULT['EXCEPTION']['Data_ATD'][each_eaup_name] = e try: # 模型精度判断 predict = MODEL.predict_system(input_data=data_cur) TVP_data = ( predict.T.set_axis(['pred'],axis=1) .join( data_cur.T.set_axis(['real'],axis=1), how = 'left' ) .dropna(axis=0) ) print(TVP_data) # 实时优化 constrains = [DewOut_constrain] opt_res = MODEL.optimize( cur_input_data = data_cur, wheel_1_TinR_ub = config_reader.get_app_info(each_eaup_name,'实时优化','前再生盘管温度上限','float'), wheel_1_TinR_lb = config_reader.get_app_info(each_eaup_name,'实时优化','前再生盘管温度下限','float'), wheel_2_TinR_ub = config_reader.get_app_info(each_eaup_name,'实时优化','后再生盘管温度上限','float'), wheel_2_TinR_lb = config_reader.get_app_info(each_eaup_name,'实时优化','后再生盘管温度下限','float'), constrains = constrains, logging = False ) opt_summary = opt_res['opt_summary'] opt_var = opt_res['opt_var'] except Exception as e: ALL_RESULT['EXCEPTION']['Opt'][each_eaup_name] = e pprint(ALL_RESULT)