Decision Optimization

Decision Optimization

Delivers prescriptive analytics capabilities and decision intelligence to improve decision-making.


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  How to output log file in a loop

    Posted 11/25/13 09:18 PM

    Originally posted by: lluvia


    Hi,

    I'm using C++ to call CPLEX to solve a MIP problem with a MIP starter.

    I want to solve this program 20 times to compare the results with different initial values, so I'm writing an code containing 20 iterations. I found that the log fie saved in txt file will be overwrite in each iteration and I would only have the log file for the last iteration.

    My code is as follows:

    ofstream logfile ("result.log");
    cplex.setOut(logfile);

    Is there any method to save these 20 different log files?

    Thank you very much for your help.

    Jacinda


    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: How to output log file in a loop

    Posted 11/26/13 01:59 AM

    CPLEX will always append to the stream that you pass to setOut(). The issue with your code is that you do not open the stream in "append" mode but in "out" mode (the default open mode) which will start writing at the beginning of the file (see also here). Try to open the file like this instead:

    ofstream logfile("result.log", ios_base::app);

    Since you want to compare the files it may even be better to write the 20 different logs to 20 different files. That will allow you to use tools like 'diff' to compare them easily.


    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: How to output log file in a loop

    Posted 11/26/13 02:24 AM

    Originally posted by: lluvia


    Thank you very much for your reply and kindly help.  

    ofstream logfile("result.log", ios_base::app);
    The above command will help me to save these 20 log files into 1 file, right?

    I'm sorry for my unclear explanation. I want to save the 20 log files into 20 different files. How could I modify the code to make it? 

    Thank you very much for your kindly help.

    Jacinda 


    #CPLEXOptimizers
    #DecisionOptimization


  • 4.  Re: How to output log file in a loop

    Posted 11/26/13 02:34 AM

    You just need to create 20 different filenames. One way to do this is

    #include <sstream>
    #include <fstream>

    int
    main(void)
    {
       for (int i = 0; i < 20; ++i) {
          std::stringstream s;
          s << "log" << i << ".txt";
          std::ofstream logfile(s.str().c_str());
          // use the stream here
       }
       return 0;
    }

     


    #CPLEXOptimizers
    #DecisionOptimization