20 #ifndef OR_TOOLS_GRAPH_CHRISTOFIDES_H_
21 #define OR_TOOLS_GRAPH_CHRISTOFIDES_H_
23 #include "absl/status/status.h"
24 #include "absl/status/statusor.h"
37 using ::util::CompleteGraph;
46 #if defined(USE_CBC) || defined(USE_SCIP)
88 CompleteGraph<NodeIndex, ArcIndex> graph_;
91 const CostFunction costs_;
97 std::vector<NodeIndex> tsp_path_;
104 template <
typename WeightFunctionType,
typename GraphType>
105 absl::StatusOr<std::vector<
106 std::pair<typename GraphType::NodeIndex, typename GraphType::NodeIndex>>>
108 const WeightFunctionType&
weight) {
123 return absl::InvalidArgumentError(
"Perfect matching failed");
125 std::vector<std::pair<NodeIndex, NodeIndex>> match;
135 #if defined(USE_CBC) || defined(USE_SCIP)
140 template <
typename WeightFunctionType,
typename GraphType>
141 absl::StatusOr<std::vector<
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];
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",
191 #elif defined(USE_CBC)
192 MPSolver mp_solver(
"MatchingWithCBC",
199 return absl::InvalidArgumentError(
"MIP-based matching failed");
203 std::vector<std::pair<NodeIndex, NodeIndex>> matching;
204 for (
ArcIndex arc = 0; arc < variable_indices.size(); ++arc) {
205 const int arc_var = variable_indices[arc];
206 if (arc_var >= 0 &&
response.variable_value(arc_var) > .9) {
208 matching.emplace_back(graph.Tail(arc), graph.Head(arc));
216 typename CostFunction>
221 costs_(std::move(costs)),
226 typename CostFunction>
228 CostFunction>::TravelingSalesmanCost() {
230 bool const ok =
Solve();
237 typename CostFunction>
241 const bool ok =
Solve();
248 typename CostFunction>
251 const NodeIndex num_nodes = graph_.num_nodes();
254 if (num_nodes == 1) {
257 if (num_nodes <= 1) {
261 const std::vector<ArcIndex> mst =
263 return costs_(graph_.Tail(arc), graph_.Head(arc));
266 std::vector<NodeIndex> degrees(num_nodes, 0);
268 degrees[graph_.Tail(arc)]++;
269 degrees[graph_.Head(arc)]++;
271 std::vector<NodeIndex> odd_degree_nodes;
272 for (
int i = 0; i < degrees.size(); ++i) {
273 if (degrees[i] % 2 != 0) {
274 odd_degree_nodes.push_back(i);
279 const NodeIndex reduced_size = odd_degree_nodes.size();
281 CompleteGraph<NodeIndex, ArcIndex> reduced_graph(reduced_size);
282 std::vector<std::pair<NodeIndex, NodeIndex>> closure_arcs;
284 case MatchingAlgorithm::MINIMUM_WEIGHT_MATCHING: {
286 reduced_graph, [
this, &reduced_graph,
288 return costs_(odd_degree_nodes[reduced_graph.Tail(arc)],
289 odd_degree_nodes[reduced_graph.Head(arc)]);
294 result->swap(closure_arcs);
297 #if defined(USE_CBC) || defined(USE_SCIP)
298 case MatchingAlgorithm::MINIMUM_WEIGHT_MATCHING_WITH_MIP: {
300 reduced_graph, [
this, &reduced_graph,
302 return costs_(odd_degree_nodes[reduced_graph.Tail(arc)],
303 odd_degree_nodes[reduced_graph.Head(arc)]);
308 result->swap(closure_arcs);
312 case MatchingAlgorithm::MINIMAL_WEIGHT_MATCHING: {
315 std::vector<ArcIndex> ordered_arcs(reduced_graph.num_arcs());
316 std::vector<CostType> ordered_arc_costs(reduced_graph.num_arcs(), 0);
317 for (
const ArcIndex arc : reduced_graph.AllForwardArcs()) {
318 ordered_arcs[arc] = arc;
319 ordered_arc_costs[arc] =
320 costs_(odd_degree_nodes[reduced_graph.Tail(arc)],
321 odd_degree_nodes[reduced_graph.Head(arc)]);
323 std::sort(ordered_arcs.begin(), ordered_arcs.end(),
325 return ordered_arc_costs[arc_a] < ordered_arc_costs[arc_b];
327 std::vector<bool> touched_nodes(reduced_size,
false);
328 for (
ArcIndex arc_index = 0; closure_arcs.size() * 2 < reduced_size;
330 const ArcIndex arc = ordered_arcs[arc_index];
334 touched_nodes[
tail] =
true;
335 touched_nodes[
head] =
true;
336 closure_arcs.emplace_back(
tail,
head);
346 num_nodes, closure_arcs.size() + mst.size());
348 egraph.
AddArc(graph_.Tail(arc), graph_.Head(arc));
350 for (
const auto arc : closure_arcs) {
351 egraph.
AddArc(odd_degree_nodes[arc.first], odd_degree_nodes[arc.second]);
353 std::vector<bool> touched(num_nodes,
false);
356 if (touched[node])
continue;
357 touched[node] =
true;
358 tsp_cost_ = SafeAdd(tsp_cost_,
359 tsp_path_.empty() ? 0 : costs_(tsp_path_.back(), node));
360 tsp_path_.push_back(node);
363 SafeAdd(tsp_cost_, tsp_path_.empty() ? 0 : costs_(tsp_path_.back(), 0));
364 tsp_path_.push_back(0);
#define DCHECK_NE(val1, val2)
#define DCHECK_GE(val1, val2)
#define DCHECK(condition)
@ MINIMUM_WEIGHT_MATCHING_WITH_MIP
@ MINIMAL_WEIGHT_MATCHING
@ MINIMUM_WEIGHT_MATCHING
std::vector< NodeIndex > TravelingSalesmanPath()
ChristofidesPathSolver(NodeIndex num_nodes, CostFunction costs)
CostType TravelingSalesmanCost()
void SetMatchingAlgorithm(MatchingAlgorithm matching)
This mathematical programming (MP) solver class is the main class though which users build and solve ...
void FillSolutionResponseProto(MPSolutionResponse *response) const
Encodes the current solution in a solution response protocol buffer.
ResultStatus
The status of solving the problem.
@ SCIP_MIXED_INTEGER_PROGRAMMING
@ CBC_MIXED_INTEGER_PROGRAMMING
MPSolverResponseStatus LoadModelFromProto(const MPModelProto &input_model, std::string *error_message)
Loads model from protocol buffer.
ResultStatus Solve()
Solves the problem using the default parameter values.
ABSL_MUST_USE_RESULT Status Solve()
int Match(int node) const
void AddEdgeWithCost(int tail, int head, int64 cost)
ArcIndexType AddArc(NodeIndexType tail, NodeIndexType head)
SharedResponseManager * response
A C++ wrapper that provides a simple and unified interface to several linear programming and mixed in...
CpSolverResponse Solve(const CpModelProto &model_proto)
Solves the given CpModelProto and returns an instance of CpSolverResponse.
The vehicle routing library lets one model and solve generic vehicle routing problems ranging from th...
absl::StatusOr< std::vector< std::pair< typename GraphType::NodeIndex, typename GraphType::NodeIndex > > > ComputeMinimumWeightMatching(const GraphType &graph, const WeightFunctionType &weight)
std::vector< typename Graph::ArcIndex > BuildPrimMinimumSpanningTree(const Graph &graph, const ArcValue &arc_value)
absl::StatusOr< std::vector< std::pair< typename GraphType::NodeIndex, typename GraphType::NodeIndex > > > ComputeMinimumWeightMatchingWithMIP(const GraphType &graph, const WeightFunctionType &weight)
int64 CapAdd(int64 x, int64 y)
std::vector< NodeIndex > BuildEulerianTourFromNode(const Graph &graph, NodeIndex root)
bool IsEulerianGraph(const Graph &graph)