SMT-RAT  24.02
Toolbox for Strategic and Parallel Satisfiability-Modulo-Theories Solving
BaseProjection.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <functional>
4 #include <vector>
5 
6 #include <carl-arith/poly/umvpoly/functions/IntervalEvaluation.h>
7 
8 #include "../common.h"
9 #include "../utils/CADConstraints.h"
10 
11 #include "PolynomialLiftingQueue.h"
12 #include "../projectionoperator/ProjectionOperator.h"
13 #include "ProjectionInformation.h"
14 #include "Projection_utils.h"
15 
16 namespace smtrat {
17 namespace cad {
18 
19  template<typename Settings>
21  protected:
23  private:
24  /// Lift of id pools to generate fresh IDs for polynomials.
25  std::vector<carl::IDPool> mIDPools;
26 
27  protected:
29  /// List of lifting queues that can be used for incremental projection.
30  std::vector<PolynomialLiftingQueue<BaseProjection>> mLiftingQueues;
31  /// Additional info on projection, projection levels and projection polynomials.
33  /// The projection operator.
35  /// Callback to be called when polynomials are removed. The arguments are the projection level and a bitset that indicate which polynomials were removed in this level.
36  std::function<void(std::size_t, const SampleLiftedWith&)> mRemoveCallback;
37 
39 
40  void callRemoveCallback(std::size_t level, const SampleLiftedWith& slw) const {
41  if (mRemoveCallback) mRemoveCallback(level, slw);
42  }
43  /// Returns a fresh polynomial id for the given level.
44  std::size_t getID(std::size_t level) {
45  assert(level <= dim());
46  return mIDPools[level].get();
47  }
48  /// Frees a currently used polynomial id for the given level.
49  void freeID(std::size_t level, std::size_t id) {
50  assert(level <= dim());
51  mIDPools[level].free(id);
52  }
53  /// Returns the variable that corresponds to the given level, that is the variable eliminated in this level.
54  carl::Variable var(std::size_t level) const {
55  assert(level > 0 && level <= dim());
56  return vars()[level - 1];
57  }
58  /// Checks whether a polynomial can safely be ignored due to the bounds.
59  bool canBePurgedByBounds(const UPoly& p) const {
60  if (Settings::simplifyProjectionByBounds) {
61  carl::Interval<Rational> res;
62  const auto& map = mConstraints.bounds().getEvalIntervalMap();
63  if (map.count(p.main_var()) > 0) {
64  res = carl::evaluate(p, map);
65  } else {
66  res = carl::evaluate(Poly(p), map);
67  }
68  SMTRAT_LOG_DEBUG("smtrat.cad.projection", "Bounds:" << std::endl << mConstraints.bounds().getEvalIntervalMap());
69  SMTRAT_LOG_DEBUG("smtrat.cad.projection", "Checking polynomial " << p << " against bounds, results in " << res);
70  if (res.is_positive() || res.is_negative()) return true;
71  SMTRAT_LOG_DEBUG("smtrat.cad.projection", "No.");
72  }
73  return false;
74  }
75 
76  bool isPurged(std::size_t level, std::size_t id) {
77  if (!mInfo(level).isEvaluated(id)) {
78  bool cbp = canBePurgedByBounds(this->getPolynomialById(level, id));
79  if (cbp) {
80  mInfo(level).setPurged(id, true);
81  }
82  mInfo(level).setEvaluated(id, true);
83  }
84  return mInfo(level).isPurged(id);
85  }
86  public:
87  /// Returns the dimension of the projection.
88  std::size_t dim() const {
89  assert(vars().size() + 1 == mIDPools.size());
90  assert(vars().size() == mLiftingQueues.size());
91  return vars().size();
92  }
93  /// Returns the variables used for projection.
94  const auto& vars() const {
95  return mConstraints.vars();
96  }
97  /// Resets all datastructures, use the given variables from now on.
98  void reset() {
99  mIDPools = std::vector<carl::IDPool>(vars().size() + 1);
100  mLiftingQueues.clear();
101  for (std::size_t i = 1; i <= vars().size(); i++) {
102  mLiftingQueues.emplace_back(this, i);
103  }
104  }
105  /// Sets a callback that is called whenever polynomials are removed.
106  template<typename F>
107  void setRemoveCallback(F&& f) {
108  mRemoveCallback = f;
109  }
110  /// Adds the given polynomial to the projection. Converts to a UPoly and calls the appropriate overload.
111  carl::Bitset addPolynomial(const Poly& p, std::size_t cid, bool isBound) {
112  return addPolynomial(carl::to_univariate_polynomial(p, var(0)), cid, isBound);
113  }
114  /// Adds the given polynomial to the projection.
115  virtual carl::Bitset addPolynomial(const UPoly& p, std::size_t cid, bool isBound) = 0;
116  /// Adds the given polynomial of an equational constraint to the projection. Converts to a UPoly and calls the appropriate overload.
117  carl::Bitset addEqConstraint(const Poly& p, std::size_t cid, bool isBound) {
118  return addEqConstraint(carl::to_univariate_polynomial(p, var(0)), cid, isBound);
119  }
120  /// Adds the given polynomial of an equational constraint to the projection.
121  virtual carl::Bitset addEqConstraint(const UPoly& p, std::size_t cid, bool isBound) {
122  return addPolynomial(p, cid, isBound);
123  }
124  /// Removes the given polynomial from the projection. Converts to a UPoly and calls the appropriate overload.
125  void removePolynomial(const Poly& p, std::size_t cid, bool isBound) {
126  removePolynomial(carl::to_univariate_polynomial(p, var(0)), cid, isBound);
127  }
128  /// Removes the given polynomial from the projection.
129  virtual void removePolynomial(const UPoly& p, std::size_t cid, bool isBound) = 0;
130 
131  virtual std::size_t size(std::size_t level) const = 0;
132  std::size_t size() const {
133  std::size_t sum = 0;
134  for (std::size_t level = 1; level <= dim(); level++) {
135  sum += size(level);
136  }
137  return sum;
138  }
139  virtual bool empty(std::size_t level) const = 0;
140  virtual bool empty() {
141  for (std::size_t level = 1; level <= dim(); level++) {
142  if (!empty(level)) return false;
143  }
144  return true;
145  }
146 
147  /// Get a polynomial from this level suited for lifting.
148  OptionalID getPolyForLifting(std::size_t level, SampleLiftedWith& slw) {
149  assert(level > 0);
150  assert(level <= dim());
151  SMTRAT_LOG_DEBUG("smtrat.cad.projection", "Getting poly for lifting from level " << level);
152  for (const auto& pid: mLiftingQueues[level - 1]) {
153  SMTRAT_LOG_TRACE("smtrat.cad.projection", "-> Checking " << getPolynomialById(level,pid));
154  if (!slw.test(pid)) {
155  SMTRAT_LOG_DEBUG("smtrat.cad.projection", "-> " << getPolynomialById(level,pid) << " can be used.");
156  slw.set(pid);
157  return OptionalID(pid);
158  }
159  }
160  SMTRAT_LOG_DEBUG("smtrat.cad.projection", "-> None.");
161  return OptionalID();
162  }
163 
164  virtual bool hasPolynomialById(std::size_t level, std::size_t id) const = 0;
165  /// Retrieves a polynomial from its id.
166  virtual const UPoly& getPolynomialById(std::size_t level, std::size_t id) const = 0;
167 
168  virtual void exportAsDot(std::ostream&) const {}
169  virtual Origin getOrigin(std::size_t level, std::size_t id) const {
170  if (mInfo.hasInfo(level, id)) return mInfo(level, id).origin;
171  return Origin();
172  }
173  };
174 
175 }
176 }
void reset()
Resets all datastructures, use the given variables from now on.
void removePolynomial(const Poly &p, std::size_t cid, bool isBound)
Removes the given polynomial from the projection. Converts to a UPoly and calls the appropriate overl...
ProjectionOperator mOperator
The projection operator.
virtual bool empty(std::size_t level) const =0
virtual bool hasPolynomialById(std::size_t level, std::size_t id) const =0
virtual carl::Bitset addPolynomial(const UPoly &p, std::size_t cid, bool isBound)=0
Adds the given polynomial to the projection.
const Constraints & mConstraints
carl::Bitset addPolynomial(const Poly &p, std::size_t cid, bool isBound)
Adds the given polynomial to the projection. Converts to a UPoly and calls the appropriate overload.
std::size_t dim() const
Returns the dimension of the projection.
std::size_t size() const
virtual void removePolynomial(const UPoly &p, std::size_t cid, bool isBound)=0
Removes the given polynomial from the projection.
void freeID(std::size_t level, std::size_t id)
Frees a currently used polynomial id for the given level.
std::function< void(std::size_t, const SampleLiftedWith &)> mRemoveCallback
Callback to be called when polynomials are removed. The arguments are the projection level and a bits...
ProjectionInformation mInfo
Additional info on projection, projection levels and projection polynomials.
std::size_t getID(std::size_t level)
Returns a fresh polynomial id for the given level.
virtual const UPoly & getPolynomialById(std::size_t level, std::size_t id) const =0
Retrieves a polynomial from its id.
virtual carl::Bitset addEqConstraint(const UPoly &p, std::size_t cid, bool isBound)
Adds the given polynomial of an equational constraint to the projection.
virtual void exportAsDot(std::ostream &) const
virtual Origin getOrigin(std::size_t level, std::size_t id) const
std::vector< PolynomialLiftingQueue< BaseProjection > > mLiftingQueues
List of lifting queues that can be used for incremental projection.
std::vector< carl::IDPool > mIDPools
Lift of id pools to generate fresh IDs for polynomials.
carl::Variable var(std::size_t level) const
Returns the variable that corresponds to the given level, that is the variable eliminated in this lev...
OptionalID getPolyForLifting(std::size_t level, SampleLiftedWith &slw)
Get a polynomial from this level suited for lifting.
bool canBePurgedByBounds(const UPoly &p) const
Checks whether a polynomial can safely be ignored due to the bounds.
BaseProjection(const Constraints &c)
carl::Bitset addEqConstraint(const Poly &p, std::size_t cid, bool isBound)
Adds the given polynomial of an equational constraint to the projection. Converts to a UPoly and call...
virtual std::size_t size(std::size_t level) const =0
void callRemoveCallback(std::size_t level, const SampleLiftedWith &slw) const
const auto & vars() const
Returns the variables used for projection.
void setRemoveCallback(F &&f)
Sets a callback that is called whenever polynomials are removed.
bool isPurged(std::size_t level, std::size_t id)
const auto & bounds() const
const std::vector< carl::Variable > & vars() const
This class represents one or more origins of some object.
Definition: Origin.h:21
bool hasInfo(std::size_t level) const
std::optional< std::size_t > OptionalID
Definition: common.h:13
carl::UnivariatePolynomial< Poly > UPoly
Definition: common.h:17
carl::Bitset SampleLiftedWith
Definition: common.h:15
Rational evaluate(carl::Variable v, const Poly &p, const Rational &r)
Definition: utils.h:11
Class to create the formulas for axioms.
carl::MultivariatePolynomial< Rational > Poly
Definition: types.h:25
#define SMTRAT_LOG_TRACE(channel, msg)
Definition: logging.h:36
#define SMTRAT_LOG_DEBUG(channel, msg)
Definition: logging.h:35