2 #include "../util/EqualitySubstitution.h"
10 std::vector<carl::Variable> elimination_vars;
13 elimination_vars.insert(elimination_vars.end(),
vars.begin(),
vars.end());
19 if (
m_formula.type() == carl::FormulaType::CONSTRAINT) constraints.push_back(
m_formula);
20 else constraints =
m_formula.subformulas();
23 for (
const auto& c : constraints) {
24 assert(c.type() == carl::FormulaType::CONSTRAINT);
26 c.constraint().relation() == carl::Relation::LEQ ||
37 return FormulaT(carl::FormulaType::FALSE);
44 if (elimination_vars.empty()) {
50 for (
const auto& c : constraints) {
51 auto vars = carl::variables(c).as_set();
52 if (std::any_of(elimination_vars.begin(),
53 elimination_vars.end(),
54 [&
vars](
const auto v){ return vars.contains(v); })
56 filtered.push_back(c);
59 constraints = filtered;
60 SMTRAT_LOG_DEBUG(
"smtrat.qe",
"Constraints after filtering: " << constraints);
67 std::vector<std::size_t> elim_cols;
68 for (std::size_t i = 0; i < elimination_vars.size(); ++i) {
69 elim_cols.push_back(i);
74 vector_t b = vector_t::Zero(constraints.size());
76 for (std::size_t i = 0; i < constraints.size(); ++i) {
77 for (
const auto& t : constraints[i].constraint().lhs()) {
78 if (t.is_constant()) b(i) =
Rational(-1)*t.coeff();
80 assert(t.is_single_variable());
88 for (std::size_t i = 0, n_rows = result_m.rows(), n_cols = result_m.cols(); i < n_rows; ++i) {
90 for (std::size_t j = 0; j < n_cols; ++j) {
92 if (!carl::is_zero(coeff)) {
107 std::vector<std::size_t> nbs, ubs, lbs;
110 for (Eigen::Index row = 0; row < constraints.rows(); ++row) {
111 if (carl::is_zero(constraints(row, col))) nbs.push_back(row);
112 else if (carl::is_positive(constraints(row, col))) ubs.push_back(row);
113 else lbs.push_back(row);
117 Eigen::Index n_new_constr = nbs.size() + (ubs.size() * lbs.size());
122 std::vector<Eigen::Index> emptyRows;
123 Eigen::Index row = 0;
124 for (; row < nbs.size(); ++row) {
125 newConstraints.row(row) = constraints.row(nbs[row]);
126 newConstants.row(row) = constants.row(nbs[row]);
129 for (; row < n_new_constr; ++row) {
130 std::size_t combinationIndex = row - nbs.size();
131 std::size_t lb_idx = combinationIndex / ubs.size();
132 std::size_t ub_idx = combinationIndex % ubs.size();
133 assert(lb_idx < lbs.size());
134 assert(ub_idx < ubs.size());
135 newConstraints.row(row) = (constraints(ubs[ub_idx], col) * constraints.row(lbs[lb_idx]))
136 - (constraints(lbs[lb_idx], col) * constraints.row(ubs[ub_idx]));
137 newConstants(row) = (constraints(ubs[ub_idx], col) * constants(lbs[lb_idx]))
138 - (constraints(lbs[lb_idx], col) * constants(ubs[ub_idx]));
140 if (newConstraints.row(row).isZero()) emptyRows.push_back(row);
143 assert(
vector_t(newConstraints.col(col)) == vector_t::Zero(newConstants.rows()));
146 if (!conservative) { newConstraints =
removeCol(newConstraints, col); }
152 return std::make_pair(newConstraints, newConstants);
157 const std::vector<std::size_t> &cols,
159 auto resultConstraints = constraints;
160 auto resultConstants = constants;
161 auto dimensionsToEliminate = cols;
162 while (!dimensionsToEliminate.empty()) {
163 std::cout <<
"starting iteration\n";
164 std::tie(resultConstraints, resultConstants) =
166 dimensionsToEliminate.front(), conservative);
170 for (
auto &idx: dimensionsToEliminate) {
171 if (idx > dimensionsToEliminate.front()) --idx;
174 dimensionsToEliminate.erase(std::begin(dimensionsToEliminate));
175 std::cout <<
"end iteration\n";
178 return std::make_pair(resultConstraints, resultConstants);
FormulaT eliminateQuantifiers()
std::pair< matrix_t, vector_t > eliminateCols(const matrix_t &constraints, const vector_t constants, const std::vector< std::size_t > &cols, bool conservative=true)
std::pair< matrix_t, vector_t > eliminateCol(const matrix_t &constraints, const vector_t &constants, std::size_t col, bool conservative=true)
FormulaT m_formula
The quantifiers to eliminate.
qe::util::VariableIndex m_var_idx
The logical representation of the solution space.
FormulasT remaining_constraints()
std::vector< carl::Variable > remaining_variables()
Eigen::Matrix< Rational, Eigen::Dynamic, Eigen::Dynamic > matrix_t
Eigen::Matrix< Rational, Eigen::Dynamic, 1 > vector_t
matrix_t removeCol(const matrix_t &original, std::size_t colIndex)
QuantifierType type(const std::pair< QuantifierType, std::vector< carl::Variable >> &p)
std::vector< carl::Variable > vars(const std::pair< QuantifierType, std::vector< carl::Variable >> &p)
carl::Formula< Poly > FormulaT
carl::MultivariatePolynomial< Rational > Poly
carl::Formulas< Poly > FormulasT
#define SMTRAT_LOG_DEBUG(channel, msg)
#define SMTRAT_STATISTICS_CALL(function)
std::size_t index(carl::Variable v) const
carl::Variable var(std::size_t i) const
void gather_variables(const FormulasT &fs)