OR-Tools  9.6
graph/assignment.cc
Go to the documentation of this file.
1 // Copyright 2010-2022 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 
23 namespace 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 
39 NodeIndex SimpleLinearSumAssignment::NumNodes() const { return num_nodes_; }
40 
41 ArcIndex SimpleLinearSumAssignment::NumArcs() const { return arc_cost_.size(); }
42 
44  return arc_tail_[arc];
45 }
46 
48  return arc_head_[arc];
49 }
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 =
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
int64_t max
Definition: alldiff_cst.cc:140
ArcIndexType AddArc(NodeIndexType tail, NodeIndexType head)
Definition: ebert_graph.h:1002
ArcIndex GetAssignmentArc(NodeIndex left_node) const
void SetArcCost(ArcIndex arc, CostValue cost)
ArcIndex AddArcWithCost(NodeIndex left_node, NodeIndex right_node, CostValue cost)
int arc
Collection of objects used to extend the Constraint Solver library.
int64_t cost