dlinear  0.0.1
Delta-complete SMT solver for linear programming
Loading...
Searching...
No Matches
PiecewiseLinearConstraint.cpp
1
6#include "dlinear/solver/PiecewiseLinearConstraint.h"
7
8#include "dlinear/util/Config.h"
9#include "dlinear/util/Infinity.h"
10#include "dlinear/util/exception.h"
11
12namespace dlinear {
13
14const Expression PiecewiseLinearConstraint::zero_soi{0};
15
16PiecewiseLinearConstraint::PiecewiseLinearConstraint(const mpq_class* const lb, const mpq_class* const ub,
17 Variable active_var, Variable inactive_var, Variable theory_var,
18 Expression active_soi, Expression inactive_soi,
20 : lower_bound_{lb},
21 upper_bound_{ub},
22 active_var_{std::move(active_var)},
23 inactive_var_{std::move(inactive_var)},
24 theory_var_{std::move(theory_var)},
25 active_soi_{std::move(active_soi)},
26 inactive_soi_{std::move(inactive_soi)},
27 state_{state} {
28 DLINEAR_ASSERT(!active_var_.is_dummy(), "active_var_ must be valid");
29 DLINEAR_ASSERT(!inactive_var_.is_dummy(), "inactive_var_ must be valid");
30 DLINEAR_ASSERT(!theory_var_.is_dummy(), "theory_var_ must be valid");
31 DLINEAR_ASSERT(active_var_.get_type() == Variable::Type::BOOLEAN, "active_var_ must be boolean");
32 DLINEAR_ASSERT(inactive_var_.get_type() == Variable::Type::BOOLEAN, "inactive_var_ must be boolean");
33 DLINEAR_ASSERT(theory_var_.get_type() == Variable::Type::CONTINUOUS, "theory_var_ must be continuous");
34}
35
42
44 switch (state_) {
46 return active_soi_;
48 return inactive_soi_;
49 default:
50 return zero_soi;
51 }
52}
53
54void PiecewiseLinearConstraint::UpdateBounds(const mpq_class& lb, const mpq_class& ub) {
55 DLINEAR_ASSERT(lb <= ub, "Invalid bounds");
58}
59void PiecewiseLinearConstraint::UpdateBounds(const mpq_class* lb, const mpq_class* ub) {
60 DLINEAR_ASSERT(lb == nullptr || ub == nullptr || *lb <= *ub, "Invalid bounds");
61 if (lb != nullptr) UpdateLowerBound(lb);
62 if (ub != nullptr) UpdateUpperBound(ub);
63}
65 DLINEAR_ASSERT(lb != nullptr, "Invalid lower bound");
66 DLINEAR_ASSERT_FMT(upper_bound_ == nullptr || *lb <= *upper_bound_,
67 "New lower bound must be less than or equal to the upper bound. Got {} > {}", *lb, *upper_bound_);
68 if (lower_bound_ == nullptr || *lb > *lower_bound_) lower_bound_ = lb;
69}
71 DLINEAR_ASSERT(ub != nullptr, "Invalid upper bound");
72 DLINEAR_ASSERT_FMT(lower_bound_ == nullptr || *ub >= *lower_bound_,
73 "New upper bound must be greater than or equal to the lower bound. Got {} < {}", *ub,
75 if (upper_bound_ == nullptr || *ub < *upper_bound_) upper_bound_ = ub;
76}
77mpq_class PiecewiseLinearConstraint::Cost(const Environment& env) const {
78 switch (state_) {
80 return active_soi_.Evaluate(env);
82 return inactive_soi_.Evaluate(env);
83 default:
84 return 0;
85 }
86}
87mpq_class PiecewiseLinearConstraint::Cost(const Environment& env, const bool active) const {
88 return (active ? active_soi_ : inactive_soi_).Evaluate(env);
89}
90
91std::ostream& operator<<(std::ostream& os, const PiecewiseLinearConstraint& gc) { return gc.Print(os); }
92
93} // namespace dlinear
@ SOPLEX
Soplex Solver. Default option.
static const mpq_class & ninfinity(const Config &config)
Get the negative infinity value for the given LP solver in the config.
Definition Infinity.cpp:31
static const mpq_class & infinity(const Config &config)
Get the positive infinity value for the given LP solver in the config.
Definition Infinity.cpp:30
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.
PiecewiseLinearConstraint(const mpq_class *lb=nullptr, const mpq_class *ub=nullptr, Variable active_var={}, Variable inactive_var={}, Variable theory_var={}, Expression active_soi={0}, Expression inactive_soi={0}, PiecewiseConstraintState state=PiecewiseConstraintState::NOT_FIXED)
Construct a new Piecewise Linear Constraint object.
void UpdateBounds(const mpq_class &lb, const mpq_class &ub)
Update the bounds of the constraint.
const mpq_class * lower_bound_
Lower bound of the constraint.
const Variable theory_var_
Theory variable .
static const Expression zero_soi
Expression used as a soi if the constraint is not fixed yet.
virtual std::ostream & Print(std::ostream &os) const =0
Print the constraint on the standard output.
const Variable active_var_
Boolean variable associated with the theory literal.
mpq_class Cost(const Environment &env) const
Calculate the cost of the violation of the constraint.
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.
Represents a symbolic environment (mapping from a variable to a value).
Represents a symbolic form of an expression.
mpq_class Evaluate(const Environment &env=Environment{}) const
Evaluates under a given environment (by default, an empty environment).
Represents a symbolic variable.
@ CONTINUOUS
A CONTINUOUS variable takes a mpq_class value.
@ BOOLEAN
A BOOLEAN variable takes a bool value.
bool is_dummy() const
Checks if this is a dummy variable (ID = 0) which is created by the default constructor.
Global namespace for the dlinear library.
PiecewiseConstraintState
State a piecewise constraint can be in.
@ ACTIVE
The constraint is active.
@ INACTIVE
The constraint is inactive.
STL namespace.