简体   繁体   English

PuLP 不在 IPython 单元格上打印 output

[英]PuLP not printing output on IPython cell

I am using PuLP and IPython/Jupyter Notebook for a project.我正在为一个项目使用PuLP和 IPython/Jupyter Notebook。

I have the following cell of code:我有以下代码单元:

import pulp
model = pulp.LpProblem('Example', pulp.LpMinimize)
x1 = pulp.LpVariable('x1', lowBound=0, cat='Integer')
x2 = pulp.LpVariable('x2', lowBound=0, cat='Integer')

model += -2*x1 - 3*x2 
model += x1 + 2*x2 <= 7
model += 2*x1 + x2 <= 7
model.solve(pulp.solvers.COIN(msg=True))

When I execute the cell, the output is simply:当我执行单元格时,output 很简单:

1

When I look at the terminal running the Notebook server, I can see the output of the solver (in this case: COIN).当我查看运行笔记本服务器的终端时,我可以看到求解器的 output(在本例中为:COIN)。 The same happens if a change the model.solve argument to如果将model.solve参数更改为

model.solve(pulp.solvers.PULP_CBC_CMD(msg=True))

or或者

model.solve(pulp.solvers.PYGLPK(msg=True))

However, when I use the Gurobi Solver, with the line但是,当我使用 Gurobi Solver 时,使用这条线

model.solve(pulp.solvers.GUROBI(msg=True))

the output of the solver is displayed on the Notebook cell, which is the behavior I want.求解器的 output 显示在笔记本单元格上,这是我想要的行为。 In fact, I would be happy with any free solver printing its output directly on the Notebook cell.事实上,我会很高兴任何免费的求解器直接在笔记本单元上打印它的 output。

I could not find directions on how to approach this issue in PuLP documentation.我无法在 PuLP 文档中找到有关如何解决此问题的说明。 Any help would be appreciated.任何帮助,将不胜感激。 I am also curious to know if someone else gets this behavior.我也很想知道其他人是否有这种行为。

I am using Linux Mint, 64 Bits, IPython 4.0.0 and PuLP 1.6.0.我正在使用 Linux Mint、64 位、IPython 4.0.0 和 PuLP 1.6.0。

Use %%python cell magic to print terminal's output.使用%%python cell magic 打印终端的 output。

%%python
import pulp
model = pulp.LpProblem('Example', pulp.LpMinimize)
x1 = pulp.LpVariable('x1', lowBound=0, cat='Integer')
x2 = pulp.LpVariable('x2', lowBound=0, cat='Integer')

model += -2*x1 - 3*x2 
model += x1 + 2*x2 <= 7
model += 2*x1 + x2 <= 7

model.solve(pulp.solvers.COIN(msg=True))

Late to the party, but for anyone still looking for a solution...派对迟到了,但对于仍在寻找解决方案的任何人......

Create a file called monkeypatch.py and put it in the same directory as your notebooks创建一个名为monkeypatch.py 的文件并将其放在与笔记本相同的目录中

Paste the following into it, and save将以下内容粘贴到其中,并保存

from pulp import PULP_CBC_CMD, LpProblem

original_solve_method = LpProblem.solve

def solve(prob):
    solver = PULP_CBC_CMD(logPath=r'log.txt', msg=False)
    original_solve_method(prob, solver=solver)
    with open('log.txt', 'r') as f:
        print(f.read())

LpProblem.solve = solve

Then in your notebook insert the line然后在你的笔记本中插入一行

import monkeypatch

near the top.靠近顶部。

It will overwrite the solve method, so that it writes the logfile to "log.txt" and then reads in this file and displays it inside the notebook.它将覆盖 solve 方法,以便将日志文件写入“log.txt”,然后读入该文件并将其显示在笔记本中。

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

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