21#include "absl/log/check.h"
22#include "absl/log/log.h"
29 subsets_.push_back(subset);
30 elements_.push_back(element);
31 weights_.push_back(weight);
33 CHECK_EQ(elements_.size(), subsets_.size());
34 CHECK_EQ(elements_.size(), weights_.size());
38 CHECK_NE(min_capacity, std::numeric_limits<CapacityWeight>::max());
39 min_capacity_ = min_capacity;
43 CHECK_NE(max_capacity, std::numeric_limits<CapacityWeight>::min());
44 max_capacity_ = max_capacity;
48 if (weights_.empty()) {
50 return min_capacity_ <= 0.0 && max_capacity_ >= 0.0;
58 min_activation += weight;
60 max_activation += weight;
64 DVLOG(1) <<
"[Capacity constraint] Activation bounds: [" << min_activation
65 <<
", " << max_activation <<
"]";
66 DVLOG(1) <<
"[Capacity constraint] Capacity bounds: [" << min_capacity_
67 <<
", " << max_capacity_ <<
"]";
68 return min_activation <= max_capacity_ && max_activation >= min_capacity_;
71std::vector<CapacityTermIndex> CapacityModel::CanonicalIndexing() {
72 std::vector<CapacityTermIndex> idx(
num_terms());
73 std::iota(idx.begin(), idx.end(), CapacityTermIndex(0));
76 std::sort(idx.begin(), idx.end(),
77 [&](CapacityTermIndex lhs, CapacityTermIndex rhs) ->
bool {
78 return subsets_[lhs] < subsets_[rhs]
80 : elements_[lhs] < elements_[rhs];
86 CapacityConstraintProto proto;
87 proto.set_min_capacity(min_capacity_);
88 proto.set_max_capacity(max_capacity_);
90 CapacityConstraintProto::CapacityTerm* current_term =
nullptr;
92 for (CapacityTermIndex i : CanonicalIndexing()) {
93 if (current_term ==
nullptr ||
94 current_term->subset() != subsets_[i].value()) {
95 current_term = proto.add_capacity_term();
96 current_term->set_subset(subsets_[i].value());
98 DCHECK(current_term !=
nullptr);
100 CapacityConstraintProto::CapacityTerm::ElementWeightPair* pair =
101 current_term->add_element_weights();
102 pair->set_element(elements_[i].value());
103 pair->set_weight(weights_[i]);
118 for (
const CapacityConstraintProto::CapacityTerm& term :
119 proto.capacity_term()) {
120 for (
const CapacityConstraintProto::CapacityTerm::ElementWeightPair& pair :
121 term.element_weights()) {
122 AddTerm(SubsetIndex(term.subset()), ElementIndex(pair.element()),
CapacityConstraintProto ExportModelAsProto()
BaseInt num_terms() const
Returns the current number of terms in the constraint.
void ImportModelFromProto(const CapacityConstraintProto &proto)
Imports the model from a CapacityConstraintProto.
bool ComputeFeasibility() const
void SetMaximumCapacity(CapacityWeight max_capacity)
void ReserveNumTerms(BaseInt num_terms)
Reserves num_terms terms in the model.
void SetMinimumCapacity(CapacityWeight min_capacity)
void AddTerm(SubsetIndex subset, ElementIndex element, CapacityWeight weight)
Adds a new term to the constraint.
In SWIG mode, we don't want anything besides these top-level includes.
int64_t CapacityWeight
Basic type for weights. For now, the same as Cost for the set covering.