SMT-RAT  24.02
Toolbox for Strategic and Parallel Satisfiability-Modulo-Theories Solving
BooleanEncoding.cpp
Go to the documentation of this file.
1 #include "BooleanEncoding.h"
2 #include "ParserState.h"
3 #include "Conversions.h"
4 #include "FunctionInstantiator.h"
5 
6 #define BOOST_SPIRIT_USE_PHOENIX_V3
7 #include <boost/spirit/include/qi.hpp>
8 
9 namespace smtrat {
10 namespace parser {
11 
13  bool operator()(const std::vector<std::size_t>& indices, const std::vector<types::TermType>& arguments, types::TermType& result, TheoryError& errors) const {
14  std::vector<FormulaT> args;
15  if (!convert(arguments, args)) return false;
16  return apply(indices, args, result, errors);
17  }
18  virtual bool apply(const std::vector<std::size_t>& indices, const std::vector<FormulaT>& arguments, types::TermType& result, TheoryError& errors) const = 0;
19  };
20 
21  void at_most_k_helper(size_t k, const std::vector<FormulaT>& set, FormulasT& working, long long int prev_idx, FormulasT& results) {
22  for (size_t i = prev_idx+1; i < set.size(); i++) {
23  working.push_back(set[i].negated());
24  if (working.size() == k+1) {
25  results.emplace_back(carl::FormulaType::OR, working);
26  } else {
27  at_most_k_helper(k, set, working, i, results);
28  }
29  working.pop_back();
30  }
31  }
32  FormulaT at_most_k(size_t k, const std::vector<FormulaT>& set) {
33  if (k >= set.size()) {
34  return FormulaT(carl::FormulaType::TRUE);
35  } else {
36  FormulasT working;
37  FormulasT results;
38  at_most_k_helper(k, set, working, -1, results);
39  return FormulaT(carl::FormulaType::AND, std::move(results));
40  }
41  }
42 
44  bool apply(const std::vector<std::size_t>& indices, const std::vector<FormulaT>& arguments, types::TermType& result, TheoryError& errors) const {
45  if (arguments.size() == 0) {
46  errors.next() << "The operator \"at-most\" expects at least one argument.";
47  return false;
48  }
49  if (indices.size() != 1) {
50  errors.next() << "The operator \"at-most\" expects exactly one index.";
51  return false;
52  }
53 
54  result = at_most_k(indices[0], arguments); // TODO better encoding, see http://www.cs.toronto.edu/~fbacchus/csc2512/at_most_k.pdf
55  return true;
56  }
57  };
58 
60  state->registerFunction("at-most", new AtMostInstantiator());
61  }
62 
63 }
64 }
carl::mpl_variant_of< TermTypes >::type TermType
Variant type for all terms.
Definition: TheoryTypes.h:160
FormulaT at_most_k(size_t k, const std::vector< FormulaT > &set)
void at_most_k_helper(size_t k, const std::vector< FormulaT > &set, FormulasT &working, long long int prev_idx, FormulasT &results)
Class to create the formulas for axioms.
carl::Formula< Poly > FormulaT
Definition: types.h:37
carl::Formulas< Poly > FormulasT
Definition: types.h:39
Base class for all theories.
bool apply(const std::vector< std::size_t > &indices, const std::vector< FormulaT > &arguments, types::TermType &result, TheoryError &errors) const
virtual bool apply(const std::vector< std::size_t > &indices, const std::vector< FormulaT > &arguments, types::TermType &result, TheoryError &errors) const =0
bool operator()(const std::vector< std::size_t > &indices, const std::vector< types::TermType > &arguments, types::TermType &result, TheoryError &errors) const
bool convert(const std::vector< types::TermType > &from, std::vector< T > &to) const
void registerFunction(const std::string &name, const FunctionInstantiator *fi)
Definition: ParserState.h:163
TheoryError & next()
Definition: Common.h:25