Google OR-Tools v9.15
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-2025 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 "ortools/graph/graph.h"
21
22namespace operations_research {
23
25
27 NodeIndex left_node, NodeIndex right_node, CostValue cost) {
28 const ArcIndex num_arcs = arc_cost_.size();
29 num_nodes_ = std::max(num_nodes_, left_node + 1);
30 num_nodes_ = std::max(num_nodes_, right_node + 1);
31 arc_tail_.push_back(left_node);
32 arc_head_.push_back(right_node);
33 arc_cost_.push_back(cost);
34 return num_arcs;
35}
36
41
45
50
55
60
62 optimal_cost_ = 0;
63 assignment_arcs_.clear();
64 if (NumNodes() == 0) return OPTIMAL;
65 // HACK(user): Detect overflows early. In ./linear_assignment.h, the cost of
66 // each arc is internally multiplied by cost_scaling_factor_ (which is equal
67 // to (num_nodes + 1)) without overflow checking.
68 const CostValue max_supported_arc_cost =
69 std::numeric_limits<CostValue>::max() / (NumNodes() + 1);
70 for (const CostValue unscaled_arc_cost : arc_cost_) {
71 if (unscaled_arc_cost > max_supported_arc_cost) return POSSIBLE_OVERFLOW;
72 }
73
74 const ArcIndex num_arcs = arc_cost_.size();
75 ::util::ListGraph<> graph(2 * num_nodes_, num_arcs);
77 num_nodes_);
78 for (ArcIndex arc = 0; arc < num_arcs; ++arc) {
79 graph.AddArc(arc_tail_[arc], num_nodes_ + arc_head_[arc]);
80 assignment.SetArcCost(arc, arc_cost_[arc]);
81 }
82 // TODO(user): Improve the LinearSumAssignment api to clearly define
83 // the error cases.
84 if (!assignment.FinalizeSetup()) return POSSIBLE_OVERFLOW;
85 if (!assignment.ComputeAssignment()) return INFEASIBLE;
86 optimal_cost_ = assignment.GetCost();
87 for (NodeIndex node = 0; node < num_nodes_; ++node) {
88 assignment_arcs_.push_back(assignment.GetAssignmentArc(node));
89 }
90 return OPTIMAL;
91}
92
93} // namespace operations_research
void SetArcCost(ArcIndex arc, CostValue cost)
ArcIndex GetAssignmentArc(NodeIndex left_node) const
NodeIndex RightNode(ArcIndex arc) const
Definition assignment.cc:51
ArcIndex AddArcWithCost(NodeIndex left_node, NodeIndex right_node, CostValue cost)
Definition assignment.cc:26
NodeIndex LeftNode(ArcIndex arc) const
Definition assignment.cc:46
ArcIndexType AddArc(NodeIndexType tail, NodeIndexType head)
Definition graph.h:1355
OR-Tools root namespace.