SMT-RAT  24.02
Toolbox for Strategic and Parallel Satisfiability-Modulo-Theories Solving
CAD.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "Projection.h"
4 
6 #include <smtrat-cad/common.h>
8 
9 namespace smtrat::qe::cad {
10 
11  template<typename Settings>
12  class CAD {
13  public:
15  private:
16  std::vector<carl::Variable> mVariables;
18  std::vector<Poly> polynomials;
21 
22  std::size_t idPL(std::size_t level) const {
23  assert(level > 0 && level <= dim());
24  return dim() - level + 1;
25  }
26  std::size_t idLP(std::size_t level) const {
27  assert(level > 0 && level <= dim());
28  return dim() - level + 1;
29  }
30  public:
31  CAD():
33  [&](const UPoly& p, std::size_t cid, bool isBound){ mProjection.addPolynomial(smtrat::cad::projection::normalize(p), cid, isBound); },
34  [&](const UPoly& p, std::size_t cid, bool isBound){ mProjection.addEqConstraint(smtrat::cad::projection::normalize(p), cid, isBound); },
35  [&](const UPoly& p, std::size_t cid, bool isBound){ mProjection.removePolynomial(smtrat::cad::projection::normalize(p), cid, isBound); }
36  ),
39  {
40  mProjection.setRemoveCallback([&](std::size_t level, const smtrat::cad::SampleLiftedWith& mask){
41  mLifting.removedPolynomialsFromLevel(idPL(level), mask);
42  });
43  }
44 
45  void reset(const std::vector<carl::Variable>& vars) {
46  mVariables = vars;
49  mLifting.reset(std::vector<carl::Variable>(vars.rbegin(), vars.rend()));
50  }
51 
52  std::size_t dim() const {
53  return mVariables.size();
54  }
55  const auto& getProjection() const {
56  return mProjection;
57  }
58  const auto& getLifting() const {
59  return mLifting;
60  }
61 
62  void addConstraint(const ConstraintT& c) {
63  SMTRAT_LOG_DEBUG("smtrat.cad", "Adding " << c);
64  mConstraints.add(c);
65  SMTRAT_LOG_DEBUG("smtrat.cad", "Current projection:" << std::endl << mProjection);
66  SMTRAT_LOG_DEBUG("smtrat.cad", "Current sampletree:" << std::endl << mLifting.getTree());
67  }
68  void removeConstraint(const ConstraintT& c) {
69  SMTRAT_LOG_DEBUG("smtrat.cad", "Removing " << c);
70  auto mask = mConstraints.remove(c);
71  mLifting.removedConstraint(mask);
72  SMTRAT_LOG_DEBUG("smtrat.cad", "Current projection:" << std::endl << mProjection);
73  SMTRAT_LOG_DEBUG("smtrat.cad", "Current sampletree:" << std::endl << mLifting.getTree());
74  }
75 
76  void addPolynomial(const Poly& p) {
77  polynomials.push_back(p);
78  }
79  void removePolynomial(std::size_t level, std::size_t id) {
80  mProjection.removeProjectionFactor(level, id);
81  }
82 
83  void project() {
84  for(auto it = polynomials.begin(); it != polynomials.end(); it++) {
86  }
87  polynomials.clear();
88  }
89  void lift() {
90  mLifting.resetFullSamples();
91  mLifting.restoreRemovedSamples();
92 
93  while (mLifting.hasNextSample()) {
94  auto it = mLifting.getNextSample();
95  auto& s = *it;
96  SMTRAT_LOG_DEBUG("smtrat.cad", "Sample " << s << " at depth " << it.depth());
97  SMTRAT_LOG_DEBUG("smtrat.cad", "Current sample: " << mLifting.printSample(it));
98  assert(0 <= it.depth() && it.depth() < dim());
99  auto polyID = mProjection.getPolyForLifting(idLP(it.depth() + 1), s.liftedWith());
100  if(polyID) {
101  if(mProjection.hasPolynomialById(idLP(it.depth() + 1), *polyID)) {
102  const auto& poly = mProjection.getPolynomialById(idLP(it.depth() + 1), *polyID);
103  SMTRAT_LOG_DEBUG("smtrat.cad", "Lifting " << s << " with " << poly);
104  mLifting.liftSample(it, poly, *polyID, true); // ignores nullifications (which is incorrect!)
105  }
106  }else {
107  mLifting.removeNextSample();
108  mLifting.addTrivialSample(it);
109  }
110  }
111 
112  std::size_t number_of_cells = 0;
113  const auto& tree = mLifting.getTree();
114  for(auto it = tree.begin_leaf(); it != tree.end_leaf(); ++it) {
115  ++number_of_cells;
116  }
117  SMTRAT_LOG_WARN("smtrat.cad", "Got " << number_of_cells << " cells");
118  }
119  };
120 
121 }
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...
virtual bool hasPolynomialById(std::size_t level, std::size_t id) const =0
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.
virtual const UPoly & getPolynomialById(std::size_t level, std::size_t id) const =0
Retrieves a polynomial from its id.
OptionalID getPolyForLifting(std::size_t level, SampleLiftedWith &slw)
Get a polynomial from this level suited for lifting.
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...
void setRemoveCallback(F &&f)
Sets a callback that is called whenever polynomials are removed.
std::size_t add(const ConstraintT &c)
carl::Bitset remove(const ConstraintT &c)
Removes a constraint.
void reset(const std::vector< carl::Variable > &vars)
Tree::iterator Iterator
Definition: LiftingTree.h:19
Projection< Settings > mProjection
Definition: CAD.h:19
void project()
Definition: CAD.h:83
const auto & getProjection() const
Definition: CAD.h:55
std::size_t dim() const
Definition: CAD.h:52
std::vector< Poly > polynomials
Definition: CAD.h:18
std::size_t idLP(std::size_t level) const
Definition: CAD.h:26
void removePolynomial(std::size_t level, std::size_t id)
Definition: CAD.h:79
void addPolynomial(const Poly &p)
Definition: CAD.h:76
smtrat::cad::LiftingTree< Settings > mLifting
Definition: CAD.h:20
smtrat::cad::CADConstraints< Settings::backtracking > mConstraints
Definition: CAD.h:17
typename smtrat::cad::LiftingTree< Settings >::Iterator TreeIterator
Definition: CAD.h:14
std::size_t idPL(std::size_t level) const
Definition: CAD.h:22
void addConstraint(const ConstraintT &c)
Definition: CAD.h:62
void lift()
Definition: CAD.h:89
void removeConstraint(const ConstraintT &c)
Definition: CAD.h:68
const auto & getLifting() const
Definition: CAD.h:58
std::vector< carl::Variable > mVariables
Definition: CAD.h:16
void reset(const std::vector< carl::Variable > &vars)
Definition: CAD.h:45
Poly normalize(const Poly &p)
Normalizes the given Poly by removing constant and duplicate factors.
Definition: utils.h:32
carl::UnivariatePolynomial< Poly > UPoly
Definition: common.h:17
carl::Bitset SampleLiftedWith
Definition: common.h:15
std::vector< carl::Variable > vars(const std::pair< QuantifierType, std::vector< carl::Variable >> &p)
Definition: QEQuery.h:43
carl::MultivariatePolynomial< Rational > Poly
Definition: types.h:25
carl::Constraint< Poly > ConstraintT
Definition: types.h:29
#define SMTRAT_LOG_WARN(channel, msg)
Definition: logging.h:33
#define SMTRAT_LOG_DEBUG(channel, msg)
Definition: logging.h:35