From dec67df8d8e1446e6dfa94e2418889f18da5c3b9 Mon Sep 17 00:00:00 2001 From: chriseth Date: Thu, 3 Feb 2022 11:41:55 +0100 Subject: [PATCH] Refactor. --- libsolutil/LP.cpp | 17 +++++++++-------- libsolutil/LinearExpression.h | 6 +++++- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/libsolutil/LP.cpp b/libsolutil/LP.cpp index ef9da97b0..a1d6ae8a2 100644 --- a/libsolutil/LP.cpp +++ b/libsolutil/LP.cpp @@ -394,6 +394,8 @@ void removeColumns(SolvingState& _state, vector const& _columnsToRemove) eraseIndices(_state.variableNames, _columnsToRemove); } +// TODO move this into a simplifier class + /// Turn constraints of the form ax <= b into an upper bound on x. /// @returns false if the system is infeasible. bool extractDirectConstraints(SolvingState& _state, bool& _changed) @@ -416,8 +418,8 @@ bool extractDirectConstraints(SolvingState& _state, bool& _changed) { // 0 <= b or 0 = b if ( - constraint.data.factors.front() < 0 || - (constraint.equality && constraint.data.factors.front() != 0) + constraint.data.front().numerator() < 0 || + (constraint.equality && constraint.data.front()) ) return false; // Infeasible. } @@ -468,7 +470,7 @@ bool removeFixedVariables(SolvingState& _state, map& _model, b // substitute variable for (Constraint& constraint: _state.constraints) - if (constraint.data.factors.at(index) != 0) + if (constraint.data[index]) { constraint.data[0] -= constraint.data[index] * lower; constraint.data[index] = 0; @@ -601,7 +603,7 @@ struct ProblemSplitter Constraint splitRow{{}, state.constraints[i].equality}; for (size_t j = 0; j < state.constraints[i].data.size(); j++) if (j == 0 || includedColumns[j]) - splitRow.data.factors.push_back(state.constraints[i].data[j]); + splitRow.data.push_back(state.constraints[i].data[j]); splitOff.constraints.push_back(move(splitRow)); } @@ -730,7 +732,7 @@ string SolvingState::toString() const result += joinHumanReadable(line, " + ") + (constraint.equality ? " = " : " <= ") + - ::toString(constraint.data.factors.front()) + + ::toString(constraint.data.front()) + "\n"; } result += "Bounds:\n"; @@ -779,9 +781,8 @@ pair> LPSolver::check(SolvingState _state) else { LinearExpression objectives; - objectives.factors = - vector(1, rational(bigint(0))) + - vector(split.constraints.front().data.size() - 1, rational(bigint(1))); + objectives.resize(1); + objectives.resize(split.constraints.front().data.size(), rational(bigint(1))); tie(lpResult, solution) = simplex(split.constraints, move(objectives)); } m_cache.emplace(move(orig), make_pair(lpResult, solution)); diff --git a/libsolutil/LinearExpression.h b/libsolutil/LinearExpression.h index 57a245ee3..add117f1a 100644 --- a/libsolutil/LinearExpression.h +++ b/libsolutil/LinearExpression.h @@ -41,8 +41,9 @@ using rational = boost::rational; * factors[0] + factors[1] * X1 + factors[2] * X2 + ... * The set and order of variables is implied. */ -struct LinearExpression +class LinearExpression { +public: /// Creates the expression "_factor * X_index" static LinearExpression factorForVariable(size_t _index, rational _factor) { @@ -74,6 +75,8 @@ struct LinearExpression auto begin() const { return factors.begin(); } auto end() const { return factors.end(); } + rational const& front() const { return factors.front(); } + void push_back(rational _value) { factors.push_back(move(_value)); } size_t size() const { return factors.size(); } @@ -169,6 +172,7 @@ struct LinearExpression return _x; } +private: std::vector factors; };