SMT-RAT  24.02
Toolbox for Strategic and Parallel Satisfiability-Modulo-Theories Solving
ConflictGraph.cpp
Go to the documentation of this file.
1 #include "ConflictGraph.h"
2 
3 namespace smtrat {
4 namespace cad {
5 
6 
7 void ConflictGraph::addSample(const Sample& sample) {
8  assert(sample.hasConflictWithConstraint());
9  std::size_t sid = mNextSID++;
10  const carl::Bitset& evalWith = sample.evaluatedWith();
11  for (std::size_t pos = evalWith.find_first(); pos != carl::Bitset::npos; pos = evalWith.find_next(pos)) {
12  if (!sample.evaluationResult().test(pos)) {
13  mData[pos].set(sid, true);
14  }
15  }
16 }
17 
19  assert(mData.size() > 0);
20  std::size_t maxID = 0;
21  std::size_t maxDegree = 0;
22  for (std::size_t id = 0; id < mData.size(); id++) {
23  std::size_t deg = mData[id].count();
24  if (deg > maxDegree) {
25  maxDegree = deg;
26  maxID = id;
27  }
28  }
29  return maxID;
30 }
31 
32 void ConflictGraph::selectConstraint(std::size_t id) {
33  assert(mData.size() > id);
34  carl::Bitset selection = mData[id];
35  for (auto& d: mData){
36  d -= selection;
37  }
38 }
39 
40 std::vector<std::size_t> ConflictGraph::selectEssentialConstraints(){
41  std::vector<std::size_t> res;
42  for (std::size_t sample = 0; sample < numSamples(); sample++){
43  std::size_t numConflicts = 0;
44  std::size_t essentialConstraint = 0;
45  for(std::size_t constraint = 0; constraint < mData.size(); constraint++){
46  if(mData[constraint].test(sample)) {
47  numConflicts++;
48  essentialConstraint = constraint;
49  }
50  }
51  if(numConflicts == 1){
52  selectConstraint(essentialConstraint);
53  res.push_back(essentialConstraint);
54  }
55  }
56  return res;
57 }
58 
59 /**
60  * Returns a new ConflictGraph whose adjacency matrix consists
61  * only of the unique columns of the adjacency matrix of this graph.
62  */
64  std::set<std::vector<uint8_t>> uniqueColumns;
65  for(std::size_t s = 0; s < numSamples(); s++){
66  std::vector<uint8_t> column (mData.size(), 0);
67  for(std::size_t constraint = 0; constraint < mData.size(); constraint++){
68  column[constraint] = mData[constraint].test(s);
69  }
70  uniqueColumns.insert(column);
71  }
72  ConflictGraph res(mData.size());
73  for(auto& b : res.mData){
74  b.resize(uniqueColumns.size());
75  }
76  std::size_t sid = 0;
77  for(auto column : uniqueColumns){
78  for(std::size_t row = 0; row < mData.size(); row++){
79  res.mData[row].set(sid, column[row]);
80  }
81  sid++;
82  }
83  return res;
84 }
85 
86 /**
87  * Checks if there are samples still uncovered.
88  */
90  for (const auto& d: mData) {
91  if (d.any()) return true;
92  }
93  return false;
94 }
95 
96 std::size_t ConflictGraph::numSamples() const{
97  std::size_t res = 0;
98  for(auto& c : mData){
99  res = std::max(res, c.size());
100  }
101  return res;
102 }
103 
105  std::size_t res = 0;
106  for(auto& c : mData){
107  if(c.any()) res++;
108  }
109  return res;
110 }
111 
112 carl::Bitset ConflictGraph::getData(size_t id){
113  return mData[id];
114 }
115 
116 std::vector<std::pair<std::size_t, carl::Bitset>> ConflictGraph::getRemainingConstraints(){
117  carl::Bitset mask(0);
118  std::vector<std::size_t> ids;
119  for (std::size_t id = 0; id < mData.size(); id++) {
120  if (mData[id].any()){
121  ids.push_back(id);
122  mask |= mData[id];
123  }
124  }
125  std::vector<std::pair<std::size_t, carl::Bitset>> res;
126  for(auto id : ids){
127  assert(mData[id].size() == mask.size());
128  res.push_back(std::pair<std::size_t, carl::Bitset>(id, mData[id] | ~mask));
129  }
130  return res;
131 }
132 
133 std::ostream& operator<<(std::ostream& os, const ConflictGraph& cg) {
134  os << "Print CG with " << cg.mData.size() << " constraints" << std::endl;
135  std::size_t numSamples = 0;
136  for (auto c : cg.mData){
137  numSamples = std::max(numSamples, c.size());
138  }
139  for (std::size_t i = 0; i < cg.mData.size(); i++) {
140  if(cg.mData[i].any()){
141  os << i << "\t" <<
142  std::string(numSamples-cg.mData[i].size(), '0') << cg.mData[i] <<
143  " : " << cg.mData[i].count() << std::endl;
144  }
145  }
146  return os;
147 }
148 
149 }
150 }
Representation of a bipartite graph (C+S, E) of vertices C and S, representing the constraints and sa...
Definition: ConflictGraph.h:23
ConflictGraph removeDuplicateColumns()
Returns a new ConflictGraph whose adjacency matrix consists only of the unique columns of the adjacen...
std::vector< std::pair< std::size_t, carl::Bitset > > getRemainingConstraints()
std::vector< size_t > selectEssentialConstraints()
Selects all essential constraints and returns their indices.
std::size_t numSamples() const
std::vector< carl::Bitset > mData
Stores for each constraints, which sample points violate the constraint.
Definition: ConflictGraph.h:28
void addSample(const Sample &sample)
bool hasRemainingSamples() const
Checks if there are samples still uncovered.
carl::Bitset getData(std::size_t id)
void selectConstraint(std::size_t id)
Removes the given constraint and disable all sample points covered by this constraint.
std::size_t numRemainingConstraints() const
std::size_t getMaxDegreeConstraint() const
Retrieves the constraint that covers the most samples.
const carl::Bitset & evaluatedWith() const
Definition: Sample.h:53
const carl::Bitset & evaluationResult() const
Definition: Sample.h:59
bool hasConflictWithConstraint() const
Definition: Sample.h:68
std::ostream & operator<<(std::ostream &os, const SampleIteratorQueue< I, C > &siq)
Class to create the formulas for axioms.