SMT-RAT  24.02
Toolbox for Strategic and Parallel Satisfiability-Modulo-Theories Solving
MaxSMT.h
Go to the documentation of this file.
1 #pragma once
2 
4 #include <carl-formula/model/Model.h>
5 #include <smtrat-solver/Module.h>
6 
7 namespace smtrat {
8 
10 inline std::ostream& operator<<(std::ostream& os, MaxSMTStrategy ucs) {
11  switch (ucs) {
12  case MaxSMTStrategy::FU_MALIK_INCREMENTAL: return os << "FU_MALIK_INCREMENTAL";
13  case MaxSMTStrategy::MSU3: return os << "MSU3";
14  case MaxSMTStrategy::LINEAR_SEARCH: return os << "LINEAR_SEARCH";
15  }
16 }
17 
18 /// Contains strategy implementations for max SMT computations.
19 namespace maxsmt {
20 
21 template<typename Solver, MaxSMTStrategy Strategy>
22 class MaxSMTBackend {};
23 
24 }
25 
26 
27 template<typename Solver, MaxSMTStrategy Strategy>
28 class MaxSMT {
29 private:
30  Solver& mSolver;
31  std::vector<FormulaT> mSoftFormulas;
32  std::map<FormulaT, std::string> mSoftFormulaIds;
33 
34  bool satisfied(const FormulaT& formula, const Model& model) {
35  return carl::substitute(formula, model).is_true();
36  }
37 
38 public:
39  MaxSMT(Solver& s) : mSolver(s) {}
40 
41  void add_soft_formula(const FormulaT& formula, Rational weight, const std::string& id) {
42  // TODO add support for weights
43  if (weight != 1) {
44  SMTRAT_LOG_WARN("smtrat.maxsmt", "Weights are not yet supported by MaxSMT backends.");
45  }
46  mSoftFormulas.push_back(formula);
47  mSoftFormulaIds.emplace(formula, id);
48  }
49 
50  void remove_soft_formula(const FormulaT& formula) {
51  mSoftFormulas.erase(std::remove(mSoftFormulas.begin(), mSoftFormulas.end(), formula));
52  mSoftFormulaIds.erase(formula);
53  }
54 
55  void reset() {
56  mSoftFormulas.clear();
57  }
58 
59  bool active() const {
60  return !mSoftFormulas.empty();
61  }
62 
63  std::tuple<Answer,Model,ObjectiveValues> compute() {
64  SMTRAT_LOG_INFO("smtrat.maxsmt", "Running MAXSMT.");
65  Answer ans = mSolver.check();
66  SMTRAT_LOG_DEBUG("smtrat.maxsmt", "Checking satisfiability of hard clauses -> " << ans);
67  if (ans != Answer::SAT) return std::make_tuple(ans, Model(), ObjectiveValues());
68  SMTRAT_LOG_INFO("smtrat.maxsmt", "Maximize weight for soft formulas " << mSoftFormulas);
69  mSolver.push();
71  mSolver.pop();
72  if (!is_sat(ans)) {
73  SMTRAT_LOG_INFO("smtrat.maxsmt", "Got unexpected response " << ans);
74  return std::make_tuple(ans, Model(), ObjectiveValues());
75  } else {
76  std::map<std::string, Rational> objectives;
77  for (const auto& f : mSoftFormulas) {
78  if (satisfied(f, mSolver.model())) {
79  SMTRAT_LOG_DEBUG("smtrat.maxsmt", "Satisfied soft clause " << f << " of weight 1 with id " << mSoftFormulaIds.at(f));
80  objectives[mSoftFormulaIds.at(f)] += 1; // weights should be resprected here
81  } else {
82  objectives[mSoftFormulaIds.at(f)];
83  }
84  }
85  ObjectiveValues results;
86  for (const auto& p : objectives) results.emplace_back(p);
87  return std::make_tuple(Answer::SAT, mSolver.model(), results);
88  }
89  }
90 };
91 
92 }
void add_soft_formula(const FormulaT &formula, Rational weight, const std::string &id)
Definition: MaxSMT.h:41
void remove_soft_formula(const FormulaT &formula)
Definition: MaxSMT.h:50
void reset()
Definition: MaxSMT.h:55
std::map< FormulaT, std::string > mSoftFormulaIds
Definition: MaxSMT.h:32
bool active() const
Definition: MaxSMT.h:59
std::vector< FormulaT > mSoftFormulas
Definition: MaxSMT.h:31
bool satisfied(const FormulaT &formula, const Model &model)
Definition: MaxSMT.h:34
MaxSMT(Solver &s)
Definition: MaxSMT.h:39
std::tuple< Answer, Model, ObjectiveValues > compute()
Definition: MaxSMT.h:63
Solver & mSolver
Definition: MaxSMT.h:30
static void remove(V &ts, const T &t)
Definition: Alg.h:36
bool substitute(const FormulaT &constr, const carl::Variable var, const carl::vs::Term< Poly > &term, FormulaT &result)
Definition: VSHelper.h:138
Class to create the formulas for axioms.
bool is_sat(Answer a)
Definition: types.h:58
carl::Formula< Poly > FormulaT
Definition: types.h:37
std::vector< std::pair< std::variant< Poly, std::string >, Model::mapped_type > > ObjectiveValues
Definition: model.h:32
carl::Model< Rational, Poly > Model
Definition: model.h:13
MaxSMTStrategy
Definition: MaxSMT.h:9
Answer
An enum with the possible answers a Module can give.
Definition: types.h:57
std::ostream & operator<<(std::ostream &os, CMakeOptionPrinter cmop)
mpq_class Rational
Definition: types.h:19
#define SMTRAT_LOG_WARN(channel, msg)
Definition: logging.h:33
#define SMTRAT_LOG_INFO(channel, msg)
Definition: logging.h:34
#define SMTRAT_LOG_DEBUG(channel, msg)
Definition: logging.h:35