OR-Tools  9.6
routing_filters.h
Go to the documentation of this file.
1 // Copyright 2010-2022 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 <utility>
18 #include <vector>
19 
25 #include "ortools/constraint_solver/routing_parameters.pb.h"
26 #include "ortools/util/bitset.h"
27 
28 namespace operations_research {
29 
31 IntVarLocalSearchFilter* MakeMaxActiveVehiclesFilter(
32  const RoutingModel& routing_model);
33 
35 IntVarLocalSearchFilter* MakeNodeDisjunctionFilter(
36  const RoutingModel& routing_model, bool filter_cost);
37 
39 IntVarLocalSearchFilter* MakeVehicleAmortizedCostFilter(
40  const RoutingModel& routing_model);
41 
43 IntVarLocalSearchFilter* MakeTypeRegulationsFilter(
44  const RoutingModel& routing_model);
45 
48 IntVarLocalSearchFilter* MakePickupDeliveryFilter(
49  const RoutingModel& routing_model, const RoutingModel::IndexPairs& pairs,
50  const std::vector<RoutingModel::PickupAndDeliveryPolicy>& vehicle_policies);
51 
53 IntVarLocalSearchFilter* MakeVehicleVarFilter(
54  const RoutingModel& routing_model);
55 
57 IntVarLocalSearchFilter* MakePathCumulFilter(const RoutingDimension& dimension,
58  bool propagate_own_objective_value,
59  bool filter_objective_cost,
60  bool can_use_lp);
61 
63 IntVarLocalSearchFilter* MakeCumulBoundsPropagatorFilter(
64  const RoutingDimension& dimension);
65 
67 IntVarLocalSearchFilter* MakeGlobalLPCumulFilter(
68  GlobalDimensionCumulOptimizer* optimizer,
69  GlobalDimensionCumulOptimizer* mp_optimizer, bool filter_objective_cost);
70 
73 LocalSearchFilter* MakeResourceAssignmentFilter(
74  LocalDimensionCumulOptimizer* optimizer,
75  LocalDimensionCumulOptimizer* mp_optimizer,
76  bool propagate_own_objective_value, bool filter_objective_cost);
77 
79 IntVarLocalSearchFilter* MakeCPFeasibilityFilter(RoutingModel* routing_model);
80 
84  const PathState* path_state,
85  const std::vector<RoutingDimension*>& dimensions,
86  std::vector<LocalSearchFilterManager::FilterEvent>* filters);
87 
89  const std::vector<RoutingDimension*>& dimensions,
90  const RoutingSearchParameters& parameters, bool filter_objective_cost,
91  bool use_chain_cumul_filter,
92  std::vector<LocalSearchFilterManager::FilterEvent>* filters);
93 
95 
97  public:
98  BasePathFilter(const std::vector<IntVar*>& nexts, int next_domain_size);
99  ~BasePathFilter() override {}
100  bool Accept(const Assignment* delta, const Assignment* deltadelta,
101  int64_t objective_min, int64_t objective_max) override;
102  void OnSynchronize(const Assignment* delta) override;
103 
104  protected:
105  static const int64_t kUnassigned;
106 
107  int64_t GetNext(int64_t node) const {
108  return (new_nexts_[node] == kUnassigned)
109  ? (IsVarSynced(node) ? Value(node) : kUnassigned)
110  : new_nexts_[node];
111  }
112  int NumPaths() const { return starts_.size(); }
113  int64_t Start(int i) const { return starts_[i]; }
114  int GetPath(int64_t node) const { return paths_[node]; }
115  int Rank(int64_t node) const { return ranks_[node]; }
116  bool IsDisabled() const { return status_ == DISABLED; }
117  const std::vector<int64_t>& GetTouchedPathStarts() const {
118  return touched_paths_.PositionsSetAtLeastOnce();
119  }
120  bool PathStartTouched(int64_t start) const { return touched_paths_[start]; }
121  const std::vector<int64_t>& GetNewSynchronizedUnperformedNodes() const {
122  return new_synchronized_unperformed_nodes_.PositionsSetAtLeastOnce();
123  }
124 
125  bool lns_detected() const { return lns_detected_; }
126 
127  private:
128  enum Status { UNKNOWN, ENABLED, DISABLED };
129 
130  virtual bool DisableFiltering() const { return false; }
131  virtual void OnBeforeSynchronizePaths() {}
132  virtual void OnAfterSynchronizePaths() {}
133  virtual void OnSynchronizePathFromStart(int64_t start) {}
134  virtual bool InitializeAcceptPath() { return true; }
135  virtual bool AcceptPath(int64_t path_start, int64_t chain_start,
136  int64_t chain_end) = 0;
137  virtual bool FinalizeAcceptPath(int64_t objective_min,
138  int64_t objective_max) {
139  return true;
140  }
142  void ComputePathStarts(std::vector<int64_t>* path_starts,
143  std::vector<int>* index_to_path);
144  bool HavePathsChanged();
145  void SynchronizeFullAssignment();
146  void UpdateAllRanks();
147  void UpdatePathRanksFromStart(int start);
148 
149  std::vector<int64_t> node_path_starts_;
150  std::vector<int64_t> starts_;
151  std::vector<int> paths_;
152  SparseBitset<int64_t> new_synchronized_unperformed_nodes_;
153  std::vector<int64_t> new_nexts_;
154  std::vector<int> delta_touched_;
155  SparseBitset<> touched_paths_;
156  // clang-format off
157  std::vector<std::pair<int64_t, int64_t> > touched_path_chain_start_ends_;
158  // clang-format on
159  std::vector<int> ranks_;
160 
161  Status status_;
162  bool lns_detected_;
163 };
164 
165 } // namespace operations_research
166 
167 #endif // OR_TOOLS_CONSTRAINT_SOLVER_ROUTING_FILTERS_H_
std::vector< int > dimensions
An Assignment is a variable -> domains mapping, used to report solutions to the user.
Generic path-based filter class.
bool Accept(const Assignment *delta, const Assignment *deltadelta, int64_t objective_min, int64_t objective_max) override
Accepts a "delta" given the assignment with which the filter has been synchronized; the delta holds t...
bool PathStartTouched(int64_t start) const
const std::vector< int64_t > & GetTouchedPathStarts() const
BasePathFilter(const std::vector< IntVar * > &nexts, int next_domain_size)
int64_t GetNext(int64_t node) const
void OnSynchronize(const Assignment *delta) override
const std::vector< int64_t > & GetNewSynchronizedUnperformedNodes() const
RoutingIndexPairs IndexPairs
Definition: routing.h:287
const std::vector< IntegerType > & PositionsSetAtLeastOnce() const
Definition: bitset.h:806
SatParameters parameters
Collection of objects used to extend the Constraint Solver library.
IntVarLocalSearchFilter * MakeCumulBoundsPropagatorFilter(const RoutingDimension &dimension)
Returns a filter handling dimension cumul bounds.
IntVarLocalSearchFilter * MakeVehicleAmortizedCostFilter(const RoutingModel &routing_model)
Returns a filter computing vehicle amortized costs.
IntVarLocalSearchFilter * MakeCPFeasibilityFilter(RoutingModel *routing_model)
Returns a filter checking the current solution using CP propagation.
LocalSearchFilter * MakeResourceAssignmentFilter(LocalDimensionCumulOptimizer *optimizer, LocalDimensionCumulOptimizer *mp_optimizer, bool propagate_own_objective_value, bool filter_objective_cost)
Returns a filter checking the feasibility and cost of the resource assignment.
IntVarLocalSearchFilter * MakeGlobalLPCumulFilter(GlobalDimensionCumulOptimizer *optimizer, GlobalDimensionCumulOptimizer *mp_optimizer, bool filter_objective_cost)
Returns a filter checking global linear constraints and costs.
IntVarLocalSearchFilter * MakeMaxActiveVehiclesFilter(const RoutingModel &routing_model)
Returns a filter ensuring that max active vehicles constraints are enforced.
void AppendDimensionCumulFilters(const std::vector< RoutingDimension * > &dimensions, const RoutingSearchParameters &parameters, bool filter_objective_cost, bool use_chain_cumul_filter, std::vector< LocalSearchFilterManager::FilterEvent > *filters)
IntVarLocalSearchFilter * MakeVehicleVarFilter(const RoutingModel &routing_model)
Returns a filter checking that vehicle variable domains are respected.
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 RoutingModel::IndexPairs &pairs, const std::vector< RoutingModel::PickupAndDeliveryPolicy > &vehicle_policies)
Returns a filter enforcing pickup and delivery constraints for the given pair of nodes and given poli...
IntVarLocalSearchFilter * MakeTypeRegulationsFilter(const RoutingModel &routing_model)
Returns a filter ensuring type regulation constraints are enforced.
void AppendLightWeightDimensionFilters(const PathState *path_state, const std::vector< RoutingDimension * > &dimensions, std::vector< LocalSearchFilterManager::FilterEvent > *filters)
Appends dimension-based filters to the given list of filters using a path state.
IntVarLocalSearchFilter * MakeNodeDisjunctionFilter(const RoutingModel &routing_model, bool filter_cost)
Returns a filter ensuring that node disjunction constraints are enforced.
int64_t delta
Definition: resource.cc:1695
int64_t start