dlinear  0.0.1
Delta-complete SMT solver for linear programming
Loading...
Searching...
No Matches
TheorySolver.h
1
7#pragma once
8
9#include <map>
10#include <set>
11#include <string>
12#include <tuple>
13#include <vector>
14
15#include "dlinear/libs/libgmp.h"
16#include "dlinear/solver/BoundIterator.h"
17#include "dlinear/solver/BoundPreprocessor.h"
18#include "dlinear/solver/LpColBound.h"
19#include "dlinear/solver/SatResult.h"
20#include "dlinear/symbolic/PredicateAbstractor.h"
21#include "dlinear/symbolic/literal.h"
22#include "dlinear/symbolic/symbolic.h"
23#include "dlinear/util/Box.h"
24#include "dlinear/util/Config.h"
25#include "dlinear/util/Stats.h"
26
27namespace dlinear {
28
38 public:
39 using Bound = std::tuple<const Variable &, LpColBound, const mpq_class &>;
41 using Explanations = std::set<LiteralSet>;
54 const std::string &class_name = "TheorySolver");
55 virtual ~TheorySolver() = default;
56
62 virtual void AddLiterals();
71 virtual void AddLiteral(const Variable &formula_var, const Formula &formula) = 0;
80 virtual Explanations PreprocessFixedLiterals(const LiteralSet &fixed_literals);
85 virtual void AddVariable(const Variable &var) = 0;
91 Explanations EnableLiterals(std::span<const Literal> theory_literals);
107 virtual Explanations EnableLiteral(const Literal &lit) = 0;
108
110 [[nodiscard]] const Box &model() const;
112 [[nodiscard]] const Config &config() const { return config_; }
114 [[nodiscard]] const PredicateAbstractor &predicate_abstractor() const { return predicate_abstractor_; }
116 [[nodiscard]] const std::map<Variable::Id, int> &var_to_theory_col() const { return var_to_theory_col_; }
118 [[nodiscard]] const std::vector<Variable> &theory_col_to_var() const { return theory_col_to_var_; }
120 [[nodiscard]] const std::map<Variable::Id, int> &lit_to_theory_row() const { return lit_to_theory_row_; }
122 [[nodiscard]] const std::vector<Literal> &theory_row_to_lit() const { return theory_row_to_lit_; }
124 [[nodiscard]] const BoundVectorMap &theory_bounds() const { return preprocessor_.theory_bounds(); }
126 [[nodiscard]] const BoundVectorMap &fixed_theory_bounds() const { return fixed_preprocessor_.theory_bounds(); }
128 [[nodiscard]] const BoundPreprocessor &preprocessor() const { return preprocessor_; }
130 [[nodiscard]] BoundPreprocessor &m_preprocessor() { return preprocessor_; }
132 [[nodiscard]] const BoundPreprocessor &fixed_preprocessor() const { return fixed_preprocessor_; }
136 [[nodiscard]] std::size_t theory_row_count() const { return theory_row_to_lit_.size(); }
138 [[nodiscard]] std::size_t theory_col_count() const { return theory_col_to_var_.size(); }
140 [[nodiscard]] std::set<Literal> enabled_literals() const;
141
156 SatResult CheckSat(const Box &box, mpq_class *actual_precision, std::set<LiteralSet> &explanations);
171 virtual SatResult CheckSatCore(mpq_class *actual_precision, std::set<LiteralSet> &explanations) = 0;
172
184 virtual void Consolidate(const Box &box);
185
193 virtual void Reset();
194
199 [[nodiscard]] const IterationStats &stats() const { return stats_; }
200
201#ifndef NDEBUG
202 void DumpEnabledLiterals();
203#endif
204
205 protected:
210 UPPER_BOUND_VIOLATION, //< The upper bound is involved in the infeasibility
211 };
212
213 void BoundsToTheoryRows(const Variable &var, const mpq_class &value, std::set<int> &theory_rows) const;
214 void BoundsToTheoryRows(const Variable &var, BoundViolationType type, std::set<int> &bound_idxs) const;
221 virtual void UpdateModelSolution() = 0;
227 virtual void UpdateModelBounds() = 0;
235 virtual void UpdateExplanation(LiteralSet &explanation) = 0;
243 void UpdateExplanations(Explanations &explanations);
244
246
249
251
252 std::map<Variable::Id, int> var_to_theory_col_;
255 std::vector<Variable> theory_col_to_var_;
258 std::map<Variable::Id, int> lit_to_theory_row_;
261 std::vector<Literal> theory_row_to_lit_;
265 std::vector<bool> theory_rows_state_;
266
268
273 //< It will be reset to the @ref fixed_preprocessor_ at every iteration.
274
276
278};
279
280} // namespace dlinear
281
282#ifdef DLINEAR_INCLUDE_FMT
283
284#include "dlinear/util/logging.h"
285
286OSTREAM_FORMATTER(dlinear::TheorySolver::Bound)
287
288#endif
BoundIterator class.
This class uses some basic algebraic operations to preprocess the constraints and identify violations...
const BoundVectorMap & theory_bounds() const
Get read-only access to the bounds of the variables in the LP solver of the BoundPreprocessor.
Collection of variables with associated intervals.
Definition Box.h:31
Simple dataclass used to store the configuration of the program.
Definition Config.h:38
Dataclass collecting statistics about some operation or process.
Definition Stats.h:71
Predicate abstraction is a method to convert a first-order logic formula into a Boolean formula.
Base class for theory solvers.
virtual void UpdateModelSolution()=0
Update the model with the solution obtained from the LP solver.
const PredicateAbstractor & predicate_abstractor() const
Get read-only access to the predicate abstractor of the TheorySolver.
virtual void Reset()
Reset the linear problem.
BoundPreprocessor & m_preprocessor()
Get read-write access to the bound preprocessor of the TheorySolver.
BoundPreprocessor preprocessor_
Preprocessor for the bounds.
virtual SatResult CheckSatCore(mpq_class *actual_precision, std::set< LiteralSet > &explanations)=0
Check the satisfiability of the theory.
const BoundVectorMap & theory_bounds() const
Get read-only access to the bounds of each theory variable of the TheorySolver.
Box model_
Model produced by the theory solver.
std::size_t theory_col_count() const
Get read-only access to the number of columns of the TheorySolver.
virtual void UpdateModelBounds()=0
Update each variable in the model with the bounds passed to the theory solver.
std::vector< Literal > theory_row_to_lit_
Theory row ⇔ Literal The row is the constraint used by the theory solver.
const std::map< Variable::Id, int > & lit_to_theory_row() const
Get read-only access to the map of literals to the theory rows of the TheorySolver.
const BoundPreprocessor & preprocessor() const
Get read-only access to the bound preprocessor of the TheorySolver.
virtual void AddLiteral(const Variable &formula_var, const Formula &formula)=0
Add a Literal to the theory solver.
TheorySolver(const PredicateAbstractor &predicate_abstractor, const std::string &class_name="TheorySolver")
Construct a new Theory Solver object.
const Config & config() const
Get read-only access to the configuration of the TheorySolver.
virtual Explanations PreprocessFixedLiterals(const LiteralSet &fixed_literals)
Add the fixed literals to the theory solver.
std::map< Variable::Id, int > lit_to_theory_row_
Literal ⇔ theory row.
std::vector< bool > theory_rows_state_
Whether each theory row is enabled or not.
BoundViolationType
Enum used to describe how the bounds on a variable participate in the infeasibility result of an LP p...
@ NO_BOUND_VIOLATION
The bounds of the variable have no role in the infeasibility.
@ LOWER_BOUND_VIOLATION
The lower bound is involved in the infeasibility.
const std::vector< Variable > & theory_col_to_var() const
Get read-only access to the map of theory columns to the variables of the TheorySolver.
BoundPreprocessor & m_fixed_preprocessor()
Get read-write access to the fixed bound preprocessor of the TheorySolver.
const BoundVectorMap & fixed_theory_bounds() const
Get read-only access to the fixed bounds of each theory variable of the TheorySolver.
const Config & config_
Configuration of the theory solver.
std::set< Literal > enabled_literals() const
Get read-only access to the vector of enabled literals of the TheorySolver.
virtual void UpdateExplanation(LiteralSet &explanation)=0
Use the result from the theory solver to update the explanation with the conflict that has been detec...
std::set< LiteralSet > Explanations
Set of explanations of the conflict.
virtual void AddVariable(const Variable &var)=0
Add a variable (column) to the theory solver.
const PredicateAbstractor & predicate_abstractor_
Predicate abstractor used to create the theory solver.
std::size_t theory_row_count() const
Get read-only access to the number of rows of the TheorySolver.
Explanations EnableLiterals(std::span< const Literal > theory_literals)
Activate the literals that have previously been added to the theory solver.
bool is_consolidated_
Whether the solver has been consolidated.
const Box & model() const
Get read-only access to the model that satisfies all the constraints of the TheorySolver.
virtual void AddLiterals()
Add a vector of literals to the theory solver.
std::map< Variable::Id, int > var_to_theory_col_
Variable ⇔ theory column.
const std::vector< Literal > & theory_row_to_lit() const
Get read-only access to the map of theory rows to the literals of the TheorySolver.
BoundPreprocessor fixed_preprocessor_
Preprocessor for the bounds. Only computed on fixed literal theories.
virtual Explanations EnableLiteral(const Literal &lit)=0
Activate the literal that had previously been added to the theory solver.
const BoundPreprocessor & fixed_preprocessor() const
Get read-only access to the fixed bound preprocessor of the TheorySolver.
void UpdateExplanations(Explanations &explanations)
Use the result from the theory solver to update the explanations with the conflict that has been dete...
std::tuple< const Variable &, LpColBound, const mpq_class & > Bound
Bound on the variable.
virtual void Consolidate(const Box &box)
Consolidate the solver.
const IterationStats & stats() const
Get the statistics of the theory solver.
const std::map< Variable::Id, int > & var_to_theory_col() const
Get read-only access to the map of variables to the theory columns of the TheorySolver.
std::vector< Variable > theory_col_to_var_
Theory column ⇔ Variable.
SatResult CheckSat(const Box &box, mpq_class *actual_precision, std::set< LiteralSet > &explanations)
Check the satisfiability of the theory.
IterationStats stats_
Statistics of the theory solver.
Represents a symbolic form of a first-order logic formula.
Represents a symbolic variable.
Global namespace for the dlinear library.
SatResult
Result of running the theory solver over the literals decided by the SAT solver.
Definition SatResult.h:17
std::set< Literal > LiteralSet
A set of literals.
Definition literal.h:28
A literal is a variable with an associated truth value, indicating whether it is true or false.
Definition literal.h:24