求解模型时如何使pyomo保持沉默(详细度0)

2024-05-12 19:23:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在以不同的条件运行一批pyomo解算器,我希望使解算器保持沉默

opt = SolverFactory('glpk') 
instance = model.create_instance(dat_file)
results = opt.solve(instance, tee=True, timelimit=300)

换句话说,当运行最后一行时,我不想在标准输出上打印任何内容。可能吗

我使用的解决方案是重定向标准输出:

from contextlib import redirect_stdout

opt = SolverFactory('glpk')
instance = model.create_instance(dat_file)
with open("log.txt", mode='w', encoding='utf-8') as fp:
    with redirect_stdout(fp):
        results = opt.solve(instance, tee=True, timelimit=300)

但我想知道是否有“更清洁”的解决方案


Tags: instancetrue标准modelcreateresultsdatfile