Commit df10f8bc authored by Johannes Mey's avatar Johannes Mey
Browse files

add gurobi solver (untested so far)

parent bffe7371
...@@ -2,6 +2,7 @@ package de.tudresden.inf.st.mquat.benchmark; ...@@ -2,6 +2,7 @@ package de.tudresden.inf.st.mquat.benchmark;
import de.tudresden.inf.st.mquat.solving.BenchmarkableSolver; import de.tudresden.inf.st.mquat.solving.BenchmarkableSolver;
import de.tudresden.inf.st.mquat.solving.ilp.GLPKSolver; import de.tudresden.inf.st.mquat.solving.ilp.GLPKSolver;
import de.tudresden.inf.st.mquat.solving.ilp.GurobiSolver;
import de.tudresden.inf.st.mquat.solving.ilp.ILPDirectSolver; import de.tudresden.inf.st.mquat.solving.ilp.ILPDirectSolver;
import de.tudresden.inf.st.mquat.solving.genetic.GeneticSolver; import de.tudresden.inf.st.mquat.solving.genetic.GeneticSolver;
import de.tudresden.inf.st.mquat.solving.ilp.SCIPSolver; import de.tudresden.inf.st.mquat.solving.ilp.SCIPSolver;
...@@ -31,6 +32,7 @@ public class SolverFactory { ...@@ -31,6 +32,7 @@ public class SolverFactory {
new EMFeRSolver(), new EMFeRSolver(),
new GLPKSolver(), new GLPKSolver(),
new SCIPSolver(), new SCIPSolver(),
new GurobiSolver(),
new ILPDirectSolver(), new ILPDirectSolver(),
new SimpleSolver(), new SimpleSolver(),
new RandomSolver(0, 0), new RandomSolver(0, 0),
......
package de.tudresden.inf.st.mquat.solving.ilp;
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.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class GurobiSolver extends ILPExternalSolver {
/**
* Create a new GLPK solver with default settings.
* Default is:
* <ul>
* <li>1 minute timeout</li>
* <li>delete temporary files on exit.</li>
* </ul>
* @see GurobiSolver#setDeleteFilesOnExit(boolean)
*/
public GurobiSolver() {
}
@Override
protected String[] getCommand(Path lp, Path solution, long remainingTimeForSolvingInMillis) {
String[] command = {"gurobi_cl", " ResultFile=" + solution.toAbsolutePath(), "TimeLimit=" + remainingTimeForSolvingInMillis/1000, String.valueOf(lp.toAbsolutePath())};
return command;
}
@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())) {
if (line.startsWith("#")) {
// comment line
if (line.startsWith("# Objective Value =")) {
result.setObjective(Double.valueOf(line.substring(20).trim()));
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 (variable == null) {
throw new SolvingException("Could not find variable with name " + tokens[0]);
}
variablesSetToOne.add(variable);
}
}
}
}
} catch (IOException e) {
throw new SolvingException("Could not open solution file", e);
} catch (NumberFormatException e) {
throw new SolvingException("Could not parse solution file", e);
}
}
@Override
public String getName() {
return "ilp-gurobi";
}
}
...@@ -75,7 +75,7 @@ public abstract class ILPExternalSolver extends AbstractILPSolver { ...@@ -75,7 +75,7 @@ public abstract class ILPExternalSolver extends AbstractILPSolver {
try { try {
lp = Files.createTempFile("ilp", ".lp"); lp = Files.createTempFile("ilp", ".lp");
// solution = Files.createTempFile("solution", null); // solution = Files.createTempFile("solution", null);
solutionReadable = Files.createTempFile("sol-read", null); solutionReadable = Files.createTempFile("sol-read", ".sol");
} catch (IOException e) { throw new SolvingException("Can not create lp or solution file", e); } } catch (IOException e) { throw new SolvingException("Can not create lp or solution file", e); }
if (!deleteFilesOnExit) { if (!deleteFilesOnExit) {
logger.info("Writing ILP to {}, solving now", lp.toAbsolutePath()); logger.info("Writing ILP to {}, solving now", lp.toAbsolutePath());
......
package de.tudresden.inf.st.mquat.solving;
import de.tudresden.inf.st.mquat.solving.ilp.GurobiSolver;
public class GurobiHandwrittenTest extends HandwrittenTestSuite {
@Override
protected Solver getSolver() {
// set to false for debugging
return new GurobiSolver().setDeleteFilesOnExit(false);
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment