PyPSA-EUR配置HiGHS求解器报错,如何正确配置使用?
问题描述
在PyPSA-EUR的config.yaml中配置HiGHS求解器:
solver: name: highs options: {}
运行时触发以下错误:
RuntimeError: Attempting to use an unavailable solver.
The SolverFactory was unable to create the solver "highs" and returned
an UnknownSolver object. This error is raised at the point where the
UnknownSolver object was used as if it were valid (by calling method
"has_capability").The original solver was created with the following parameters:
executable: highs
type: highs
_args: ()
options: {}
已确认环境中highspy安装正常,执行python -c "import highspy"无报错。
解决方案
验证Pyomo对HiGHS的兼容性
PyPSA依赖Pyomo调用求解器,确保Pyomo版本≥6.4.0(此版本开始完善对HiGHS的支持)。检查当前版本:pip show pyomo版本过低则升级:
pip install --upgrade pyomo调整config.yaml的求解器配置
尝试两种配置方式:- 若使用命令行版HiGHS,明确指定可执行文件路径(若系统PATH中未包含):
solver: name: highs options: {} executable: "/path/to/your/highs/executable" # 替换为实际路径,比如~/envs/pypsa-eur/bin/highs - 若使用highspy的Python接口,改用
highs_python作为求解器名称:solver: name: highs_python options: {}
- 若使用命令行版HiGHS,明确指定可执行文件路径(若系统PATH中未包含):
测试Pyomo能否调用HiGHS
在环境中运行以下代码验证:from pyomo.environ import SolverFactory solver = SolverFactory("highs") # 或对应highs_python print(solver.available()) print(solver.has_capability("linear"))若输出均为
True,说明Pyomo已能正常识别HiGHS,重启PyPSA-EUR流程即可。确保HiGHS可执行文件在PATH中
执行highs --version验证命令行版HiGHS是否可调用,若提示命令不存在,需将HiGHS的安装目录添加到系统PATH,或在config.yaml中指定绝对路径。
内容的提问来源于stack exchange,提问作者Dakait

