Google OR-Tools v9.14
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
assignment.cc
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
15
16#include "absl/log/check.h"
17#include "absl/log/log.h"
23
24namespace operations_research {
25
27 cost_ = Cost(0.0);
28 values_.assign(model_.subset_costs().size(), false);
29 DCHECK_EQ(values_.size(), model_.subset_costs().size())
30 << "The cost vector (length: " << model_.subset_costs().size()
31 << ") is inconsistent with the assignment (length: " << values_.size()
32 << ")";
33}
34
36 CHECK(constraint_ == nullptr);
37 constraint_ = i;
38}
39
41 CHECK(constraint_ != nullptr);
42 side_constraints_.push_back(i);
43 // TODO(user): call i->SetAssignment or similar so that each and every
44 // constraint uses the same solution storage.
45}
46
48 SubsetIndex subset, bool is_selected,
49 SetCoverInvariant::ConsistencyLevel set_cover_consistency) {
50 DVLOG(1) << "[Assignment] Subset " << subset << " becoming " << is_selected
51 << "; used to be " << values_[subset];
52
53 DCHECK(CheckConsistency());
54 if (values_[subset] == is_selected) return;
55
56 values_[subset] = is_selected;
57 if (is_selected) {
58 cost_ += model_.subset_costs()[subset];
59 if (constraint_) {
60 constraint_->Select(subset, set_cover_consistency);
61 }
62 for (CapacityInvariant* const capacity_constraint : side_constraints_) {
63 capacity_constraint->Select(subset);
64 }
65 } else {
66 cost_ -= model_.subset_costs()[subset];
67 if (constraint_) {
68 constraint_->Deselect(subset, set_cover_consistency);
69 }
70 for (CapacityInvariant* const capacity_constraint : side_constraints_) {
71 capacity_constraint->Deselect(subset);
72 }
73 }
74 DCHECK(CheckConsistency());
75}
76
77SetCoverSolutionResponse SetCoverAssignment::ExportSolutionAsProto() const {
78 SetCoverSolutionResponse message;
79 message.set_num_subsets(values_.size());
80 message.set_cost(cost_);
81 for (SubsetIndex subset(0);
82 subset < SubsetIndex(model_.subset_costs().size()); ++subset) {
83 if (values_[subset]) {
84 message.add_subset(subset.value());
85 }
86 }
87 return message;
88}
89
91 DCHECK_EQ(solution.size(), values_.size());
92 values_ = solution;
93 cost_ = ComputeCost(values_);
94}
95
97 const SetCoverSolutionResponse& message) {
98 values_.resize(SubsetIndex(message.num_subsets()), false);
99 cost_ = Cost(0.0);
100 for (auto s : message.subset()) {
101 SubsetIndex subset(s);
102 values_[subset] = true;
103 cost_ += model_.subset_costs()[subset];
104 }
105 CHECK(MathUtil::AlmostEquals(message.cost(), cost_));
106 DCHECK(CheckConsistency());
107}
108
110 Cost cst = ComputeCost(values_);
111 CHECK(MathUtil::AlmostEquals(cost_, cst));
112 return true;
113}
114
115Cost SetCoverAssignment::ComputeCost(const SubsetBoolVector& choices) const {
116 Cost cst = 0.0;
117 SubsetIndex subset(0);
118 for (const bool b : choices) {
119 if (b) {
120 cst += model_.subset_costs()[subset];
121 }
122 ++subset;
123 }
124 return cst;
125}
126
127} // namespace operations_research
static bool AlmostEquals(const T x, const T y)
Definition mathutil.h:314
void LoadAssignment(const SubsetBoolVector &solution)
Definition assignment.cc:90
void ImportSolutionFromProto(const SetCoverSolutionResponse &message)
Definition assignment.cc:96
void Clear()
Clears the current assignment.
Definition assignment.cc:26
void SetValue(SubsetIndex subset, bool is_selected, SetCoverInvariant::ConsistencyLevel set_cover_consistency)
Sets the subset's assignment to the given bool.
Definition assignment.cc:47
SetCoverSolutionResponse ExportSolutionAsProto() const
Returns the current solution as a proto.
Definition assignment.cc:77
void AttachInvariant(SetCoverInvariant *i)
Definition assignment.cc:35
const SubsetCostVector & subset_costs() const
Vector of costs for each subset.
In SWIG mode, we don't want anything besides these top-level includes.
Select next search node to expand Select next item_i to add this new search node to the search Generate a new search node where item_i is not in the knapsack Check validity of this new partial solution(using propagators) - If valid
util_intops::StrongVector< SubsetIndex, bool > SubsetBoolVector
Definition base_types.h:71
double Cost
Basic non-strict type for cost. The speed penalty for using double is ~2%.
Definition base_types.h:32