dlinear  0.0.1
Delta-complete SMT solver for linear programming
Loading...
Searching...
No Matches
LeakyReluConstraint.cpp
1
6#include "LeakyReluConstraint.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 LeakyReluConstraint::zero{0};
17
18LeakyReluConstraint::LeakyReluConstraint(const Variable& relu_var, const Expression& e, const float alpha,
19 const PredicateAbstractor& pa)
20 : LeakyReluConstraint(pa[LinearFormulaFlattener{pa.config()}.Flatten(relu_var - e == 0)],
21 pa[LinearFormulaFlattener{pa.config()}.Flatten(relu_var - alpha * e == 0)], relu_var,
22 relu_var - e, relu_var - alpha * e) {}
24 Expression active_soi, Expression inactive_soi)
26 nullptr,
27 std::move(active_var),
28 std::move(inactive_var),
29 relu_var,
30 std::move(active_soi),
31 std::move(inactive_soi)} {}
32
33void LeakyReluConstraint::UpdateLowerBound(const mpq_class* const lower_bound) {
35 if (*lower_bound > 0) {
36 DLINEAR_ASSERT(state_ != PiecewiseConstraintState::INACTIVE, "constraint is already INACTIVE");
38 }
39}
40void LeakyReluConstraint::UpdateUpperBound(const mpq_class* const upper_bound) {
42 if (*upper_bound <= 0) {
43 DLINEAR_ASSERT(state_ != PiecewiseConstraintState::ACTIVE, "constraint is already ACTIVE");
45 }
46}
47
48void LeakyReluConstraint::EnableLiteral(const Variable&) {
49 // DLINEAR_ASSERT(inactive_var_.equal_to(var) || active_var_.equal_to(var), "Invalid variable");
50}
51
52std::set<LiteralSet> LeakyReluConstraint::TightenBounds(BoundPreprocessor& preprocessor) {
53 std::set<LiteralSet> active_explanations;
54 std::set<LiteralSet> inactive_explanations;
55
56 const Bound inactive_upper_bound{&zero, LpColBound::U, {inactive_var_, true}, {}};
57 const Bound active_lower_bound{&zero, LpColBound::L, {active_var_, true}, {}};
58 preprocessor.PropagateBoundsPolynomial({inactive_var_, true}, theory_var_, {inactive_upper_bound},
59 inactive_explanations);
60 preprocessor.PropagateBoundsPolynomial({active_var_, true}, theory_var_, {active_lower_bound}, active_explanations);
61 if (!active_explanations.empty() && !inactive_explanations.empty()) {
62 return active_explanations;
63 }
64
65 DLINEAR_DEV_FMT("Bounds: [{} {}]", preprocessor.theory_bounds().at(theory_var_).active_lower_bound(),
66 preprocessor.theory_bounds().at(theory_var_).active_upper_bound());
67 UpdateLowerBound(&preprocessor.theory_bounds().at(theory_var_).active_lower_bound());
68 UpdateUpperBound(&preprocessor.theory_bounds().at(theory_var_).active_upper_bound());
69 return {};
70}
71
73 DLINEAR_ASSERT(!fixed(), "LeakyReluConstraint::Assumptions() should not be called on a fixed constraint");
74 const bool is_inactive = *upper_bound_ == 0;
75 return {{inactive_var_, is_inactive}, {active_var_, !is_inactive}};
76}
77
79 if (fixed()) {
80 const bool is_inactive = *upper_bound_ == 0;
81 return {{inactive_var_, !is_inactive}, {active_var_, is_inactive}};
82 }
83 return {};
84}
85
86std::ostream& LeakyReluConstraint::Print(std::ostream& os) const {
87 os << "LeakyReluConstraint(" << active_var_ << " or " << inactive_var_ << " ["
88 << (lower_bound_ ? std::to_string(lower_bound_->get_d()) : "-inf") << ", "
89 << (upper_bound_ ? std::to_string(upper_bound_->get_d()) : "+inf") << "])";
90 return os;
91}
92
93} // 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.
bool PropagateBoundsPolynomial(const Literal &lit, const Variable &var_to_propagate, Explanations &explanations)
Propagate the bounds of the variables in the given formula.
void UpdateUpperBound(const mpq_class *upper_bound) override
Update the upper bounds of the constraint.
void UpdateLowerBound(const mpq_class *lower_bound) override
Update the lower bounds of the constraint.
std::ostream & Print(std::ostream &os) const override
Print the constraint on the standard output.
LeakyReluConstraint(const Variable &relu_var, const Expression &e, float alpha, const PredicateAbstractor &pa)
Construct a new LeakyReluConstraint object.
static const mpq_class zero
Zero constant all the pointers to the default lower bound will point to.
LiteralSet LearnedClauses() const override
Produce some learned clauses the SAT solver will use to prune the sub-problems this piecewise constra...
LiteralSet Assumptions() const override
The assumptions used to guide the SAT solver towards the most promising sub-problems.
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...
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.
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.
@ U
Upper bound.
@ L
Lower bound.
std::set< Literal > LiteralSet
A set of literals.
Definition literal.h:28
STL namespace.
Tuple containing the value, bound type and theory literal associated with the bound.
Definition Bound.h:24