简体   繁体   English

Cplex Python如何避免打印输出

[英]Cplex Python how to avoid printing the output

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, 实际上,我只想打印目标值,然而,它显示了很多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? 我只想显示最后一行0.5,如何避免Cplex打印其他信息? Thank you in advance. 先感谢您。

cplex specifies 3 output streams: log, error, warning and results. cplex指定3个输出流:日志,错误,警告和结果。 You can disable the output with the commands. 您可以使用命令禁用输出。 set_xxx_stream(None). set_xxx_stream(无)。 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. 您也可以指定输出文件,而不是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. 您还可以设置几个参数来控制cplex输出的详细程度,但这是防止cplex打印任何内容的最佳方法。

You can adjust the verbosity level using the mip.display parameter: 您可以使用mip.display参数调整详细级别:

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

See here for more info. 有关详细信息,请参见此处

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. 由于Cplex是商业广告,我无法测试我的解决方案是否有效。 But you get the idea: split the string, get only what you want. 但是你明白了:拆分字符串,只得到你想要的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM