dlinear  0.0.1
Delta-complete SMT solver for linear programming
Loading...
Searching...
No Matches
SmtSolver.cpp
1
6#include "SmtSolver.h"
7
8#include <iostream>
9#include <utility>
10
11#include "dlinear/parser/mps/Driver.h"
12#include "dlinear/parser/onnx/Driver.h"
13#include "dlinear/parser/smt2/Driver.h"
14#include "dlinear/parser/vnnlib/Driver.h"
15#include "dlinear/solver/SmtSolverOutput.h"
16#include "dlinear/symbolic/symbolic.h"
17#include "dlinear/util/OptionValue.hpp"
18#include "dlinear/util/Timer.h"
19#include "dlinear/util/concepts.h"
20#include "dlinear/util/exception.h"
21#include "dlinear/util/logging.h"
22
23namespace dlinear {
24
25namespace {
26template <IsAnyOf<smt2::Smt2Driver, mps::MpsDriver, onnx::OnnxDriver, vnnlib::VnnlibDriver> T>
27inline bool ParseInputCore(const Config &config, Context &context) {
28 DLINEAR_DEBUG("SmtSolver::ParseSmt2");
29 T driver{context};
30 return config.read_from_stdin() ? driver.ParseStream(std::cin, "(stdin)") : driver.ParseFile(config.filename());
31}
32
33template <>
34inline bool ParseInputCore<onnx::OnnxDriver>(const Config &config, Context &context) {
35 DLINEAR_DEBUG("SmtSolver::ParseOnnx");
36 onnx::OnnxDriver driver{context};
37 return driver.ParseFile(config.onnx_file());
38}
39} // namespace
40
41SmtSolver::SmtSolver(const std::string &filename) : SmtSolver{Config{filename}} {}
42SmtSolver::SmtSolver(Config config) : config_{std::move(config)}, output_{config_}, context_{config_, &output_} {}
43
45 DLINEAR_TRACE("SmtSolver::CheckSat");
46 TimerGuard timer_guard{&output_.smt_solver_timer, true};
47
48 if (config_.skip_check_sat()) {
50 return output_;
51 }
52
54 DLINEAR_INFO("SmtSolver::CheckSat: Already solved");
55 return output_;
56 }
57
60 else
62
63 return output_;
64}
65
66bool SmtSolver::Verify(const Box &model) const { return context_.Verify(model); }
67
68const SmtSolverOutput &SmtSolver::Parse(const std::string &filename) {
69 config_.m_filename() = filename;
70 config_.m_read_from_stdin() = filename.empty();
71 return Parse();
72}
74 DLINEAR_TRACE("SmtSolver::Parse");
75
77 DLINEAR_INFO("SmtSolver::CheckSat: Already solved");
78 return output_;
79 }
80
81 if (!ParseInput()) DLINEAR_RUNTIME_ERROR("Error parsing the input");
82 return output_;
83}
84
85std::string SmtSolver::GetInfo(const std::string &key) const { return context_.GetInfo(key); }
86std::string SmtSolver::GetOption(const std::string &key) const { return context_.GetOption(key); }
87SmtResult SmtSolver::GetExpected() const {
88 std::string status = context_.GetInfo(":status");
89 if (status == "sat") return SmtResult::SAT;
90 if (status == "unsat") return SmtResult::UNSAT;
91 return SmtResult::UNKNOWN;
92}
93
94void SmtSolver::Assert(const Formula &f) {
95 DLINEAR_TRACE_FMT("SmtSolver::Assert: {}", f);
97 for (const Variable &v : f.GetFreeVariables()) context_.DeclareVariable(v);
99}
100
102 DLINEAR_TRACE("SmtSolver::ParseInput");
103 if (!config_.read_from_stdin() && config_.filename().empty()) {
104 DLINEAR_INFO("SmtSolver::ParseInput: No input file provided");
105 return true;
106 } else if (config_.read_from_stdin() && config_.filename().empty()) {
107 DLINEAR_INFO("SmtSolver::ParseInput: Reading from stdin");
108 } else {
109 DLINEAR_INFO_FMT("SmtSolver::ParseInput: Reading from file: {}", config_.filename());
110 }
111 TimerGuard timer_guard{&output_.smt_solver_timer, true};
112
113 switch (config_.actual_format()) {
115 return ParseInputCore<smt2::Smt2Driver>(config_, context_);
117 return ParseInputCore<mps::MpsDriver>(config_, context_);
119 if (!ParseInputCore<onnx::OnnxDriver>(config_, context_)) return false;
120 return ParseInputCore<vnnlib::VnnlibDriver>(config_, context_);
121 default:
122 DLINEAR_UNREACHABLE();
123 }
124}
125
126} // 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
@ VNNLIB
VNNLIB format.
OptionValue< std::string > & m_filename()
Get read-write access to the filename extension of the configuration.
Definition Config.h:109
OptionValue< bool > & m_read_from_stdin()
Get read-only access to the read_from_stdin parameter of the configuration.
Definition Config.h:197
Format actual_format() const
Get read-only access to the actual format parameter of the configuration.
Definition Config.cpp:33
const bool & skip_check_sat() const
Get read-write access to the skip_check_sat parameter of the configuration.
Definition Config.h:206
const bool & read_from_stdin() const
Get read-write access to the read_from_stdin parameter of the configuration.
Definition Config.h:197
std::string filename() const
Get read-only access to the filename parameter of the configuration.
Definition Config.h:105
bool have_objective() const
Check whether or not there is an objective function (which may be zero) to optimize.
Definition Context.cpp:72
std::string GetInfo(const std::string &key) const
Get the info key.
Definition Context.cpp:58
void Assert(const Formula &f)
Assert a formula f.
Definition Context.cpp:24
std::string GetOption(const std::string &key) const
Get the option key.
Definition Context.cpp:64
bool Verify(const Box &model) const
Check whether the model satisfies all the assertions loaded in the context.
Definition Context.cpp:74
void DeclareVariable(const Variable &v, bool is_model_variable=true)
Declare a variable v.
Definition Context.cpp:31
LpResult CheckOpt(mpq_class *obj_lo, mpq_class *obj_up)
Check the satisfiability of the asserted formulas, and (where possible) optimizes an objective functi...
Definition Context.cpp:30
SatResult CheckSat(mpq_class *actual_precision)
Check the satisfiability of the asserted formulas, and sets actual_precision to the actual max infeas...
Definition Context.cpp:29
This class provides an easy interface for using the underling solver.
Definition SmtSolver.h:41
SmtSolver(Config config=Config{})
Construct a new SmtSolver object with the given config .
Definition SmtSolver.cpp:42
const SmtSolverOutput & Parse()
Parse the problem from the input indicated in the configuration.
Definition SmtSolver.cpp:73
Context context_
Context obtained from the input file and passed to the SAT and SMT solvers.
Definition SmtSolver.h:128
SmtSolverOutput output_
Output of the solver.
Definition SmtSolver.h:127
const SmtSolverOutput & CheckSat()
Check the satisfiability of the current context.
Definition SmtSolver.cpp:44
bool ParseInput()
Parse the problem from the input indicated in the configuration.
void Assert(const Formula &f)
Assert a formula to the current context.
Definition SmtSolver.cpp:94
bool Verify(const Box &model) const
Check whether the model satisfies all the assertions loaded in the context.
Definition SmtSolver.cpp:66
Config config_
Configuration of the solver.
Definition SmtSolver.h:126
The TimeGuard wraps a timer object and pauses it when the guard object is destructed.
Definition Timer.h:129
Represents a symbolic form of a first-order logic formula.
const Variables & GetFreeVariables() const
Gets free variables (unquantified variables).
Represents a symbolic variable.
Global namespace for the dlinear library.
SmtResult
SmtSolver Result based on the result of the solver.
@ UNSAT
The problem is unsatisfiable.
@ SAT
The problem is satisfiable.
@ UNKNOWN
Could not determine satisfiability.
@ SKIP_SAT
The user asked to skip the satisfiability check.
@ UNSOLVED
The solver has not yet been run.
STL namespace.
Data struct containing the output of the solver, such as the result of the computation as well as som...
mpq_class lower_bound
Lower bound of the result.
mpq_class upper_bound
Upper bound of the result.
mpq_class actual_precision
Actual precision of the computation. Always <= than precision.
Timer smt_solver_timer
Timer keeping track of the time spent in the SMT solver.
SmtResult result
Result of the computation.