SMT-RAT  24.02
Toolbox for Strategic and Parallel Satisfiability-Modulo-Theories Solving
FMplexQE.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "../QEQuery.h"
5 #include "../util/VariableIndex.h"
6 #include "../util/Matrix.h"
7 #include "../util/EqualitySubstitution.h"
8 #include "Node.h"
9 #include "FMplexQEStatistics.h"
10 
11 namespace smtrat::qe::fmplex {
12 
13 class FMplexQE {
14 
15 public: // type definitions
19  using Row = std::vector<Matrix::RowEntry>;
20 
21  struct cmp_row {
22  bool operator()(const Row& a, const Row& b) const {
23  auto it_a = a.begin(), it_b = b.begin();
24  auto end_a = a.end(), end_b = b.end();
25  while (it_a != end_a && it_b != end_b) {
26  if (it_a->col_index != it_b->col_index)
27  return it_a->col_index < it_b->col_index;
28  if (it_a->value != it_b->value)
29  return it_a->value < it_b->value;
30  ++it_a;
31  ++it_b;
32  }
33  return (it_b != end_b); // at this point: a < b only if a is at end and b is not
34  }
35  };
36 
37 private: // members
40  std::vector<Node> m_nodes;
44  std::set<Row, cmp_row> m_found_rows;
45 
46 
47 public:
48 
49  FMplexQE(const FormulaT& qfree, const QEQuery& quantifiers)
50  : m_query(quantifiers)
51  , m_formula(qfree) {
52  assert(
53  qfree.type() == carl::FormulaType::CONSTRAINT ||
54  qfree.is_real_constraint_conjunction()
55  );
56  }
57 
59 
60 private:
61  std::vector<carl::Variable> gather_elimination_variables() const;
62 
63  ColIndex constant_column() const { return m_var_idx.size(); }
64  ColIndex delta_column() const { return constant_column() + 1; }
65  ColIndex origin_column(RowIndex row) const { return delta_column() + 1 + row; }
66 
67  bool is_trivial(const Row& row) const {
68  assert(!row.empty());
69  return row.begin()->col_index >= constant_column();
70  }
71 
72  bool is_conflict(const Row& row) const {
73  assert(is_trivial(row));
74  return ((row.front().col_index <= delta_column()) && (row.front().value > 0));
75  }
76 
77  bool is_positive_combination(const Row& row);
78 
79  FormulaT constraint_from_row(const Row& row) const;
80 
81  /**
82  * truncates the given row to not contain any "origin" information
83  * and inserts the result into the set of final projected constraints-
84  * This assumes that the row does contain origins and does not contain elimination variables.
85  */
86  void collect_constraint(const Row& row) {
87  Row truncated = row;
88  for (std::size_t i = 0; ; ++i) {
89  if (truncated[i].col_index > delta_column()) {
90  truncated.resize(i);
91  break;
92  }
93  }
94  m_found_rows.insert(truncated);
95  }
96 
97  /**
98  * Splits the given node into multiple nodes that are independent in the following sense:
99  * - they partition the elimination variables and
100  * - the constraints of a node to not contain any of the elimination variables of another node.
101  * Thus, these eliminations can be carried out independently.
102  */
103  std::vector<Node> split_into_independent_nodes(const Node& n) const;
104 
105  /**
106  * Constructs the starting nodes from m_query and m_formula as follows:
107  * - Collects the variables in m_query and constraints in m_formula
108  * - Uses equations in m_formula to eliminate as many variables as possible
109  * - Filters finished constraints from the result
110  * - Splits the variables and constraints into independent blocks
111  * - Builds a matrix for each block using build_initial_matrix
112  * - Adds an according Node for each block to the stack of nodes
113  */
114  void build_initial_systems();
115 
116  /**
117  * Constructs a matrix from the given constraints.
118  * Each row of the matrix has the form [a_1 ... a_n | a'_1 ... a'_k| b | d | 0 ... 1 ... 0] where
119  * - The corresponding constraint is equivalent to
120  * (a_1 x_1 + ... + a_n x_n) + (a'_1 y_1 + ... a'_k y_k) + (b + delta*d) <= 0,
121  * - x_1,...x_n are the quantified variables, y_1,...,y_k are the parameter variables,
122  * - d is 0 if the constraint is weak and 1 if it is strict,
123  * - the 0 ... 1 ... 0 part has one entry for each constraint, where the 1 indidicates at which
124  * index in the input constraints the corresponding constraint is.
125  */
126  Matrix build_initial_matrix(const FormulasT& constraints);
127 
128  /**
129  * Eliminates the chosen column in parent by dropping all rows with non-zero entry in that column.
130  * Should only be applied if the type of parent is NBS, i.e. if the chosen column is unbounded.
131  */
133 
134  /**
135  * Eliminates the chosen column in parent using the next eliminator in parent.
136  * This eliminator is then erased and added to the ignored rows.
137  * Should only be applied if the type of parent is LBS or UBS.
138  */
139  Node bounded_elimination(Node& parent);
140 
141  /**
142  * Eliminates the chosen column in parent using Fourier-Motzkin, but discards rows with 0 coeff.
143  * Should only be called if type of parent is FM, i.e. if only one variable is left to eliminate.
144  * @return false if a global conflict is found and true otherwise.
145  */
146  bool fm_elimination(Node& parent);
147 
148  /**
149  * writes the given qe problem as a .ine file as used in CDD lib.
150  */
151  void write_matrix_to_ine(const Matrix& m, const std::string& filename) const;
152  void write_matrix_to_redlog(const Matrix& m, const std::string& filename) const;
153 };
154 
155 } // namespace smtrat::qe::fmplex
qe::util::Matrix Matrix
Definition: FMplexQE.h:16
Node bounded_elimination(Node &parent)
Eliminates the chosen column in parent using the next eliminator in parent.
Definition: FMplexQE.cpp:300
std::vector< Node > m_nodes
Definition: FMplexQE.h:40
FormulaT eliminate_quantifiers()
Definition: FMplexQE.cpp:17
void write_matrix_to_redlog(const Matrix &m, const std::string &filename) const
Definition: FMplexQE.cpp:455
ColIndex constant_column() const
Definition: FMplexQE.h:63
bool is_trivial(const Row &row) const
Definition: FMplexQE.h:67
bool is_positive_combination(const Row &row)
Definition: FMplexQE.cpp:290
bool fm_elimination(Node &parent)
Eliminates the chosen column in parent using Fourier-Motzkin, but discards rows with 0 coeff.
Definition: FMplexQE.cpp:391
void collect_constraint(const Row &row)
truncates the given row to not contain any "origin" information and inserts the result into the set o...
Definition: FMplexQE.h:86
std::vector< Node > split_into_independent_nodes(const Node &n) const
Splits the given node into multiple nodes that are independent in the following sense:
Definition: FMplexQE.cpp:139
Matrix::RowIndex RowIndex
Definition: FMplexQE.h:17
Matrix::ColIndex ColIndex
Definition: FMplexQE.h:18
Node unbounded_elimination(Node &parent)
Eliminates the chosen column in parent by dropping all rows with non-zero entry in that column.
Definition: FMplexQE.cpp:255
ColIndex delta_column() const
Definition: FMplexQE.h:64
FormulaSetT m_found_conjuncts
Definition: FMplexQE.h:41
void write_matrix_to_ine(const Matrix &m, const std::string &filename) const
writes the given qe problem as a .ine file as used in CDD lib.
Definition: FMplexQE.cpp:420
FMplexQE(const FormulaT &qfree, const QEQuery &quantifiers)
Definition: FMplexQE.h:49
qe::util::VariableIndex m_var_idx
Definition: FMplexQE.h:42
Matrix build_initial_matrix(const FormulasT &constraints)
Constructs a matrix from the given constraints.
Definition: FMplexQE.cpp:100
ColIndex origin_column(RowIndex row) const
Definition: FMplexQE.h:65
FormulaT constraint_from_row(const Row &row) const
Definition: FMplexQE.cpp:80
std::vector< Matrix::RowEntry > Row
Definition: FMplexQE.h:19
std::set< Row, cmp_row > m_found_rows
Definition: FMplexQE.h:44
void build_initial_systems()
Constructs the starting nodes from m_query and m_formula as follows:
Definition: FMplexQE.cpp:188
bool is_conflict(const Row &row) const
Definition: FMplexQE.h:72
std::vector< carl::Variable > gather_elimination_variables() const
Definition: FMplexQE.cpp:70
std::size_t RowIndex
Definition: Matrix.h:11
std::size_t ColIndex
Definition: Matrix.h:12
std::vector< std::pair< QuantifierType, std::vector< carl::Variable > >> QEQuery
Definition: QEQuery.h:17
carl::FormulaSet< Poly > FormulaSetT
Definition: types.h:41
carl::Formula< Poly > FormulaT
Definition: types.h:37
carl::Formulas< Poly > FormulasT
Definition: types.h:39
bool operator()(const Row &a, const Row &b) const
Definition: FMplexQE.h:22