如何使用pulp使model.solve()在python中不显示任何消息



我正在python中使用pulp实现一个运行时非常重要的代码。

#Initialize model
model = LpProblem('eUCB_Model', sense=LpMaximize)
#Define decision variables
y = LpVariable.dicts('tenant', [(i) for i in range(size)], lowBound=None, upBound=None, cat='Binary')
#Define model
model += lpSum([y[i]*th_hat[t][i] for i in range(size)])
#Define Constraints
model += lpSum([y[i]*R[t][i] for i in range(size)]) <= C
#solving the model
model.solve()

我的问题是,每次我调用model.solve()求解模型时,方法都会在终端中打印大量信息,如下所示:

Welcome to the CBC MILP Solver 
Version: 2.9.0 
Build Date: Feb 12 2015 
command line - /Users/henriquelima/opt/anaconda3/lib/python3.7/site-packages/pulp/apis/../solverdir/cbc/osx/64/cbc /var/folders/_6/r2j2fp7n5mxd5_1w2sbs8rvw0000gn/T/514d0624e4d645ae8582e6fa5203bc54-pulp.mps max ratio None allow None threads None presolve on strong None gomory on knapsack on probing on branch printingOptions all solution /var/folders/_6/r2j2fp7n5mxd5_1w2sbs8rvw0000gn/T/514d0624e4d645ae8582e6fa5203bc54-pulp.sol (default strategy 1)
At line 2 NAME          MODEL
At line 3 ROWS
At line 6 COLUMNS
At line 19 RHS
At line 21 BOUNDS
At line 25 ENDATA
Problem MODEL has 1 rows, 3 columns and 3 elements
Coin0008I MODEL read with 0 errors
String of None is illegal for double parameter ratioGap value remains 0
String of None is illegal for double parameter allowableGap value remains 0
String of None is illegal for integer parameter threads value remains 0
String of None is illegal for integer parameter strongBranching value remains 5
Option for gomoryCuts changed from ifmove to on
Option for knapsackCuts changed from ifmove to on
Continuous objective value is 2.03584 - 0.00 seconds
Cgl0004I processed model has 0 rows, 0 columns (0 integer (0 of which binary)) and 0 elements
Cbc3007W No integer variables - nothing to do
Cuts at root node changed objective from -2.03584 to -1.79769e+308
Probing was tried 0 times and created 0 cuts of which 0 were active after adding rounds of cuts (0.000 seconds)
Gomory was tried 0 times and created 0 cuts of which 0 were active after adding rounds of cuts (0.000 seconds)
Knapsack was tried 0 times and created 0 cuts of which 0 were active after adding rounds of cuts (0.000 seconds)
Clique was tried 0 times and created 0 cuts of which 0 were active after adding rounds of cuts (0.000 seconds)
MixedIntegerRounding2 was tried 0 times and created 0 cuts of which 0 were active after adding rounds of cuts (0.000 seconds)
FlowCover was tried 0 times and created 0 cuts of which 0 were active after adding rounds of cuts (0.000 seconds)
TwoMirCuts was tried 0 times and created 0 cuts of which 0 were active after adding rounds of cuts (0.000 seconds)

我想在终端中不打印任何类型的信息的情况下运行此方法,这可以显著减少程序的执行时间。你知道怎么做吗?

谢谢大家。

我不认为显示日志是性能问题。无论如何,正如文档所示:https://coin-or.github.io/pulp/technical/solvers.html#pulp.apis.PULP_CBC_CMD

您可以通过以下操作将msg=False作为参数传递:

model.solve(PULP_CBC_CMD(msg=False))

最新更新