Reputation: 152
I am using PuLP and IPython/Jupyter Notebook for a project.
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:
1
When I look at the terminal running the Notebook server, I can see the output of the solver (in this case: COIN). The same happens if a change the model.solve argument to
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
model.solve(pulp.solvers.GUROBI(msg=True))
the output of the solver is displayed on the Notebook cell, which is the behavior I want. In fact, I would be happy with any free solver printing its output directly on the Notebook cell.
I could not find directions on how to approach this issue in PuLP documentation. 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.
Upvotes: 2
Views: 1997
Reputation: 2271
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
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.
Upvotes: 0
Reputation: 1782
Use %%python
cell magic to print terminal's 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))
Upvotes: 1