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

improve ilp solvers

parent f55b13fd
No related branches found
No related tags found
No related merge requests found
...@@ -83,31 +83,20 @@ public class ILPDirectSolver extends AbstractILPSolver { ...@@ -83,31 +83,20 @@ public class ILPDirectSolver extends AbstractILPSolver {
prob = GLPK.glp_create_prob(); prob = GLPK.glp_create_prob();
GLPK.glp_set_prob_name(prob, model.description()); GLPK.glp_set_prob_name(prob, model.description());
// only add variables not being ignored (which are remaining in the info object) // 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 // helper structure, map IlpVariable to its index
Map<IlpVariable, Integer> varToIndex = new HashMap<>(ilp.getNumIlpVariable()); Map<IlpVariable, Integer> varToIndex = new HashMap<>(ilp.getNumIlpVariable());
// create bounds // define variables and count the number of columns
int colCount = ilp.getNumIlpBound(); int colCount = 0;
final Set<IlpVariable> toIgnore = new HashSet<>(); for (IlpVariable variable : ilp.getIlpVariableList()) {
for (int index = 1; index <= ilp.getNumIlpBound(); index++) { if (!variable.getIllegal()) {
IlpBound bound = ilp.getIlpBound(index - 1); colCount++;
varToIndex.put(bound.getRef(), index); varToIndex.put(variable, colCount);
switch (bound.getType()) { GLPK.glp_set_col_kind(prob, colCount, GLPKConstants.GLP_BV);
case BINARY: GLPK.glp_set_col_name(prob, colCount, variable.getName());
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;
} }
GLPK.glp_set_col_name(prob, index, bound.getRef().getName());
} }
// create objective // create objective
...@@ -116,7 +105,7 @@ public class ILPDirectSolver extends AbstractILPSolver { ...@@ -116,7 +105,7 @@ public class ILPDirectSolver extends AbstractILPSolver {
GLPKConstants.GLP_MIN : GLPKConstants.GLP_MAX); 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? // 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()) { 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()); GLPK.glp_set_obj_coef(prob, varToIndex.get(term.getRef()), term.getValue());
} }
} }
...@@ -161,7 +150,7 @@ public class ILPDirectSolver extends AbstractILPSolver { ...@@ -161,7 +150,7 @@ public class ILPDirectSolver extends AbstractILPSolver {
int colIndex = 1; int colIndex = 1;
for (int termIndex = 0; termIndex < lhs.getNumIlpTerm(); termIndex++) { for (int termIndex = 0; termIndex < lhs.getNumIlpTerm(); termIndex++) {
IlpTerm term = lhs.getIlpTerm(termIndex); IlpTerm term = lhs.getIlpTerm(termIndex);
if (toIgnore.contains(term.getRef())) { if (term.getRef().getIllegal()) {
continue; continue;
} }
GLPK.intArray_setitem(ind, colIndex, varToIndex.get(term.getRef())); GLPK.intArray_setitem(ind, colIndex, varToIndex.get(term.getRef()));
...@@ -283,7 +272,7 @@ public class ILPDirectSolver extends AbstractILPSolver { ...@@ -283,7 +272,7 @@ public class ILPDirectSolver extends AbstractILPSolver {
double val = GLPK.glp_mip_col_val(prob, i); double val = GLPK.glp_mip_col_val(prob, i);
logger.trace("{} (at index {}) = {}", name, i, val); logger.trace("{} (at index {}) = {}", name, i, val);
if (val == 1) { 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 { ...@@ -144,11 +144,11 @@ public class ILPExternalSolver extends AbstractILPSolver {
ILPSolution result = new ILPSolution(model); ILPSolution result = new ILPSolution(model);
// readFromPrintableSolution(ilp, solution, result, variablesSetToOne); // readFromPrintableSolution(ilp, solution, result, variablesSetToOne);
readFromPlainTextSolution(model.getILP().getInfo(), solutionReadable, result, variablesSetToOne); readFromPlainTextSolution(model.getILP(), solutionReadable, result, variablesSetToOne);
return result.getObjective(); 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<IlpVariable> variablesSetToOne) throws SolvingException {
List<String> varNamesSetToOne = new ArrayList<>(); List<String> varNamesSetToOne = new ArrayList<>();
String name = null; String name = null;
...@@ -202,7 +202,7 @@ public class ILPExternalSolver extends AbstractILPSolver { ...@@ -202,7 +202,7 @@ public class ILPExternalSolver extends AbstractILPSolver {
throw new SolvingException("Could not parse solution file", e); throw new SolvingException("Could not parse solution file", e);
} }
for (String varName : varNamesSetToOne) { for (String varName : varNamesSetToOne) {
IlpVariable variable = info.vars.get(varName); IlpVariable variable = ilp.resolve(varName);
if (variable == null) { if (variable == null) {
throw new SolvingException("Could not find variable with name " + varName); 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