What exactly do you mean by "correct" LP solving time?
If you mean the time it requires CPLEX to solve the initial LP relaxation then one simple way to get that would be to just grab it from the log output. After solving the root relaxation, CPLEX prints a line like this:
Root relaxation solution time = 0.00 sec. (1.64 ticks)
If you register an output stream with IloCplex.setOut() then you can just parse CPLEX log output for this line. This is the least intrusive way to get this time. It also does not require you to build your model twice. Here is a simple example:
import ilog.concert.IloException;
import ilog.cplex.IloCplex;
import java.io.IOException;
import java.io.OutputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RootTime {
public static final class Stream extends OutputStream {
private static final Pattern pattern = Pattern.compile("^Root relaxation solution time = *([0-9e.+-]+) sec\\. *\\(([0-9.e+-]+) ticks\\)");
private final StringBuffer buffer = new StringBuffer();
private double rootTime = Double.NaN;
private double rootTicks = Double.NaN;
@Override
public void write(int b) throws IOException {
final char c = (char)b;
buffer.append(c);
if (c == '\r' || c == '\n') {
final String line = buffer.toString();
buffer.setLength(0);
System.out.print(line);
final Matcher m = pattern.matcher(line);
if (m.find()) {
rootTime = Double.parseDouble(m.group(1));
rootTicks = Double.parseDouble(m.group(2));
}
}
}
public double getRootTime() { return rootTime; }
public double getRootTicks() { return rootTicks; }
}
public static void main(String[] args) {
for (final String model : args) {
try {
final Stream stream = new Stream();
final IloCplex cplex = new IloCplex();
cplex.importModel(model);
cplex.setOut(stream);
cplex.setParam(IloCplex.IntParam.NodeLim, 1);
cplex.solve();
System.out.println("Root relaxation time: " + stream.getRootTime() + "/" + stream.getRootTicks());
cplex.end();
}
catch (IloException e) {
System.err.println(e.getMessage());
System.exit(-1);
}
}
}
}
#CPLEXOptimizers#DecisionOptimization