Skip to content
Snippets Groups Projects
Commit add4858f authored by Johannes Mey's avatar Johannes Mey
Browse files

fix performance bug for uncached jastadd and reading of large files.

parent 809ee00a
No related branches found
No related tags found
No related merge requests found
......@@ -85,7 +85,7 @@ public abstract class AbstractILPSolver implements BenchmarkableSolver {
// call to abstract method
try {
solve0(model, watch, variablesSetToOne);
solve0(model, ilp, watch, variablesSetToOne);
this.lastSolving = watch.time(TimeUnit.MILLISECONDS) - this.lastGeneration;
// translate ilp-encoded solution to MQuAT solution
return populateSolution(variablesSetToOne, new ILPSolution(model));
......@@ -103,12 +103,11 @@ public abstract class AbstractILPSolver implements BenchmarkableSolver {
* @return the objective value
* @throws SolvingException if anything went wrong
*/
protected abstract double solve0(Root model, StopWatch watch, List<IlpVariable> variablesSetToOne) throws SolvingException;
protected abstract double solve0(Root model, ILP ilp, StopWatch watch, List<IlpVariable> variablesSetToOne) throws SolvingException;
protected ILPSolution populateSolution(List<IlpVariable> variablesSetToOne, ILPSolution result) throws SolvingException {
List<Assignment> listOfAssignments = new ArrayList<>();
for (IlpVariable var : variablesSetToOne) {
logger.debug("Found, that {} = 1", var.getName());
if (var.isMappingVariable()) {
IlpMappingVariable mappingVar = var.asMappingVariable();
Assignment assignment = new Assignment();
......
......@@ -4,6 +4,9 @@ import de.tudresden.inf.st.mquat.jastadd.model.ILP;
import de.tudresden.inf.st.mquat.jastadd.model.IlpVariable;
import de.tudresden.inf.st.mquat.solving.SolvingException;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
......@@ -23,6 +26,7 @@ public class GurobiSolver extends ILPExternalSolver {
* @see GurobiSolver#setDeleteFilesOnExit(boolean)
*/
public GurobiSolver() {
}
......@@ -35,8 +39,11 @@ public class GurobiSolver extends ILPExternalSolver {
@Override
protected void readFromPlainTextSolution(ILP ilp, Path solution, ILPSolution result,
List<IlpVariable> variablesSetToOne) throws SolvingException {
try (Stream<String> lines = Files.lines(solution)) {
for (String line : lines.collect(Collectors.toList())) {
logger.debug("reading solution from {}.", solution);
try(BufferedReader br = new BufferedReader(new FileReader(solution.toFile()))) {
for(String line; (line = br.readLine()) != null; ) {
if (line.startsWith("#")) {
// comment line
......@@ -45,24 +52,20 @@ public class GurobiSolver extends ILPExternalSolver {
logger.debug("read objective {}", Double.valueOf(line.substring(20).trim()));
}
} else {
String[] tokens = line.split("\\s+");
if (tokens.length == 2) {
// tokens: name, value
if (Math.round(Double.parseDouble(tokens[1])*1000000000)==1000000000) {
logger.debug("found new variable {} with value {}", tokens[0], tokens[1]);
IlpVariable variable = ilp.resolve(tokens[0]);
if (line.endsWith(" 1")) {
String varString = line.substring(0,line.length()-2);
IlpVariable variable = ilp.resolve(varString);
if (variable == null) {
throw new SolvingException("Could not find variable with name " + tokens[0]);
throw new SolvingException("Could not find variable with name " + varString);
}
variablesSetToOne.add(variable);
}
}
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
throw new SolvingException("Could not open solution file", e);
} catch (NumberFormatException e) {
throw new SolvingException("Could not parse solution file", e);
e.printStackTrace();
}
}
......
......@@ -57,8 +57,7 @@ public class ILPDirectSolver extends AbstractILPSolver {
this.prob = null;
}
protected double solve0(Root model, StopWatch watch, List<IlpVariable> variablesSetToOne) throws SolvingException {
ILP ilp = model.getILP();
protected double solve0(Root model, ILP ilp, StopWatch watch, List<IlpVariable> variablesSetToOne) throws SolvingException {
if (logger.isTraceEnabled()) {
logger.trace(ilp.printIlp().toString());
......
......@@ -70,7 +70,7 @@ public abstract class ILPExternalSolver extends AbstractILPSolver {
}
}
protected double solve0(Root model, StopWatch watch, List<IlpVariable> variablesSetToOne) throws SolvingException {
protected double solve0(Root model, ILP ilp, StopWatch watch, List<IlpVariable> variablesSetToOne) throws SolvingException {
long startOfWriteOutInMillis = watch.time(TimeUnit.MILLISECONDS);
......@@ -85,7 +85,9 @@ public abstract class ILPExternalSolver extends AbstractILPSolver {
}
// write out lp file
IlpString output = model.getILP().printIlp();
logger.debug("Starting ILP string construction.");
IlpString output = ilp.printIlp();
logger.debug("ILP string construction completed.");
try (BufferedWriter writer = Files.newBufferedWriter(
lp, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)) {
writer.write(output.toString());
......@@ -140,8 +142,10 @@ public abstract class ILPExternalSolver extends AbstractILPSolver {
// read the solution file
ILPSolution result = new ILPSolution(model);
logger.debug("created empty solution");
// readFromPrintableSolution(ilp, solution, result, variablesSetToOne);
readFromPlainTextSolution(model.getILP(), solutionReadable, result, variablesSetToOne);
readFromPlainTextSolution(ilp, solutionReadable, result, variablesSetToOne);
return result.getObjective();
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment