14 #ifndef OR_TOOLS_SAT_PB_CONSTRAINT_H_
15 #define OR_TOOLS_SAT_PB_CONSTRAINT_H_
25 #include "absl/container/flat_hash_map.h"
26 #include "absl/strings/string_view.h"
27 #include "absl/types/span.h"
34 #include "ortools/sat/sat_parameters.pb.h"
92 std::vector<LiteralWithCoeff>* cst,
Coefficient* bound_shift,
107 std::vector<LiteralWithCoeff>* cst,
Coefficient* bound_shift,
131 const std::vector<LiteralWithCoeff>& cst);
136 std::vector<LiteralWithCoeff>* cst,
Coefficient* rhs);
162 std::vector<LiteralWithCoeff>* cst);
167 const std::vector<LiteralWithCoeff>&
Constraint(
int i)
const {
168 return constraints_[i];
172 bool AddConstraint(
const std::vector<LiteralWithCoeff>& cst,
175 std::vector<Coefficient> rhs_;
176 std::vector<std::vector<LiteralWithCoeff>> constraints_;
195 return AbsCoefficient(terms_[
var]);
231 int trail_index)
const;
243 const Trail& trail,
int trail_index);
285 const BooleanVariable
var =
literal.Variable();
293 rhs_ -=
std::min(coeff, AbsCoefficient(terms_[
var]));
294 max_sum_ += AbsCoefficient(term_encoding + terms_[
var]) -
295 AbsCoefficient(terms_[
var]);
300 CHECK_GE(max_sum_, 0) <<
"Overflow";
301 terms_[
var] += term_encoding;
308 const BooleanVariable
var =
literal.Variable();
310 return std::min(coeff, AbsCoefficient(terms_[
var]));
346 class UpperBoundedLinearConstraint;
387 const std::vector<LiteralWithCoeff>& cst);
440 BooleanVariable propagated_variable,
441 std::vector<Literal>* reason);
462 const Trail& trail,
int trail_index,
482 uint64_t
hash()
const {
return hash_; }
490 return (index_ < 0) ? threshold : coeffs_[index_] + threshold;
493 *threshold = (index_ < 0) ? slack : slack - coeffs_[index_];
494 already_propagated_end_ = starts_[index_ + 1];
499 bool is_marked_for_deletion_;
501 int first_reason_trail_index_;
506 int already_propagated_end_;
515 std::vector<Coefficient> coeffs_;
516 std::vector<int> starts_;
517 std::vector<Literal> literals_;
529 conflicting_constraint_index_(-1),
530 num_learned_constraint_before_cleanup_(0),
531 constraint_activity_increment_(1.0),
532 parameters_(
model->GetOrCreate<SatParameters>()),
533 stats_(
"PbConstraints"),
534 num_constraint_lookups_(0),
535 num_inspected_constraint_literals_(0),
536 num_threshold_updates_(0) {
537 model->GetOrCreate<
Trail>()->RegisterPropagator(
this);
542 LOG(INFO) <<
"num_constraint_lookups_: " << num_constraint_lookups_;
543 LOG(INFO) <<
"num_threshold_updates_: " << num_threshold_updates_;
548 void Untrail(
const Trail& trail,
int trail_index)
final;
549 absl::Span<const Literal>
Reason(
const Trail& trail,
550 int trail_index)
const final;
557 if (!constraints_.empty()) {
558 to_update_.
resize(num_variables << 1);
559 enqueue_helper_.
reasons.resize(num_variables);
583 bool IsEmpty() const final {
return constraints_.empty(); }
592 if (conflicting_constraint_index_ == -1)
return nullptr;
593 return constraints_[conflicting_constraint_index_.value()].get();
608 constraints_[
index]->MarkForDeletion();
609 DeleteConstraintMarkedForDeletion();
615 return num_inspected_constraint_literals_;
620 bool PropagateNext(
Trail* trail);
624 void ComputeNewLearnedConstraintLimit();
625 void DeleteSomeLearnedConstraintIfNeeded();
630 void DeleteConstraintMarkedForDeletion();
639 DEFINE_STRONG_INDEX_TYPE(ConstraintIndex);
640 struct ConstraintIndexWithCoeff {
641 ConstraintIndexWithCoeff() {}
642 ConstraintIndexWithCoeff(
bool n, ConstraintIndex i,
Coefficient c)
644 bool need_untrail_inspection;
645 ConstraintIndex
index;
650 std::vector<std::unique_ptr<UpperBoundedLinearConstraint>> constraints_;
661 SparseBitset<ConstraintIndex> to_untrail_;
665 absl::flat_hash_map<int64_t, std::vector<UpperBoundedLinearConstraint*>>
666 possible_duplicates_;
669 PbConstraintsEnqueueHelper enqueue_helper_;
673 ConstraintIndex conflicting_constraint_index_;
676 int target_number_of_learned_constraint_;
677 int num_learned_constraint_before_cleanup_;
678 double constraint_activity_increment_;
681 SatParameters* parameters_;
684 mutable StatsGroup stats_;
685 int64_t num_constraint_lookups_;
686 int64_t num_inspected_constraint_literals_;
687 int64_t num_threshold_updates_;
704 first_variable_.
resize(num_variables);
715 if (seen_[
var])
return first_variable_[
var];
716 const BooleanVariable reference_var =
718 if (reference_var ==
var)
return var;
719 if (seen_[reference_var])
return first_variable_[reference_var];
720 seen_.
Set(reference_var);
721 first_variable_[reference_var] =
var;
void resize(size_type new_size)
const std::vector< IntegerType > & PositionsSetAtLeastOnce() const
void Set(IntegerType index)
void ClearAndResize(IntegerType size)
std::string StatString() const
int NumConstraints() const
bool AddLinearConstraint(bool use_lower_bound, Coefficient lower_bound, bool use_upper_bound, Coefficient upper_bound, std::vector< LiteralWithCoeff > *cst)
CanonicalBooleanLinearProblem()
const Coefficient Rhs(int i) const
const std::vector< LiteralWithCoeff > & Constraint(int i) const
LiteralIndex Index() const
std::string DebugString() const
Class that owns everything related to a particular optimization model.
void ReduceCoefficients()
Coefficient MaxSum() const
Literal GetLiteral(BooleanVariable var) const
Coefficient ComputeSlackForTrailPrefix(const Trail &trail, int trail_index) const
Coefficient ReduceCoefficientsAndComputeSlackForTrailPrefix(const Trail &trail, int trail_index)
void ReduceSlackTo(const Trail &trail, int trail_index, Coefficient initial_slack, Coefficient target)
const std::vector< BooleanVariable > & PossibleNonZeros() const
std::string DebugString()
Coefficient CancelationAmount(Literal literal, Coefficient coeff) const
void ReduceGivenCoefficient(BooleanVariable var)
void AddTerm(Literal literal, Coefficient coeff)
void AddToRhs(Coefficient value)
void ClearAndResize(int num_variables)
void CopyIntoVector(std::vector< LiteralWithCoeff > *output)
Coefficient GetCoefficient(BooleanVariable var) const
int NumberOfConstraints() const
bool Propagate(Trail *trail) final
~PbConstraints() override
void RescaleActivities(double scaling_factor)
void ClearConflictingConstraint()
int64_t num_inspected_constraint_literals() const
absl::Span< const Literal > Reason(const Trail &trail, int trail_index) const final
bool AddConstraint(const std::vector< LiteralWithCoeff > &cst, Coefficient rhs, Trail *trail)
int64_t num_threshold_updates() const
void DeleteConstraint(int index)
UpperBoundedLinearConstraint * ConflictingConstraint()
PbConstraints(Model *model)
UpperBoundedLinearConstraint * ReasonPbConstraint(int trail_index) const
bool IsEmpty() const final
void UpdateActivityIncrement()
void BumpActivity(UpperBoundedLinearConstraint *constraint)
void Untrail(const Trail &trail, int trail_index) final
int64_t num_constraint_lookups() const
void Resize(int num_variables)
bool AddLearnedConstraint(const std::vector< LiteralWithCoeff > &cst, Coefficient rhs, Trail *trail)
void Enqueue(Literal true_literal, int propagator_id)
BooleanVariable ReferenceVarWithSameReason(BooleanVariable var) const
void set_activity(double activity)
Coefficient ComputeCancelation(const Trail &trail, int trail_index, const MutableUpperBoundedLinearConstraint &conflict)
bool Propagate(int trail_index, Coefficient *threshold, Trail *trail, PbConstraintsEnqueueHelper *helper)
void set_is_learned(bool is_learned)
void FillReason(const Trail &trail, int source_trail_index, BooleanVariable propagated_variable, std::vector< Literal > *reason)
bool HasIdenticalTerms(const std::vector< LiteralWithCoeff > &cst)
void ResolvePBConflict(const Trail &trail, BooleanVariable var, MutableUpperBoundedLinearConstraint *conflict, Coefficient *conflict_slack)
bool InitializeRhs(Coefficient rhs, int trail_index, Coefficient *threshold, Trail *trail, PbConstraintsEnqueueHelper *helper)
int already_propagated_end() const
void Untrail(Coefficient *threshold, int trail_index)
void AddToConflict(MutableUpperBoundedLinearConstraint *conflict)
UpperBoundedLinearConstraint(const std::vector< LiteralWithCoeff > &cst)
bool is_used_as_a_reason() const
bool is_marked_for_deletion() const
BooleanVariable FirstVariableWithSameReason(BooleanVariable var)
VariableWithSameReasonIdentifier(const Trail &trail)
void Resize(int num_variables)
std::tuple< int64_t, int64_t, const double > Coefficient
Coefficient ComputeCanonicalRhs(Coefficient upper_bound, Coefficient bound_shift, Coefficient max_value)
std::ostream & operator<<(std::ostream &os, const BoolVar &var)
DEFINE_STRONG_INT64_TYPE(IntegerValue)
bool ApplyLiteralMapping(const absl::StrongVector< LiteralIndex, LiteralIndex > &mapping, std::vector< LiteralWithCoeff > *cst, Coefficient *bound_shift, Coefficient *max_value)
Coefficient ComputeNegatedCanonicalRhs(Coefficient lower_bound, Coefficient bound_shift, Coefficient max_value)
void SimplifyCanonicalBooleanLinearConstraint(std::vector< LiteralWithCoeff > *cst, Coefficient *rhs)
bool ComputeBooleanLinearExpressionCanonicalForm(std::vector< LiteralWithCoeff > *cst, Coefficient *bound_shift, Coefficient *max_value)
H AbslHashValue(H h, const IntVar &i)
bool BooleanLinearExpressionIsCanonical(const std::vector< LiteralWithCoeff > &cst)
const Coefficient kCoefficientMax(std::numeric_limits< Coefficient::ValueType >::max())
Collection of objects used to extend the Constraint Solver library.
#define IF_STATS_ENABLED(instructions)
bool operator==(const LiteralWithCoeff &other) const
LiteralWithCoeff(Literal l, Coefficient c)
LiteralWithCoeff(Literal l, int64_t c)
UpperBoundedLinearConstraint * pb_constraint
std::vector< ReasonInfo > reasons
void Enqueue(Literal l, int source_trail_index, UpperBoundedLinearConstraint *ct, Trail *trail)
std::vector< Literal > conflict