Cplex Python how to avoid printing output

after setting the objective function and limitations, I use

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

Actually, I just want to print the object value, however it displays a lot of information about 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 of 0.5, how to avoid printing other information using Cplex? Thank you in advance.

+4
source share
3 answers

cplex defines 3 output streams: log, error, warning, and results. You can disable output using commands. set_xxx_stream (no). In your example

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

. None. cplex, cplex -.

+6

mip.display:

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

. .

0

Try the following:

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

Since Cplex is commercial, I cannot check if my solution works. But you get the idea: split the line, get only what you want.

-1
source

All Articles