如何禁用Pulp计算日志

8

我正在使用Python中的“pulp”与GUROBI一起解决一些优化问题。例如,GUROBI的计算日志如下:

Optimize a model with 12 rows, 25 columns and 39 nonzeros
Coefficient statistics:
  Matrix range    [1e+00, 1e+00]
  Objective range [1e+00, 1e+00]
  Bounds range    [1e+00, 1e+00]
  RHS range       [1e+00, 1e+00]
Found heuristic solution: objective 12
Presolve removed 3 rows and 12 columns
Presolve time: 0.00s
Presolved: 9 rows, 13 columns, 27 nonzeros
Variable types: 0 continuous, 13 integer (13 binary)

Root relaxation: objective 7.000000e+00, 11 iterations, 0.00 seconds

    Nodes    |    Current Node    |     Objective Bounds      |     Work
 Expl Unexpl |  Obj  Depth IntInf | Incumbent    BestBd   Gap | It/Node Time

*    0     0               0       7.0000000    7.00000  0.00%     -    0s

Explored 0 nodes (11 simplex iterations) in 0.00 seconds
Thread count was 4 (of 4 available processors)

Optimal solution found (tolerance 1.00e-04)
Best objective 7.000000000000e+00, best bound 7.000000000000e+00, gap 0.0%
('Gurobi status=', 2)

我想禁用这个输出,因为我要解决800k的优化问题,而且在输出中写入这些日志会使我的代码变得太慢。有什么方法可以禁用这些日志吗?
3个回答

9

你需要明确声明一个求解器,因此替换这行代码。

p.solve()

通过这个

p.solve(PULP_CBC_CMD(msg=0))

或者将 PULP_CBC_CMD 替换为您正在使用的任何求解器。


5
我发现了一种实现方式: 不使用默认的Gurobi调用方式,而是:
pulp.GUROBI().solve(prob)

我们需要编写:

pulp.GUROBI(msg=0).solve(prob)

3

m0_as的回答是正确的。我想分享一些更多的细节。在PuLP库中,每个求解器类都派生自相同的基类LpSolver。

class LpSolver:
    """A generic LP Solver"""

    def __init__(self, mip = True, msg = True, options = [], *args, **kwargs):
        self.mip = mip
        self.msg = msg
        self.options = options

在构造函数中,有一个名为msg的参数,它定义了是否要将来自求解器的信息输出到标准输出(stdout)。这是GUROBI类的实现。
请查看第1774行,它表示:
    @param msg: displays information from the solver to stdout

您可以非常容易地在此源文件中找到其他求解器特定参数。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接