Commit 00012d11 authored by Johannes Mey's avatar Johannes Mey
Browse files

add CPLEX solver

parent 1b48965a
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 org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.*;
import java.nio.file.Path;
import java.util.List;
public class CPLEXSolver extends ILPExternalSolver {
private static final Logger logger = LogManager.getLogger(CPLEXSolver.class);
public CPLEXSolver(ILPTransformer transformer) {
super(transformer);
}
@Override
protected String[] getCommand(Path lp, Path solution, long remainingTimeForSolvingInMillis) {
String[] command = {
"cplex",
"-c",
"read " + lp.toAbsolutePath() +"",
"set clocktype 2",
" set timelimit " + remainingTimeForSolvingInMillis/1000,
"optimize",
"write " + solution.toAbsolutePath()};
return command;
}
@Override
protected void readFromPlainTextSolution(ILP ilp, Path solution, ILPSolution result,
List<IlpVariable> variablesSetToOne) throws SolvingException {
logger.debug("reading solution from {}.", solution);
try {
File file = solution.toFile();
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.parse(file);
// iterate over all variables
NodeList variables = document.getDocumentElement().getElementsByTagName("variable");
logger.debug(variables.getLength());
for (int nodeIndex = 0; nodeIndex < variables.getLength(); nodeIndex++) {
Element variable = (Element)variables.item(nodeIndex);
String name = variable.getAttribute("name");
int value = Integer.valueOf(variable.getAttribute("value"));
logger.info("found variable {} with value {}", name, value);
if (value == 1) {
IlpVariable ilpVariable = ilp.resolve(name);
variablesSetToOne.add(ilpVariable);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public String getName() {
return "ilp-cplex";
}
}
......@@ -79,6 +79,7 @@ public abstract class ILPExternalSolver extends AbstractILPSolver {
lp = Files.createTempFile("ilp", ".lp");
// solution = Files.createTempFile("solution", null);
solutionReadable = Files.createTempFile("sol-read", ".sol");
Files.delete(solutionReadable);
} catch (IOException e) { throw new SolvingException("Can not create lp or solution file", e); }
if (!deleteFilesOnExit) {
logger.info("Writing ILP to {}, solving now", lp.toAbsolutePath());
......
package de.tudresden.inf.st.mquat.solving;
import de.tudresden.inf.st.mquat.solving.ilp.AbstractILPTransformer;
import de.tudresden.inf.st.mquat.solving.ilp.CPLEXSolver;
import de.tudresden.inf.st.mquat.solving.ilp.GurobiSolver;
public class AbstractCPLEXHandwrittenTest extends AbstractHandwrittenTestSuite {
@Override
protected Solver getSolver() {
// set to false for debugging
return new CPLEXSolver(new AbstractILPTransformer()).setDeleteFilesOnExit(false);
}
}
package de.tudresden.inf.st.mquat.solving;
import de.tudresden.inf.st.mquat.solving.ilp.CPLEXSolver;
import de.tudresden.inf.st.mquat.solving.ilp.ConcreteILPTransformer;
public class CPLEXHandwrittenTest extends OptimalHandwrittenTestSuite {
@Override
protected Solver getSolver() {
// set to false for debugging
return new CPLEXSolver(new ConcreteILPTransformer()).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