Google OR-Tools v9.15
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
routing_utils.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 <algorithm>
17#include <cstddef>
18#include <cstdint>
19#include <functional>
20#include <queue>
21#include <tuple>
22#include <utility>
23#include <vector>
24
25#include "absl/log/check.h"
26#include "absl/types/span.h"
28
29namespace operations_research {
30
32 std::function<int64_t(int, int)> load_demand_of_item_for_bin,
33 std::vector<LoadLimit> load_limit_per_bin) {
34 DCHECK_EQ(num_bins_, load_limit_per_bin.size());
35 for (const LoadLimit& limit : load_limit_per_bin) {
36 const int64_t violation = std::max<int64_t>(0, CapOpp(limit.soft_max_load));
37 total_cost_ =
38 CapAdd(total_cost_, CapProd(violation, limit.cost_above_soft_max_load));
39 }
40 load_demands_per_dimension_.push_back(std::move(load_demand_of_item_for_bin));
41 for (int b = 0; b < num_bins_; ++b) {
42 load_per_bin_[b].push_back(0);
43 load_limits_per_bin_[b].push_back(load_limit_per_bin[b]);
44 }
45}
46
47bool BinCapacities::CheckAdditionFeasibility(int item, int bin) const {
48 return CheckAdditionsFeasibility({item}, bin);
49}
50
51bool BinCapacities::CheckAdditionsFeasibility(absl::Span<const int> items,
52 int bin) const {
53 for (size_t dim = 0; dim < load_demands_per_dimension_.size(); ++dim) {
54 const LoadLimit& limit = load_limits_per_bin_[bin][dim];
55 int64_t new_load = load_per_bin_[bin][dim];
56 for (const int item : items) {
57 new_load = CapAdd(new_load, load_demands_per_dimension_[dim](item, bin));
58 }
59 // TODO(user): try to reorder on failure, so that tight dimensions are
60 // checked first.
61 if (new_load > limit.max_load) return false;
62 }
63 return true;
64}
65
66bool BinCapacities::AddItemToBin(int item, int bin) {
67 bool feasible = true;
68 for (size_t dim = 0; dim < load_demands_per_dimension_.size(); ++dim) {
69 int64_t& load = load_per_bin_[bin][dim];
70 const LoadLimit& limit = load_limits_per_bin_[bin][dim];
71 const int64_t prev_violation =
72 std::max<int64_t>(0, CapSub(load, limit.soft_max_load));
73 load = CapAdd(load, load_demands_per_dimension_[dim](item, bin));
74 const int64_t curr_violation =
75 std::max<int64_t>(0, CapSub(load, limit.soft_max_load));
76 total_cost_ =
77 CapAdd(total_cost_, CapProd(CapSub(curr_violation, prev_violation),
79 feasible &= load <= limit.max_load;
80 }
81 return feasible;
82}
83
84bool BinCapacities::RemoveItemFromBin(int item, int bin) {
85 bool feasible = true;
86 for (size_t dim = 0; dim < load_demands_per_dimension_.size(); ++dim) {
87 int64_t& load = load_per_bin_[bin][dim];
88 const LoadLimit& limit = load_limits_per_bin_[bin][dim];
89 const int64_t prev_violation =
90 std::max<int64_t>(0, CapSub(load, limit.soft_max_load));
91 load = CapSub(load, load_demands_per_dimension_[dim](item, bin));
92 const int64_t curr_violation =
93 std::max<int64_t>(0, CapSub(load, limit.soft_max_load));
94 total_cost_ =
95 CapAdd(total_cost_, CapProd(CapSub(curr_violation, prev_violation),
97 feasible &= load <= limit.max_load;
98 }
99 return feasible;
100}
101
103 for (size_t dim = 0; dim < load_demands_per_dimension_.size(); ++dim) {
104 int64_t& load = load_per_bin_[bin][dim];
105 const LoadLimit& limit = load_limits_per_bin_[bin][dim];
106 const int64_t prev_violation =
107 std::max<int64_t>(0, CapSub(load, limit.soft_max_load));
108 load = 0;
109 const int64_t curr_violation =
110 std::max<int64_t>(0, CapOpp(limit.soft_max_load));
111 total_cost_ =
112 CapAdd(total_cost_, CapProd(CapSub(curr_violation, prev_violation),
114 }
115}
116
118 for (int bin = 0; bin < num_bins_; ++bin) {
119 ClearItemsOfBin(bin);
120 }
121}
122
124 int num_arcs, int64_t start,
125 const std::function<int64_t(int64_t)>& next_accessor,
126 const std::function<bool(int64_t)>& is_end,
127 const std::function<int64_t(int64_t, int64_t, int64_t)>&
128 arc_cost_for_route_start,
129 std::vector<std::pair<int64_t, int>>* most_expensive_arc_starts_and_ranks,
130 std::pair<int, int>* first_expensive_arc_indices) {
131 if (is_end(next_accessor(start))) {
132 // Empty route.
133 *first_expensive_arc_indices = {-1, -1};
134 return false;
135 }
136
137 // NOTE: The negative ranks are so that for a given cost, lower ranks are
138 // given higher priority.
139 using ArcCostNegativeRankStart = std::tuple<int64_t, int, int64_t>;
140 std::priority_queue<ArcCostNegativeRankStart,
141 std::vector<ArcCostNegativeRankStart>,
142 std::greater<ArcCostNegativeRankStart>>
143 arc_info_pq;
144
145 int64_t before_node = start;
146 int rank = 0;
147 while (!is_end(before_node)) {
148 const int64_t after_node = next_accessor(before_node);
149 const int64_t arc_cost =
150 arc_cost_for_route_start(before_node, after_node, start);
151 arc_info_pq.emplace(arc_cost, -rank, before_node);
152
153 before_node = after_node;
154 rank++;
155
156 if (rank > num_arcs) {
157 arc_info_pq.pop();
158 }
159 }
160
161 DCHECK_GE(rank, 2);
162 DCHECK_EQ(arc_info_pq.size(), std::min(rank, num_arcs));
163
164 most_expensive_arc_starts_and_ranks->resize(arc_info_pq.size());
165 int arc_index = arc_info_pq.size() - 1;
166 while (!arc_info_pq.empty()) {
167 const ArcCostNegativeRankStart& arc_info = arc_info_pq.top();
168 (*most_expensive_arc_starts_and_ranks)[arc_index] = {
169 std::get<2>(arc_info), -std::get<1>(arc_info)};
170 arc_index--;
171 arc_info_pq.pop();
172 }
173
174 *first_expensive_arc_indices = {0, 1};
175 return true;
176}
177
178} // namespace operations_research
bool RemoveItemFromBin(int item, int bin)
void AddDimension(std::function< int64_t(int, int)> load_demand_of_item_for_bin, std::vector< LoadLimit > load_limit_per_bin)
bool CheckAdditionFeasibility(int item, int bin) const
bool AddItemToBin(int item, int bin)
bool CheckAdditionsFeasibility(absl::Span< const int > items, int bin) const
OR-Tools root namespace.
int64_t CapAdd(int64_t x, int64_t y)
int64_t CapSub(int64_t x, int64_t y)
int64_t CapProd(int64_t x, int64_t y)
bool FindMostExpensiveArcsOnRoute(int num_arcs, int64_t start, const std::function< int64_t(int64_t)> &next_accessor, const std::function< bool(int64_t)> &is_end, const std::function< int64_t(int64_t, int64_t, int64_t)> &arc_cost_for_route_start, std::vector< std::pair< int64_t, int > > *most_expensive_arc_starts_and_ranks, std::pair< int, int > *first_expensive_arc_indices)
int64_t CapOpp(int64_t v)