48# include <gtest/gtest.h>
64 size_t m = rows.
size();
65 size_t n = rows.begin()->size();
69 for (
const auto & row : rows)
72 for (
const auto & val : row)
85 for (
auto it = pairs.get_it(); it.has_curr(); it.next())
87 auto [
r, c] = it.get_curr();
96 size_t rows,
size_t cols)
100 for (
size_t i = 0; i < rows; ++i)
102 long j = row_to_col[i];
106 <<
"Row " << i <<
" assigned to out-of-range column " << j;
108 <<
"Column " << j <<
" assigned to multiple rows";
113 for (
size_t j = 0; j < cols; ++j)
115 long i = col_to_row[j];
119 <<
"Column " << j <<
" assigned to out-of-range row " << i;
120 EXPECT_EQ(row_to_col[
static_cast<size_t>(i)],
121 static_cast<long>(j))
122 <<
"Inconsistent row/col mapping at row=" << i <<
" col=" << j;
153 auto pairs =
ha.get_assignments();
188 auto pairs =
ha.get_assignments();
226 auto pairs =
ha.get_assignments();
242 auto pairs =
ha.get_assignments();
320 auto pairs =
ha.get_assignments();
331 auto pairs =
ha.get_assignments();
453 auto pairs = result.get_pairs();
463 auto pairs =
ha.get_assignments();
467 for (
auto it = pairs.get_it(); it.has_curr(); it.next())
469 auto [
r, c] = it.get_curr();
486 ha.rows(),
ha.cols());
489 auto pairs =
ha.get_assignments();
508 for (
size_t i = 0; i < 3; ++i)
510 long col =
ha.get_assignment(i);
514 <<
"Column " << col <<
" assigned twice";
527 auto pairs =
ha.get_assignments();
543 {0,1,2}, {0,2,1}, {1,0,2}, {1,2,0}, {2,0,1}, {2,1,0}
546 int min_cost = numeric_limits<int>::max();
550 for (
int i = 0; i < 3; ++i)
551 cost +=
static_cast<int>(mat.read(i,
perm[i]));
552 min_cost =
min(min_cost, cost);
569 int perm[4] = {0, 1, 2, 3};
570 int min_cost = numeric_limits<int>::max();
574 for (
int i = 0; i < 4; ++i)
575 cost +=
static_cast<int>(mat.read(i,
perm[i]));
576 min_cost =
min(min_cost, cost);
595 for (
size_t i = 0; i < 10; ++i)
596 for (
size_t j = 0; j < 10; ++j)
597 mat(i, j) = dist(
rng);
603 auto pairs =
ha.get_assignments();
615 for (
size_t i = 0; i < 50; ++i)
616 for (
size_t j = 0; j < 50; ++j)
617 mat(i, j) = dist(
rng);
623 auto pairs =
ha.get_assignments();
635 for (
size_t i = 0; i < 100; ++i)
636 for (
size_t j = 0; j < 100; ++j)
637 mat(i, j) = dist(
rng);
643 auto pairs =
ha.get_assignments();
655 for (
size_t i = 0; i < 30; ++i)
656 for (
size_t j = 0; j < 50; ++j)
657 mat(i, j) = dist(
rng);
663 auto pairs =
ha.get_assignments();
706 for (
size_t i = 0; i <
N; ++i)
707 for (
size_t j = 0; j <
N; ++j)
711 std_costs[i][j] =
static_cast<double>(val);
Hungarian (Munkres) algorithm for the assignment problem.
Simple dynamic array with automatic resizing and functional operations.
Dynamic singly linked list with functional programming support.
Dynamic matrix with sparse storage.
const T & read(const size_t i, const size_t j) const
Read the entry at position (i, j).
void allocate()
Pre-allocate memory for the entire matrix.
Hungarian (Munkres) algorithm for optimal assignment.
constexpr size_t size() const noexcept
Returns the number of entries in the table.
__gmp_expr< typename __gmp_resolve_expr< T, V >::value_type, __gmp_binary_expr< __gmp_expr< T, U >, __gmp_expr< V, W >, __gmp_min_function > > min(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
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).
DynMatrix< T > make_matrix(initializer_list< initializer_list< T > > rows)
void verify_permutation(const Array< long > &row_to_col, const Array< long > &col_to_row, size_t rows, size_t cols)
T compute_assignment_cost(const DynMatrix< T > &cost, const DynList< pair< size_t, size_t > > &pairs)
Main namespace for Aleph-w library functions.
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
std::decay_t< typename HeadC::Item_Type > T
bool next_permutation(Array< T > &a, Compare cmp=Compare(), const bool reset_on_last=true)
Compute the next lexicographic permutation of an Array.
std::pair< First, Second > pair
Alias to std::pair kept for backwards compatibility.
AssignmentResult< Cost_Type > solve_assignment(const std::vector< std::vector< Cost_Type > > &costs)
Solve the assignment problem using min-cost flow.
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
Dynamic matrix with lazy allocation.
Advanced minimum cost flow algorithms.