SMT-RAT  24.02
Toolbox for Strategic and Parallel Satisfiability-Modulo-Theories Solving
Sampling.h
Go to the documentation of this file.
1 /**
2  * @file Sampling.h
3  * @author Philip Kroll <Philip.Kroll@rwth-aachen.de>
4  * Created on 2022-03-16.
5  */
6 #pragma once
7 
11 #include "Helper.h"
12 #include "NewCoveringStatistics.h"
14 
15 namespace smtrat {
16 
18  LOWER_UPPER_BETWEEN_SAMPLING // First checks lower bound and then upper bound, then checks between the cells if they are covered.
19 };
20 
22  DEFAULT // Check if its outside of each cell individually
23 };
24 
25 inline std::string get_name(SamplingAlgorithm samplingAlgorithm) {
26  switch (samplingAlgorithm) {
28  return "LOWER_UPPER_BETWEEN_SAMPLING";
29  default:
30  return "UNKNOWN";
31  }
32 }
33 
34 inline std::string get_name(IsSampleOutsideAlgorithm samplingAlgorithm) {
35  switch (samplingAlgorithm) {
37  return "DEFAULT";
38  default:
39  return "UNKNOWN";
40  }
41 }
42 
43 /*
44  * @brief Sampling algorithm checking if a the given set of derivations ref is fully covering
45  * @param derivations Vector of sampled derivations, assumed as being sorted by lower bounds
46  * @param sample, RAN which is set if the vector of derivations is not covering
47  * @note redundancies of the first kind are not removed
48  * @return 0 if the vector of derivations is covering, 1 if the vector of derivations is not covering
49  */
50 template<SamplingAlgorithm S>
51 struct sampling {
52  template<typename T>
54 };
55 
56 /*
57  * @brief Algorithm to check if the given sample is outside of the given derivations/cells
58  * @param sample, RAN which is to be checked
59  * @param Set of derivations sorted by lower bound
60  * @Note In the given set of derivations, the redundancies of the first kind are not removed
61  * @return True if the sample is outside of the given set of derivations, false otherwise
62  */
63 template<IsSampleOutsideAlgorithm S>
65  template<typename T>
67 };
68 
69 template<>
71  template<typename T>
73 
74  // remove redundancies of the first kind
75  // the derivations are already sorted by lower bound
76  std::vector<cadcells::datastructures::SampledDerivationRef<T>> derivationsVector;
77  auto iter = derivationsSet.begin();
78  while (iter != derivationsSet.end()) {
79  derivationsVector.push_back(*iter);
80  auto& last_cell = (*iter)->cell();
81  iter++;
82  while (iter != derivationsSet.end() && !cadcells::datastructures::upper_lt_upper(last_cell, (*iter)->cell()))
83  iter++;
84  }
85 
86  if (derivationsVector.empty()) {
87  // There are no cells, just take trivially 0
88  sample = cadcells::RAN(0);
89  return 0;
90  }
91 
92  if (!derivationsVector.front()->cell().lower_unbounded()) {
93  // Lower bound is finite, just take a sufficiently large negative number
94  sample = carl::sample_below(derivationsVector.front()->cell().lower()->first);
95  return 0;
96  }
97 
98  if (!derivationsVector.back()->cell().upper_unbounded()) {
99  // Upper bound is finite, just take a sufficiently large positive number
100  sample = carl::sample_above(derivationsVector.back()->cell().upper()->first);
101  return 0;
102  }
103 
104  // Search for adjacent disjoint cells and sample between
105  for (size_t i = 0; i + 1 < derivationsVector.size(); i++) {
106  // We know that the cells are ordered by lower bound - so for checking disjointness the following suffices
107  if (!derivationsVector[i]->cell().upper_unbounded() && !derivationsVector[i + 1]->cell().lower_unbounded() && cadcells::datastructures::upper_lt_lower(derivationsVector[i]->cell(), derivationsVector[i + 1]->cell())) {
108  if (derivationsVector[i]->cell().upper()->first == derivationsVector[i + 1]->cell().lower()->first) {
109  sample = derivationsVector[i]->cell().upper()->first;
110  return 0;
111  } else {
112  sample = carl::sample_between(derivationsVector[i]->cell().upper()->first, derivationsVector[i + 1]->cell().lower()->first);
113  return 0;
114  }
115  }
116  }
117 
118  // The cells cover the number line -> There is no sample to be found
119  return 1;
120  }
121 };
122 
123 template<>
125  template<typename T>
127 
128  SMTRAT_LOG_DEBUG("smtrat.covering", "Checking if " << sample << " is outside of " << derivations);
129 
130  if (derivations.empty()) {
131  SMTRAT_LOG_DEBUG("smtrat.covering", "No cells, so " << sample << " is outside");
132  return true;
133  }
134 
135  for (const auto& deriv : derivations) {
136  auto& cell = deriv->cell();
137  SMTRAT_LOG_DEBUG("smtrat.covering", "Checking if " << sample << " is outside of " << cell);
138  SMTRAT_LOG_DEBUG("smtrat.covering", " Lower unbounded " << cell.lower_unbounded() << " Upper unbounded " << cell.upper_unbounded());
139 
140  // When one bound is infty, we must have a sector
141  if (cell.lower_unbounded() && cell.upper_unbounded()) {
142  // (-infty, infty)
143  return false;
144  } else if (!cell.lower_unbounded() && cell.upper_unbounded()) {
145  // (a, infty)
146  if (sample > cell.lower()->first) {
147  return false;
148  }
149  } else if (cell.lower_unbounded() && !cell.upper_unbounded()) {
150  // (-infty, a)
151  if (sample < cell.upper()->first) {
152  return false;
153  }
154  } else {
155  // both cells bounds are not infty
156  if (cell.is_section()) {
157  // [a,b]
158  // Non-strict bounds
159  if (cell.lower()->first <= sample && sample <= cell.upper()->first) {
160  return false;
161  }
162  } else {
163  // Strict bounds
164  // (a,b)
165  if (cell.lower()->first < sample && sample < cell.upper()->first) {
166  return false;
167  }
168  }
169  }
170  }
171  return true;
172  }
173 };
174 
175 }; // namespace smtrat
bool upper_lt_upper(const DelineationInterval &del1, const DelineationInterval &del2)
Compares the upper bounds of two DelineationIntervals.
Definition: delineation.h:286
bool upper_lt_lower(const DelineationInterval &del1, const DelineationInterval &del2)
Compares an upper bound with a lower bound of DelineationIntervals.
Definition: delineation.h:296
std::shared_ptr< SampledDerivation< Properties > > SampledDerivationRef
Definition: derivation.h:25
Polynomial::RootType RAN
Definition: common.h:24
Class to create the formulas for axioms.
IsSampleOutsideAlgorithm
Definition: Sampling.h:21
@ DEFAULT
Definition: Sampling.h:22
std::string get_name(SamplingAlgorithm samplingAlgorithm)
Definition: Sampling.h:25
SamplingAlgorithm
Definition: Sampling.h:17
@ LOWER_UPPER_BETWEEN_SAMPLING
Definition: Sampling.h:18
#define SMTRAT_LOG_DEBUG(channel, msg)
Definition: logging.h:35
static bool is_outside(const cadcells::RAN &sample, const std::set< cadcells::datastructures::SampledDerivationRef< T >, SampledDerivationRefCompare > &derivations)
Definition: Sampling.h:126
static bool is_outside(const cadcells::RAN &sample, const std::set< cadcells::datastructures::SampledDerivationRef< T >, SampledDerivationRefCompare > &derivations)
static size_t sample_outside(cadcells::RAN &sample, const std::set< cadcells::datastructures::SampledDerivationRef< T >, SampledDerivationRefCompare > &derivationsSet)
Definition: Sampling.h:72
static size_t sample_outside(cadcells::RAN &sample, const std::set< cadcells::datastructures::SampledDerivationRef< T >, SampledDerivationRefCompare > &derivations)