Python中ENERGYM库实例化后触发AttributeError问题求助
ENERGYM库运行报错:NoneType对象无get_output属性
问题背景
参考相关论文与官方文档在Python中实现ENERGYM库,已按要求完成EnergyPlus最小化安装并添加到系统路径,可正常执行import energym语句。
执行代码
运行以下强化学习环境交互代码:
env = energym.make("Apartments2Grid-v0") out = env.get_output() for i in range(100): forecast = env.get_forecast(forecast_length=10); inp = get_input(out, forecast); out = env.step(inp); kpis = env.get_kpi(); env.close()
异常现象
- 程序先输出约百万行重复的Pandas未来警告:
C:\Users\mf635543\AppData\Local\anaconda3\envs\citylearn_env\lib\site-packages\energym-0.1-py3.10.egg\energym\schedules\EVSchedule.py:281: FutureWarning: Setting an item of incompatible dtype is deprecated and will raise an error in a future version of pandas. Value '[0.14101058 0.14101058 0.14101058 ...]'
- 输出大量FMU实例化日志后,抛出最终错误:
[OK] fmi2Instantiate: The Resource location of FMU with instance name instance1746118692 is file:///C:/Users/mf635543/Energym_runs/1746118692_2715543/resources. [WARNING] fmi2Instantiate: Argument loggingOn is set to 1. This is not supported. loggingOn will default to '0'. ... [OK] fmi2Instantiate: Instantiation of instance1746118692 succeded. --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) Cell In[2], line 2 1 env = energym.make("Apartments2Grid-v0") ----> 2 out = env.get_output() 3 for i in range(100): 4 forecast = env.get_forecast(forecast_length=10); AttributeError: 'NoneType' object has no attribute 'get_output'
排查与解决方向
- 环境实例化失败:
energym.make()返回None,说明环境创建过程中存在未被捕获的错误,被大量警告日志掩盖。- 先屏蔽Pandas警告,减少日志干扰,便于定位核心问题:
import warnings warnings.filterwarnings("ignore", category=FutureWarning) - 检查版本兼容性:当前使用的energym-0.1可能与Python3.10、EnergyPlus版本不兼容,尝试切换到Python3.8/3.9版本,或升级ENERGYM至最新兼容版本。
- 先屏蔽Pandas警告,减少日志干扰,便于定位核心问题:
- FMU实例化异常:尽管日志显示实例化成功,但可能存在路径权限或资源文件缺失问题。
- 检查
C:\Users\mf635543\Energym_runs和Output_EPExport_instanceXXX目录的读写权限,确保程序能创建、修改文件。 - 删除Energym_runs下的旧实例缓存文件,重新运行程序,避免残留文件干扰。
- 检查
- 环境调用顺序错误:部分强化学习环境需要先执行
reset()初始化,才能获取输出,修改代码尝试:env = energym.make("Apartments2Grid-v0") out = env.reset() # 替代直接调用get_output() for i in range(100): forecast = env.get_forecast(forecast_length=10) inp = get_input(out, forecast) out, _, _, _ = env.step(inp) # step方法通常返回多值(输出、奖励、终止标志、额外信息) kpis = env.get_kpi() env.close() - 依赖包版本冲突:EVSchedule.py中的dtype警告由pandas版本过高导致,尝试降级pandas到1.X稳定版本(如1.5.3)。
内容的提问来源于stack exchange,提问作者Matthew Fleishman
相关产品推荐
相关产品推荐

