SMT-RAT  24.02
Toolbox for Strategic and Parallel Satisfiability-Modulo-Theories Solving
triangular_ordering.cpp
Go to the documentation of this file.
1 #include "triangular_ordering.h"
2 
3 #include <algorithm>
4 #include <numeric>
5 
7 
8 template<typename T>
9 class VariableMap {
10 private:
11  std::vector<T> mData;
12 public:
13  T operator[](carl::Variable v) const {
14  assert(v != carl::Variable::NO_VARIABLE);
15  if (mData.size() <= v.id() - 1) return 0;
16  return mData[v.id() - 1];
17  }
18  T& operator[](carl::Variable v) {
19  assert(v != carl::Variable::NO_VARIABLE);
20  if (mData.size() <= v.id() - 1) mData.resize(v.id());
21  return mData[v.id() - 1];
22  }
23 };
24 
29 
30  bool operator()(carl::Variable lhs, carl::Variable rhs) const {
31  SMTRAT_LOG_TRACE("smtrat.cad.variableordering", "Comparing maxdeg " << lhs << " < " << rhs << "? " << max_deg[lhs] << " > " << max_deg[rhs]);
32  if (max_deg[lhs] != max_deg[rhs]) return max_deg[lhs] < max_deg[rhs];
33  SMTRAT_LOG_TRACE("smtrat.cad.variableordering", "Comparing maxtdeg " << lhs << " < " << rhs << "? " << max_tdeg[lhs] << " > " << max_tdeg[rhs]);
34  if (max_tdeg[lhs] != max_tdeg[rhs]) return max_tdeg[lhs] < max_tdeg[rhs];
35  SMTRAT_LOG_TRACE("smtrat.cad.variableordering", "Comparing degsum " << lhs << " < " << rhs << "? " << sum_deg[lhs] << " > " << sum_deg[rhs]);
36  if (sum_deg[lhs] != sum_deg[rhs]) return sum_deg[lhs] < sum_deg[rhs];
37  return false;
38  }
39 };
40 
41 std::vector<carl::Variable> triangular_ordering(const std::vector<Poly>& polys) {
42  SMTRAT_LOG_DEBUG("smtrat.cad.variableordering", "Building order based on " << polys);
43  carl::carlVariables vars;
44  triangular_data data;
45  std::vector<VariableMap<std::size_t>> maxdeg;
46  maxdeg.resize(polys.size());
47  for (std::size_t i = 0; i < polys.size(); ++i) {
48  carl::variables(polys[i], vars);
49  for (auto var: carl::variables(polys[i])) {
50  maxdeg[i][var] = polys[i].degree(var);
51  data.max_deg[var] = std::max(data.max_deg[var], maxdeg[i][var]);
52  data.max_tdeg[var] = std::max(data.max_tdeg[var], polys[i].lcoeff(var).total_degree());
53  }
54  }
55  for (auto var: vars) {
56  data.sum_deg[var] = std::accumulate(maxdeg.begin(), maxdeg.end(), 0ul, [var](std::size_t i, const auto& m) { return i + m[var]; });
57  }
58  SMTRAT_LOG_DEBUG("smtrat.cad.variableordering", "Collected variables: " << vars);
59  std::vector<carl::Variable> res(std::move(vars.as_vector()));
60  std::sort(res.begin(), res.end(), data);
61  SMTRAT_LOG_DEBUG("smtrat.cad.variableordering", "Sorted: " << res);
62  return res;
63 }
64 
65 }
void sort(T *array, int size, LessThan lt)
Definition: Sort.h:67
int var(Lit p)
Definition: SolverTypes.h:64
std::vector< carl::Variable > triangular_ordering(const std::vector< Poly > &polys)
std::vector< carl::Variable > vars(const std::pair< QuantifierType, std::vector< carl::Variable >> &p)
Definition: QEQuery.h:43
#define SMTRAT_LOG_TRACE(channel, msg)
Definition: logging.h:36
#define SMTRAT_LOG_DEBUG(channel, msg)
Definition: logging.h:35
bool operator()(carl::Variable lhs, carl::Variable rhs) const