SMT-RAT  24.02
Toolbox for Strategic and Parallel Satisfiability-Modulo-Theories Solving
greedy_max_univariate.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "helper.h"
4 
5 #include <carl-arith/core/Variable.h>
6 #include <carl-common/datastructures/Bitset.h>
8 
9 #include <algorithm>
10 #include <map>
11 #include <vector>
12 
13 namespace smtrat {
14 namespace mcsat {
15 namespace variableordering {
16 
17 namespace detail {
18 inline carl::Bitset variablesOf(const ConstraintT& c, VariableIDs& vids) {
19  carl::Bitset res;
20  for (auto v: c.variables()) {
21  res.set(vids[v]);
22  }
23  return res;
24 }
25 
26 inline long countUnivariates(const std::vector<carl::Bitset>& constraints, std::size_t id) {
27  return std::count(constraints.begin(), constraints.end(), carl::Bitset({id}));
28 }
29 inline bool stillOccurs(const std::vector<carl::Bitset>& constraints, std::size_t id) {
30  return std::find_if(constraints.begin(), constraints.end(), [id](const auto& b){ return b.test(id); }) != constraints.end();
31 }
32 inline carl::Variable findMax(const std::vector<carl::Bitset>& constraints, const VariableIDs& vids) {
33  carl::Variable maxVar = carl::Variable::NO_VARIABLE;
34  carl::Variable possible = carl::Variable::NO_VARIABLE;
35  long maxCnt = 0;
36  for (const auto& v: vids.mIDs) {
37  auto cnt = detail::countUnivariates(constraints, v.second);
38  if (cnt > maxCnt) {
39  maxVar = v.first;
40  maxCnt = cnt;
41  }
42  if (possible == carl::Variable::NO_VARIABLE) {
43  if (stillOccurs(constraints, v.second)) {
44  possible = v.first;
45  }
46  }
47  }
48  if (maxVar != carl::Variable::NO_VARIABLE) return maxVar;
49  return possible;
50 }
51 
52 inline void purgeVariable(std::vector<carl::Bitset>& constraints, carl::Variable v, const VariableIDs& vids) {
53  std::size_t id = vids[v];
54  for (auto it = constraints.begin(); it != constraints.end(); ) {
55  auto& bitset = *it;
56  bitset.reset(id);
57  if (bitset.none()) {
58  it = constraints.erase(it);
59  } else {
60  ++it;
61  }
62  }
63 }
64 
65 }
66 
67 template<typename Constraints>
68 std::vector<carl::Variable> greedy_max_univariate(const Constraints& c) {
69  VariableIDs vids;
70  std::vector<carl::Bitset> constraints;
71  for (const auto& cons: c) {
72  constraints.emplace_back(detail::variablesOf(cons, vids));
73  SMTRAT_LOG_DEBUG("smtrat.mcsat", "Constraint " << cons << " -> " << constraints.back());
74  }
75  SMTRAT_LOG_DEBUG("smtrat.mcsat", "Collected variables " << vids);
76 
77  std::vector<carl::Variable> res;
78  while (!constraints.empty()) {
79  res.emplace_back(detail::findMax(constraints, vids));
80  detail::purgeVariable(constraints, res.back(), vids);
81  }
82  SMTRAT_LOG_DEBUG("smtrat.mcsat", "Calculated variable ordering " << res);
83  return res;
84 
85 }
86 
87 }
88 }
89 }
bool stillOccurs(const std::vector< carl::Bitset > &constraints, std::size_t id)
long countUnivariates(const std::vector< carl::Bitset > &constraints, std::size_t id)
void purgeVariable(std::vector< carl::Bitset > &constraints, carl::Variable v, const VariableIDs &vids)
carl::Bitset variablesOf(const ConstraintT &c, VariableIDs &vids)
carl::Variable findMax(const std::vector< carl::Bitset > &constraints, const VariableIDs &vids)
std::vector< carl::Variable > greedy_max_univariate(const Constraints &c)
Class to create the formulas for axioms.
carl::Constraint< Poly > ConstraintT
Definition: types.h:29
#define SMTRAT_LOG_DEBUG(channel, msg)
Definition: logging.h:35
std::map< carl::Variable, std::size_t > mIDs
Definition: helper.h:13