79#include <initializer_list>
96template <
typename Cost_Type =
double>
99 static_assert(std::is_signed_v<std::remove_cv_t<Cost_Type>> or
100 std::is_floating_point_v<std::remove_cv_t<Cost_Type>>,
101 "Cost_Type must be signed or floating-point to avoid "
102 "underflow when negating costs");
121 pairs.
append(std::make_pair(i,
static_cast<size_t>(j)));
140template <
typename Cost_Type =
double>
144 static_assert(std::is_signed_v<std::remove_cv_t<Cost_Type>> or
145 std::is_floating_point_v<std::remove_cv_t<Cost_Type>>,
146 "Cost_Type must be signed or floating-point to avoid "
147 "underflow when negating costs");
155 static constexpr bool Integer = std::is_integral_v<Cost_Type>;
160 using Wide = std::conditional_t<
Integer and sizeof(Cost_Type) <
sizeof(
long long),
161 long long, Cost_Type>;
169 void add(
const Cost_Type & c)
noexcept
181 ah_overflow_error() <<
"Hungarian_Assignment: the costs, with the potentials, come too "
182 <<
"near the limits of their integer type";
192 const long n =
static_cast<long>(
n_);
193 const auto sz =
static_cast<size_t>(n + 1);
202 const Wide Inf = std::numeric_limits<Wide>::max() / 2;
213 constexpr Wide Wide_Max = std::numeric_limits<Wide>::max();
214 [[maybe_unused]]
Wide room{-1};
216 if (
Wide(
range.lo) != std::numeric_limits<Wide>::lowest())
219 if (c <= Wide_Max / 4)
220 room =
Wide((Wide_Max / 4 - c) / 2);
224 for (
long i = 1; i <= n; ++i)
244 const long i0 = p(
j0);
252 for (
long j = 1; j <= n; ++j)
259 Wide(cost.
read(
static_cast<size_t>(i0 - 1),
static_cast<size_t>(j - 1)))
262 if (reduced < dist(j))
276 for (
long j = 0; j <= n; ++j)
288 <<
"Hungarian_Assignment: the costs are too large for their type";
291 room -= delta >= 0 ? delta :
Wide(-delta);
294 }
while (p(
j0) != 0);
299 const long j1 = way(
j0);
308 if (-v(0) >
Wide(std::numeric_limits<Cost_Type>::max())
309 or -v(0) <
Wide(std::numeric_limits<Cost_Type>::lowest()))
316 for (
long j = 1; j <= n; ++j)
318 const long row = p(j) - 1;
319 const long col = j - 1;
349 <<
"Hungarian_Assignment: cost matrix must not be empty";
351 if constexpr (std::is_floating_point_v<Cost_Type>)
355 <<
"Hungarian_Assignment: cost[" << i <<
"][" << j <<
"] is not finite";
366 const Cost_Type c = cost.
read(i, j);
400 <<
"Hungarian_Assignment: cost matrix must not be empty";
405 <<
"Hungarian_Assignment: cost matrix must not have zero columns";
416 <<
"Hungarian_Assignment: all rows must have the same length";
418 for (
const auto &val :
row)
420 if constexpr (std::is_floating_point_v<Cost_Type>)
422 <<
"Hungarian_Assignment: non-finite cost value at row " << i;
423 padded(i, j++) = val;
447 :
n_(std::exchange(other.n_, 0)),
orig_rows_(std::exchange(other.orig_rows_, 0)),
448 orig_cols_(std::exchange(other.orig_cols_, 0)),
449 total_cost_(std::exchange(other.total_cost_, Cost_Type{0})),
476 std::swap(
n_, other.n_);
517 pairs.
append(std::make_pair(i,
static_cast<size_t>(j)));
574 [[nodiscard]]
size_t rows() const noexcept
582 [[nodiscard]]
size_t cols() const noexcept
606template <
typename Cost_Type>
609 static_assert(std::is_signed_v<std::remove_cv_t<Cost_Type>> or
610 std::is_floating_point_v<std::remove_cv_t<Cost_Type>>,
611 "Cost_Type must be signed or floating-point to avoid "
612 "underflow when negating costs");
620 result.
row_to_col = std::move(ha).extract_row_assignments();
621 result.
col_to_row = std::move(ha).extract_col_assignments();
647template <
typename Cost_Type>
650 static_assert(std::is_signed_v<std::remove_cv_t<Cost_Type>> or
651 std::is_floating_point_v<std::remove_cv_t<Cost_Type>>,
652 "Cost_Type must be signed or floating-point to avoid "
653 "underflow when negating costs");
657 using Common = std::common_type_t<Cost_Type, long long>;
658 using Promoted =
typename std::conditional_t<std::is_floating_point_v<Common>,
659 std::type_identity<Common>,
660 std::make_signed<Common>>::type;
666 for (
size_t i = 0; i <
rows; ++i)
667 for (
size_t j = 0; j <
cols; ++j)
669 if constexpr (std::is_integral_v<Promoted>
and std::is_signed_v<Promoted>)
671 auto v =
static_cast<Promoted
>(cost.
read(i, j));
673 <<
"Cannot negate minimum integer value";
677 negated(i, j) = -
static_cast<Promoted
>(cost.
read(i, j));
684 if constexpr (std::is_integral_v<Promoted>)
686 or -inner.total_cost > Promoted(std::numeric_limits<Cost_Type>::max())
687 or -inner.total_cost < Promoted(std::numeric_limits<Cost_Type>::lowest()))
688 <<
"hungarian_max_assignment(): the maximum does not fit in the type of the costs";
691 result.
total_cost =
static_cast<Cost_Type
>(-inner.total_cost);
692 result.
row_to_col = std::move(inner.row_to_col);
693 result.
col_to_row = std::move(inner.col_to_row);
Exception handling system with formatted messages for Aleph-w.
#define ah_out_of_range_error_if(C)
Throws std::out_of_range if condition holds.
#define ah_overflow_error_if(C)
Throws std::overflow_error if condition holds.
#define ah_overflow_error()
Throws std::overflow_error unconditionally.
#define ah_invalid_argument_if(C)
Throws std::invalid_argument if condition holds.
Standard functor implementations and comparison objects.
Simple dynamic array with automatic resizing and functional operations.
void swap(Array &s) noexcept
Swap this with s
Doubly-linked list (defined in tpl_dynList.H).
T & append(const T &item)
Dynamic matrix with sparse storage.
const T & read(const size_t i, const size_t j) const
Read the entry at position (i, j).
constexpr size_t cols() const noexcept
Get the number of columns.
void allocate()
Pre-allocate memory for the entire matrix.
constexpr size_t rows() const noexcept
Get the number of rows.
Implementation of the Hungarian (Munkres) algorithm.
static void throw_beyond()
void solve(const DynMatrix< Cost_Type > &cost, const Cost_Range &range)
Core algorithm: shortest augmenting paths with dual variables.
DynList< std::pair< size_t, size_t > > get_assignments() const
Get all assignment pairs.
long get_assignment(const size_t row) const
Get the column assigned to a given row.
const Array< long > & get_row_assignments() const noexcept
Get the row-to-column assignment array.
Hungarian_Assignment & operator=(Hungarian_Assignment &&other) noexcept
Exchange this solver with another, which is left with the previous assignment of this one.
Array< long > col_to_row_
Hungarian_Assignment(std::initializer_list< std::initializer_list< Cost_Type > > rows)
Construct and solve from an initializer list of rows.
Array< long > extract_col_assignments() &&noexcept
Move out the column-to-row assignment array.
const Array< long > & get_col_assignments() const noexcept
Get the column-to-row assignment array.
size_t cols() const noexcept
Get the original number of columns.
Hungarian_Assignment(const Hungarian_Assignment &other)=default
Copy the assignment of another solver.
Hungarian_Assignment & operator=(const Hungarian_Assignment &other)=default
Copy the assignment of another solver.
Array< long > row_to_col_
size_t dimension() const noexcept
Get the padded square dimension.
Array< long > extract_row_assignments() &&noexcept
Move out the row-to-column assignment array.
size_t rows() const noexcept
Get the original number of rows.
Hungarian_Assignment(Hungarian_Assignment &&other) noexcept
Take the assignment of another solver, which is left empty.
Hungarian_Assignment(const DynMatrix< Cost_Type > &cost)
Construct and solve from a DynMatrix cost matrix.
Cost_Type get_total_cost() const noexcept
Get the optimal total cost.
std::conditional_t< Integer and sizeof(Cost_Type)< sizeof(long long), long long, Cost_Type > Wide
void swap(Hungarian_Assignment &other) noexcept
Exchange in O(1) this solver and another.
static constexpr bool Integer
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_j0_function > > j0(const __gmp_expr< T, U > &expr)
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_j1_function > > j1(const __gmp_expr< T, U > &expr)
Hungarian_Result< Cost_Type > hungarian_max_assignment(const DynMatrix< Cost_Type > &cost)
Compute maximum-profit assignment (free function).
Hungarian_Result< Cost_Type > hungarian_assignment(const DynMatrix< Cost_Type > &cost)
Compute minimum-cost assignment (free function).
Singly linked list implementations with head-tail access.
Main namespace for Aleph-w library functions.
and
Check uniqueness with explicit hash + equality functors.
Container< T > range(const T start, const T end, const T step=1)
Generate a range of values [start, end] with a given step.
void add(const Cost_Type &c) noexcept
Result of the Hungarian assignment algorithm.
Array< long > col_to_row
column j is assigned to row col_to_row[j]
Cost_Type total_cost
Optimal total cost.
Array< long > row_to_col
row i is assigned to column row_to_col[i]
DynList< std::pair< size_t, size_t > > get_pairs() const
Get the assignment pairs, excluding dummy entries.
size_t orig_rows
Original number of rows.
size_t orig_cols
Original number of columns.
Dynamic array container with automatic resizing.
Dynamic matrix with lazy allocation.