14#ifndef OR_TOOLS_SAT_PB_CONSTRAINT_H_
15#define OR_TOOLS_SAT_PB_CONSTRAINT_H_
24#include "absl/container/flat_hash_map.h"
25#include "absl/strings/string_view.h"
26#include "absl/types/span.h"
33#include "ortools/sat/sat_parameters.pb.h"
91 std::vector<LiteralWithCoeff>* cst,
Coefficient* bound_shift,
106 std::vector<LiteralWithCoeff>* cst,
Coefficient* bound_shift,
130 const std::vector<LiteralWithCoeff>& cst);
135 std::vector<LiteralWithCoeff>* cst,
Coefficient* rhs);
161 std::vector<LiteralWithCoeff>* cst);
166 const std::vector<LiteralWithCoeff>&
Constraint(
int i)
const {
167 return constraints_[i];
171 bool AddConstraint(
const std::vector<LiteralWithCoeff>& cst,
174 std::vector<Coefficient> rhs_;
175 std::vector<std::vector<LiteralWithCoeff>> constraints_;
194 return AbsCoefficient(terms_[
var]);
230 int trail_index)
const;
242 const Trail& trail,
int trail_index);
284 const BooleanVariable
var =
literal.Variable();
293 max_sum_ += AbsCoefficient(term_encoding + terms_[
var]) -
294 AbsCoefficient(terms_[
var]);
299 CHECK_GE(max_sum_, 0) <<
"Overflow";
300 terms_[
var] += term_encoding;
307 const BooleanVariable
var =
literal.Variable();
345class UpperBoundedLinearConstraint;
386 const std::vector<LiteralWithCoeff>& cst);
439 BooleanVariable propagated_variable,
440 std::vector<Literal>* reason);
461 const Trail& trail,
int trail_index,
481 uint64_t
hash()
const {
return hash_; }
489 return (index_ < 0) ? threshold : coeffs_[index_] + threshold;
492 *threshold = (index_ < 0) ? slack : slack - coeffs_[index_];
493 already_propagated_end_ = starts_[index_ + 1];
498 bool is_marked_for_deletion_;
500 int first_reason_trail_index_;
505 int already_propagated_end_;
514 std::vector<Coefficient> coeffs_;
515 std::vector<int> starts_;
516 std::vector<Literal> literals_;
528 conflicting_constraint_index_(-1),
529 num_learned_constraint_before_cleanup_(0),
530 constraint_activity_increment_(1.0),
531 parameters_(
model->GetOrCreate<SatParameters>()),
532 stats_(
"PbConstraints"),
533 num_constraint_lookups_(0),
534 num_inspected_constraint_literals_(0),
535 num_threshold_updates_(0) {
536 model->GetOrCreate<
Trail>()->RegisterPropagator(
this);
541 LOG(
INFO) <<
"num_constraint_lookups_: " << num_constraint_lookups_;
542 LOG(
INFO) <<
"num_threshold_updates_: " << num_threshold_updates_;
547 void Untrail(
const Trail& trail,
int trail_index)
final;
548 absl::Span<const Literal>
Reason(
const Trail& trail,
549 int trail_index)
const final;
556 if (!constraints_.empty()) {
557 to_update_.
resize(num_variables << 1);
558 enqueue_helper_.
reasons.resize(num_variables);
582 bool IsEmpty() const final {
return constraints_.empty(); }
591 if (conflicting_constraint_index_ == -1)
return nullptr;
592 return constraints_[conflicting_constraint_index_.value()].get();
607 constraints_[
index]->MarkForDeletion();
608 DeleteConstraintMarkedForDeletion();
614 return num_inspected_constraint_literals_;
619 bool PropagateNext(
Trail* trail);
623 void ComputeNewLearnedConstraintLimit();
624 void DeleteSomeLearnedConstraintIfNeeded();
629 void DeleteConstraintMarkedForDeletion();
638 DEFINE_STRONG_INDEX_TYPE(ConstraintIndex);
639 struct ConstraintIndexWithCoeff {
640 ConstraintIndexWithCoeff() {}
641 ConstraintIndexWithCoeff(
bool n, ConstraintIndex i,
Coefficient c)
643 bool need_untrail_inspection;
644 ConstraintIndex
index;
649 std::vector<std::unique_ptr<UpperBoundedLinearConstraint>> constraints_;
660 SparseBitset<ConstraintIndex> to_untrail_;
664 absl::flat_hash_map<int64_t, std::vector<UpperBoundedLinearConstraint*>>
665 possible_duplicates_;
668 PbConstraintsEnqueueHelper enqueue_helper_;
672 ConstraintIndex conflicting_constraint_index_;
675 int target_number_of_learned_constraint_;
676 int num_learned_constraint_before_cleanup_;
677 double constraint_activity_increment_;
680 SatParameters* parameters_;
683 mutable StatsGroup stats_;
684 int64_t num_constraint_lookups_;
685 int64_t num_inspected_constraint_literals_;
686 int64_t num_threshold_updates_;
703 first_variable_.
resize(num_variables);
714 if (seen_[
var])
return first_variable_[
var];
715 const BooleanVariable reference_var =
717 if (reference_var ==
var)
return var;
718 if (seen_[reference_var])
return first_variable_[reference_var];
719 seen_.
Set(reference_var);
720 first_variable_[reference_var] =
var;
#define CHECK_GE(val1, val2)
#define CHECK_GT(val1, val2)
#define DCHECK_GT(val1, val2)
void resize(size_type new_size)
void Set(IntegerType index)
const std::vector< IntegerType > & PositionsSetAtLeastOnce() const
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)
const std::vector< LiteralWithCoeff > & Constraint(int i) const
CanonicalBooleanLinearProblem()
const Coefficient Rhs(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
const std::vector< BooleanVariable > & PossibleNonZeros() 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)
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)
UpperBoundedLinearConstraint * ConflictingConstraint()
int64_t num_threshold_updates() const
void DeleteConstraint(int index)
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)
bool BooleanLinearExpressionIsCanonical(const std::vector< LiteralWithCoeff > &cst)
H AbslHashValue(H h, const LiteralWithCoeff &term)
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