Cplex Python how to avoid printing the output
Asked Answered
T

4

5

after setting the objective function and constraints, i use

prob.solve()
print prob.solution.get_objective_value()

actually, I just want to print the objective value, however, it displays a lot of information of cplex,

Tried aggregator 1 time.
LP Presolve eliminated 5 rows and 1 columns.
All rows and columns eliminated.
Presolve time = -0.00 sec. (0.00 ticks)
0.5

I just want to display the last line 0.5, how to avoid printing other information by Cplex? Thank you in advance.

Towrey answered 4/12, 2013 at 2:37 Comment(0)
O
11

cplex specifies 3 output streams: log, error, warning and results. You can disable the output with the commands. set_xxx_stream(None). In your example,

prob.set_log_stream(None)
prob.set_error_stream(None)
prob.set_warning_stream(None)
prob.set_results_stream(None)

will disable all output. You can also specify an output file, instead of None. There are also several parameters that you can set to control the verbosity of the cplex output, but this is the best way to prevent cplex from printing anything.

Onassis answered 4/12, 2013 at 17:24 Comment(0)
D
3

You can adjust the verbosity level using the mip.display parameter:

# where c is a Cplex object
c.parameters.mip.display.set(0)

See here for more info.

Declivity answered 4/12, 2013 at 3:12 Comment(1)
thanks, but it doesn't work actually, I see the link, it is to block the message for mixed integer problems, I tried to find the one for linear programming problems, but failed. It seems cplex.setout(null) will work for C++ and Java, but no such command for python....sad about itTowrey
P
0

Searching for a way to suppress logging from docplex in 2024 lead me to this question, however, the accepted answer is not working with docplex.mp 2.25...

The only thing that really helped was to use this:

logging.getLogger("docplex.util.environment.logger").setLevel(logging. WARNING)

of course you can also set logging.ERROR

Papillon answered 4/10, 2024 at 21:51 Comment(0)
F
-1

Try this:

 ans = prob.solution.get_objective_value()
 print ans.split('\n')[-1]

Since Cplex is commercial I can't test if my solution is working. But you get the idea: split the string, get only what you want.

Fidelis answered 4/12, 2013 at 3:5 Comment(2)
Actually I am running a loop to solve an LP each iteration, I do can split the solution, but every time I need to scroll down A LOT to see the results even I split it. Thanks any way.Towrey
what do you mean you need to scroll down? where is the output going out to? to your teminal? when is it outputing?Fidelis

© 2022 - 2025 — McMap. All rights reserved.