TIAN YUAN
TIAN YUAN

Reputation: 31

How to create a logfile when calling CPLEX from python

In the website of https://www.ibm.com/support/knowledgecenter/SSSA5P_12.6.2/ilog.odms.cplex.help/CPLEX/GettingStarted/topics/tutorials/InteractiveOptimizer/solnOptions.html I knew that when CPLEX solved a problem,it will create a logfile named as "cplex.log", but when I use CPLEX to solve a problem in python, this file wasn't created. And I'm so confused about that if this problem have some matters with the difference of languages. I mean, when using CPLEX to solve a problem in MATLAB, Java or C++, logfile will be created, but not created in python. I'm expecting for your help.Thanks so much.

Upvotes: 1

Views: 1921

Answers (1)

rkersh
rkersh

Reputation: 4465

The cplex.log file is specific to the CPLEX interactive. It is not created automatically when using the other APIs (e.g., Python, Java, etc.). However, you can create it yourself doing the following (e.g., with the Python API):

cpx = cplex.Cplex()
cplexlog = "cplex.log"
cpx.set_results_stream(cplexlog)
cpx.set_warning_stream(cplexlog)
cpx.set_error_stream(cplexlog)
cpx.set_log_stream(cplexlog)

The argument to the set_*_stream methods can be a path (as above) or a file-like object, so you can do pretty much anything you want (e.g., implement a file-like object to display output on stdout but also write it to a log file, etc.). See the documentation for set_results_stream for more details.

NOTE: There is some output that gets displayed from the interactive that is not available in the other APIs. However, you should be able to recreate it easily as all of the information is available through the programmatic APIs.

EDIT:

With CPLEX 12.10 using a filename with the set_results_stream, set_warning_stream, set_error_stream, and set_log_stream methods has been removed (see announcement here). Instead, a file-like object should be passed in, like so:

with cplex.Cplex() as cpx, \
     open("cplex.log") as cplexlog:
    cpx.set_results_stream(cplexlog)
    cpx.set_warning_stream(cplexlog)
    cpx.set_error_stream(cplexlog)
    cpx.set_log_stream(cplexlog)
    ...

Upvotes: 1

Related Questions