23 #include "absl/container/flat_hash_map.h"
24 #include "absl/random/distributions.h"
25 #include "gtest/gtest.h"
36 const absl::flat_hash_map<int, int>& direct_assignment,
37 const absl::flat_hash_map<int, int>& reverse_assignment,
38 const int expected_agents[],
const int expected_tasks[]) {
39 EXPECT_EQ(expected_assignment_size, direct_assignment.size());
40 EXPECT_EQ(expected_assignment_size, reverse_assignment.size());
41 for (
int i = 0; i < expected_assignment_size; ++i) {
47 for (
const auto& direct_iter : direct_assignment) {
50 << direct_iter.first <<
" -> " << direct_iter.second;
55 const int expected_assignment_size,
56 const int expected_agents[],
const int expected_tasks[]) {
57 absl::flat_hash_map<int, int> direct_assignment;
58 absl::flat_hash_map<int, int> reverse_assignment;
60 SCOPED_TRACE(
"Minimization");
61 GenericCheck(expected_assignment_size, direct_assignment, reverse_assignment,
62 expected_agents, expected_tasks);
66 const int expected_assignment_size,
67 const int expected_agents[],
const int expected_tasks[]) {
68 absl::flat_hash_map<int, int> direct_assignment;
69 absl::flat_hash_map<int, int> reverse_assignment;
71 SCOPED_TRACE(
"Maximization");
72 GenericCheck(expected_assignment_size, direct_assignment, reverse_assignment,
73 expected_agents, expected_tasks);
78 TEST(LinearAssignmentTest, NullMatrix) {
79 std::vector<std::vector<double>>
cost;
80 const int* expected_agents =
nullptr;
81 const int* expected_tasks =
nullptr;
87 TEST(LinearAssignmentTest, InvalidMatrix) {
88 const std::vector<std::vector<double>> cost_nan = {{1, 2},
90 const int* expected_agents =
nullptr;
91 const int* expected_tasks =
nullptr;
98 std::vector<std::vector<double>> cost(kMatrixHeight); \
99 for (int row = 0; row < kMatrixHeight; ++row) { \
100 cost[row].resize(kMatrixWidth); \
101 for (int col = 0; col < kMatrixWidth; ++col) { \
102 cost[row][col] = kCost[row][col]; \
105 EXPECT_EQ(arraysize(expected_agents_for_min), \
106 arraysize(expected_tasks_for_min)); \
107 EXPECT_EQ(arraysize(expected_agents_for_max), \
108 arraysize(expected_tasks_for_max)); \
109 const int assignment_size = arraysize(expected_agents_for_max); \
110 TestMinimization(cost, assignment_size, expected_agents_for_min, \
111 expected_tasks_for_min); \
112 TestMaximization(cost, assignment_size, expected_agents_for_max, \
113 expected_tasks_for_max); \
118 TEST(LinearAssignmentTest, SizeOneMatrix) {
119 const int kMatrixHeight = 1;
120 const int kMatrixWidth = 1;
121 const double kCost[kMatrixHeight][kMatrixWidth] = {{4}};
122 const int expected_agents_for_min[] = {0};
123 const int expected_tasks_for_min[] = {0};
124 const int expected_agents_for_max[] = {0};
125 const int expected_tasks_for_max[] = {0};
131 TEST(LinearAssignmentTest, Small4x4Matrix) {
132 const int kMatrixHeight = 4;
133 const int kMatrixWidth = 4;
134 const double kCost[kMatrixHeight][kMatrixWidth] = {{90, 75, 75, 80},
138 const int expected_agents_for_min[] = {0, 1, 2, 3};
139 const int expected_tasks_for_min[] = {3, 2, 1, 0};
140 const int expected_agents_for_max[] = {0, 1, 2, 3};
141 const int expected_tasks_for_max[] = {2, 1, 0, 3};
146 TEST(LinearAssignmentTest, Small3x4Matrix) {
147 const int kMatrixHeight = 3;
148 const int kMatrixWidth = 4;
149 const double kCost[kMatrixHeight][kMatrixWidth] = {
150 {90, 75, 75, 80}, {35, 85, 55, 65}, {125, 95, 90, 105}};
151 const int expected_agents_for_min[] = {0, 1, 2};
152 const int expected_tasks_for_min[] = {1, 0, 2};
153 const int expected_agents_for_max[] = {0, 1, 2};
154 const int expected_tasks_for_max[] = {3, 1, 0};
159 TEST(LinearAssignmentTest, Small4x3Matrix) {
160 const int kMatrixHeight = 4;
161 const int kMatrixWidth = 3;
162 const double kCost[kMatrixHeight][kMatrixWidth] = {
163 {90, 75, 75}, {35, 85, 55}, {125, 95, 90}, {45, 110, 95}};
164 const int expected_agents_for_min[] = {0, 1, 3};
165 const int expected_tasks_for_min[] = {1, 2, 0};
166 const int expected_agents_for_max[] = {0, 2, 3};
167 const int expected_tasks_for_max[] = {2, 0, 1};
const Collection::value_type::second_type & FindOrDie(const Collection &collection, const typename Collection::value_type::first_type &key)
Collection of objects used to extend the Constraint Solver library.
void MaximizeLinearAssignment(const std::vector< std::vector< double >> &cost, absl::flat_hash_map< int, int > *direct_assignment, absl::flat_hash_map< int, int > *reverse_assignment)
void MinimizeLinearAssignment(const std::vector< std::vector< double >> &cost, absl::flat_hash_map< int, int > *direct_assignment, absl::flat_hash_map< int, int > *reverse_assignment)
TEST(LinearAssignmentTest, NullMatrix)
void TestMaximization(const std::vector< std::vector< double >> &cost, const int expected_assignment_size, const int expected_agents[], const int expected_tasks[])
void GenericCheck(const int expected_assignment_size, const absl::flat_hash_map< int, int > &direct_assignment, const absl::flat_hash_map< int, int > &reverse_assignment, const int expected_agents[], const int expected_tasks[])
void TestMinimization(const std::vector< std::vector< double >> &cost, const int expected_assignment_size, const int expected_agents[], const int expected_tasks[])