Google OR-Tools v9.14
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
capacity_model.h
Go to the documentation of this file.
1// Copyright 2010-2025 Google LLC
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14#ifndef OR_TOOLS_SET_COVER_CAPACITY_MODEL_H_
15#define OR_TOOLS_SET_COVER_CAPACITY_MODEL_H_
16
17#include <cmath>
18#include <cstdint>
19#include <limits>
20#include <vector>
21
22#include "absl/log/check.h"
28
29// Representation class for the capacity side-constraint for a weighted
30// set-covering problem.
31//
32// This constraint restricts the selection of elements within subsets that
33// respect the constraint. Such a constraint can mix elements in any subset.
34//
35// Using the same mixed-integer-programming formulation as `set_cover_model.h`,
36// this class corresponds to the following constraint:
37// min_capacity <= \sum_{e in elements} weight_e * x_e <= max_capacity
38
39namespace operations_research {
40// Basic type for weights. For now, the same as `Cost` for the set covering.
41using CapacityWeight = int64_t;
42
43// Term index in a capacity constraint.
44DEFINE_STRONG_INT_TYPE(CapacityTermIndex, BaseInt);
45
46// The terms are represented as three aligned vectors: the element, the subset,
47// and the weight. Each vector is indexed by the term.
54
55// Main class for describing a single capacity constraint in the context of a
56// set-covering problem.
58 public:
59 // Builds an empty capacity constraint.
60 //
61 // Use either WithMinimumWeight or WithMaximumWeight to set only one of the
62 // two bounds.
64 : elements_(),
65 subsets_(),
66 weights_(),
67 min_capacity_(min),
68 max_capacity_(max) {
69 // At least one bound must be set. Otherwise, the constraint is vacuous.
70 CHECK(min_capacity_ != std::numeric_limits<CapacityWeight>::min() ||
71 max_capacity_ != std::numeric_limits<CapacityWeight>::max());
72 }
73
75 return CapacityModel(min, std::numeric_limits<CapacityWeight>::max());
76 }
77
79 return CapacityModel(std::numeric_limits<CapacityWeight>::min(), max);
80 }
81
82 // Returns the current number of terms in the constraint.
83 BaseInt num_terms() const { return elements_.size(); }
84
85 // Returns the range of terms.
86 util_intops::StrongIntRange<CapacityTermIndex> TermRange() const {
87 return util_intops::StrongIntRange<CapacityTermIndex>(
88 CapacityTermIndex(num_terms()));
89 }
90
91 // Adds a new term to the constraint.
92 void AddTerm(SubsetIndex subset, ElementIndex element, CapacityWeight weight);
93
94 // Returns the element, subset, or capacity of the given term.
95 ElementIndex GetTermElementIndex(CapacityTermIndex term) const {
96 return elements_[term];
97 }
98 SubsetIndex GetTermSubsetIndex(CapacityTermIndex term) const {
99 return subsets_[term];
100 }
101 CapacityWeight GetTermCapacityWeight(CapacityTermIndex term) const {
102 return weights_[term];
103 }
104
105 // Sets the lower/upper bounds for the constraint.
106 // This will CHECK-fail if a capacity is a NaN.
107 void SetMinimumCapacity(CapacityWeight min_capacity);
108 void SetMaximumCapacity(CapacityWeight max_capacity);
109
110 // Returns the lower/upper bounds for the constraint.
111 CapacityWeight GetMinimumCapacity() const { return min_capacity_; }
112 CapacityWeight GetMaximumCapacity() const { return max_capacity_; }
113
114 // Returns true if the constraint is feasible, i.e. there is at least one
115 // assignment that satisfies the constraint.
116 bool ComputeFeasibility() const;
117
118 // Reserves num_terms terms in the model.
120 ReserveNumTerms(CapacityTermIndex(num_terms));
121 }
122
123 void ReserveNumTerms(CapacityTermIndex num_terms) {
124 subsets_.reserve(num_terms);
125 elements_.reserve(num_terms);
126 weights_.reserve(num_terms);
127 }
128
129 // Returns the model as a CapacityConstraintProto.
130 //
131 // The function is not const because the terms need to be sorted for the
132 // representation as a protobuf to be canonical.
134
135 // Imports the model from a CapacityConstraintProto.
137
138 private:
139 // The terms in the constraint.
140 CapacityElements elements_;
141 CapacitySubsets subsets_;
142 CapacityWeights weights_;
143
144 // The bounds of the constraint. Both are always active at the same time.
145 // An inactive constraint corresponds to a capacity set to ±∞.
146 CapacityWeight min_capacity_;
147 CapacityWeight max_capacity_;
148
149 // Returns a canonical indexing of the constraint, i.e. reading the terms in
150 // this order yields the order that is explained in the proto.
151 std::vector<CapacityTermIndex> CanonicalIndexing();
152};
153} // namespace operations_research
154
155#endif // OR_TOOLS_SET_COVER_CAPACITY_MODEL_H_
CapacityConstraintProto ExportModelAsProto()
CapacityModel(CapacityWeight min, CapacityWeight max)
BaseInt num_terms() const
Returns the current number of terms in the constraint.
static CapacityModel WithMaximumWeight(CapacityWeight max)
CapacityWeight GetMinimumCapacity() const
Returns the lower/upper bounds for the constraint.
void ImportModelFromProto(const CapacityConstraintProto &proto)
Imports the model from a CapacityConstraintProto.
ElementIndex GetTermElementIndex(CapacityTermIndex term) const
Returns the element, subset, or capacity of the given term.
CapacityWeight GetTermCapacityWeight(CapacityTermIndex term) const
CapacityWeight GetMaximumCapacity() const
util_intops::StrongIntRange< CapacityTermIndex > TermRange() const
Returns the range of terms.
static CapacityModel WithMinimumWeight(CapacityWeight min)
SubsetIndex GetTermSubsetIndex(CapacityTermIndex term) const
void ReserveNumTerms(CapacityTermIndex num_terms)
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.
util_intops::StrongVector< CapacityTermIndex, ElementIndex > CapacityElements
int64_t CapacityWeight
Basic type for weights. For now, the same as Cost for the set covering.
util_intops::StrongVector< CapacityTermIndex, CapacityWeight > CapacityWeights
util_intops::StrongVector< CapacityTermIndex, SubsetIndex > CapacitySubsets
#define DEFINE_STRONG_INT_TYPE(type_name, value_type)