Commit 37d57570 authored by Johannes Mey's avatar Johannes Mey
Browse files

improve ilp solvers

parent f55b13fd
......@@ -83,31 +83,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
......@@ -116,7 +105,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());
}
}
......@@ -161,7 +150,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()));
......@@ -283,7 +272,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));
}
}
......
......@@ -144,11 +144,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;
......@@ -202,7 +202,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);
}
......
Markdown is supported
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