OR-Tools  9.6
routing_cuts.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_SAT_ROUTING_CUTS_H_
15 #define OR_TOOLS_SAT_ROUTING_CUTS_H_
16 
17 #include <functional>
18 #include <optional>
19 #include <utility>
20 #include <vector>
21 
22 #include "ortools/sat/cp_model.pb.h"
23 #include "ortools/sat/cuts.h"
24 #include "ortools/sat/integer.h"
25 #include "ortools/sat/model.h"
26 
27 namespace operations_research {
28 namespace sat {
29 
30 // Given a graph with nodes in [0, num_nodes) and a set of arcs (the order is
31 // important), this will:
32 // - Start with each nodes in separate "subsets".
33 // - Consider the arc in order, and each time one connects two separate
34 // subsets, merge the two subsets into a new one.
35 // - Stops when there is only 2 subset left.
36 // - Output all subsets generated this way (at most 2 * num_nodes). The
37 // subsets spans will point in the subset_data vector (which will be of size
38 // exactly num_nodes).
39 //
40 // Only subsets of size >= min_subset_size will be returned. This is mainly here
41 // to exclude subsets of size 1.
42 //
43 // This is an heuristic to generate interesting cuts for TSP or other graph
44 // based constraints. We roughly follow the algorithm described in section 6 of
45 // "The Traveling Salesman Problem, A computational Study", David L. Applegate,
46 // Robert E. Bixby, Vasek Chvatal, William J. Cook.
47 //
48 // Note that this is mainly a "symmetric" case algo, but it does still work for
49 // the asymmetric case.
50 void GenerateInterestingSubsets(int num_nodes,
51  const std::vector<std::pair<int, int>>& arcs,
52  int min_subset_size, int stop_at_num_components,
53  std::vector<int>* subset_data,
54  std::vector<absl::Span<const int>>* subsets);
55 
56 // Cut generator for the circuit constraint, where in any feasible solution, the
57 // arcs that are present (variable at 1) must form a circuit through all the
58 // nodes of the graph. Self arc are forbidden in this case.
59 //
60 // In more generality, this currently enforce the resulting graph to be strongly
61 // connected. Note that we already assume basic constraint to be in the lp, so
62 // we do not add any cuts for components of size 1.
64  int num_nodes, std::vector<int> tails, std::vector<int> heads,
65  std::vector<Literal> literals, Model* model);
66 
67 // Almost the same as CreateStronglyConnectedGraphCutGenerator() but for each
68 // components, computes the demand needed to serves it, and depending on whether
69 // it contains the depot (node zero) or not, compute the minimum number of
70 // vehicle that needs to cross the component border.
71 CutGenerator CreateCVRPCutGenerator(int num_nodes, std::vector<int> tails,
72  std::vector<int> heads,
73  std::vector<Literal> literals,
74  std::vector<int64_t> demands,
75  int64_t capacity, Model* model);
76 
77 // Try to find a subset where the current LP capacity of the outgoing or
78 // incoming arc is not enough to satisfy the demands.
79 //
80 // We support the special value -1 for tail or head that means that the arc
81 // comes from (or is going to) outside the nodes in [0, num_nodes). Such arc
82 // must still have a capacity assigned to it.
83 //
84 // TODO(user): Support general linear expression for capacities.
85 // TODO(user): Some model applies the same capacity to both an arc and its
86 // reverse. Also support this case.
87 CutGenerator CreateFlowCutGenerator(
88  int num_nodes, const std::vector<int>& tails, const std::vector<int>& heads,
89  const std::vector<AffineExpression>& arc_capacities,
90  std::function<void(const std::vector<bool>& in_subset,
91  IntegerValue* min_incoming_flow,
92  IntegerValue* min_outgoing_flow)>
93  get_flows,
94  Model* model);
95 
96 } // namespace sat
97 } // namespace operations_research
98 
99 #endif // OR_TOOLS_SAT_ROUTING_CUTS_H_
GRBmodel * model
CutGenerator CreateStronglyConnectedGraphCutGenerator(int num_nodes, std::vector< int > tails, std::vector< int > heads, std::vector< Literal > literals, Model *model)
CutGenerator CreateCVRPCutGenerator(int num_nodes, std::vector< int > tails, std::vector< int > heads, std::vector< Literal > literals, std::vector< int64_t > demands, int64_t capacity, Model *model)
void GenerateInterestingSubsets(int num_nodes, const std::vector< std::pair< int, int >> &arcs, int min_subset_size, int stop_at_num_components, std::vector< int > *subset_data, std::vector< absl::Span< const int >> *subsets)
CutGenerator CreateFlowCutGenerator(int num_nodes, const std::vector< int > &tails, const std::vector< int > &heads, const std::vector< AffineExpression > &arc_capacities, std::function< void(const std::vector< bool > &in_subset, IntegerValue *min_incoming_flow, IntegerValue *min_outgoing_flow)> get_flows, Model *model)
Collection of objects used to extend the Constraint Solver library.
int64_t capacity