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

improve ilp solvers

parent 44f69857
No related branches found
No related tags found
No related merge requests found
......@@ -78,10 +78,6 @@ public abstract class AbstractILPSolver implements BenchmarkableSolver {
return Solution.emptySolutionOf(model);
}
if (ilp.getNumIlpVariable() != ilp.getNumIlpBound()) {
logger.warn("Different variable ({}) and bound ({}) count", ilp.getNumIlpVariable(), ilp.getNumIlpBound());
}
// temporary update timeout to the remaining time.
// calling cleanup will reset it to the original value
this.timeoutValueOriginal = this.timeoutValue;
......
......@@ -94,31 +94,20 @@ public class ILPDirectSolver extends AbstractILPSolver {
prob = GLPK.glp_create_prob();
GLPK.glp_set_prob_name(prob, model.description());
// only add variables not being ignored (which are remaining in the info object)
GLPK.glp_add_cols(prob, ilp.getInfo().vars.size());
GLPK.glp_add_cols(prob, ilp.variableMap().size());
// helper structure, map IlpVariable to its index
Map<IlpVariable, Integer> varToIndex = new HashMap<>(ilp.getNumIlpVariable());
// create bounds
int colCount = ilp.getNumIlpBound();
final Set<IlpVariable> toIgnore = new HashSet<>();
for (int index = 1; index <= ilp.getNumIlpBound(); index++) {
IlpBound bound = ilp.getIlpBound(index - 1);
varToIndex.put(bound.getRef(), index);
switch (bound.getType()) {
case BINARY:
GLPK.glp_set_col_kind(prob, index, GLPKConstants.GLP_BV);
break;
case ZERO:
toIgnore.add(bound.getRef());
--colCount;
continue;
default: // >= 0
GLPK.glp_set_col_kind(prob, index, GLPKConstants.GLP_IV);
GLPK.glp_set_col_bnds(prob, index, GLPKConstants.GLP_LO, 0, 0);
break;
// define variables and count the number of columns
int colCount = 0;
for (IlpVariable variable : ilp.getIlpVariableList()) {
if (!variable.getIllegal()) {
colCount++;
varToIndex.put(variable, colCount);
GLPK.glp_set_col_kind(prob, colCount, GLPKConstants.GLP_BV);
GLPK.glp_set_col_name(prob, colCount, variable.getName());
}
GLPK.glp_set_col_name(prob, index, bound.getRef().getName());
}
// create objective
......@@ -127,7 +116,7 @@ public class ILPDirectSolver extends AbstractILPSolver {
GLPKConstants.GLP_MIN : GLPKConstants.GLP_MAX);
// TODO only variables mentioned in objective are set to a value. Do the others need to be set to zero?
for (IlpTerm term : ilp.getIlpObjective().getIlpLeftHandSide().getIlpTermList()) {
if (!toIgnore.contains(term.getRef())) {
if (!term.getRef().getIllegal()) {
GLPK.glp_set_obj_coef(prob, varToIndex.get(term.getRef()), term.getValue());
}
}
......@@ -172,7 +161,7 @@ public class ILPDirectSolver extends AbstractILPSolver {
int colIndex = 1;
for (int termIndex = 0; termIndex < lhs.getNumIlpTerm(); termIndex++) {
IlpTerm term = lhs.getIlpTerm(termIndex);
if (toIgnore.contains(term.getRef())) {
if (term.getRef().getIllegal()) {
continue;
}
GLPK.intArray_setitem(ind, colIndex, varToIndex.get(term.getRef()));
......@@ -302,7 +291,7 @@ public class ILPDirectSolver extends AbstractILPSolver {
double val = GLPK.glp_mip_col_val(prob, i);
logger.trace("{} (at index {}) = {}", name, i, val);
if (val == 1) {
variablesSetToOne.add(ilp.getInfo().vars.get(name));
variablesSetToOne.add(ilp.variableMap().get(name));
}
}
......
......@@ -131,11 +131,11 @@ public class ILPExternalSolver extends AbstractILPSolver {
ILPSolution result = new ILPSolution(model);
// readFromPrintableSolution(ilp, solution, result, variablesSetToOne);
readFromPlainTextSolution(model.getILP().getInfo(), solutionReadable, result, variablesSetToOne);
readFromPlainTextSolution(model.getILP(), solutionReadable, result, variablesSetToOne);
return result.getObjective();
}
private static void readFromPlainTextSolution(IlpVarInfo info, Path solution, ILPSolution result,
private static void readFromPlainTextSolution(ILP ilp, Path solution, ILPSolution result,
List<IlpVariable> variablesSetToOne) throws SolvingException {
List<String> varNamesSetToOne = new ArrayList<>();
String name = null;
......@@ -189,7 +189,7 @@ public class ILPExternalSolver extends AbstractILPSolver {
throw new SolvingException("Could not parse solution file", e);
}
for (String varName : varNamesSetToOne) {
IlpVariable variable = info.vars.get(varName);
IlpVariable variable = ilp.resolve(varName);
if (variable == null) {
throw new SolvingException("Could not find variable with name " + varName);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment