我正在使用 PuLP和一个项目的 IPython/Jupyter Notebook。
我有以下代码单元格:
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))
当我执行单元格时,输出很简单:
1
当我查看运行 Notebook 服务器的终端时,我可以看到求解器的输出(在本例中为:COIN)。如果将 model.solve 参数更改为
,也会发生同样的情况model.solve(pulp.solvers.PULP_CBC_CMD(msg=True))
或
model.solve(pulp.solvers.PYGLPK(msg=True))
但是,当我使用 Gurobi Solver 时,用这条线
model.solve(pulp.solvers.GUROBI(msg=True))
解算器的输出显示在笔记本单元格上,这是我想要的行为。事实上,我很乐意使用任何免费的求解器直接在笔记本单元上打印输出。
我无法在 PuLP 文档中找到有关如何解决此问题的说明。任何帮助,将不胜感激。我也很想知道其他人是否有这种行为。
我正在使用 Linux Mint、64 位、IPython 4.0.0 和 PuLP 1.6.0。
最佳答案
使用 %%python
单元格魔法打印终端的输出。
%%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))
关于python - PuLP 不在 IPython 单元格上打印输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34475510/