Google OR-Tools
v9.15
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 ORTOOLS_SET_COVER_CAPACITY_INVARIANT_H_
15
#define ORTOOLS_SET_COVER_CAPACITY_INVARIANT_H_
16
17
#include "absl/log/check.h"
18
#include "
ortools/set_cover/base_types.h
"
19
#include "
ortools/set_cover/capacity_model.h
"
20
#include "
ortools/set_cover/set_cover_model.h
"
21
22
namespace
operations_research
{
23
class
CapacityInvariant
{
24
public
:
25
// Constructs an empty capacity invariant state.
26
// The model may not change after the invariant was built.
27
explicit
CapacityInvariant
(
CapacityModel
* m,
SetCoverModel
* sc)
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
// ORTOOLS_SET_COVER_CAPACITY_INVARIANT_H_
base_types.h
capacity_model.h
operations_research::CapacityInvariant::Clear
void Clear()
Definition
capacity_invariant.cc:27
operations_research::CapacityInvariant::CapacityInvariant
CapacityInvariant(CapacityModel *m, SetCoverModel *sc)
Definition
capacity_invariant.h:27
operations_research::CapacityInvariant::Select
bool Select(SubsetIndex subset)
Definition
capacity_invariant.cc:70
operations_research::CapacityInvariant::Deselect
bool Deselect(SubsetIndex subset)
Definition
capacity_invariant.cc:97
operations_research::CapacityInvariant::CanSelect
bool CanSelect(SubsetIndex subset) const
Definition
capacity_invariant.cc:87
operations_research::CapacityInvariant::CanDeselect
bool CanDeselect(SubsetIndex subset) const
Definition
capacity_invariant.cc:114
operations_research::CapacityModel
Definition
capacity_model.h:57
operations_research::SetCoverModel
Definition
set_cover_model.h:58
operations_research
OR-Tools root namespace.
Definition
binary_indexed_tree.h:21
operations_research::CapacityWeight
int64_t CapacityWeight
Definition
capacity_model.h:41
operations_research::SubsetBoolVector
util_intops::StrongVector< SubsetIndex, bool > SubsetBoolVector
Definition
base_types.h:71
set_cover_model.h
ortools
set_cover
capacity_invariant.h
Generated by
1.15.0