SMT-RAT  24.02
Toolbox for Strategic and Parallel Satisfiability-Modulo-Theories Solving
ShortFormulaEncoder.cpp
Go to the documentation of this file.
1 #include "ShortFormulaEncoder.h"
2 
3 namespace smtrat {
4  std::optional<FormulaT> ShortFormulaEncoder::doEncode(const ConstraintT& constraint) {
5  SMTRAT_LOG_DEBUG("smtrat.pbc", "Trying to convert small formula: " << constraint);
6  assert(constraint.variables().size() == 1);
7 
8  carl::Relation cRel = constraint.relation();
9  const Poly& cLHS = constraint.lhs();
10  Rational lhsCoeff;
11  FormulaT lhsVar;
12 
13  for (const auto& term : cLHS) {
14  if (term.is_constant()) continue;
15 
16  lhsCoeff = term.coeff();
17  lhsVar = FormulaT(term.single_variable());
18  }
19 
20  Rational cRHS = -constraint.lhs().constant_part();
21 
22  if (cRel == carl::Relation::LEQ) {
23  if(lhsCoeff > 0){
24  //10 x1 <= 3 or 10 x1 < 3 ===> not x1
25  if(cRHS < lhsCoeff && cRHS > 0) return FormulaT(lhsVar.negated());
26 
27  //10 x1 <= -2 or 10 x1 < -2 ===> FALSE
28  if(cRHS < lhsCoeff && cRHS < 0) return FormulaT(carl::FormulaType::FALSE);
29 
30  //10 x1 <= 0 ===> not x1
31  if(cRHS == 0) return FormulaT(lhsVar.negated());
32 
33  //6 x1 <= 39 or 3 x1 < 23 ===> TRUE
34  if(cRHS >= lhsCoeff) return FormulaT(carl::FormulaType::TRUE);
35 
36 
37  }else if(lhsCoeff < 0){
38  //-3 x1 <= -43 or -3 x1 < -43 ===> FALSE
39  if(cRHS < lhsCoeff) return FormulaT(carl::FormulaType::FALSE);
40 
41  //-3 x1 <= 5 or -3 x1 < 5 ===> TRUE
42  if(cRHS >= 0) return FormulaT(carl::FormulaType::TRUE);
43 
44  //-30 x1 <= -5 or -30 x1 < -5 ===> x1
45  if(cRHS >= lhsCoeff && cRHS < 0) return FormulaT(lhsVar);
46 
47  } else { //lhsCoeff == 0
48  //0 x2 <= 0 ===> TRUE
49  if(cRHS >= 0) return FormulaT(carl::FormulaType::TRUE);
50 
51  //0 x3 < 0 ===> FALSE
52  if(cRHS < 0) return FormulaT(carl::FormulaType::FALSE);
53 
54  }
55  }else if(cRel == carl::Relation::EQ){
56  if(lhsCoeff != 0){
57  if(lhsCoeff == cRHS){
58  //-2 x1 = -2 or 3 x1 = 3 ===> x1
59  return FormulaT(lhsVar);
60  }else if(cRHS == 0){
61  //2 x1 = 0 or -3 x1 = 0 ===> not x1
62  return FormulaT(lhsVar.negated());
63  }else{
64  //2 x1 = 4 ===> FALSE
65  return FormulaT(carl::FormulaType::FALSE);
66  }
67  }else{
68  if(cRHS == 0){
69  // 0 x2 = 0 ===> TRUE
70  return FormulaT(carl::FormulaType::TRUE);
71  }else if(cRHS != 0){
72  // 0 x3 = 3 ===> FALSE
73  return FormulaT(carl::FormulaType::FALSE);
74  }
75  }
76  }else if(cRel == carl::Relation::NEQ){
77  if(lhsCoeff != 0){
78  if(lhsCoeff == cRHS){
79  //3 x1 != 3 ===> not x1
80  return FormulaT(lhsVar.negated());
81  }else if(cRHS == 0){
82  //3 x1 != 0 ===> x1
83  return FormulaT(lhsVar);
84  }else if(lhsCoeff != cRHS){
85  //3 x1 != 5 ===> TRUE
86  return FormulaT(carl::FormulaType::TRUE);
87  }
88  }else{
89  if(cRHS == 0){
90  // 0 x2 != 0 ===> FALSE
91  return FormulaT(carl::FormulaType::FALSE);
92  }else if(cRHS != 0){
93  // 0 x3 != 3 ===> TRUE
94  return FormulaT(carl::FormulaType::TRUE);
95  }
96  }
97  }
98 
99  // could not convert
100  return {};
101  }
102 
103  bool ShortFormulaEncoder::canEncode(const ConstraintT& constraint) {
104  return constraint.variables().size() == 1;
105  }
106 
108  if (!canEncode(constraint)) return PseudoBoolEncoder::encodingSize(constraint);
109 
110  return 1;
111  }
112 
113 }
virtual Rational encodingSize(const ConstraintT &constraint)
bool canEncode(const ConstraintT &constraint)
std::optional< FormulaT > doEncode(const ConstraintT &constraint)
Rational encodingSize(const ConstraintT &constraint)
Class to create the formulas for axioms.
carl::Formula< Poly > FormulaT
Definition: types.h:37
carl::MultivariatePolynomial< Rational > Poly
Definition: types.h:25
carl::Constraint< Poly > ConstraintT
Definition: types.h:29
mpq_class Rational
Definition: types.h:19
#define SMTRAT_LOG_DEBUG(channel, msg)
Definition: logging.h:35