google / or-tools

Google's Operations Research tools:
https://developers.google.com/optimization/
Apache License 2.0
11.33k stars 2.14k forks source link

Cannot display solver output python #1077

Closed rlloretb closed 5 years ago

rlloretb commented 5 years ago

I cannot see the output from the solver in either GUROBI or CBC under or-tools python. I am using spyder (Ipython)

I use the following command: solver.EnableOutput()

For which solvers is it implemented?

Mizux commented 5 years ago

It is an API of MPsolver interface -> only available for MPSolver... https://github.com/google/or-tools/blob/554cbccaa95b4c11eced13f145de1bf468e1f919/ortools/linear_solver/linear_solver.h#L534

https://github.com/google/or-tools/blob/554cbccaa95b4c11eced13f145de1bf468e1f919/ortools/linear_solver/linear_solver.cc#L1253

https://github.com/google/or-tools/blob/554cbccaa95b4c11eced13f145de1bf468e1f919/ortools/linear_solver/linear_solver.h#L1317

Seems to be implemented for most of them:

CBC: https://github.com/google/or-tools/blob/554cbccaa95b4c11eced13f145de1bf468e1f919/ortools/linear_solver/cbc_interface.cc#L348-L353 CLP: https://github.com/google/or-tools/blob/554cbccaa95b4c11eced13f145de1bf468e1f919/ortools/linear_solver/clp_interface.cc#L420-L423 Gurobi: https://github.com/google/or-tools/blob/554cbccaa95b4c11eced13f145de1bf468e1f919/ortools/linear_solver/gurobi_interface.cc#L652-L654 SCIP: https://github.com/google/or-tools/blob/554cbccaa95b4c11eced13f145de1bf468e1f919/ortools/linear_solver/scip_interface.cc#L431-L432 GLPK: https://github.com/google/or-tools/blob/554cbccaa95b4c11eced13f145de1bf468e1f919/ortools/linear_solver/glpk_interface.cc#L530-L533

rlloretb commented 5 years ago

Am I not using that command properly then?

Mizux commented 5 years ago

For Gurobi, I don't know, don't have a license/lib available to test (e.d. did you compile from source with Gurobi enabled ?)

For CBC the code on master/stable branch is: https://github.com/google/or-tools/blob/8fbf82332c391b71cdfc7c46d3db6225f8f36f88/ortools/linear_solver/cbc_interface.cc#L343-L356

Don't know why level 1 and 2 are still set to zero -> maybe you can try to modify this setting ?

IIRC log should be on std::cerr by default

Mizux commented 5 years ago

Using this sample

from __future__ import print_function

from ortools.linear_solver import pywraplp

def main():
    # Create the linear solver with the GLOP backend.
    solver = pywraplp.Solver('simple_lp_program',
                             pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING)

    solver.EnableOutput()

    # Create the variables x and y.
    x = solver.NumVar(0, 1, 'x')
    y = solver.NumVar(0, 2, 'y')

    # Create a linear constraint, 0 <= x + y <= 2.
    ct = solver.Constraint(0, 2, 'ct')
    ct.SetCoefficient(x, 1)
    ct.SetCoefficient(y, 1)

    # Create the objective function, 3 * x + y.
    objective = solver.Objective()
    objective.SetCoefficient(x, 3)
    objective.SetCoefficient(y, 1)
    objective.SetMaximization()

    # Call the solver and display the results.
    solver.Solve()
    print('Solution:')
    print('Objective value = ', objective.Value())
    print('x = ', x.solution_value())
    print('y = ', y.solution_value())

if __name__ == '__main__':
    main()

got nothing, but if i recompile using

   message_handler.setLogLevel(0, 1);  // Coin messages 
   message_handler.setLogLevel(1, 1);  // Clp messages 
   message_handler.setLogLevel(2, 1);  // Presolve messages 
   message_handler.setLogLevel(3, 1);  // Cgl messages 

I got:

make run SOURCE=ortools/linear_solver/samples/simple_lp_program.py
PYTHONPATH=~/work/master:~/work/master/dependencies/sources/protobuf-3.6.1/python "/usr/bin/python3.5" ortools/linear_solver/samples/simple_lp_program.py 
Welcome to the CBC MILP Solver 
Version: 2.9.9 
Build Date: Dec 21 2018 

command line - cbc -solve -quit (default strategy 1)
Presolve 0 (-1) rows, 0 (-3) columns and 0 (-2) elements
Empty problem - 0 rows, 0 columns and 0 elements
Optimal - objective value 4
After Postsolve, objective 4, infeasibilities - dual 0 (0), primal 0 (0)
Optimal objective 4 - 0 iterations time 0.002, Presolve 0.00
Total time (CPU seconds):       0.00   (Wallclock seconds):       0.00

Solution:
Objective value =  4.0
x =  1.0
y =  1.0
rlloretb commented 5 years ago

Thanks for your reply! Would it be possible to fix the CBC case for the next version such that we can get the output without doing the manual changes before compiling? Else, I will recompile the tools by myself once I have time.

rlloretb commented 5 years ago

Just an update. I am using Spyder as GUI on Mac, Python 2. The output is actually shown in the Mac terminal that Spyder opens, not in the iPython console.

Mizux commented 5 years ago

@rlloretb Concerning Spider IDE, in Preferences -> Console -> Display did you check **Merge process standard output/error channels** ?

note: I don't use any Python IDE only gvim (+Valloric/YouCompleteMe plugin) / terminal (urxvt)

rlloretb commented 5 years ago

@rlloretb Concerning Spider IDE, in Preferences -> Console -> Display did you check **Merge process standard output/error channels** ?

note: I don't use any Python IDE only gvim (+Valloric/YouCompleteMe plugin) / terminal (urxvt)

I am using Spyder beta 4. This option is not displayed. No worries.