dlinear  0.0.1
Delta-complete SMT solver for linear programming
Loading...
Searching...
No Matches
ContextImpl.h
1
7#pragma once
8
9#include <memory>
10#include <optional>
11#include <string>
12#include <unordered_map>
13#include <unordered_set>
14#ifndef NDEBUG
15#include <set>
16#endif
17
18#include "dlinear/libs/libgmp.h"
19#include "dlinear/solver/Context.h"
20#include "dlinear/solver/Logic.h"
21#include "dlinear/solver/LpResult.h"
22#include "dlinear/solver/PiecewiseLinearConstraint.h"
23#include "dlinear/solver/SatResult.h"
24#include "dlinear/solver/SatSolver.h"
25#include "dlinear/solver/SmtSolverOutput.h"
26#include "dlinear/solver/TheorySolver.h"
27#include "dlinear/symbolic/IfThenElseEliminator.h"
28#include "dlinear/symbolic/PredicateAbstractor.h"
29#include "dlinear/symbolic/literal.h"
30#include "dlinear/symbolic/symbolic.h"
31#include "dlinear/util/Box.h"
32#include "dlinear/util/Config.h"
33#include "dlinear/util/ScopedVector.hpp"
34
35namespace dlinear {
36
43 public:
48 explicit Impl(Config &config, SmtSolverOutput *output = nullptr);
49 Impl(const Impl &) = delete;
50 Impl(Impl &&) = delete;
51 Impl &operator=(const Impl &) = delete;
52 Impl &operator=(Impl &&) = delete;
53
60 void Assert(const Formula &f);
61
84 void AssertPiecewiseLinearFunction(const Variable &var, const Formula &cond, const Expression &active,
85 const Expression &inactive);
86
87 const PiecewiseLinearConstraint &AddGuidedConstraint(std::unique_ptr<PiecewiseLinearConstraint> &&constraint);
88
90 void Pop();
92 void Push();
101 SatResult CheckSat(mpq_class *precision);
109 LpResult CheckOpt(mpq_class *obj_lo, mpq_class *obj_up);
118 void DeclareVariable(const Variable &v, bool is_model_variable);
125 void SetDomain(const Variable &v, const Expression &lb, const Expression &ub);
130 void Minimize(const Expression &obj_function);
135 void Maximize(const Expression &obj_function);
141 void SetInfo(const std::string &key, const std::string &val);
147 [[nodiscard]] std::string GetInfo(const std::string &key) const;
154 void SetInterval(const Variable &v, const mpq_class &lb, const mpq_class &ub);
159 void SetLogic(Logic logic);
165 void SetOption(const std::string &key, const std::string &val);
171 [[nodiscard]] std::string GetOption(const std::string &key) const;
176 const Config &config() const { return config_; }
182 const ScopedVector<Formula> &assertions() const;
187 Box &box() { return boxes_.last(); }
192 const Box &box() const { return boxes_.last(); }
197 const Box &get_model() { return model_; }
198
201 [[nodiscard]] const SmtSolverOutput *solver_output() const { return output_; }
202 SmtSolverOutput *m_solver_output() { return output_; }
208 bool have_objective() const;
214 bool is_max() const;
223 [[nodiscard]] bool Verify(const Box &model) const;
224
225 private:
230 std::unique_ptr<TheorySolver> GetTheorySolver();
235 std::unique_ptr<SatSolver> GetSatSolver();
236
246 void AddToBox(const Variable &v);
247
255 void LearnExplanation(const LiteralSet &explanation);
263 void LearnExplanations(const TheorySolver::Explanations &explanations);
264
273 SatResult CheckSatCore(mpq_class *actual_precision);
283 LpResult CheckOptCore(mpq_class *obj_lo, mpq_class *obj_up);
284
285 void MinimizeCore(const Expression &obj_expr);
286
291 void MarkModelVariable(const Variable &v);
292
299 bool IsModelVariable(const Variable &v) const;
300
309 Box ExtractModel(const Box &box) const;
310
319 void UpdateAndPrintOutput(SmtResult smt_result) const;
320
323
324 std::optional<Logic> logic_;
325 std::unordered_map<std::string, std::string> info_;
326 std::unordered_map<std::string, std::string> option_;
327
330 std::unordered_set<Variable::Id> model_variables_;
331
333
335 bool is_max_;
336
337 std::vector<std::unique_ptr<PiecewiseLinearConstraint>>
340
343 // TODO: these could become templated classes for added efficiency
344 const std::unique_ptr<SatSolver> sat_solver_;
345 const std::unique_ptr<TheorySolver> theory_solver_;
346
347#ifndef NDEBUG
348 std::set<LiteralSet> explanations_so_far;
349#endif
350};
351
352} // namespace dlinear
Collection of variables with associated intervals.
Definition Box.h:31
Simple dataclass used to store the configuration of the program.
Definition Config.h:38
The context juggles between the SAT solver and the theory solver, in order to produce a model.
Definition ContextImpl.h:42
const Config & config() const
Get the configuration of the context.
void Assert(const Formula &f)
Assert a formula f.
std::unordered_map< std::string, std::string > option_
Key-value pairs of options.
Box ExtractModel(const Box &box) const
Extract a model from the box.
Box model_
Stores the result of the latest checksat. If the checksat result was UNSAT, the model is empty.
std::unordered_set< Variable::Id > model_variables_
Set of model variables.
void Push()
Push the current set of assertions to the stack.
void SetDomain(const Variable &v, const Expression &lb, const Expression &ub)
Set a domain for the variable v, restricting it to the interval .
SatResult CheckSat(mpq_class *precision)
Check the satisfiability of the asserted formulas, and sets actual_precision to the actual max infeas...
void UpdateAndPrintOutput(SmtResult smt_result) const
Update the output_ with the last smt_result .
bool have_objective_
Keeps track of whether or not there is an objective function.
bool have_objective() const
Check whether or not there is an objective function (which may be zero) to optimize.
SmtSolverOutput *const output_
Output of the SMT solver. Stores the result of the checksat and some statistics.
void SetOption(const std::string &key, const std::string &val)
Set an option key with a value val.
const std::unique_ptr< TheorySolver > theory_solver_
Theory solver.
const Box & get_model()
Get a representation of a model computed by the solver in response to the last invocation of the chec...
void LearnExplanations(const TheorySolver::Explanations &explanations)
The TheorySolver found a number of conflicts and the explanations are the set of literals that are re...
void SetInterval(const Variable &v, const mpq_class &lb, const mpq_class &ub)
Set the interval of v to in the current box (top one in boxes_).
bool Verify(const Box &model) const
Check whether the model satisfies all the assertions loaded in the context.
void SetLogic(Logic logic)
Set the current logic to logic.
const PredicateAbstractor & predicate_abstractor() const
Get read-only access to the predicate_abstractor of the context.
const ScopedVector< Formula > & assertions() const
Get the the asserted formulas.
std::unordered_map< std::string, std::string > info_
Key-value pairs of information.
Box & box()
Get the current active box from the top of the stack_ of boxes.
void MarkModelVariable(const Variable &v)
Mark the variable v as a model variable.
std::optional< Logic > logic_
SMT Logic of the context. Must be among the supported logics.
std::vector< std::unique_ptr< PiecewiseLinearConstraint > > pl_constraints_
Special constraints that can be used to guide the SAT solver towards a possible SAT assignment.
ScopedVector< Formula > stack_
Stack of asserted formulas.
std::string GetInfo(const std::string &key) const
Get the info key.
void DeclareVariable(const Variable &v, bool is_model_variable)
Declare a variable v.
IfThenElseEliminator ite_eliminator_
Eliminates if-then-else expressions from the formula.
std::string GetOption(const std::string &key) const
Get the option key.
void SetInfo(const std::string &key, const std::string &val)
Set an info key with a value val.
bool is_max() const
Check whether or not the objective function (if present) is a maximization.
void Minimize(const Expression &obj_function)
Assert a formula minimizing a cost function f.
void AddToBox(const Variable &v)
Add the variable v to the current box.
void LearnExplanation(const LiteralSet &explanation)
The TheorySolver found a conflict and the explanation is the set of literals that are responsible.
SatResult CheckSatCore(mpq_class *actual_precision)
Check the satisfiability of the current set of assertions.
ScopedVector< Box > boxes_
Stack of boxes. The top one is the current box.
LpResult CheckOpt(mpq_class *obj_lo, mpq_class *obj_up)
Check the satisfiability of the asserted formulas, and (where possible) optimizes an objective functi...
const std::unique_ptr< SatSolver > sat_solver_
SAT solver.
void Maximize(const Expression &obj_function)
Assert a formula maximizing a cost function f.
std::unique_ptr< SatSolver > GetSatSolver()
Get the correct sat solver subclass based on the configuration.
const Box & box() const
Get the current active box from the top of the stack_ of boxes.
bool IsModelVariable(const Variable &v) const
Check if the variable v is a model variable.
Impl(Config &config, SmtSolverOutput *output=nullptr)
Construct a context with config.
void Pop()
Pop the top of the stack of assertions.
void AssertPiecewiseLinearFunction(const Variable &var, const Formula &cond, const Expression &active, const Expression &inactive)
Assert a piecewise linear function with two possible branches.
LpResult CheckOptCore(mpq_class *obj_lo, mpq_class *obj_up)
Check the satisfiability of the asserted formulas, and (where possible) optimizes an objective functi...
std::set< LiteralSet > explanations_so_far
Set of explanations that have been learned so far.
PredicateAbstractor predicate_abstractor_
Converts the theory literals to boolean variables.
Config & config_
Configuration of the context. It could be modified by the problem instance.
bool is_max_
Keeps track of whether or not the objective function is being maximized.
std::unique_ptr< TheorySolver > GetTheorySolver()
Get the correct theory solver subclass based on the configuration.
const Box & model() const
Get a representation of a model computed by the solver in response to the last invocation of the chec...
Definition Context.cpp:67
IfThenElseEliminator class.
A piecewise linear constraint is a constraint that assumes different linear functions depending on th...
Predicate abstraction is a method to convert a first-order logic formula into a Boolean formula.
std::set< LiteralSet > Explanations
Set of explanations of the conflict.
Represents a symbolic form of an expression.
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
Logic
The SMT-LIB logic the SMT2 file is using.
Definition Logic.h:17
SmtResult
SmtSolver Result based on the result of the solver.
LpResult
Result of running the LP solver over the input problem.
Definition LpResult.h:14
std::set< Literal > LiteralSet
A set of literals.
Definition literal.h:28
Data struct containing the output of the solver, such as the result of the computation as well as som...