OR-Tools  9.0
routing_parameters.cc
Go to the documentation of this file.
1 // Copyright 2010-2021 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 <cstdint>
17 
18 #include "absl/strings/str_cat.h"
19 #include "absl/time/time.h"
20 #include "google/protobuf/descriptor.h"
21 #include "google/protobuf/duration.pb.h"
22 #include "google/protobuf/message.h"
23 #include "google/protobuf/text_format.h"
24 #include "ortools/base/logging.h"
25 #include "ortools/base/protoutil.h"
30 
31 namespace operations_research {
32 
33 RoutingModelParameters DefaultRoutingModelParameters() {
34  RoutingModelParameters parameters;
35  ConstraintSolverParameters* const solver_parameters =
36  parameters.mutable_solver_parameters();
37  *solver_parameters = Solver::DefaultSolverParameters();
38  solver_parameters->set_compress_trail(
39  ConstraintSolverParameters::COMPRESS_WITH_ZLIB);
40  solver_parameters->set_skip_locally_optimal_paths(true);
41  parameters.set_reduce_vehicle_cost_model(true);
42  return parameters;
43 }
44 
45 // static
46 RoutingSearchParameters DefaultRoutingSearchParameters() {
47  static const char* const kSearchParameters =
48  "first_solution_strategy: AUTOMATIC "
49  "use_unfiltered_first_solution_strategy: false "
50  "savings_neighbors_ratio: 1 "
51  "savings_max_memory_usage_bytes: 6e9 "
52  "savings_add_reverse_arcs: false "
53  "savings_arc_coefficient: 1 "
54  "savings_parallel_routes: false "
55  "cheapest_insertion_farthest_seeds_ratio: 0 "
56  "cheapest_insertion_first_solution_neighbors_ratio: 1 "
57  "cheapest_insertion_first_solution_min_neighbors: 1 "
58  "cheapest_insertion_ls_operator_neighbors_ratio: 1 "
59  "cheapest_insertion_ls_operator_min_neighbors: 1 "
60  "cheapest_insertion_first_solution_use_neighbors_ratio_for_"
61  "initialization: false "
62  "cheapest_insertion_add_unperformed_entries: false "
63  "local_search_operators {"
64  " use_relocate: BOOL_TRUE"
65  " use_relocate_pair: BOOL_TRUE"
66  " use_light_relocate_pair: BOOL_TRUE"
67  " use_relocate_subtrip: BOOL_TRUE"
68  " use_relocate_neighbors: BOOL_FALSE"
69  " use_exchange: BOOL_TRUE"
70  " use_exchange_pair: BOOL_TRUE"
71  " use_exchange_subtrip: BOOL_TRUE"
72  " use_cross: BOOL_TRUE"
73  " use_cross_exchange: BOOL_FALSE"
74  " use_relocate_expensive_chain: BOOL_TRUE"
75  " use_two_opt: BOOL_TRUE"
76  " use_or_opt: BOOL_TRUE"
77  " use_lin_kernighan: BOOL_TRUE"
78  " use_tsp_opt: BOOL_FALSE"
79  " use_make_active: BOOL_TRUE"
80  " use_relocate_and_make_active: BOOL_FALSE" // costly if true by default
81  " use_make_inactive: BOOL_TRUE"
82  " use_make_chain_inactive: BOOL_FALSE"
83  " use_swap_active: BOOL_TRUE"
84  " use_extended_swap_active: BOOL_FALSE"
85  " use_node_pair_swap_active: BOOL_TRUE"
86  " use_path_lns: BOOL_FALSE"
87  " use_full_path_lns: BOOL_FALSE"
88  " use_tsp_lns: BOOL_FALSE"
89  " use_inactive_lns: BOOL_FALSE"
90  " use_global_cheapest_insertion_path_lns: BOOL_TRUE"
91  " use_local_cheapest_insertion_path_lns: BOOL_TRUE"
92  " use_relocate_path_global_cheapest_insertion_insert_unperformed: "
93  "BOOL_TRUE"
94  " use_global_cheapest_insertion_expensive_chain_lns: BOOL_FALSE"
95  " use_local_cheapest_insertion_expensive_chain_lns: BOOL_FALSE"
96  " use_global_cheapest_insertion_close_nodes_lns: BOOL_FALSE"
97  " use_local_cheapest_insertion_close_nodes_lns: BOOL_FALSE"
98  "}"
99  "use_multi_armed_bandit_concatenate_operators: false "
100  "multi_armed_bandit_compound_operator_memory_coefficient: 0.04 "
101  "multi_armed_bandit_compound_operator_exploration_coefficient: 1e12 "
102  "relocate_expensive_chain_num_arcs_to_consider: 4 "
103  "heuristic_expensive_chain_lns_num_arcs_to_consider: 4 "
104  "heuristic_close_nodes_lns_num_nodes: 5 "
105  "local_search_metaheuristic: AUTOMATIC "
106  "guided_local_search_lambda_coefficient: 0.1 "
107  "use_depth_first_search: false "
108  "use_cp: BOOL_TRUE "
109  "use_cp_sat: BOOL_FALSE "
110  "continuous_scheduling_solver: GLOP "
111  "mixed_integer_scheduling_solver: CP_SAT "
112  "optimization_step: 0.0 "
113  "number_of_solutions_to_collect: 1 "
114  // No "time_limit" by default.
115  "solution_limit: 0x7fffffffffffffff " // kint64max
116  "lns_time_limit: { seconds:0 nanos:100000000 } " // 0.1s
117  "use_full_propagation: false "
118  "log_search: false "
119  "log_cost_scaling_factor: 1.0 "
120  "log_cost_offset: 0.0";
121  RoutingSearchParameters parameters;
122  if (!google::protobuf::TextFormat::ParseFromString(kSearchParameters,
123  &parameters)) {
124  LOG(DFATAL) << "Unsupported default search parameters: "
125  << kSearchParameters;
126  }
127  const std::string error = FindErrorInRoutingSearchParameters(parameters);
128  LOG_IF(DFATAL, !error.empty())
129  << "The default search parameters aren't valid: " << error;
130  return parameters;
131 }
132 
133 namespace {
134 bool IsValidNonNegativeDuration(const google::protobuf::Duration& d) {
135  const auto status_or_duration = util_time::DecodeGoogleApiProto(d);
136  return status_or_duration.ok() &&
137  status_or_duration.value() >= absl::ZeroDuration();
138 }
139 } // namespace
140 
142  const RoutingSearchParameters& search_parameters) {
143  using absl::StrCat;
144  // Check that all local search operators are set to either BOOL_TRUE or
145  // BOOL_FALSE (and not BOOL_UNSPECIFIED).
146  {
147  using Reflection = google::protobuf::Reflection;
148  using Descriptor = google::protobuf::Descriptor;
149  using FieldDescriptor = google::protobuf::FieldDescriptor;
150  const RoutingSearchParameters::LocalSearchNeighborhoodOperators& operators =
151  search_parameters.local_search_operators();
152  const Reflection* ls_reflection = operators.GetReflection();
153  const Descriptor* ls_descriptor = operators.GetDescriptor();
154  for (int /*this is NOT the field's tag number*/ field_index = 0;
155  field_index < ls_descriptor->field_count(); ++field_index) {
156  const FieldDescriptor* field = ls_descriptor->field(field_index);
157  if (field->type() != FieldDescriptor::TYPE_ENUM ||
158  field->enum_type() != OptionalBoolean_descriptor()) {
159  DLOG(FATAL)
160  << "In RoutingSearchParameters::LocalSearchNeighborhoodOperators,"
161  << " field '" << field->name() << "' is not an OptionalBoolean.";
162  return "The file 'routing_search_parameters.proto' itself is invalid!";
163  }
164  const int value = ls_reflection->GetEnum(operators, field)->number();
165  if (!OptionalBoolean_IsValid(value) || value == 0) {
166  return StrCat("local_search_neighborhood_operator.", field->name(),
167  " should be set to BOOL_TRUE or BOOL_FALSE instead of ",
169  " (value: ", value, ")");
170  }
171  }
172  }
173  {
174  const double ratio = search_parameters.savings_neighbors_ratio();
175  if (std::isnan(ratio) || ratio <= 0 || ratio > 1) {
176  return StrCat("Invalid savings_neighbors_ratio:", ratio);
177  }
178  }
179  {
180  const double max_memory =
181  search_parameters.savings_max_memory_usage_bytes();
182  if (std::isnan(max_memory) || max_memory <= 0 || max_memory > 1e10) {
183  return StrCat("Invalid savings_max_memory_usage_bytes: ", max_memory);
184  }
185  }
186  {
187  const double coefficient = search_parameters.savings_arc_coefficient();
188  if (std::isnan(coefficient) || coefficient <= 0 ||
189  std::isinf(coefficient)) {
190  return StrCat("Invalid savings_arc_coefficient:", coefficient);
191  }
192  }
193  {
194  const double ratio =
195  search_parameters.cheapest_insertion_farthest_seeds_ratio();
196  if (std::isnan(ratio) || ratio < 0 || ratio > 1) {
197  return StrCat("Invalid cheapest_insertion_farthest_seeds_ratio:", ratio);
198  }
199  }
200  {
201  const double ratio =
202  search_parameters.cheapest_insertion_first_solution_neighbors_ratio();
203  if (std::isnan(ratio) || ratio <= 0 || ratio > 1) {
204  return StrCat(
205  "Invalid cheapest_insertion_first_solution_neighbors_ratio: ", ratio);
206  }
207  }
208  {
209  const int32_t min_neighbors =
210  search_parameters.cheapest_insertion_first_solution_min_neighbors();
211  if (min_neighbors < 1) {
212  return StrCat("Invalid cheapest_insertion_first_solution_min_neighbors: ",
213  min_neighbors, ". Must be greater or equal to 1.");
214  }
215  }
216  {
217  const double ratio =
218  search_parameters.cheapest_insertion_ls_operator_neighbors_ratio();
219  if (std::isnan(ratio) || ratio <= 0 || ratio > 1) {
220  return StrCat("Invalid cheapest_insertion_ls_operator_neighbors_ratio: ",
221  ratio);
222  }
223  }
224  {
225  const int32_t min_neighbors =
226  search_parameters.cheapest_insertion_ls_operator_min_neighbors();
227  if (min_neighbors < 1) {
228  return StrCat("Invalid cheapest_insertion_ls_operator_min_neighbors: ",
229  min_neighbors, ". Must be greater or equal to 1.");
230  }
231  }
232  {
233  const int32_t num_arcs =
234  search_parameters.relocate_expensive_chain_num_arcs_to_consider();
235  if (num_arcs < 2 || num_arcs > 1e6) {
236  return StrCat("Invalid relocate_expensive_chain_num_arcs_to_consider: ",
237  num_arcs, ". Must be between 2 and 10^6 (included).");
238  }
239  }
240  {
241  const int32_t num_arcs =
242  search_parameters.heuristic_expensive_chain_lns_num_arcs_to_consider();
243  if (num_arcs < 2 || num_arcs > 1e6) {
244  return StrCat(
245  "Invalid heuristic_expensive_chain_lns_num_arcs_to_consider: ",
246  num_arcs, ". Must be between 2 and 10^6 (included).");
247  }
248  }
249  {
250  const int32_t num_nodes =
251  search_parameters.heuristic_close_nodes_lns_num_nodes();
252  if (num_nodes < 0 || num_nodes > 1e4) {
253  return StrCat("Invalid heuristic_close_nodes_lns_num_nodes: ", num_nodes,
254  ". Must be between 0 and 10000 (included).");
255  }
256  }
257  {
258  const double gls_coefficient =
259  search_parameters.guided_local_search_lambda_coefficient();
260  if (std::isnan(gls_coefficient) || gls_coefficient < 0 ||
261  std::isinf(gls_coefficient)) {
262  return StrCat("Invalid guided_local_search_lambda_coefficient: ",
263  gls_coefficient);
264  }
265  }
266  {
267  const double step = search_parameters.optimization_step();
268  if (std::isnan(step) || step < 0.0) {
269  return StrCat("Invalid optimization_step: ", step);
270  }
271  }
272  {
273  const int32_t num = search_parameters.number_of_solutions_to_collect();
274  if (num < 1) return StrCat("Invalid number_of_solutions_to_collect:", num);
275  }
276  {
277  const int64_t lim = search_parameters.solution_limit();
278  if (lim < 1) return StrCat("Invalid solution_limit:", lim);
279  }
280  if (!IsValidNonNegativeDuration(search_parameters.time_limit())) {
281  return "Invalid time_limit: " +
282  search_parameters.time_limit().ShortDebugString();
283  }
284  if (!IsValidNonNegativeDuration(search_parameters.lns_time_limit())) {
285  return "Invalid lns_time_limit: " +
286  search_parameters.lns_time_limit().ShortDebugString();
287  }
288  if (!FirstSolutionStrategy::Value_IsValid(
289  search_parameters.first_solution_strategy())) {
290  return StrCat("Invalid first_solution_strategy: ",
291  search_parameters.first_solution_strategy());
292  }
293  if (!LocalSearchMetaheuristic::Value_IsValid(
294  search_parameters.local_search_metaheuristic())) {
295  return StrCat("Invalid metaheuristic: ",
296  search_parameters.local_search_metaheuristic());
297  }
298 
299  const double scaling_factor = search_parameters.log_cost_scaling_factor();
300  if (scaling_factor == 0 || std::isnan(scaling_factor) ||
301  std::isinf(scaling_factor)) {
302  return StrCat("Invalid value for log_cost_scaling_factor: ",
303  scaling_factor);
304  }
305  const double offset = search_parameters.log_cost_offset();
306  if (std::isnan(offset) || std::isinf(offset)) {
307  return StrCat("Invalid value for log_cost_offset: ", offset);
308  }
309  const RoutingSearchParameters::SchedulingSolver continuous_scheduling_solver =
310  search_parameters.continuous_scheduling_solver();
311  if (continuous_scheduling_solver == RoutingSearchParameters::UNSET ||
312  continuous_scheduling_solver == RoutingSearchParameters::CP_SAT) {
313  return StrCat("Invalid value for continuous_scheduling_solver: ",
314  RoutingSearchParameters::SchedulingSolver_Name(
315  continuous_scheduling_solver));
316  }
317  const RoutingSearchParameters::SchedulingSolver
318  mixed_integer_scheduling_solver =
319  search_parameters.mixed_integer_scheduling_solver();
320  if (mixed_integer_scheduling_solver == RoutingSearchParameters::UNSET) {
321  return StrCat("Invalid value for mixed_integer_scheduling_solver: ",
322  RoutingSearchParameters::SchedulingSolver_Name(
323  mixed_integer_scheduling_solver));
324  }
325 
326  if (search_parameters.has_improvement_limit_parameters()) {
327  const double improvement_rate_coefficient =
328  search_parameters.improvement_limit_parameters()
329  .improvement_rate_coefficient();
330  if (std::isnan(improvement_rate_coefficient) ||
331  improvement_rate_coefficient <= 0) {
332  return StrCat(
333  "Invalid value for "
334  "improvement_limit_parameters.improvement_rate_coefficient: ",
335  improvement_rate_coefficient);
336  }
337 
338  const int32_t improvement_rate_solutions_distance =
339  search_parameters.improvement_limit_parameters()
340  .improvement_rate_solutions_distance();
341  if (improvement_rate_solutions_distance <= 0) {
342  return StrCat(
343  "Invalid value for "
344  "improvement_limit_parameters.improvement_rate_solutions_distance: ",
345  improvement_rate_solutions_distance);
346  }
347  }
348 
349  {
350  const double memory_coefficient =
351  search_parameters
352  .multi_armed_bandit_compound_operator_memory_coefficient();
353  if (std::isnan(memory_coefficient) || memory_coefficient < 0 ||
354  memory_coefficient > 1) {
355  return StrCat(
356  "Invalid value for "
357  "multi_armed_bandit_compound_operator_memory_coefficient: ",
358  memory_coefficient);
359  }
360  }
361  {
362  const double exploration_coefficient =
363  search_parameters
364  .multi_armed_bandit_compound_operator_exploration_coefficient();
365  if (std::isnan(exploration_coefficient) || exploration_coefficient < 0) {
366  return StrCat(
367  "Invalid value for "
368  "multi_armed_bandit_compound_operator_exploration_coefficient: ",
369  exploration_coefficient);
370  }
371  }
372 
373  return ""; // = Valid (No error).
374 }
375 
376 } // namespace operations_research
#define LOG_IF(severity, condition)
Definition: base/logging.h:482
#define DLOG(severity)
Definition: base/logging.h:883
#define LOG(severity)
Definition: base/logging.h:423
static ConstraintSolverParameters DefaultSolverParameters()
Create a ConstraintSolverParameters proto with all the default values.
SatParameters parameters
int64_t value
const int FATAL
Definition: log_severity.h:32
Collection of objects used to extend the Constraint Solver library.
RoutingModelParameters DefaultRoutingModelParameters()
std::string FindErrorInRoutingSearchParameters(const RoutingSearchParameters &search_parameters)
Returns an empty std::string if the routing search parameters are valid, and a non-empty,...
bool OptionalBoolean_IsValid(int value)
const ::PROTOBUF_NAMESPACE_ID::EnumDescriptor * OptionalBoolean_descriptor()
const std::string & OptionalBoolean_Name(T enum_t_value)
RoutingSearchParameters DefaultRoutingSearchParameters()
inline ::absl::StatusOr< absl::Duration > DecodeGoogleApiProto(const google::protobuf::Duration &proto)
Definition: protoutil.h:42
Fractional ratio
int64_t coefficient