Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
routing_filters.h
Go to the documentation of this file.
1// Copyright 2010-2024 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 OR_TOOLS_CONSTRAINT_SOLVER_ROUTING_FILTERS_H_
15#define OR_TOOLS_CONSTRAINT_SOLVER_ROUTING_FILTERS_H_
16
17#include <cstdint>
18#include <functional>
19#include <memory>
20#include <string>
21#include <utility>
22#include <vector>
23
24#include "absl/strings/string_view.h"
29#include "ortools/constraint_solver/routing_parameters.pb.h"
31#include "ortools/util/bitset.h"
32
33namespace operations_research {
34
36IntVarLocalSearchFilter* MakeMaxActiveVehiclesFilter(
37 const RoutingModel& routing_model);
38
40IntVarLocalSearchFilter* MakeNodeDisjunctionFilter(
41 const RoutingModel& routing_model, bool filter_cost);
42
44IntVarLocalSearchFilter* MakeVehicleAmortizedCostFilter(
45 const RoutingModel& routing_model);
46
48IntVarLocalSearchFilter* MakeTypeRegulationsFilter(
49 const RoutingModel& routing_model);
50
53IntVarLocalSearchFilter* MakePickupDeliveryFilter(
54 const RoutingModel& routing_model,
55 const std::vector<PickupDeliveryPair>& pairs,
56 const std::vector<RoutingModel::PickupAndDeliveryPolicy>& vehicle_policies);
57
59IntVarLocalSearchFilter* MakeVehicleVarFilter(
60 const RoutingModel& routing_model);
61
63IntVarLocalSearchFilter* MakePathCumulFilter(const RoutingDimension& dimension,
64 bool propagate_own_objective_value,
65 bool filter_objective_cost,
66 bool can_use_lp);
67
69IntVarLocalSearchFilter* MakeCumulBoundsPropagatorFilter(
70 const RoutingDimension& dimension);
71
73IntVarLocalSearchFilter* MakeGlobalLPCumulFilter(
74 GlobalDimensionCumulOptimizer* optimizer,
75 GlobalDimensionCumulOptimizer* mp_optimizer, bool filter_objective_cost);
76
79LocalSearchFilter* MakeResourceAssignmentFilter(
80 LocalDimensionCumulOptimizer* optimizer,
81 LocalDimensionCumulOptimizer* mp_optimizer,
82 bool propagate_own_objective_value, bool filter_objective_cost);
83
85IntVarLocalSearchFilter* MakeCPFeasibilityFilter(RoutingModel* routing_model);
86
88 public:
90 const PathState* path_state, std::vector<int> force_class,
91 std::vector<const std::function<int64_t(int64_t)>*> force_per_class,
92 std::vector<int> distance_class,
93 std::vector<const std::function<int64_t(int64_t, int64_t)>*>
94 distance_per_class,
95 std::vector<int64_t> path_unit_costs,
96 std::vector<bool> path_has_cost_when_empty);
97 bool Check();
98 void Commit();
99 int64_t CommittedCost() const { return committed_total_cost_; }
100 int64_t AcceptedCost() const { return accepted_total_cost_; }
101
102 private:
103 int64_t ComputePathCost(int64_t path_start) const;
104 const PathState* const path_state_;
105 const std::vector<int> force_class_;
106 const std::vector<int> distance_class_;
107 const std::vector<const std::function<int64_t(int64_t)>*> force_per_class_;
108 const std::vector<const std::function<int64_t(int64_t, int64_t)>*>
109 distance_per_class_;
110 const std::vector<int64_t> path_unit_costs_;
111 const std::vector<bool> path_has_cost_when_empty_;
112
113 // Incremental cost computation.
114 int64_t committed_total_cost_;
115 int64_t accepted_total_cost_;
116 std::vector<int64_t> committed_path_cost_;
117};
118
119LocalSearchFilter* MakePathEnergyCostFilter(
120 Solver* solver, std::unique_ptr<PathEnergyCostChecker> checker,
121 absl::string_view dimension_name);
122
126 const PathState* path_state,
127 const std::vector<RoutingDimension*>& dimensions,
128 std::vector<LocalSearchFilterManager::FilterEvent>* filters);
129
131 const std::vector<RoutingDimension*>& dimensions,
132 const RoutingSearchParameters& parameters, bool filter_objective_cost,
133 bool use_chain_cumul_filter,
134 std::vector<LocalSearchFilterManager::FilterEvent>* filters);
135
137
139 public:
140 BasePathFilter(const std::vector<IntVar*>& nexts, int next_domain_size);
141 ~BasePathFilter() override {}
142 bool Accept(const Assignment* delta, const Assignment* deltadelta,
143 int64_t objective_min, int64_t objective_max) override;
144 void OnSynchronize(const Assignment* delta) override;
145
146 protected:
147 static const int64_t kUnassigned;
148
149 int64_t GetNext(int64_t node) const {
150 return (new_nexts_[node] == kUnassigned)
151 ? (IsVarSynced(node) ? Value(node) : kUnassigned)
152 : new_nexts_[node];
153 }
154 int NumPaths() const { return starts_.size(); }
155 int64_t Start(int i) const { return starts_[i]; }
156 int GetPath(int64_t node) const { return paths_[node]; }
157 int Rank(int64_t node) const { return ranks_[node]; }
158 bool IsDisabled() const { return status_ == DISABLED; }
159 const std::vector<int64_t>& GetTouchedPathStarts() const {
160 return touched_paths_.PositionsSetAtLeastOnce();
161 }
162 bool PathStartTouched(int64_t start) const { return touched_paths_[start]; }
163 const std::vector<int64_t>& GetNewSynchronizedUnperformedNodes() const {
164 return new_synchronized_unperformed_nodes_.PositionsSetAtLeastOnce();
165 }
166
167 bool lns_detected() const { return lns_detected_; }
168
169 private:
170 enum Status { UNKNOWN, ENABLED, DISABLED };
171
172 virtual bool DisableFiltering() const { return false; }
173 virtual void OnBeforeSynchronizePaths() {}
174 virtual void OnAfterSynchronizePaths() {}
175 virtual void OnSynchronizePathFromStart(int64_t) {}
176 virtual bool InitializeAcceptPath() { return true; }
177 virtual bool AcceptPath(int64_t path_start, int64_t chain_start,
178 int64_t chain_end) = 0;
179 virtual bool FinalizeAcceptPath(int64_t, int64_t) { return true; }
181 void ComputePathStarts(std::vector<int64_t>* path_starts,
182 std::vector<int>* index_to_path);
183 bool HavePathsChanged();
184 void SynchronizeFullAssignment();
185 void UpdateAllRanks();
186 void UpdatePathRanksFromStart(int start);
187
188 std::vector<int64_t> node_path_starts_;
189 std::vector<int64_t> starts_;
190 std::vector<int> paths_;
191 SparseBitset<int64_t> new_synchronized_unperformed_nodes_;
192 std::vector<int64_t> new_nexts_;
193 std::vector<int> delta_touched_;
194 SparseBitset<> touched_paths_;
195 // clang-format off
196 std::vector<std::pair<int64_t, int64_t> > touched_path_chain_start_ends_;
197 // clang-format on
198 std::vector<int> ranks_;
199
200 Status status_;
201 bool lns_detected_;
202};
203
204} // namespace operations_research
205
206#endif // OR_TOOLS_CONSTRAINT_SOLVER_ROUTING_FILTERS_H_
std::vector< int > dimensions
Generic path-based filter class.
BasePathFilter(const std::vector< IntVar * > &nexts, int next_domain_size)
bool PathStartTouched(int64_t start) const
int64_t GetNext(int64_t node) const
const std::vector< int64_t > & GetTouchedPathStarts() const
bool Accept(const Assignment *delta, const Assignment *deltadelta, int64_t objective_min, int64_t objective_max) override
const std::vector< int64_t > & GetNewSynchronizedUnperformedNodes() const
void OnSynchronize(const Assignment *delta) override
PathEnergyCostChecker(const PathState *path_state, std::vector< int > force_class, std::vector< const std::function< int64_t(int64_t)> * > force_per_class, std::vector< int > distance_class, std::vector< const std::function< int64_t(int64_t, int64_t)> * > distance_per_class, std::vector< int64_t > path_unit_costs, std::vector< bool > path_has_cost_when_empty)
const std::vector< IntegerType > & PositionsSetAtLeastOnce() const
Definition bitset.h:878
SatParameters parameters
In SWIG mode, we don't want anything besides these top-level includes.
IntVarLocalSearchFilter * MakeVehicleAmortizedCostFilter(const RoutingModel &routing_model)
Returns a filter computing vehicle amortized costs.
IntVarLocalSearchFilter * MakeMaxActiveVehiclesFilter(const RoutingModel &routing_model)
Returns a filter ensuring that max active vehicles constraints are enforced.
void AppendLightWeightDimensionFilters(const PathState *path_state, const std::vector< RoutingDimension * > &dimensions, std::vector< LocalSearchFilterManager::FilterEvent > *filters)
IntVarLocalSearchFilter * MakeNodeDisjunctionFilter(const RoutingModel &routing_model, bool filter_cost)
Returns a filter ensuring that node disjunction constraints are enforced.
LocalSearchFilter * MakePathEnergyCostFilter(Solver *solver, std::unique_ptr< PathEnergyCostChecker > checker, absl::string_view dimension_name)
IntVarLocalSearchFilter * MakeCumulBoundsPropagatorFilter(const RoutingDimension &dimension)
Returns a filter handling dimension cumul bounds.
IntVarLocalSearchFilter * MakeGlobalLPCumulFilter(GlobalDimensionCumulOptimizer *optimizer, GlobalDimensionCumulOptimizer *mp_optimizer, bool filter_objective_cost)
Returns a filter checking global linear constraints and costs.
IntVarLocalSearchFilter * MakeCPFeasibilityFilter(RoutingModel *routing_model)
Returns a filter checking the current solution using CP propagation.
IntVarLocalSearchFilter * MakePathCumulFilter(const RoutingDimension &dimension, bool propagate_own_objective_value, bool filter_objective_cost, bool can_use_lp)
Returns a filter handling dimension costs and constraints.
IntVarLocalSearchFilter * MakePickupDeliveryFilter(const RoutingModel &routing_model, const std::vector< PickupDeliveryPair > &pairs, const std::vector< RoutingModel::PickupAndDeliveryPolicy > &vehicle_policies)
void AppendDimensionCumulFilters(const std::vector< RoutingDimension * > &dimensions, const RoutingSearchParameters &parameters, bool filter_objective_cost, bool use_chain_cumul_filter, std::vector< LocalSearchFilterManager::FilterEvent > *filters)
LocalSearchFilter * MakeResourceAssignmentFilter(LocalDimensionCumulOptimizer *optimizer, LocalDimensionCumulOptimizer *mp_optimizer, bool propagate_own_objective_value, bool filter_objective_cost)
IntVarLocalSearchFilter * MakeVehicleVarFilter(const RoutingModel &routing_model)
Returns a filter checking that vehicle variable domains are respected.
IntVarLocalSearchFilter * MakeTypeRegulationsFilter(const RoutingModel &routing_model)
Returns a filter ensuring type regulation constraints are enforced.
int64_t delta
Definition resource.cc:1709
int64_t start