Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
assignment.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// Simple interface to solve the linear sum assignment problem. It
15// uses about twice as much memory as directly using the
16// LinearSumAssignment class template, but it is as fast and presents
17// a simpler interface. This is the class you should use in most
18// situations.
19//
20// The assignment problem: Given N "left" nodes and N "right" nodes,
21// and a set of left->right arcs with integer costs, find a perfect
22// matching (i.e., each "left" node is assigned to one "right" node)
23// that minimizes the overall cost.
24//
25// Example usage:
26//
27// #include "ortools/graph/assignment.h"
28//
29// SimpleLinearSumAssignment assignment;
30// for (int arc = 0; arc < num_arcs; ++arc) {
31// assignment.AddArcWithCost(head(arc), tail(arc), cost(arc));
32// }
33// if (assignment.Solve() == SimpleLinearSumAssignment::OPTIMAL) {
34// printf("A perfect matching exists.\n");
35// printf("The best possible cost is %d.\n", assignment.OptimalCost());
36// printf("An optimal assignment is:\n");
37// for (int node = 0; node < assignment.NumNodes(); ++node) {
38// printf("left node %d assigned to right node %d with cost %d.\n",
39// node,
40// assignment.RightMate(node),
41// assignment.AssignmentCost(node));
42// }
43// printf("Note that it may not be the unique optimal assignment.");
44// } else {
45// printf("There is an issue with the input or no perfect matching exists.");
46// }
47
48#ifndef OR_TOOLS_GRAPH_ASSIGNMENT_H_
49#define OR_TOOLS_GRAPH_ASSIGNMENT_H_
50
51#include <vector>
52
54
55namespace operations_research {
56
58 public:
59 // The constructor takes no size.
60 // New node indices will be created lazily by AddArcWithCost().
62
63#ifndef SWIG
64 // This type is neither copyable nor movable.
67 delete;
68#endif
69
70 // Adds an arc from a left node to a right node with a given cost.
71 // * Node indices must be non-negative (>= 0). For a perfect
72 // matching to exist on n nodes, the values taken by "left_node"
73 // must cover [0, n), same for "right_node".
74 // * The arc cost can be any integer, negative, positive or zero.
75 // * After the method finishes, NumArcs() == the returned ArcIndex + 1.
76 ArcIndex AddArcWithCost(NodeIndex left_node, NodeIndex right_node,
77 CostValue cost);
78
79 // Returns the current number of left nodes which is the same as the
80 // number of right nodes. This is one greater than the largest node
81 // index seen so far in AddArcWithCost().
82 NodeIndex NumNodes() const;
83
84 // Returns the current number of arcs in the graph.
85 ArcIndex NumArcs() const;
86
87 // Returns user-provided data.
88 // The implementation will crash if "arc" is not in [0, NumArcs()).
92
93 // Solves the problem (finds the perfect matching that minimizes the
94 // cost) and returns the solver status.
95 enum Status {
96 OPTIMAL, // The algorithm found a minimum-cost perfect matching.
97 INFEASIBLE, // The given problem admits no perfect matching.
98 POSSIBLE_OVERFLOW, // Some cost magnitude is too large.
99 };
100 Status Solve();
101
102 // Returns the cost of an assignment with minimal cost.
103 // This is 0 if the last Solve() didn't return OPTIMAL.
104 CostValue OptimalCost() const { return optimal_cost_; }
105
106 // Returns the right node assigned to the given left node in the
107 // last solution computed by Solve(). This works only if Solve()
108 // returned OPTIMAL.
109 //
110 // Note: It is possible that there is more than one optimal
111 // solution. The algorithm is deterministic so it will always return
112 // the same solution for a given problem. There is no such guarantee
113 // from one code version to the next, but the code does not change
114 // often.
115 NodeIndex RightMate(NodeIndex left_node) const {
116 return arc_head_[assignment_arcs_[left_node]];
117 }
118
119 // Returns the cost of the arc used for "left_node"'s assignment.
120 // This works only if Solve() returned OPTIMAL.
122 return arc_cost_[assignment_arcs_[left_node]];
123 }
124
125 private:
126 NodeIndex num_nodes_;
127 std::vector<NodeIndex> arc_tail_;
128 std::vector<NodeIndex> arc_head_;
129 std::vector<CostValue> arc_cost_;
130 std::vector<ArcIndex> assignment_arcs_;
131 CostValue optimal_cost_;
132};
133
134} // namespace operations_research
135
136#endif // OR_TOOLS_GRAPH_ASSIGNMENT_H_
SimpleLinearSumAssignment(const SimpleLinearSumAssignment &)=delete
This type is neither copyable nor movable.
NodeIndex RightNode(ArcIndex arc) const
Definition assignment.cc:47
ArcIndex AddArcWithCost(NodeIndex left_node, NodeIndex right_node, CostValue cost)
Definition assignment.cc:27
ArcIndex NumArcs() const
Returns the current number of arcs in the graph.
Definition assignment.cc:41
NodeIndex RightMate(NodeIndex left_node) const
Definition assignment.h:115
NodeIndex LeftNode(ArcIndex arc) const
Definition assignment.cc:43
CostValue AssignmentCost(NodeIndex left_node) const
Definition assignment.h:121
SimpleLinearSumAssignment & operator=(const SimpleLinearSumAssignment &)=delete
int arc
In SWIG mode, we don't want anything besides these top-level includes.
double Cost
Basic non-strict type for cost. The speed penalty for using double is ~2%.