Google OR-Tools v9.14
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
capacity_invariant.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_INVARIANT_H_
15#define OR_TOOLS_SET_COVER_CAPACITY_INVARIANT_H_
16
17#include "absl/log/check.h"
21
22namespace operations_research {
24 public:
25 // Constructs an empty capacity invariant state.
26 // The model may not change after the invariant was built.
28 : model_(m), set_cover_model_(sc) {
29 DCHECK(model_->ComputeFeasibility());
30 Clear();
31 }
32
33 // Clears the invariant.
34 void Clear();
35
36 // Returns `true` when the constraint is not violated by selecting all of the
37 // items in the subset and incrementally updates the invariant. Otherwise,
38 // returns `false` and does not change the object. (If the subset is already
39 // selected, the behavior is undefined.)
40 bool Select(SubsetIndex subset);
41
42 // Returns `true` when the constraint would not be violated by selecting all
43 // of the items in the subset. Otherwise, returns `false`. The object never
44 // changes. (If the subset is already selected, the behavior is undefined.)
45 bool CanSelect(SubsetIndex subset) const;
46
47 // Returns `true` when the constraint is not violated by unselecting all of
48 // the items in the subset and incrementally updates the invariant. Otherwise,
49 // returns `false` and does not change the object. (If the subset is already
50 // not selected, the behavior is undefined.)
51 bool Deselect(SubsetIndex subset);
52
53 // Returns `true` when the constraint would not be violated by unselecting all
54 // of the items in the subset. Otherwise, returns `false`. The object never
55 // changes. (If the subset is already not selected, the behavior is
56 // undefined.)
57 bool CanDeselect(SubsetIndex subset) const;
58
59 // TODO(user): implement the functions where you only select/deselect an
60 // item of a subset (instead of all items at once). The behavior gets much
61 // more interesting: if two subsets cover one item and the two item-subset
62 // combinations are terms in this capacity constraint, only one of them counts
63 // towards the capacity.
64 //
65 // The solver is not yet ready for this move: you need to
66 // decide which subset covers a given item, instead of ensuring that an item
67 // is covered by at least one subset. Currently, we could aggregate the terms
68 // per subset to make the code much faster when (de)selecting at the cost of
69 // increased initialization times.
70
71 private:
72 // The capacity-constraint model on which the invariant runs.
73 CapacityModel* model_;
74
75 // The set-cover model on which the invariant runs.
76 SetCoverModel* set_cover_model_;
77
78 // Current slack of the constraint.
79 CapacityWeight current_slack_;
80
81 // Current solution assignment.
82 // TODO(user): reuse the assignment of a SetCoverInvariant.
83 SubsetBoolVector is_selected_;
84
85 // Determines the change in slack when (de)selecting the given subset.
86 // The returned value is nonnegative; add it to the slack when selecting
87 // and subtract it when deselecting.
88 CapacityWeight ComputeSlackChange(SubsetIndex subset) const;
89
90 // Determines whether the given slack change violates the constraint
91 // (`false`) or not (`true`).
92 bool SlackChangeFitsConstraint(CapacityWeight slack_change) const;
93};
94} // namespace operations_research
95
96#endif // OR_TOOLS_SET_COVER_CAPACITY_INVARIANT_H_
CapacityInvariant(CapacityModel *m, SetCoverModel *sc)
bool CanSelect(SubsetIndex subset) const
bool CanDeselect(SubsetIndex subset) const
Main class for describing a weighted set-covering problem.
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.
util_intops::StrongVector< SubsetIndex, bool > SubsetBoolVector
Definition base_types.h:71