dlinear  0.0.1
Delta-complete SMT solver for linear programming
Loading...
Searching...
No Matches
ReluConstraint.cpp
1
6#include "ReluConstraint.h"
7
8#include <utility>
9
10#include "dlinear/symbolic/LinearFormulaFlattener.h"
11#include "dlinear/util/Infinity.h"
12#include "dlinear/util/exception.h"
13
14namespace dlinear {
15
16const mpq_class ReluConstraint::zero{0};
17
19 : ReluConstraint(pa[LinearFormulaFlattener{pa.config()}.Flatten(relu_var - e == 0)], pa[relu_var == 0], relu_var,
20 relu_var - e) {}
21ReluConstraint::ReluConstraint(Variable active_var, Variable inactive_var, Variable relu_var, Expression active_soi)
23 &zero, nullptr, std::move(active_var), std::move(inactive_var), relu_var, std::move(active_soi), relu_var} {}
24
25void ReluConstraint::UpdateLowerBound(const mpq_class* const lower_bound) {
27 if (*lower_bound > 0) {
28 DLINEAR_ASSERT(state_ != PiecewiseConstraintState::INACTIVE, "constraint is already INACTIVE");
30 }
31}
32void ReluConstraint::UpdateUpperBound(const mpq_class* const upper_bound) {
34 if (*upper_bound <= 0) {
35 DLINEAR_ASSERT(state_ != PiecewiseConstraintState::ACTIVE, "constraint is already ACTIVE");
37 }
38}
39
40void ReluConstraint::EnableLiteral(const Variable&) {
41 // DLINEAR_ASSERT(inactive_var_.equal_to(var) || active_var_.equal_to(var), "Invalid variable");
42}
43
44std::set<LiteralSet> ReluConstraint::TightenBounds(BoundPreprocessor& preprocessor) {
45 std::set<LiteralSet> explanations;
46 preprocessor.PropagateBoundsPolynomial({active_var_, true}, theory_var_, explanations);
47 // The active assignment creates a conflict. Fix the constraint to the inactive state.
48 if (!explanations.empty()) {
49 DLINEAR_DEV_FMT("Failed active because {}", explanations);
50 explanations = preprocessor.EnableLiteral({inactive_var_, true});
51 }
52 // Both active and inactive assignments are unsat. Return the explanation for the latter.
53 if (!explanations.empty()) return explanations;
54
55 UpdateLowerBound(&preprocessor.theory_bounds().at(theory_var_).active_lower_bound());
56 UpdateUpperBound(&preprocessor.theory_bounds().at(theory_var_).active_upper_bound());
57 return {};
58}
59
61 DLINEAR_ASSERT(!fixed(), "ReluConstraint::Assumptions() should not be called on a fixed constraint");
62 const bool is_inactive = *upper_bound_ == 0;
63 return {{inactive_var_, is_inactive}, {active_var_, !is_inactive}};
64}
65
67 if (fixed()) {
68 const bool is_inactive = *upper_bound_ == 0;
69 return {{inactive_var_, !is_inactive}, {active_var_, is_inactive}};
70 }
71 return {};
72}
73
74std::ostream& ReluConstraint::Print(std::ostream& os) const {
75 os << "ReluConstraint(" << active_var_ << " or " << inactive_var_ << " ["
76 << (lower_bound_ ? lower_bound_->get_str() : "-inf") << ", " << (upper_bound_ ? upper_bound_->get_str() : "+inf")
77 << "])";
78 return os;
79}
80
81} // namespace dlinear
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.
std::set< LiteralSet > EnableLiteral(const Literal &lit)
Enable the literal lit.
bool PropagateBoundsPolynomial(const Literal &lit, const Variable &var_to_propagate, Explanations &explanations)
Propagate the bounds of the variables in the given formula.
Used by the PredicateAbstractor to ensure that all the theory literals are in the flattened standard ...
A piecewise linear constraint is a constraint that assumes different linear functions depending on th...
virtual void UpdateLowerBound(const mpq_class *lb)
Update the lower bounds of the constraint.
virtual void UpdateUpperBound(const mpq_class *ub)
Update the upper bounds of the constraint.
bool fixed() const
Check whether the constraint is fixed.
const mpq_class * lower_bound_
Lower bound of the constraint.
const Variable theory_var_
Theory variable .
const Variable active_var_
Boolean variable associated with the theory literal.
PiecewiseConstraintState state_
State of the constraint.
const mpq_class & lower_bound() const
Get read-only access to the lower bound of the constraint.
const mpq_class * upper_bound_
Upper bound of the constraint.
const Variable inactive_var_
Boolean variable associated with the theory literal.
const mpq_class & upper_bound() const
Get read-only access to the upper bound of the constraint.
Predicate abstraction is a method to convert a first-order logic formula into a Boolean formula.
A ReLU constraint is a kind of piecewise linear constraint.
ReluConstraint(const Variable &relu_var, const Expression &e, const PredicateAbstractor &pa)
Construct a new Relu Constraint object.
void UpdateUpperBound(const mpq_class *upper_bound) override
Update the upper bounds of the constraint.
LiteralSet Assumptions() const override
The assumptions used to guide the SAT solver towards the most promising sub-problems.
LiteralSet LearnedClauses() const override
Produce some learned clauses the SAT solver will use to prune the sub-problems this piecewise constra...
std::ostream & Print(std::ostream &os) const override
Print the constraint on the standard output.
std::set< LiteralSet > TightenBounds(BoundPreprocessor &preprocessor) override
Use the preprocessor to tighten the bounds of the constraint and hopefully fix its state to either ac...
void UpdateLowerBound(const mpq_class *lower_bound) override
Update the lower bounds of the constraint.
static const mpq_class zero
Zero constant all the pointers to the default lower bound will point to.
Represents a symbolic form of an expression.
Represents a symbolic variable.
Global namespace for the dlinear library.
@ ACTIVE
The constraint is active.
@ INACTIVE
The constraint is inactive.
std::set< Literal > LiteralSet
A set of literals.
Definition literal.h:28
STL namespace.