Files
ortools-clone/ortools/graph/assignment.cc

94 lines
3.3 KiB
C++
Raw Permalink Normal View History

2025-01-10 11:35:44 +01:00
// Copyright 2010-2025 Google LLC
2013-06-11 15:07:14 +00:00
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "ortools/graph/assignment.h"
2013-06-11 15:07:14 +00:00
2022-03-15 13:57:17 +01:00
#include <algorithm>
2022-07-22 14:35:40 +02:00
#include <limits>
2022-03-15 13:57:17 +01:00
#include "ortools/graph/graph.h"
#include "ortools/graph/linear_assignment.h"
2013-06-11 15:07:14 +00:00
namespace operations_research {
SimpleLinearSumAssignment::SimpleLinearSumAssignment() : num_nodes_(0) {}
2025-01-06 13:20:57 +01:00
SimpleLinearSumAssignment::ArcIndex SimpleLinearSumAssignment::AddArcWithCost(
NodeIndex left_node, NodeIndex right_node, CostValue cost) {
2013-06-11 15:07:14 +00:00
const ArcIndex num_arcs = arc_cost_.size();
num_nodes_ = std::max(num_nodes_, left_node + 1);
num_nodes_ = std::max(num_nodes_, right_node + 1);
2013-06-11 15:07:14 +00:00
arc_tail_.push_back(left_node);
arc_head_.push_back(right_node);
2013-06-11 15:07:14 +00:00
arc_cost_.push_back(cost);
return num_arcs;
}
2025-01-06 13:20:57 +01:00
SimpleLinearSumAssignment::NodeIndex SimpleLinearSumAssignment::NumNodes()
const {
return num_nodes_;
}
2013-06-11 15:07:14 +00:00
2025-01-06 13:20:57 +01:00
SimpleLinearSumAssignment::ArcIndex SimpleLinearSumAssignment::NumArcs() const {
return arc_cost_.size();
}
2013-06-11 15:07:14 +00:00
2025-01-06 13:20:57 +01:00
SimpleLinearSumAssignment::NodeIndex SimpleLinearSumAssignment::LeftNode(
ArcIndex arc) const {
2013-06-11 15:07:14 +00:00
return arc_tail_[arc];
}
2025-01-06 13:20:57 +01:00
SimpleLinearSumAssignment::NodeIndex SimpleLinearSumAssignment::RightNode(
ArcIndex arc) const {
2013-06-11 15:07:14 +00:00
return arc_head_[arc];
}
2025-01-06 13:20:57 +01:00
SimpleLinearSumAssignment::CostValue SimpleLinearSumAssignment::Cost(
ArcIndex arc) const {
2013-06-11 15:07:14 +00:00
return arc_cost_[arc];
}
SimpleLinearSumAssignment::Status SimpleLinearSumAssignment::Solve() {
optimal_cost_ = 0;
assignment_arcs_.clear();
2020-10-22 23:36:58 +02:00
if (NumNodes() == 0) return OPTIMAL;
// HACK(user): Detect overflows early. In ./linear_assignment.h, the cost of
// each arc is internally multiplied by cost_scaling_factor_ (which is equal
// to (num_nodes + 1)) without overflow checking.
const CostValue max_supported_arc_cost =
std::numeric_limits<CostValue>::max() / (NumNodes() + 1);
for (const CostValue unscaled_arc_cost : arc_cost_) {
2020-10-22 23:36:58 +02:00
if (unscaled_arc_cost > max_supported_arc_cost) return POSSIBLE_OVERFLOW;
}
2013-06-11 15:07:14 +00:00
const ArcIndex num_arcs = arc_cost_.size();
::util::ListGraph<> graph(2 * num_nodes_, num_arcs);
LinearSumAssignment<::util::ListGraph<>, CostValue> assignment(graph,
num_nodes_);
2013-06-11 15:07:14 +00:00
for (ArcIndex arc = 0; arc < num_arcs; ++arc) {
graph.AddArc(arc_tail_[arc], num_nodes_ + arc_head_[arc]);
assignment.SetArcCost(arc, arc_cost_[arc]);
}
// TODO(user): Improve the LinearSumAssignment api to clearly define
// the error cases.
2020-10-22 23:36:58 +02:00
if (!assignment.FinalizeSetup()) return POSSIBLE_OVERFLOW;
if (!assignment.ComputeAssignment()) return INFEASIBLE;
2013-06-11 15:07:14 +00:00
optimal_cost_ = assignment.GetCost();
for (NodeIndex node = 0; node < num_nodes_; ++node) {
assignment_arcs_.push_back(assignment.GetAssignmentArc(node));
}
return OPTIMAL;
}
2020-10-22 23:36:58 +02:00
} // namespace operations_research