4

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

4

3 回答 3

4

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

p.solve()

有了这个

p.solve(PULP_CBC_CMD(msg=0))

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

于 2020-11-12T06:09:24.173 回答
4

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

pulp.GUROBI().solve(prob)

我们需要写:

pulp.GUROBI(msg=0).solve(prob)
于 2016-07-07T05:33:40.353 回答
2

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

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

于 2016-07-25T04:23:49.523 回答