繁体   English   中英

如何禁用纸浆的计算日志

[英]How to disable the calculations log of pulp

我在 python 中使用“纸浆”和 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 优化问题,并且在输出中写入这些日志会使我的代码太慢。 禁用这些日志的任何想法?

我刚刚找到了我们如何做到这一点:而不是调用 Gurobi 的默认方式,即:

pulp.GUROBI().solve(prob)

我们需要写:

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

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 参数,它定义是否要将信息从求解器回显到标准输出。 这是GUROBI 类的实现

见它说的第 1774 行

    @param msg: displays information from the solver to stdout

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

您需要显式声明求解器,因此请替换此行

p.solve()

有了这个

p.solve(PULP_CBC_CMD(msg=0))

或者用你正在使用的任何求解器替换`PULP_CBC_CMD。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM