20 #ifndef OR_TOOLS_GRAPH_CHRISTOFIDES_H_
21 #define OR_TOOLS_GRAPH_CHRISTOFIDES_H_
23 #include "ortools/base/integral_types.h"
24 #include "ortools/base/logging.h"
28 #include "ortools/graph/perfect_matching.h"
29 #include "ortools/linear_solver/linear_solver.h"
30 #include "ortools/linear_solver/linear_solver.pb.h"
31 #include "ortools/util/saturated_arithmetic.h"
35 using ::util::CompleteGraph;
37 template <
typename CostType,
typename ArcIndex = int64,
44 #if defined(USE_CBC) || defined(USE_SCIP)
46 #endif // defined(USE_CBC) || defined(USE_SCIP)
80 int64 SafeAdd(int64 a, int64 b) {
88 CompleteGraph<NodeIndex, ArcIndex> graph_;
91 const CostFunction costs_;
97 std::vector<NodeIndex> tsp_path_;
104 template <
typename WeightFunctionType,
typename GraphType>
106 std::pair<typename GraphType::NodeIndex, typename GraphType::NodeIndex>>
108 const WeightFunctionType& weight) {
111 MinCostPerfectMatching matching(graph.num_nodes());
112 for (
NodeIndex tail : graph.AllNodes()) {
113 for (
const ArcIndex arc : graph.OutgoingArcs(tail)) {
117 matching.AddEdgeWithCost(tail, head, weight(arc));
121 MinCostPerfectMatching::Status status = matching.Solve();
122 DCHECK_EQ(status, MinCostPerfectMatching::OPTIMAL);
123 std::vector<std::pair<NodeIndex, NodeIndex>> match;
124 for (
NodeIndex tail : graph.AllNodes()) {
125 const NodeIndex head = matching.Match(tail);
127 match.emplace_back(tail, head);
133 #if defined(USE_CBC) || defined(USE_SCIP)
140 template <
typename WeightFunctionType,
typename GraphType>
142 std::pair<typename GraphType::NodeIndex, typename GraphType::NodeIndex>>
144 const WeightFunctionType& weight) {
148 model.set_maximize(
false);
153 std::vector<int> variable_indices(graph.num_arcs(), -1);
154 for (
NodeIndex node : graph.AllNodes()) {
156 for (
const ArcIndex arc : graph.OutgoingArcs(node)) {
159 variable_indices[arc] = model.variable_size();
160 MPVariableProto*
const arc_var = model.add_variable();
161 arc_var->set_lower_bound(0);
162 arc_var->set_upper_bound(1);
163 arc_var->set_is_integer(
true);
164 arc_var->set_objective_coefficient(weight(arc));
169 MPConstraintProto*
const one_of_ct = model.add_constraint();
170 one_of_ct->set_lower_bound(1);
171 one_of_ct->set_upper_bound(1);
173 for (
NodeIndex node : graph.AllNodes()) {
174 for (
const ArcIndex arc : graph.OutgoingArcs(node)) {
177 const int arc_var = variable_indices[arc];
178 DCHECK_GE(arc_var, 0);
179 MPConstraintProto* one_of_ct = model.mutable_constraint(node);
180 one_of_ct->add_var_index(arc_var);
181 one_of_ct->add_coefficient(1);
182 one_of_ct = model.mutable_constraint(head);
183 one_of_ct->add_var_index(arc_var);
184 one_of_ct->add_coefficient(1);
188 #if defined(USE_SCIP)
189 MPSolver mp_solver(
"MatchingWithSCIP",
190 MPSolver::SCIP_MIXED_INTEGER_PROGRAMMING);
191 #elif defined(USE_CBC)
192 MPSolver mp_solver(
"MatchingWithCBC",
193 MPSolver::CBC_MIXED_INTEGER_PROGRAMMING);
196 mp_solver.LoadModelFromProto(model, &error);
197 MPSolver::ResultStatus status = mp_solver.Solve();
198 CHECK_EQ(status, MPSolver::OPTIMAL);
199 MPSolutionResponse response;
200 mp_solver.FillSolutionResponseProto(&response);
201 std::vector<std::pair<NodeIndex, NodeIndex>> matching;
202 for (
ArcIndex arc = 0; arc < variable_indices.size(); ++arc) {
203 const int arc_var = variable_indices[arc];
204 if (arc_var >= 0 && response.variable_value(arc_var) > .9) {
205 DCHECK_GE(response.variable_value(arc_var), 1.0 - 1e-4);
206 matching.emplace_back(graph.Tail(arc), graph.Head(arc));
211 #endif // defined(USE_CBC) || defined(USE_SCIP)
214 typename CostFunction>
219 costs_(std::move(costs)),
224 typename CostFunction>
226 CostFunction>::TravelingSalesmanCost() {
234 typename CostFunction>
244 typename CostFunction>
246 CostFunction>::Solve() {
247 const NodeIndex num_nodes = graph_.num_nodes();
250 if (num_nodes == 1) {
253 if (num_nodes <= 1) {
257 const std::vector<ArcIndex> mst =
259 return costs_(graph_.Tail(arc), graph_.Head(arc));
262 std::vector<NodeIndex> degrees(num_nodes, 0);
264 degrees[graph_.Tail(arc)]++;
265 degrees[graph_.Head(arc)]++;
267 std::vector<NodeIndex> odd_degree_nodes;
268 for (
int i = 0; i < degrees.size(); ++i) {
269 if (degrees[i] % 2 != 0) {
270 odd_degree_nodes.push_back(i);
275 const NodeIndex reduced_size = odd_degree_nodes.size();
276 DCHECK_NE(0, reduced_size);
277 CompleteGraph<NodeIndex, ArcIndex> reduced_graph(reduced_size);
278 std::vector<std::pair<NodeIndex, NodeIndex>> closure_arcs;
280 case MatchingAlgorithm::MINIMUM_WEIGHT_MATCHING: {
282 reduced_graph, [
this, &reduced_graph,
284 return costs_(odd_degree_nodes[reduced_graph.Tail(arc)],
285 odd_degree_nodes[reduced_graph.Head(arc)]);
289 #if defined(USE_CBC) || defined(USE_SCIP)
290 case MatchingAlgorithm::MINIMUM_WEIGHT_MATCHING_WITH_MIP: {
292 reduced_graph, [
this, &reduced_graph,
294 return costs_(odd_degree_nodes[reduced_graph.Tail(arc)],
295 odd_degree_nodes[reduced_graph.Head(arc)]);
299 #endif // defined(USE_CBC) || defined(USE_SCIP)
300 case MatchingAlgorithm::MINIMAL_WEIGHT_MATCHING: {
303 std::vector<ArcIndex> ordered_arcs(reduced_graph.num_arcs());
304 std::vector<CostType> ordered_arc_costs(reduced_graph.num_arcs(), 0);
305 for (
const ArcIndex arc : reduced_graph.AllForwardArcs()) {
306 ordered_arcs[arc] = arc;
307 ordered_arc_costs[arc] =
308 costs_(odd_degree_nodes[reduced_graph.Tail(arc)],
309 odd_degree_nodes[reduced_graph.Head(arc)]);
311 std::sort(ordered_arcs.begin(), ordered_arcs.end(),
313 return ordered_arc_costs[arc_a] < ordered_arc_costs[arc_b];
315 std::vector<bool> touched_nodes(reduced_size,
false);
316 for (
ArcIndex arc_index = 0; closure_arcs.size() * 2 < reduced_size;
318 const ArcIndex arc = ordered_arcs[arc_index];
319 const NodeIndex tail = reduced_graph.Tail(arc);
320 const NodeIndex head = reduced_graph.Head(arc);
321 if (head != tail && !touched_nodes[tail] && !touched_nodes[head]) {
322 touched_nodes[tail] =
true;
323 touched_nodes[head] =
true;
324 closure_arcs.emplace_back(tail, head);
334 num_nodes, closure_arcs.size() + mst.size());
336 egraph.
AddArc(graph_.Tail(arc), graph_.Head(arc));
338 for (
const auto arc : closure_arcs) {
339 egraph.AddArc(odd_degree_nodes[arc.first], odd_degree_nodes[arc.second]);
341 std::vector<bool> touched(num_nodes,
false);
344 if (touched[node])
continue;
345 touched[node] =
true;
346 tsp_cost_ = SafeAdd(tsp_cost_,
347 tsp_path_.empty() ? 0 : costs_(tsp_path_.back(), node));
348 tsp_path_.push_back(node);
351 SafeAdd(tsp_cost_, tsp_path_.empty() ? 0 : costs_(tsp_path_.back(), 0));
352 tsp_path_.push_back(0);
357 #endif // OR_TOOLS_GRAPH_CHRISTOFIDES_H_