Variable names only for debug.

This commit is contained in:
chriseth 2022-06-29 11:19:10 +02:00
parent eafe0e0621
commit 82604f8269
2 changed files with 21 additions and 3 deletions

View File

@ -158,6 +158,7 @@ void LPSolver::addConstraint(Constraint const& _constraint, optional<size_t> _re
//cerr << "Added constraint:\n" << toString() << endl; //cerr << "Added constraint:\n" << toString() << endl;
} }
#ifdef DEBUG
void LPSolver::setVariableName(size_t _variable, string _name) void LPSolver::setVariableName(size_t _variable, string _name)
{ {
// TODO it might be constly to do this before we know hich variables relate to which // TODO it might be constly to do this before we know hich variables relate to which
@ -165,6 +166,12 @@ void LPSolver::setVariableName(size_t _variable, string _name)
SubProblem& p = unsealForVariable(_variable); SubProblem& p = unsealForVariable(_variable);
p.variables[p.varMapping.at(_variable)].name = move(_name); p.variables[p.varMapping.at(_variable)].name = move(_name);
} }
#else
void LPSolver::setVariableName(size_t _variable, string)
{
unsealForVariable(_variable);
}
#endif
void LPSolver::addLowerBound(size_t _variable, RationalWithDelta _bound, optional<size_t> _reason) void LPSolver::addLowerBound(size_t _variable, RationalWithDelta _bound, optional<size_t> _reason)
{ {
@ -224,6 +231,7 @@ string LPSolver::toString() const
map<string, rational> LPSolver::model() const map<string, rational> LPSolver::model() const
{ {
map<string, rational> result; map<string, rational> result;
#ifdef DEBUG
for (auto const& problem: m_subProblems) for (auto const& problem: m_subProblems)
if (problem) if (problem)
for (auto&& [outerIndex, innerIndex]: problem->varMapping) for (auto&& [outerIndex, innerIndex]: problem->varMapping)
@ -231,6 +239,7 @@ map<string, rational> LPSolver::model() const
result[problem->variables[innerIndex].name] = result[problem->variables[innerIndex].name] =
problem->variables[innerIndex].value.m_main + problem->variables[innerIndex].value.m_main +
problem->variables[innerIndex].value.m_delta / rational(100000); problem->variables[innerIndex].value.m_delta / rational(100000);
#endif
return result; return result;
} }
@ -481,6 +490,13 @@ LPResult LPSolver::SubProblem::check()
string LPSolver::SubProblem::toString() const string LPSolver::SubProblem::toString() const
{ {
auto varName = [&](size_t _i) {
#ifdef DEBUG
return variables[_i].name;
#else
return "x" + to_string(_i);
#endif
};
string resultString; string resultString;
for (auto&& [i, v]: variables | ranges::views::enumerate) for (auto&& [i, v]: variables | ranges::views::enumerate)
{ {
@ -488,7 +504,7 @@ string LPSolver::SubProblem::toString() const
resultString += v.bounds.lower->toString() + " <= "; resultString += v.bounds.lower->toString() + " <= ";
else else
resultString += " "; resultString += " ";
resultString += v.name; resultString += varName(i);
if (v.bounds.upper) if (v.bounds.upper)
resultString += " <= " + v.bounds.upper->toString(); resultString += " <= " + v.bounds.upper->toString();
else else
@ -505,13 +521,13 @@ string LPSolver::SubProblem::toString() const
{ {
solAssert(f == -1); solAssert(f == -1);
solAssert(basicVarPrefix.empty()); solAssert(basicVarPrefix.empty());
basicVarPrefix = variables[i].name + " = "; basicVarPrefix = varName(i) + " = ";
} }
else if (f != 0) else if (f != 0)
{ {
string joiner = f < 0 ? " - " : f > 0 && !rowString.empty() ? " + " : " "; string joiner = f < 0 ? " - " : f > 0 && !rowString.empty() ? " + " : " ";
string factor = f == 1 || f == -1 ? "" : ::toString(abs(f)) + " "; string factor = f == 1 || f == -1 ? "" : ::toString(abs(f)) + " ";
string var = variables[i].name; string var = varName(i);
rowString += joiner + factor + var; rowString += joiner + factor + var;
} }
} }

View File

@ -193,7 +193,9 @@ private:
}; };
struct Variable struct Variable
{ {
#ifdef DEBUG
std::string name = {}; std::string name = {};
#endif
RationalWithDelta value = {}; RationalWithDelta value = {};
Bounds bounds = {}; Bounds bounds = {};
}; };