Google OR-Tools v9.15
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
routing_breaks.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_CONSTRAINT_SOLVER_ROUTING_BREAKS_H_
15#define ORTOOLS_CONSTRAINT_SOLVER_ROUTING_BREAKS_H_
16
17#include <cstdint>
18#include <utility>
19#include <vector>
20
21#include "absl/types/span.h"
23
24namespace operations_research {
25
27 public:
28 explicit BreakPropagator(int num_nodes);
29
30 // Result of a propagation: kInfeasible means some infeasibility was found,
31 // kChanged means that the propagation tightened the bounds of some intervals,
32 // kUnchanged means that the propagation did not change anything.
34 // TODO(user): when the OSS version is at C++20, replace this by
35 // using enum PropagationResult;
36 static constexpr PropagationResult kInfeasible =
40
41 // Applies fast propagations, O(log |path|) per break, to the given path.
43 DimensionValues& dimension_values,
44 const PrePostVisitValues& visits);
45 // Propagates interbreak rules on a given path, with a covering reasoning.
46 // Each interbreak is a pair (interbreak_limit, min_break_duration).
48 int path, DimensionValues& dimension,
49 absl::Span<const std::pair<int64_t, int64_t>> interbreaks);
50
51 private:
52 using Interval = DimensionValues::Interval;
53 using VehicleBreak = DimensionValues::VehicleBreak;
54
55 static bool IncreaseMin(int64_t new_min, Interval* interval,
56 PropagationResult* propagation_result) {
57 if (interval->min >= new_min) return true;
58 if (!interval->IncreaseMin(new_min)) {
59 *propagation_result = kInfeasible;
60 return false;
61 }
62 *propagation_result = kChanged;
63 return true;
64 }
65 static bool DecreaseMax(int64_t new_max, Interval* interval,
66 PropagationResult* propagation_result) {
67 if (interval->max <= new_max) return true;
68 if (!interval->DecreaseMax(new_max)) {
69 *propagation_result = kInfeasible;
70 return false;
71 }
72 *propagation_result = kChanged;
73 return true;
74 }
75 static bool IntersectWith(Interval source, Interval* target,
76 PropagationResult* propagation_result) {
77 if (!source.IntersectWith(*target)) {
78 *propagation_result = kInfeasible;
79 } else if (source != *target) {
80 *propagation_result = kChanged;
81 }
82 *target = source;
83 return *propagation_result != kInfeasible;
84 }
85 // In cases where propagators expect some property of variables to hold,
86 // for instance "cumuls[i].min should be weakly increasing in i",
87 // it is necessary to delay modification of the variables until after all
88 // propagations are done.
89 // This struct can be used to store such delayed propagations.
90 struct DelayedPropagation {
91 int64_t value; // New bound of the variable.
92 int index; // Some information on which variable to modify.
93 bool is_min; // The bound is a min if this is true, otherwise a max.
94 };
95 std::vector<DelayedPropagation> delayed_propagations_;
96 // Events used in PropagateInterbreak().
97 struct UsageEvent {
98 int64_t time;
99 int index;
100 bool is_start;
101 bool operator<(const UsageEvent& other) const { return time < other.time; }
102 };
103 std::vector<UsageEvent> usage_events_;
104 // Per-transition reasoning.
105 CommittableArray<int64_t> break_duration_on_transition_;
106};
107
108} // namespace operations_research
109
110#endif // ORTOOLS_CONSTRAINT_SOLVER_ROUTING_BREAKS_H_
PropagationResult PropagateInterbreak(int path, DimensionValues &dimension, absl::Span< const std::pair< int64_t, int64_t > > interbreaks)
static constexpr PropagationResult kInfeasible
static constexpr PropagationResult kChanged
PropagationResult FastPropagations(int path, DimensionValues &dimension_values, const PrePostVisitValues &visits)
static constexpr PropagationResult kUnchanged
OR-Tools root namespace.