Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  [cplex in JAVA] How do I get the message from console?

    Posted 10/12/12 04:00 AM

    Originally posted by: Sherwin32


    When I run my java-cplex program in Eclipse, the program generates all the solution data and prints them all into the console block.
    My problem here is: How can I get the console messages and append them into the textArea of my user interface?
    For now I have one imperfect way, which I found in the internet, to solve this problem:

    /**************************************************************/
    //(partial code)
    OutputStream jtextOut;

    jtextOut = new OutputStream()
    {
    final int BUFFER_LENGTH = 1024;
    byte buf[] = new byteBUFFER_LENGTH;
    int pos = 0;
    public void write(int b) throws IOException
    {
    bufpos ++ = (byte)b;
    if (pos >= BUFFER_LENGTH)
    flush();
    }

    public void flush() throws IOException
    {
    if (pos >= BUFFER_LENGTH)
    ta2.append(new String(buf));
    else
    ta2.append(new String(buf, 0, pos));

    pos = 0;
    }
    };

    System.setOut( new PrintStream(jtextOut, true));
    /***************************************************************/
    In this way, I can append the messages in the console into my textArea(in this case it'd be ta2)
    But there's still a problem that all the messages are printed at once exactly when the computing has been terminated(due to out-of-memory problem).
    What I need is to get the messages and print them instantaneously, instead of getting all of them in the end.

    Can anyone understand my question? Sorry for the poor English.
    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: [cplex in JAVA] How do I get the message from console?

    Posted 10/12/12 04:39 AM

    Originally posted by: SystemAdmin


    You can redirect CPLEX output to an instance of java.io.OutputStream using IloCplex.setOut() and IloCplex.setWarning().
    So you can do something like this:
    java.io.OutputStream stream = new java.io.OutputStream() {
       public void write(int b) {
          // write the byte b to your user interface
       }
    }
    cplex.setOut(stream);
    cplex.setWarning(stream);
    


    About the problem that all text is printed in one shot at the end of the program: What do you use to implement your GUI? You need to make sure that the CPLEX solve does not happen in the same thread that processes GUI events. Otherwise the solve will just block the GUI event dispatcher and your GUI will be frozen as long as the solve continues.
    In SWT or Swing I would usually start the solve in a different thread and have this thread post updates to the GUI using either Display.asyncExec() or SwingUtilities.invokeLater().
    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: [cplex in JAVA] How do I get the message from console?

    Posted 10/12/12 04:48 AM

    Originally posted by: Sherwin32


    Got it!
    Thanks a lot :)
    #CPLEXOptimizers
    #DecisionOptimization