将PuLP与HiGHS CMD求解器结合时,卡在写入解决方案环节
Windows下PuLP调用HiGHS CMD求解MIP卡在
prob.solve()的解决建议 问题场景
用Python的PuLP库构建混合整数规划(MIP)模型,采用HiGHS CMD求解器时,Mac系统下几秒即可完成计算,但Windows系统中脚本会卡在prob.solve()步骤。从输出日志来看,HiGHS已经算出最优解并提示"Writing the solution to Arrangement_Pl-pulp.sol",但脚本始终无法继续执行。
代码片段
import pulp as pl prob = pl.LpProblem("Arrangement Pl", pl.LpMaximize) cutVariables = pl.LpVariable.dict("cuts", cuts, 0, None, pl.LpInteger) # 目标函数 prob += pl.lpSum([WidthOrder[i] * cutVariables[i] for i in cuts]) # 约束条件 prob += pl.lpSum([WidthOrder[i] * cutVariables[i] for i in cuts]) <= Deckle prob += pl.lpSum([cutVariables[i] for i in cuts]) <= maxCuts for i in cuts: prob += cutVariables[i] <= OrderDict[i] solver = pl.HiGHS_CMD(path=path_to_HiGHS) result = prob.solve(solver)
求解器输出日志
Running HiGHS 1.5.3 [date: 1970-01-01, git hash: 45a127b78] Copyright (c) 2023 HiGHS under MIT licence terms MIP Arrangement_Pl-pulp has 3 rows; 1 cols; 3 nonzeros; 1 integer variables Presolving model 0 rows, 0 cols, 0 nonzeros 0 rows, 0 cols, 0 nonzeros Presolve: Optimal Solving report Status Optimal Primal bound 400 Dual bound 400 Gap 0% (tolerance: 0.01%) Solution status feasible 400 (objective) 0 (bound viol.) 0 (int. viol.) 0 (row viol.) Timing 0.01 (total) 0.01 (presolve) 0.00 (postsolve) Nodes 0 LP iterations 0 (total) 0 (strong br.) 0 (separation) 0 (heuristics) Writing the solution to Arrangement_Pl-pulp.sol
解决建议
- 检查文件读写权限:Windows下PuLP默认会在当前目录生成
.lp、.sol等临时文件,若当前目录无写入权限,HiGHS会卡在写文件环节。可尝试用管理员权限运行脚本,或手动指定一个有读写权限的临时目录存放求解器输出文件。 - 禁用临时文件保留:初始化HiGHS_CMD时添加
keepFiles=False参数,阻止求解器保留中间文件,避免文件IO阻塞:solver = pl.HiGHS_CMD(path=path_to_HiGHS, keepFiles=False) - 切换PuLP内置HiGHS求解器:放弃手动指定HiGHS路径,改用PuLP自带的
HiGHS()求解器(无需额外配置路径),避免路径或进程调用的兼容性问题:solver = pl.HiGHS() result = prob.solve(solver) - 关闭求解器输出:添加
msg=False参数关闭HiGHS的日志输出,减少命令行IO交互可能导致的阻塞:solver = pl.HiGHS_CMD(path=path_to_HiGHS, msg=False) - 升级依赖版本:当前使用的HiGHS为1.5.3,尝试升级到最新稳定版;同时升级PuLP到最新版本,修复旧版本的Windows兼容bug:
pip install --upgrade pulp
内容的提问来源于stack exchange,提问作者siddharth goel
相关产品推荐
相关产品推荐

