Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
assignment.cc
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
15
16#include <algorithm>
17#include <limits>
18
19#include "absl/flags/flag.h"
22
23namespace operations_research {
24
26
28 NodeIndex right_node,
29 CostValue cost) {
30 const ArcIndex num_arcs = arc_cost_.size();
31 num_nodes_ = std::max(num_nodes_, left_node + 1);
32 num_nodes_ = std::max(num_nodes_, right_node + 1);
33 arc_tail_.push_back(left_node);
34 arc_head_.push_back(right_node);
35 arc_cost_.push_back(cost);
36 return num_arcs;
37}
38
39NodeIndex SimpleLinearSumAssignment::NumNodes() const { return num_nodes_; }
40
41ArcIndex SimpleLinearSumAssignment::NumArcs() const { return arc_cost_.size(); }
42
46
50
52 return arc_cost_[arc];
53}
54
56 optimal_cost_ = 0;
57 assignment_arcs_.clear();
58 if (NumNodes() == 0) return OPTIMAL;
59 // HACK(user): Detect overflows early. In ./linear_assignment.h, the cost of
60 // each arc is internally multiplied by cost_scaling_factor_ (which is equal
61 // to (num_nodes + 1)) without overflow checking.
62 const CostValue max_supported_arc_cost =
63 std::numeric_limits<CostValue>::max() / (NumNodes() + 1);
64 for (const CostValue unscaled_arc_cost : arc_cost_) {
65 if (unscaled_arc_cost > max_supported_arc_cost) return POSSIBLE_OVERFLOW;
66 }
67
68 const ArcIndex num_arcs = arc_cost_.size();
69 ForwardStarGraph graph(2 * num_nodes_, num_arcs);
70 LinearSumAssignment<ForwardStarGraph> assignment(graph, num_nodes_);
71 for (ArcIndex arc = 0; arc < num_arcs; ++arc) {
72 graph.AddArc(arc_tail_[arc], num_nodes_ + arc_head_[arc]);
73 assignment.SetArcCost(arc, arc_cost_[arc]);
74 }
75 // TODO(user): Improve the LinearSumAssignment api to clearly define
76 // the error cases.
77 if (!assignment.FinalizeSetup()) return POSSIBLE_OVERFLOW;
78 if (!assignment.ComputeAssignment()) return INFEASIBLE;
79 optimal_cost_ = assignment.GetCost();
80 for (NodeIndex node = 0; node < num_nodes_; ++node) {
81 assignment_arcs_.push_back(assignment.GetAssignmentArc(node));
82 }
83 return OPTIMAL;
84}
85
86} // namespace operations_research
This class does not take ownership of its underlying graph.
void SetArcCost(ArcIndex arc, CostValue cost)
Sets the cost of an arc already present in the given graph.
ArcIndex GetAssignmentArc(NodeIndex left_node) const
Returns the arc through which the given node is matched.
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 LeftNode(ArcIndex arc) const
Definition assignment.cc:43
GraphType graph
int arc
In SWIG mode, we don't want anything besides these top-level includes.