Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  error output stream in java

    Posted 09/22/11 12:52 AM

    Originally posted by: GuneyPetek


    Hello,

    Using IloCplex class methods: setOut() and setWarning(), one can redirect default output stream and warning output stream. I am wondering how to redirect error stream to a file, since it seems like there is not any setError(outputStream s) in the Cplex java API.

    Thanks\\Guney
    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: error output stream in java

    Posted 09/22/11 01:51 AM

    Originally posted by: SystemAdmin


    There should be no need to do that.
    In Java the error messages are not written to the screen (or some other channel) but are returned as message in the IloException that is thrown due to the error.
    Do you see any error message written to the screen?
    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: error output stream in java

    Posted 09/22/11 01:08 PM

    Originally posted by: GuneyPetek


    I think I do, I am assuming that eclipse's red lines below are due to Cplex error stream.

    IBM ILOG License Manager: "IBM ILOG Optimization Suite for Academic Initiative" is accessing CPLEX 12 with option(s): "e m b q ".

    and

    Warning: Output names have been modified to conform to LP format.

    Are they exceptions I forgot to catch?
    #CPLEXOptimizers
    #DecisionOptimization


  • 4.  Re: error output stream in java

    Posted 09/23/11 01:57 AM

    Originally posted by: SystemAdmin


    "Warning: Output names have been modified to conform to LP format." should appear on the warning channel and should get redirected once you changed the warning channel via IloCplex.setWarning().
    Getting rid of the license manager message is a little more difficult. In C the way to do that is described here: https://www.ibm.com/developerworks/forums/thread.jspa?messageID=14601130&#14601130. In Java you can redirect the standard error output stream like this:
    java.io.PrintStream old = System.err;
    System.setErr(new java.io.PrintStream(new java.io.FileOutputStream("redirect")));
    System.err.println("This goes to redirected file");
    System.setErr(old);
    System.err.println("This goes to screen");
    

    I think the ilog license manager message is printed either by the IloCplex constructor or by method IloCplex.solve(). To get rid of the message you have to redirect System.err for these methods.
    #CPLEXOptimizers
    #DecisionOptimization


  • 5.  Re: error output stream in java

    Posted 09/23/11 01:36 PM

    Originally posted by: GuneyPetek


    I would also expect "Warning: Output names have been modified to conform to LP format." to go into the warning stream, but it does not. I have already redirected outputs using IloCplex.setOut() and warnings using IloCplex.setWarning(). Warning file comes up empty. That had strengthened my understanding there may be an third stream somewhere. But OK, I'll redirect errors on the stderr level.

    Thank you for your insight.
    #CPLEXOptimizers
    #DecisionOptimization


  • 6.  Re: error output stream in java

    Posted 09/23/11 06:04 PM

    Originally posted by: SystemAdmin


    That is strange. What version do you use and which operating system? I tried the following program here with 12.2 and 12.3 on Linux and in both cases it sent the "Warning: Output names have been modified to conform to LP format." message to the warning stream -- just as I would have expected.
    import java.io.*;
    import ilog.cplex.*;
    import ilog.concert.*;
     
    public final class ErrorOut {
       public static void main(String[] args) {
          try {
             IloCplex cplex = new IloCplex();
     
             IloNumVar x = cplex.numVar(0, 1, "invalid name");
             IloNumVar y = cplex.numVar(0, 1, "y");
             cplex.addGe(cplex.sum(x, y), 3);
     
             boolean disableOutput = true;
     
             FileOutputStream warn = null;
             FileOutputStream out = null;
             if (disableOutput) {
                cplex.setOut(out = new FileOutputStream("out"));
                cplex.setWarning(warn = new FileOutputStream("warn"));
                System.out.println("Output disabled.");
             }
     
             cplex.exportModel("model.lp");
             System.out.println("Model exported");
             if (warn != null) {
                System.out.println("--- Output on warning stream ---");
                warn.close();
                FileInputStream in = new FileInputStream("warn");
                byte[] buffer = new byte[1];
                while (in.read(buffer) == 1)
                   System.out.print((char)buffer[0]);
                in.close();
                System.out.println("--- End of warning output ---");
             }
             if (out != null)
                out.close();
             cplex.end();
     
          } catch (IOException e) {
             System.err.println(e.getMessage());
             e.printStackTrace();
             System.exit(-1);
          } catch (IloException e) {
             System.err.println(e.getMessage());
             e.printStackTrace();
             System.exit(-1);
          }
       }
    }
    

    Can you try and run this short program? Does it redirect the message for you?
    #CPLEXOptimizers
    #DecisionOptimization


  • 7.  Re: error output stream in java

    Posted 09/23/11 10:50 PM

    Originally posted by: GuneyPetek


    My mistake.

    12.2 on GNU/Linux here,too. But I was actually calling writeSolution() before redirection! :-X

    Thank you for the code snippet.
    #CPLEXOptimizers
    #DecisionOptimization