From d4d9309085eaa6ac903e6cf969b7af14937583b4 Mon Sep 17 00:00:00 2001 From: Johannes Mey <johannes.mey@tu-dresden.de> Date: Wed, 15 Aug 2018 09:25:20 +0200 Subject: [PATCH] improve ilp solvers --- .../mquat/solving/ilp/AbstractILPSolver.java | 4 --- .../st/mquat/solving/ilp/ILPDirectSolver.java | 35 +++++++------------ .../mquat/solving/ilp/ILPExternalSolver.java | 6 ++-- 3 files changed, 15 insertions(+), 30 deletions(-) diff --git a/jastadd-mquat-solver-ilp/src/main/java/de/tudresden/inf/st/mquat/solving/ilp/AbstractILPSolver.java b/jastadd-mquat-solver-ilp/src/main/java/de/tudresden/inf/st/mquat/solving/ilp/AbstractILPSolver.java index 0127a8d..da82350 100644 --- a/jastadd-mquat-solver-ilp/src/main/java/de/tudresden/inf/st/mquat/solving/ilp/AbstractILPSolver.java +++ b/jastadd-mquat-solver-ilp/src/main/java/de/tudresden/inf/st/mquat/solving/ilp/AbstractILPSolver.java @@ -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; diff --git a/jastadd-mquat-solver-ilp/src/main/java/de/tudresden/inf/st/mquat/solving/ilp/ILPDirectSolver.java b/jastadd-mquat-solver-ilp/src/main/java/de/tudresden/inf/st/mquat/solving/ilp/ILPDirectSolver.java index 4c3eaaa..a7fd082 100644 --- a/jastadd-mquat-solver-ilp/src/main/java/de/tudresden/inf/st/mquat/solving/ilp/ILPDirectSolver.java +++ b/jastadd-mquat-solver-ilp/src/main/java/de/tudresden/inf/st/mquat/solving/ilp/ILPDirectSolver.java @@ -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)); } } diff --git a/jastadd-mquat-solver-ilp/src/main/java/de/tudresden/inf/st/mquat/solving/ilp/ILPExternalSolver.java b/jastadd-mquat-solver-ilp/src/main/java/de/tudresden/inf/st/mquat/solving/ilp/ILPExternalSolver.java index 0338522..06e1b83 100644 --- a/jastadd-mquat-solver-ilp/src/main/java/de/tudresden/inf/st/mquat/solving/ilp/ILPExternalSolver.java +++ b/jastadd-mquat-solver-ilp/src/main/java/de/tudresden/inf/st/mquat/solving/ilp/ILPExternalSolver.java @@ -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); } -- GitLab