SMT-RAT  24.02
Toolbox for Strategic and Parallel Satisfiability-Modulo-Theories Solving
TotalizerEncoder.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "PseudoBoolEncoder.h"
4 #include "PseudoBoolNormalizer.h"
5 
6 namespace smtrat {
7  // forward declaration
8  class TotalizerTree;
9 
10  /**
11  * TotalizerEncoder implements the Totalizer encoding as described in "Incremental Cardinality Constraints for MaxSAT" by Martins et al
12  * https://doi.org/10.1007/978-3-319-10428-7_39
13  */
15  public:
18 
19 
20  bool canEncode(const ConstraintT& constraint);
21  Rational encodingSize(const ConstraintT& constraint);
22 
23  std::string name() { return "TotalizerEncoder"; }
24 
25 
26  protected:
27  std::optional<FormulaT> doEncode(const ConstraintT& constraint);
28 
29  private:
30  // we need this cache because with recurring calls we might produce a tree multiple times although the set
31  // of variables is identical, introducing a load of redundant variables.
32  std::map<carl::Variables, TotalizerTree*> mTreeCache;
33  std::map<carl::Variables, FormulaT> mSumPropagationFormulaCache;
34 
35  TotalizerTree buildTree(const std::set<carl::Variable>& variables);
38 
39  };
40 
41  class TotalizerTree {
42  public:
43  TotalizerTree(const std::set<carl::Variable>& variables);
44 
46  if (mLeft) delete mLeft;
47  if (mRight) delete mRight;
48  }
49 
50  bool isLeaf() { return !(mLeft && mRight); }
51 
53  assert(mLeft != nullptr);
54  return *mLeft;
55  }
56 
58  assert(mRight != nullptr);
59  return *mRight;
60  }
61 
62  std::vector<carl::Variable> variables() { return mNodeVariables; }
63 
64 
65  private:
66  std::vector<carl::Variable> mNodeVariables;
67  TotalizerTree* mLeft = nullptr;
68  TotalizerTree* mRight = nullptr;
69  };
70 }
Base class for a PseudoBoolean Encoder.
TotalizerEncoder implements the Totalizer encoding as described in "Incremental Cardinality Constrain...
std::optional< FormulaT > doEncode(const ConstraintT &constraint)
Rational encodingSize(const ConstraintT &constraint)
FormulaT encodeCardinalityRestriction(TotalizerTree &tree, Rational constantPart)
std::map< carl::Variables, FormulaT > mSumPropagationFormulaCache
FormulaT encodeSumPropagation(TotalizerTree &tree)
std::map< carl::Variables, TotalizerTree * > mTreeCache
TotalizerTree buildTree(const std::set< carl::Variable > &variables)
bool canEncode(const ConstraintT &constraint)
std::vector< carl::Variable > mNodeVariables
TotalizerTree * mLeft
TotalizerTree * mRight
TotalizerTree & left()
std::vector< carl::Variable > variables()
TotalizerTree & right()
TotalizerTree(const std::set< carl::Variable > &variables)
Class to create the formulas for axioms.
carl::Formula< Poly > FormulaT
Definition: types.h:37
carl::Constraint< Poly > ConstraintT
Definition: types.h:29
mpq_class Rational
Definition: types.h:19