SMT-RAT  24.02
Toolbox for Strategic and Parallel Satisfiability-Modulo-Theories Solving
UnsatCore.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <iostream>
5 
6 namespace smtrat {
7 
8 enum class UnsatCoreStrategy { /* Counting, */ ModelExclusion };
9 inline std::ostream& operator<<(std::ostream& os, UnsatCoreStrategy ucs) {
10  switch (ucs) {
11  // case UnsatCoreStrategy::Counting: return os << "Counting";
12  case UnsatCoreStrategy::ModelExclusion: return os << "ModelExclusion";
13  }
14 }
15 
16 /// Contains strategy implementations for unsat core computations.
17 namespace unsatcore {
18 
19 template<typename Solver, UnsatCoreStrategy Strategy>
21 
22 }
23 
24 
25 template<typename Solver, UnsatCoreStrategy Strategy>
26 class UnsatCore {
27 private:
28  std::map<FormulaT, std::string> mAnnotatedNames;
29  std::vector<FormulaT> mFormulas;
30  Solver& mSolver;
31 
32 public:
33  UnsatCore(Solver& s) : mSolver(s) {}
34 
35  void add_annotated_name(const FormulaT& formula, const std::string& name) {
36  mAnnotatedNames.emplace(formula, name);
37  mFormulas.emplace_back(formula);
38  }
39 
40  void remove_annotated_name(const FormulaT& formula) {
41  mAnnotatedNames.erase(formula);
42  mFormulas.erase(std::find(mFormulas.begin(), mFormulas.end(), formula));
43  }
44 
45  void reset() {
46  mAnnotatedNames.clear();
47  }
48 
49  bool active() const {
50  return mAnnotatedNames.empty();
51  }
52 
53  std::pair<Answer,FormulasT> compute_core(const FormulasT& formulas) {
54  // we assume that mSolver is UNSAT
55  if (false) {
56  // check satisfiability
57  Answer ans = mSolver.check();
58  SMTRAT_LOG_DEBUG("smtrat.unsatcore", "Checking satisfiability -> " << ans);
59  if (is_sat(ans))
60  return std::make_pair(Answer::SAT, FormulasT());
61  }
62 
63  for (const auto& f : formulas) {
64  mSolver.remove(f);
65  }
66 
67  SMTRAT_LOG_INFO("smtrat.unsatcore", "Calculate unsat core for " << formulas);
68  mSolver.push();
70  mSolver.pop();
71 
72  for (const auto& f : formulas) {
73  mSolver.add(f);
74  }
75 
76  return result;
77  }
78 
79  std::pair<Answer,std::vector<std::string>> compute() {
80  SMTRAT_LOG_INFO("smtrat.unsatcore", "Running unsat cores using named formulas " << mAnnotatedNames);
82  if (result.first == Answer::UNSAT) {
83  SMTRAT_LOG_DEBUG("smtrat.unsatcore", "Got unsat core " << result.second);
84  std::vector<std::string> core;
85  for (const auto& f : result.second) {
86  assert(mAnnotatedNames.find(f) != mAnnotatedNames.end());
87  core.push_back(mAnnotatedNames[f]);
88  }
89  return std::make_pair(Answer::UNSAT, core);
90  } else {
91  return std::make_pair(result.first, std::vector<std::string>());
92  }
93  }
94 };
95 
96 }
Solver & mSolver
Definition: UnsatCore.h:30
std::pair< Answer, std::vector< std::string > > compute()
Definition: UnsatCore.h:79
bool active() const
Definition: UnsatCore.h:49
std::map< FormulaT, std::string > mAnnotatedNames
Definition: UnsatCore.h:28
std::pair< Answer, FormulasT > compute_core(const FormulasT &formulas)
Definition: UnsatCore.h:53
std::vector< FormulaT > mFormulas
Definition: UnsatCore.h:29
void add_annotated_name(const FormulaT &formula, const std::string &name)
Definition: UnsatCore.h:35
void remove_annotated_name(const FormulaT &formula)
Definition: UnsatCore.h:40
UnsatCore(Solver &s)
Definition: UnsatCore.h:33
static bool find(V &ts, const T &t)
Definition: Alg.h:47
Class to create the formulas for axioms.
bool is_sat(Answer a)
Definition: types.h:58
carl::Formula< Poly > FormulaT
Definition: types.h:37
Answer
An enum with the possible answers a Module can give.
Definition: types.h:57
UnsatCoreStrategy
Definition: UnsatCore.h:8
std::ostream & operator<<(std::ostream &os, CMakeOptionPrinter cmop)
carl::Formulas< Poly > FormulasT
Definition: types.h:39
#define SMTRAT_LOG_INFO(channel, msg)
Definition: logging.h:34
#define SMTRAT_LOG_DEBUG(channel, msg)
Definition: logging.h:35