SMT-RAT  24.02
Toolbox for Strategic and Parallel Satisfiability-Modulo-Theories Solving
GBPPModule.tpp
Go to the documentation of this file.
1 /**
2  * @file GBPP.cpp
3  * @author YOUR NAME <YOUR EMAIL ADDRESS>
4  *
5  * @version 2018-03-09
6  * Created on 2018-03-09.
7  */
8 
9 #include "GBPPModule.h"
10 
11 #include <carl-arith/poly/umvpoly/functions/Complexity.h>
12 
13 namespace smtrat
14 {
15  template<class Settings>
16  GBPPModule<Settings>::GBPPModule(const ModuleInput* _formula, Conditionals& _conditionals, Manager* _manager):
17  PModule( _formula, _conditionals, _manager, Settings::moduleName )
18  {
19  simplifyInequalityFunction = std::bind(&GBPPModule<Settings>::simplifyInequality, this, std::placeholders::_1);
20  }
21 
22  template<class Settings>
23  GBPPModule<Settings>::~GBPPModule()
24  {}
25 
26  template<class Settings>
27  void GBPPModule<Settings>::updateModel() const
28  {
29  mModel.clear();
30  if( solverState() == Answer::SAT )
31  {
32  // Your code.
33  }
34  }
35 
36  template<class Settings>
37  Answer GBPPModule<Settings>::checkCore()
38  {
39  mEqualities.clear();
40  mEqualityComplexity = 0;
41  mBasis.reset();
42 
43  // Extract top-level Equalities
44  for (const auto& f: rReceivedFormula()) {
45  if (f.formula().type() == carl::FormulaType::CONSTRAINT) {
46  if (f.formula().constraint().relation() == carl::Relation::EQ) {
47  SMTRAT_LOG_DEBUG("smtrat.gbpp", "Found equality " << f.formula().constraint());
48  mEqualities.emplace(f.formula().constraint());
49  mEqualityComplexity += carl::complexity(f.formula().constraint().lhs());
50  }
51  }
52  }
53 
54  // Compute GBasis
55  for (const auto& eq: mEqualities) {
56  SMTRAT_LOG_DEBUG("smtrat.gbpp", "Adding to Gröbner Basis: " << gpoly(eq.constraint().lhs().normalize()));
57  mBasis.addPolynomial(gpoly(eq.constraint().lhs().normalize()));
58  }
59  mBasis.calculate();
60  SMTRAT_LOG_DEBUG("smtrat.gbpp", "Constructed Gröbner Basis:" << std::endl << mBasis.getIdeal());
61 
62  // Simplify all inequalities w.r.t. GBasis and forward to backend
63  for (const auto& f: rReceivedFormula()) {
64  if (mEqualities.find(f.formula()) != mEqualities.end()) continue;
65  auto res = carl::visit_result(f.formula(), simplifyInequalityFunction);
66 
67  if (res != f.formula()) {
68  SMTRAT_LOG_INFO("smtrat.gbpp", "Reduced " << f.formula() << " to " << res);
69  }
70  if (!res.is_true()) {
71  addSubformulaToPassedFormula(res, f.formula());
72  }
73  }
74 
75  // Forward basis to backend
76  std::size_t basisComplexity = 0;
77  for (const auto& p: mBasis.getIdeal().getGenerators()) {
78  basisComplexity += carl::complexity(p);
79  }
80  if (basisComplexity >= mEqualityComplexity) {
81  for (const auto& f: mEqualities) {
82  addSubformulaToPassedFormula(f, f);
83  }
84  } else {
85  for (const auto& p: mBasis.getIdeal().getGenerators()) {
86  addSubformulaToPassedFormula(FormulaT(ConstraintT(Poly(p), carl::Relation::EQ)));
87  }
88  }
89 
90  Answer ans = runBackends();
91  if (ans == UNSAT) {
92  generateTrivialInfeasibleSubset();
93  }
94  return ans;
95  }
96 
97  template<typename Settings>
98  FormulaT GBPPModule<Settings>::simplifyInequality(const FormulaT& formula) const {
99  if (formula.type() != carl::FormulaType::CONSTRAINT) return formula;
100  // This case should be catched by ESModule...
101  if (mEqualities.find(formula) != mEqualities.end()) return formula;
102  const auto& c = formula.constraint();
103 
104  typename Settings::Reductor reductor(mBasis.getIdeal(), gpoly(c.lhs()));
105  auto reduced = reductor.fullReduce();
106  SMTRAT_LOG_DEBUG("smtrat.gbpp", "Reduced " << c.lhs() << " to " << reduced);
107 
108  if (reduced.nr_terms() >= c.lhs().nr_terms()) return formula;
109  return FormulaT(ConstraintT(Poly(reduced), c.relation()));
110  }
111 }
112 
113