嗨,我正在尝试使用 python 模块 grinpy,如果有帮助的话,我正在使用 jupyter 笔记本。我写了以下代码:
import grinpy as gp
G = gp.petersen_graph()
a = gp.chromatic_number(G)
print(a)
现在
a
应该是一个数字,所以我只想让它打印一个数字,但由于第3行的结果,它似乎打印了很多其他东西,然后是第4行的数字。
如何阻止它打印不需要的东西?
也许还值得注意的是,我打算在更大的程序中以类似的方式使用这个模块。
谢谢!
这是输出:
Welcome to the CBC MILP Solver
Version: 2.10.3
Build Date: Dec 15 2019
command line - /opt/homebrew/lib/python3.9/site-packages/pulp/solverdir/cbc/osx/64/cbc /var/folders/wb/tmprjpmd5vd6zkhfy1sbs57r0000gn/T/37918f7940534834b5bb691051ec13cd-pulp.mps timeMode elapsed branch printingOptions all solution /var/folders/wb/tmprjpmd5vd6zkhfy1sbs57r0000gn/T/37918f7940534834b5bb691051ec13cd-pulp.sol (default strategy 1)
At line 2 NAME MODEL
At line 3 ROWS
At line 265 COLUMNS
At line 1096 RHS
At line 1357 BOUNDS
At line 1468 ENDATA
Problem MODEL has 260 rows, 110 columns and 600 elements
Coin0008I MODEL read with 0 errors
Option for timeMode changed from cpu to elapsed
Continuous objective value is 1 - 0.00 seconds
Cgl0003I 0 fixed, 0 tightened bounds, 139 strengthened rows, 0 substitutions
Cgl0003I 0 fixed, 0 tightened bounds, 91 strengthened rows, 0 substitutions
Cgl0003I 0 fixed, 0 tightened bounds, 20 strengthened rows, 0 substitutions
Cgl0004I processed model has 160 rows, 110 columns (110 integer (110 of which binary)) and 550 elements
Cutoff increment increased from 1e-05 to 0.9999
Cbc0045I 10 integer variables out of 110 objects (110 integer) have cost of 1 - high priority
Cbc0045I branch on satisfied N create fake objective Y random cost Y
Cbc0038I Initial state - 20 integers unsatisfied sum - 10
Cbc0038I Pass 1: suminf. 10.00000 (20) obj. 2 iterations 15
Cbc0038I Pass 2: suminf. 10.00000 (20) obj. 2 iterations 15
Cbc0038I Pass 3: suminf. 0.00000 (0) obj. 7 iterations 111
Cbc0038I Solution found of 7
Cbc0038I Rounding solution of 4 is better than previous of 7
Cbc0038I Before mini branch and bound, 81 integers at bound fixed and 0 continuous
Cbc0038I Full problem 160 rows 110 columns, reduced to 38 rows 20 columns
Cbc0038I Mini branch and bound did not improve solution (0.01 seconds)
Cbc0038I Round again with cutoff of 2.90009
Cbc0038I Pass 4: suminf. 10.00000 (20) obj. 2 iterations 0
Cbc0038I Pass 5: suminf. 10.00000 (20) obj. 2 iterations 16
Cbc0038I Pass 6: suminf. 2.29793 (21) obj. 2.90009 iterations 100
Cbc0038I Pass 7: suminf. 1.39874 (24) obj. 2.90009 iterations 51
Cbc0038I Pass 8: suminf. 8.50000 (17) obj. 2.5 iterations 76
Cbc0038I Pass 9: suminf. 8.50000 (17) obj. 2.5 iterations 40
Cbc0038I Pass 10: suminf. 1.69847 (17) obj. 2.90009 iterations 66
Cbc0038I Pass 11: suminf. 1.39874 (24) obj. 2.90009 iterations 56
Cbc0038I Pass 12: suminf. 1.39874 (24) obj. 2.90009 iterations 69
Cbc0038I Pass 13: suminf. 1.39874 (24) obj. 2.90009 iterations 37
Cbc0038I Pass 14: suminf. 10.00000 (20) obj. 2 iterations 52
Cbc0038I Pass 15: suminf. 10.00000 (20) obj. 2 iterations 12
Cbc0038I Pass 16: suminf. 10.00000 (20) obj. 2 iterations 57
Cbc0038I Pass 17: suminf. 1.39874 (24) obj. 2.90009 iterations 66
Cbc0038I Pass 18: suminf. 1.39874 (24) obj. 2.90009 iterations 50
Cbc0038I Pass 19: suminf. 8.50000 (17) obj. 2.5 iterations 72
Cbc0038I Pass 20: suminf. 8.50000 (17) obj. 2.5 iterations 17
Cbc0038I Pass 21: suminf. 1.69847 (17) obj. 2.90009 iterations 55
Cbc0038I Pass 22: suminf. 1.39874 (24) obj. 2.90009 iterations 46
Cbc0038I Pass 23: suminf. 1.89829 (18) obj. 2.90009 iterations 77
Cbc0038I Pass 24: suminf. 1.39874 (24) obj. 2.90009 iterations 38
Cbc0038I Pass 25: suminf. 1.39874 (24) obj. 2.90009 iterations 64
Cbc0038I Pass 26: suminf. 1.39874 (24) obj. 2.90009 iterations 31
Cbc0038I Pass 27: suminf. 8.50000 (17) obj. 2.5 iterations 77
Cbc0038I Pass 28: suminf. 8.50000 (17) obj. 2.5 iterations 7
Cbc0038I Pass 29: suminf. 1.69847 (17) obj. 2.90009 iterations 71
Cbc0038I Pass 30: suminf. 1.39874 (24) obj. 2.90009 iterations 59
Cbc0038I Pass 31: suminf. 1.49865 (25) obj. 2.90009 iterations 79
Cbc0038I Pass 32: suminf. 1.39874 (24) obj. 2.90009 iterations 35
Cbc0038I Pass 33: suminf. 2.09811 (19) obj. 2.90009 iterations 77
Cbc0038I Pass 34: suminf. 1.39874 (24) obj. 2.90009 iterations 31
Cbc0038I Pass 35: suminf. 8.50000 (17) obj. 2.5 iterations 95
Cbc0038I Pass 36: suminf. 8.50000 (17) obj. 2.5 iterations 19
Cbc0038I Pass 37: suminf. 1.69847 (17) obj. 2.90009 iterations 65
Cbc0038I Pass 38: suminf. 1.39874 (24) obj. 2.90009 iterations 46
Cbc0038I Pass 39: suminf. 1.49865 (24) obj. 2.90009 iterations 55
Cbc0038I Pass 40: suminf. 1.39874 (24) obj. 2.90009 iterations 31
Cbc0038I Pass 41: suminf. 8.29973 (19) obj. 2.90009 iterations 82
Cbc0038I Pass 42: suminf. 2.09811 (19) obj. 2.90009 iterations 27
Cbc0038I Pass 43: suminf. 1.39874 (24) obj. 2.90009 iterations 28
Cbc0038I Pass 44: suminf. 8.50000 (17) obj. 2.5 iterations 58
Cbc0038I Pass 45: suminf. 8.50000 (17) obj. 2.5 iterations 18
Cbc0038I Pass 46: suminf. 1.69847 (17) obj. 2.90009 iterations 64
Cbc0038I Pass 47: suminf. 1.39874 (24) obj. 2.90009 iterations 50
Cbc0038I Pass 48: suminf. 10.00000 (20) obj. 2 iterations 41
Cbc0038I Pass 49: suminf. 10.00000 (20) obj. 2 iterations 24
Cbc0038I Pass 50: suminf. 10.00000 (20) obj. 2 iterations 30
Cbc0038I Pass 51: suminf. 2.09811 (21) obj. 2.90009 iterations 73
Cbc0038I Pass 52: suminf. 1.39874 (24) obj. 2.90009 iterations 58
Cbc0038I Pass 53: suminf. 1.39874 (24) obj. 2.90009 iterations 74
Cbc0038I Pass 54: suminf. 1.39874 (24) obj. 2.90009 iterations 30
Cbc0038I Pass 55: suminf. 10.00000 (20) obj. 2 iterations 68
Cbc0038I Pass 56: suminf. 10.00000 (20) obj. 2 iterations 32
Cbc0038I Pass 57: suminf. 10.00000 (20) obj. 2 iterations 29
Cbc0038I Pass 58: suminf. 7.29973 (28) obj. 2.90009 iterations 80
Cbc0038I Pass 59: suminf. 1.89829 (18) obj. 2.90009 iterations 62
Cbc0038I Pass 60: suminf. 1.39874 (24) obj. 2.90009 iterations 28
Cbc0038I Pass 61: suminf. 1.39874 (24) obj. 2.90009 iterations 58
Cbc0038I Pass 62: suminf. 1.39874 (24) obj. 2.90009 iterations 23
Cbc0038I Pass 63: suminf. 8.50000 (17) obj. 2.5 iterations 68
Cbc0038I Pass 64: suminf. 8.50000 (17) obj. 2.5 iterations 17
Cbc0038I Pass 65: suminf. 1.69847 (17) obj. 2.90009 iterations 54
Cbc0038I Pass 66: suminf. 1.39874 (24) obj. 2.90009 iterations 34
Cbc0038I Pass 67: suminf. 1.39874 (24) obj. 2.90009 iterations 69
Cbc0038I Pass 68: suminf. 1.39874 (24) obj. 2.90009 iterations 21
Cbc0038I Pass 69: suminf. 10.00000 (20) obj. 2 iterations 75
Cbc0038I Pass 70: suminf. 10.00000 (20) obj. 2 iterations 26
Cbc0038I Pass 71: suminf. 10.00000 (20) obj. 2 iterations 9
Cbc0038I Pass 72: suminf. 2.89739 (24) obj. 2.90009 iterations 80
Cbc0038I Pass 73: suminf. 1.39874 (24) obj. 2.90009 iterations 52
Cbc0038I Pass 74: suminf. 3.49685 (25) obj. 2.90009 iterations 78
Cbc0038I Pass 75: suminf. 1.39874 (24) obj. 2.90009 iterations 25
Cbc0038I Pass 76: suminf. 8.50000 (17) obj. 2.5 iterations 54
Cbc0038I Pass 77: suminf. 8.50000 (17) obj. 2.5 iterations 16
Cbc0038I Pass 78: suminf. 1.69847 (17) obj. 2.90009 iterations 48
Cbc0038I Pass 79: suminf. 1.39874 (24) obj. 2.90009 iterations 51
Cbc0038I Pass 80: suminf. 2.49775 (21) obj. 2.90009 iterations 56
Cbc0038I Pass 81: suminf. 1.39874 (24) obj. 2.90009 iterations 46
Cbc0038I Pass 82: suminf. 1.39874 (24) obj. 2.90009 iterations 97
Cbc0038I Pass 83: suminf. 1.39874 (24) obj. 2.90009 iterations 35
Cbc0038I Pass 84: suminf. 10.00000 (20) obj. 2 iterations 24
Cbc0038I Pass 85: suminf. 10.00000 (20) obj. 2 iterations 12
Cbc0038I Pass 86: suminf. 10.00000 (20) obj. 2 iterations 26
Cbc0038I Pass 87: suminf. 10.90009 (24) obj. 2.90009 iterations 82
Cbc0038I Pass 88: suminf. 11.80018 (32) obj. 2.90009 iterations 62
Cbc0038I Pass 89: suminf. 2.49775 (24) obj. 2.90009 iterations 70
Cbc0038I Pass 90: suminf. 1.39874 (24) obj. 2.90009 iterations 52
Cbc0038I Pass 91: suminf. 1.39874 (24) obj. 2.90009 iterations 59
Cbc0038I Pass 92: suminf. 1.39874 (24) obj. 2.90009 iterations 23
Cbc0038I Pass 93: suminf. 1.39874 (24) obj. 2.90009 iterations 84
Cbc0038I Pass 94: suminf. 1.39874 (24) obj. 2.90009 iterations 33
Cbc0038I Pass 95: suminf. 9.50000 (19) obj. 2.5 iterations 62
Cbc0038I Pass 96: suminf. 8.00000 (25) obj. 2.5 iterations 13
Cbc0038I Pass 97: suminf. 1.89829 (25) obj. 2.90009 iterations 46
Cbc0038I Pass 98: suminf. 1.39874 (24) obj. 2.90009 iterations 34
Cbc0038I Pass 99: suminf. 1.39874 (24) obj. 2.90009 iterations 50
Cbc0038I Pass 100: suminf. 1.39874 (24) obj. 2.90009 iterations 36
Cbc0038I Pass 101: suminf. 8.50000 (17) obj. 2.5 iterations 63
Cbc0038I Pass 102: suminf. 8.50000 (17) obj. 2.5 iterations 23
Cbc0038I Pass 103: suminf. 1.39874 (24) obj. 2.90009 iterations 64
Cbc0038I No solution found this major pass
Cbc0038I Before mini branch and bound, 8 integers at bound fixed and 0 continuous
Cbc0038I Full problem 160 rows 110 columns, reduced to 150 rows 102 columns - 6 fixed gives 83, 62 - ok now
Cbc0038I Mini branch and bound did not improve solution (0.06 seconds)
Cbc0038I After 0.06 seconds - Feasibility pump exiting with objective of 4 - took 0.05 seconds
Cbc0012I Integer solution of 4 found by feasibility pump after 0 iterations and 0 nodes (0.06 seconds)
Cbc0012I Integer solution of 3 found by DiveCoefficient after 0 iterations and 0 nodes (0.06 seconds)
Cbc0038I Full problem 160 rows 110 columns, reduced to 30 rows 10 columns
Cbc0031I 9 added rows had average density of 17.555556
Cbc0013I At root node, 18 cuts changed objective from 2 to 2.0625 in 8 passes
Cbc0014I Cut generator 0 (Probing) - 0 row cuts average 0.0 elements, 0 column cuts (0 active) in 0.002 seconds - new frequency is -100
Cbc0014I Cut generator 1 (Gomory) - 25 row cuts average 79.2 elements, 0 column cuts (0 active) in 0.001 seconds - new frequency is 1
Cbc0014I Cut generator 2 (Knapsack) - 0 row cuts average 0.0 elements, 0 column cuts (0 active) in 0.002 seconds - new frequency is -100
Cbc0014I Cut generator 3 (Clique) - 0 row cuts average 0.0 elements, 0 column cuts (0 active) in 0.000 seconds - new frequency is -100
Cbc0014I Cut generator 4 (MixedIntegerRounding2) - 0 row cuts average 0.0 elements, 0 column cuts (0 active) in 0.000 seconds - new frequency is -100
Cbc0014I Cut generator 5 (FlowCover) - 0 row cuts average 0.0 elements, 0 column cuts (0 active) in 0.000 seconds - new frequency is -100
Cbc0014I Cut generator 6 (TwoMirCuts) - 28 row cuts average 58.3 elements, 0 column cuts (0 active) in 0.005 seconds - new frequency is -100
Cbc0014I Cut generator 7 (ZeroHalf) - 60 row cuts average 15.7 elements, 0 column cuts (0 active) in 0.028 seconds - new frequency is 1
Cbc0001I Search completed - best objective 3, took 545 iterations and 0 nodes (0.13 seconds)
Cbc0035I Maximum depth 0, 0 variables fixed on reduced cost
Cuts at root node changed objective from 2 to 2.0625
Probing was tried 8 times and created 0 cuts of which 0 were active after adding rounds of cuts (0.002 seconds)
Gomory was tried 8 times and created 25 cuts of which 0 were active after adding rounds of cuts (0.001 seconds)
Knapsack was tried 8 times and created 0 cuts of which 0 were active after adding rounds of cuts (0.002 seconds)
Clique was tried 8 times and created 0 cuts of which 0 were active after adding rounds of cuts (0.000 seconds)
MixedIntegerRounding2 was tried 8 times and created 0 cuts of which 0 were active after adding rounds of cuts (0.000 seconds)
FlowCover was tried 8 times and created 0 cuts of which 0 were active after adding rounds of cuts (0.000 seconds)
TwoMirCuts was tried 8 times and created 28 cuts of which 0 were active after adding rounds of cuts (0.005 seconds)
ZeroHalf was tried 8 times and created 60 cuts of which 0 were active after adding rounds of cuts (0.028 seconds)
Result - Optimal solution found
Objective value: 3.00000000
Enumerated nodes: 0
Total iterations: 545
Time (CPU seconds): 0.11
Time (Wallclock seconds): 0.13
Option for printingOptions changed from normal to all
Total time (CPU seconds): 0.12 (Wallclock seconds): 0.13
3
正如你所看到的,底部有一个数字3,这是来自打印命令的。所以我问如何摆脱其余的。
首先添加一个新单元格并在该单元格中运行
%pip install wurlitzer
。 (一旦安装在您工作的地方,您可以根据需要删除该单元。)
将当前提供的代码分成两部分,并添加使用 wurlitzer 来包含除
print()
之外的所有内容,以便嘈杂的部分由 with
上下文管理器控制:
笔记本电池:
from wurlitzer import pipes
with pipes() as (out, err):
import grinpy as gp
G = gp.petersen_graph()
a = gp.chromatic_number(G)
print(a)
Wurlitzer 在这里是必要的,因为主要的令人讨厌的噪声输出是 C 级 stdout/stderr 管道,常用的
%%capture
(参见下面的部分)将无法抑制。有关 wurlitzer 此类用途的更多信息,请参阅此处。
有时在我的测试中,我第一次以这种方式调用 wurlitzer 时会看到一条警告。 如果这是一个一致的问题并且您根本不希望显示该警告,您也许可以添加使用
%%capture
。此外,您还必须将 print(a)
移动到另一个单元格,或者重复它,否则 %%capture
单元格魔法将阻止它。添加 %%capture
作为单元格的第一行,与 wurlizter 结合使用,可以确保警告也被静音。请参阅下一节,了解有关 %%capture
单元魔法使用的更多信息。您也可以通过以下方法来删除此过程中的任何警告:
第一个单元格:
%%capture
from wurlitzer import pipes
with pipes() as (out, err):
import grinpy as gp
G = gp.petersen_graph()
a = gp.chromatic_number(G)
第二个单元格:
print(a)
如果这只是普通Python和基于shell的嘈杂输出的问题,那么单独将
%%capture
单元魔法添加到第一个单元的第一行可能就足够了(我看到一个地方没有在单元格中使用 %run -i
运行脚本时就足够了,有点像这样:
第一个单元格:
%%capture
import grinpy as gp
G = gp.petersen_graph()
a = gp.chromatic_number(G)
第二个单元格:
print(a)
使用 %%capture
单元魔法来抑制典型输出基于回答“如何抑制运行 IPython 的 Jupyter 中的输出?”. 但这还不够;
在 OP 提供的可重现代码的情况下,需要使用 wurlitzer。