Change Output Stream

465 views
Skip to first unread message

Martin Fuchsberger

unread,
Nov 2, 2010, 11:06:40 AM11/2/10
to Gurobi Optimization
Dear colleagues,

I was wondering if there is a possibility to change the default screen
output in the GUROBI JAVA API by passing an output stream as a
parameter to the gurobi environment instance.

I realized from the documentation that you can change the logfile, but
I was not able to find a way on changing the output console.

Cplex provides this as follows:

OutputStream outputStream = ...;

cplex = new IloCplex();
cplex.setOut(outputStream);

Since I migrate from cplex to gurobi, I hope a similar functionality
is available which I don't know yet.

Any help is greatly appreciated!

Sincerly,

Martin Fuchsberger

Victor Pillac

unread,
Nov 2, 2010, 7:45:10 PM11/2/10
to Gurobi Optimization
Hi Martin,

Have you tried System.setOut()? (http://download.oracle.com/javase/
1.4.2/docs/api/java/lang/System.html#setOut(java.io.PrintStream))
You can redirect to a custom stream that prints in a file and in the
console.

Regards

On Nov 2, 10:06 am, Martin Fuchsberger <fumar...@ifor.math.ethz.ch>
wrote:

Greg Glockner

unread,
Nov 2, 2010, 8:11:32 PM11/2/10
to gur...@googlegroups.com
Alternately, you can create an informational callback that captures the logfile information and uses it however you want. See Callback.java in the examples\java subdirectory.

Martin Fuchsberger

unread,
Nov 3, 2010, 8:14:16 AM11/3/10
to Gurobi Optimization
Hi Victor,

I actually use System.setOut in another class which is basically a
console window with a text area as part of an application (See below).
But Gurobi is not directly affected by that as well as cplex. That's
why I passed cplex.setOut() the outputStream of that class, which
worked well.

Maybe I miss the obvious solution here?

<code>

import java.awt.BorderLayout;
import java.awt.Point;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.PrintStream;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

public class Console extends JFrame {
class ReaderThread extends Thread {
PipedInputStream pi;

ReaderThread(final PipedInputStream pi) {
this.pi = pi;
}

@Override
public void run() {
final byte[] buf = new byte[1024];
try {
while (true) {
final int len = pi.read(buf);
if (len == -1) {
break;
}
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
textArea.append(new String(buf, 0, len));

// Make sure the last line is always visible
textArea.setCaretPosition(textArea.getDocument().getLength());

// Keep the text area down to a certain character
// size
final int idealSize = 1000;
final int maxExcess = 500;
final int excess = textArea.getDocument().getLength() -
idealSize;
if (excess >= maxExcess) {
textArea.replaceRange("", 0, excess);
}
}
});
}
} catch (final IOException e) {
}
}
}

PipedInputStream piOut;
PipedInputStream piErr;
PipedOutputStream poOut;
PipedOutputStream poErr;

JTextArea textArea = new JTextArea();

public Console(final int xPos, final int yPos) throws IOException {
// Set up System.out
piOut = new PipedInputStream();
poOut = new PipedOutputStream(piOut);
System.setOut(new PrintStream(poOut, true));

// Set up System.err
piErr = new PipedInputStream();
poErr = new PipedOutputStream(piErr);
System.setErr(new PrintStream(poErr, true));

// Add a scrolling text area
textArea.setEditable(false);
textArea.setRows(20);
textArea.setColumns(130);

getContentPane().add(new JScrollPane(textArea),
BorderLayout.CENTER);
pack();
setLocation(new Point(xPos, yPos));
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create reader threads
new ReaderThread(piOut).start();
new ReaderThread(piErr).start();
}

public OutputStream getOutputStream() {
return poOut;
}
}

</code>

Martin Fuchsberger

unread,
Nov 3, 2010, 8:16:27 AM11/3/10
to Gurobi Optimization
Agreed, that is a possible solution. Is there a specific reason why
you can't redirect the console output stream of gurobi?

Greg Glockner

unread,
Nov 3, 2010, 8:38:44 AM11/3/10
to gur...@googlegroups.com
If you want an even simpler approach, just turn off the console logging and use the log file.

Reply all
Reply to author
Forward
0 new messages