SMT-RAT  24.02
Toolbox for Strategic and Parallel Satisfiability-Modulo-Theories Solving
onecell.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <set>
6 
11 
14 
15 #include <carl-formula/formula/functions/Visit.h>
16 
17 namespace smtrat::mcsat::onecell {
18 
19 /**
20  * An MCSAT-style single cell explanation function.
21  *
22  * A set of constraints is called infeasible w.r.t. an assignment if the defining polynomials are univariate under the sample and there does not exists a value for the unassigned variable that satisfies all constraints.
23  *
24  * This method eliminates the unassigned variable using @ref get_level_covering or @ref get_delineation and then constructs a single cell in the assigned variables level by level.
25  *
26  * @param constraints Atoms of the same level such that @ref sample cannot be extended for the highest variable without making one atom false. Note that this property is not checked.
27  * @param sample A sample such that all but the highest variable in @ref constraints are assigned.
28  * @return A set of constraints whose conjunction describes an unsatisfying cell that can be concluded from the input constraints.
29  */
30 template<typename Settings>
31 std::optional<cadcells::DNF> onecell(const std::vector<cadcells::Atom>& constraints, const cadcells::Polynomial::ContextType& context, const cadcells::Assignment& sample) {
32  #ifdef SMTRAT_DEVOPTION_Statistics
33  cadcells::OCApproximationStatistics& stats = cadcells::OCApproximationStatistics::get_instance();
34  stats.newCell();
35  #endif
36  SMTRAT_STATISTICS_CALL(cadcells::statistics().set_max_level(sample.size()+1));
37 
38  bool consider_approximation = Settings::use_approximation && cadcells::representation::approximation::ApxCriteria::cell(constraints);
39  #ifdef SMTRAT_DEVOPTION_Statistics
40  if (consider_approximation) stats.approximationConsidered();
41  #endif
42 
43  SMTRAT_LOG_FUNC("smtrat.mcsat.onecell", constraints << ", " << context << ", " << sample);
46 
47  // if all input constraints are "strict" (their unsat cells can be closed), then we can close the cell, i.e. make the bounds weak
48  bool constraints_all_strict = std::find_if(constraints.begin(), constraints.end(), [&](const auto& f) {
49  if (std::holds_alternative<cadcells::Constraint>(f)) return !carl::is_strict(std::get<cadcells::Constraint>(f).relation());
50  else if (std::holds_alternative<cadcells::VariableComparison>(f)) {
51  auto vc = std::get<cadcells::VariableComparison>(f);
52  // Negated VariableComparisons evaluate to true where its MultivariateRoot is undefined. Thus, their unsatisfying region is never closed!
53  if (vc.negated()) return true;
54  if (!carl::is_strict(vc.relation())) return true;
55  // If the VariableComparison is not well-defined at the given sample, then the unsatisfying region is not closed, as it might get well-defined at the boundary.
56  auto mv = std::get<carl::MultivariateRoot<cadcells::Polynomial>>(vc.value());
57  auto mvp = pool(mv.poly());
58  if (proj.is_nullified(sample, mvp) || proj.real_roots(sample, mvp).size() < mv.k()) return true;
59  return false;
60  }
61  assert(false);
62  return false;
63  }) == constraints.end();
64  SMTRAT_LOG_TRACE("smtrat.mcsat.onecell", "constraints_all_strict = " << constraints_all_strict);
65 
66  auto derivation = cadcells::algorithms::get_level_covering<typename Settings::op, Settings::covering_heuristic>(proj, constraints, sample);
67  SMTRAT_LOG_TRACE("smtrat.mcsat.onecell", "Polynomials: " << pool);
68  if (!derivation) {
69  return std::nullopt;
70  }
71 
72  cadcells::DNF description;
73  while ((*derivation)->level() > 0) {
74  std::optional<std::pair<carl::Variable, cadcells::datastructures::SymbolicInterval>> lvl;
75  if constexpr (Settings::use_approximation) {
76  if (consider_approximation && cadcells::representation::approximation::ApxCriteria::level((*derivation)->level())) {
77  lvl = cadcells::algorithms::get_interval<typename Settings::op, Settings::cell_apx_heuristic>(*derivation);
78  } else {
79  lvl = cadcells::algorithms::get_interval<typename Settings::op, Settings::cell_heuristic>(*derivation);
80  }
81  } else {
82  lvl = cadcells::algorithms::get_interval<typename Settings::op, Settings::cell_heuristic>(*derivation);
83  }
84  SMTRAT_LOG_TRACE("smtrat.mcsat.onecell", "Polynomials: " << pool);
85  if (!lvl) {
86  return std::nullopt;
87  }
88  if (Settings::exploit_strict_constraints && constraints_all_strict) {
89  lvl->second.set_to_closure();
90  }
91  auto res = cadcells::helper::to_formula(proj.polys(), lvl->first, lvl->second);
92  if (constraints_all_strict) {
93  for (const auto& a : res) {
94  for (const auto& b : a) {
95  if (std::holds_alternative<cadcells::VariableComparison>(b)) {
96  constraints_all_strict = false;
97  SMTRAT_LOG_TRACE("smtrat.mcsat.onecell", "constraints_all_strict = false due to " << b);
98  break;
99  }
100  }
101  if (!constraints_all_strict) break;
102  }
103  }
104  description.insert(description.end(), res.begin(), res.end());
105  proj.clear_assignment_cache((*derivation)->sample());
106  (*derivation) = (*derivation)->underlying().sampled_ref();
107  proj.clear_cache((*derivation)->level()+2);
108  }
109  proj.clear_assignment_cache(cadcells::empty_assignment);
110 
111  return description;
112 }
113 
114 }
Encapsulates all computations on polynomials.
Definition: projections.h:46
static bool cell(const std::vector< Atom > &constraints)
Definition: criteria.h:112
DNF to_formula(const datastructures::PolyPool &pool, carl::Variable main_var, const datastructures::SymbolicInterval &c)
Converts a datastructures::SymbolicInterval to a DNF.
carl::Assignment< RAN > Assignment
Definition: common.h:25
std::vector< Disjunction > DNF
Definition: common.h:23
static const Assignment empty_assignment
Definition: common.h:27
std::optional< cadcells::DNF > onecell(const std::vector< cadcells::Atom > &constraints, const cadcells::Polynomial::ContextType &context, const cadcells::Assignment &sample)
An MCSAT-style single cell explanation function.
Definition: onecell.h:31
#define SMTRAT_LOG_TRACE(channel, msg)
Definition: logging.h:36
#define SMTRAT_LOG_FUNC(channel, args)
Definition: logging.h:38
#define SMTRAT_STATISTICS_CALL(function)
Definition: Statistics.h:23