-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Copy pathrouting_utils.cc
175 lines (155 loc) · 6.19 KB
/
routing_utils.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Copyright 2010-2024 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "ortools/constraint_solver/routing_utils.h"
#include <algorithm>
#include <functional>
#include <queue>
#include <tuple>
#include <utility>
#include <vector>
#include "absl/log/check.h"
#include "ortools/util/saturated_arithmetic.h"
namespace operations_research {
void BinCapacities::AddDimension(
std::function<int64_t(int, int)> load_demand_of_item_for_bin,
std::vector<LoadLimit> load_limit_per_bin) {
DCHECK_EQ(num_bins_, load_limit_per_bin.size());
for (const LoadLimit& limit : load_limit_per_bin) {
const int64_t violation = std::max<int64_t>(0, CapOpp(limit.soft_max_load));
total_cost_ =
CapAdd(total_cost_, CapProd(violation, limit.cost_above_soft_max_load));
}
load_demands_per_dimension_.push_back(std::move(load_demand_of_item_for_bin));
for (int b = 0; b < num_bins_; ++b) {
load_per_bin_[b].push_back(0);
load_limits_per_bin_[b].push_back(load_limit_per_bin[b]);
}
}
bool BinCapacities::CheckAdditionFeasibility(int item, int bin) const {
return CheckAdditionsFeasibility({item}, bin);
}
bool BinCapacities::CheckAdditionsFeasibility(const std::vector<int>& items,
int bin) const {
for (size_t dim = 0; dim < load_demands_per_dimension_.size(); ++dim) {
const LoadLimit& limit = load_limits_per_bin_[bin][dim];
int64_t new_load = load_per_bin_[bin][dim];
for (const int item : items) {
new_load = CapAdd(new_load, load_demands_per_dimension_[dim](item, bin));
}
// TODO(user): try to reorder on failure, so that tight dimensions are
// checked first.
if (new_load > limit.max_load) return false;
}
return true;
}
bool BinCapacities::AddItemToBin(int item, int bin) {
bool feasible = true;
for (size_t dim = 0; dim < load_demands_per_dimension_.size(); ++dim) {
int64_t& load = load_per_bin_[bin][dim];
const LoadLimit& limit = load_limits_per_bin_[bin][dim];
const int64_t prev_violation =
std::max<int64_t>(0, CapSub(load, limit.soft_max_load));
load = CapAdd(load, load_demands_per_dimension_[dim](item, bin));
const int64_t curr_violation =
std::max<int64_t>(0, CapSub(load, limit.soft_max_load));
total_cost_ =
CapAdd(total_cost_, CapProd(CapSub(curr_violation, prev_violation),
limit.cost_above_soft_max_load));
feasible &= load <= limit.max_load;
}
return feasible;
}
bool BinCapacities::RemoveItemFromBin(int item, int bin) {
bool feasible = true;
for (size_t dim = 0; dim < load_demands_per_dimension_.size(); ++dim) {
int64_t& load = load_per_bin_[bin][dim];
const LoadLimit& limit = load_limits_per_bin_[bin][dim];
const int64_t prev_violation =
std::max<int64_t>(0, CapSub(load, limit.soft_max_load));
load = CapSub(load, load_demands_per_dimension_[dim](item, bin));
const int64_t curr_violation =
std::max<int64_t>(0, CapSub(load, limit.soft_max_load));
total_cost_ =
CapAdd(total_cost_, CapProd(CapSub(curr_violation, prev_violation),
limit.cost_above_soft_max_load));
feasible &= load <= limit.max_load;
}
return feasible;
}
void BinCapacities::ClearItemsOfBin(int bin) {
for (size_t dim = 0; dim < load_demands_per_dimension_.size(); ++dim) {
int64_t& load = load_per_bin_[bin][dim];
const LoadLimit& limit = load_limits_per_bin_[bin][dim];
const int64_t prev_violation =
std::max<int64_t>(0, CapSub(load, limit.soft_max_load));
load = 0;
const int64_t curr_violation =
std::max<int64_t>(0, CapOpp(limit.soft_max_load));
total_cost_ =
CapAdd(total_cost_, CapProd(CapSub(curr_violation, prev_violation),
limit.cost_above_soft_max_load));
}
}
void BinCapacities::ClearItems() {
for (int bin = 0; bin < num_bins_; ++bin) {
ClearItemsOfBin(bin);
}
}
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) {
if (is_end(next_accessor(start))) {
// Empty route.
*first_expensive_arc_indices = {-1, -1};
return false;
}
// NOTE: The negative ranks are so that for a given cost, lower ranks are
// given higher priority.
using ArcCostNegativeRankStart = std::tuple<int64_t, int, int64_t>;
std::priority_queue<ArcCostNegativeRankStart,
std::vector<ArcCostNegativeRankStart>,
std::greater<ArcCostNegativeRankStart>>
arc_info_pq;
int64_t before_node = start;
int rank = 0;
while (!is_end(before_node)) {
const int64_t after_node = next_accessor(before_node);
const int64_t arc_cost =
arc_cost_for_route_start(before_node, after_node, start);
arc_info_pq.emplace(arc_cost, -rank, before_node);
before_node = after_node;
rank++;
if (rank > num_arcs) {
arc_info_pq.pop();
}
}
DCHECK_GE(rank, 2);
DCHECK_EQ(arc_info_pq.size(), std::min(rank, num_arcs));
most_expensive_arc_starts_and_ranks->resize(arc_info_pq.size());
int arc_index = arc_info_pq.size() - 1;
while (!arc_info_pq.empty()) {
const ArcCostNegativeRankStart& arc_info = arc_info_pq.top();
(*most_expensive_arc_starts_and_ranks)[arc_index] = {
std::get<2>(arc_info), -std::get<1>(arc_info)};
arc_index--;
arc_info_pq.pop();
}
*first_expensive_arc_indices = {0, 1};
return true;
}
} // namespace operations_research