Originally posted by: SystemAdmin
OK, in Java the quickest thing to do is to use IloCplex.setOut() and redirect output to a stream that adds time stamps:
import ilog.cplex.*;
import ilog.concert.*;
public
final
class Timestamp
{
/** A java.io.OutputStream that prints to stdout and prepends each line * with a timestamp. * The implementation is utterly ineffecient but should illustrate how * to get timestamps on CPLEX output. */
private
static
final
class TimestampOutput
extends java.io.OutputStream
{
/** Time when this instance was created. */
private
final
long start;
public TimestampOutput()
{ start = System.currentTimeMillis();
}
/** Write <code>character</code> to stdout and print a timestamp * when we started a new line. */
public
void write(
int character)
{ System.out.print((
char)character);
if (character ==
'\n')
{
// We just started a new line, so print out the number of
// elapsed seconds.
final
long elapsed = System.currentTimeMillis() - start; System.out.print(String.format(
"%10.2f", 1e-3 * elapsed) +
": ");
}
}
}
public
static
void main(String[] args)
{
// Solve all models that were specifiy on the command line.
for (String model : args)
{
try
{
// Load the model. IloCplex cplex =
new IloCplex(); cplex.importModel(model);
// Redirect output to a stream that adds timestamps. cplex.setOut(
new TimestampOutput());
// Solve the model. cplex.solve(); cplex.end();
}
catch (IloException e)
{ System.err.println(e.getMessage()); System.exit(-1);
}
}
}
}
Another option would be to use a MIPInfoCallback and print timestamp and progress from this callback. Starting with CPLEX 12.5 this callback provides a getCplexTime() member. So you could write:
import ilog.cplex.*;
import ilog.concert.*;
public
final
class TimestampCB
{
private
static
final
class TimestampOutput
extends IloCplex.MIPInfoCallback
{
private
final Double start;
public TimestampOutput(Double start)
{ this.start = start;
}
public
void main()
throws IloException
{ System.out.println(
"Elapsed: " + (getCplexTime() - start) +
" Gap: " + getMIPRelativeGap());
}
}
public
static
void main(String[] args)
{
// Solve all models that were specifiy on the command line.
for (String model : args)
{
try
{
// Load the model. IloCplex cplex =
new IloCplex(); cplex.importModel(model); cplex.use(
new TimestampOutput(cplex.getCplexTime()));
// Solve the model. cplex.solve(); cplex.end();
}
catch (IloException e)
{ System.err.println(e.getMessage()); System.exit(-1);
}
}
}
}
Prior to version 12.5 this getCplexTime() function does not exist but you could use System.getCurrentTimeMillis() instead.
#CPLEXOptimizers#DecisionOptimization