Search code examples
pythonjupyter-notebookmathematical-optimizationpulp

PuLP not printing output on IPython cell


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.


Solution

  • 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))