20#include "absl/container/flat_hash_map.h"
21#include "gtest/gtest.h"
32 const absl::flat_hash_map<int, int>& direct_assignment,
33 const absl::flat_hash_map<int, int>& reverse_assignment,
34 const int expected_agents[],
const int expected_tasks[]) {
35 EXPECT_EQ(expected_assignment_size, direct_assignment.size());
36 EXPECT_EQ(expected_assignment_size, reverse_assignment.size());
37 for (
int i = 0; i < expected_assignment_size; ++i) {
43 for (
const auto& direct_iter : direct_assignment) {
46 << direct_iter.first <<
" -> " << direct_iter.second;
51 const int expected_assignment_size,
52 const int expected_agents[],
const int expected_tasks[]) {
53 absl::flat_hash_map<int, int> direct_assignment;
54 absl::flat_hash_map<int, int> reverse_assignment;
56 SCOPED_TRACE(
"Minimization");
57 GenericCheck(expected_assignment_size, direct_assignment, reverse_assignment,
58 expected_agents, expected_tasks);
62 const int expected_assignment_size,
63 const int expected_agents[],
const int expected_tasks[]) {
64 absl::flat_hash_map<int, int> direct_assignment;
65 absl::flat_hash_map<int, int> reverse_assignment;
67 SCOPED_TRACE(
"Maximization");
68 GenericCheck(expected_assignment_size, direct_assignment, reverse_assignment,
69 expected_agents, expected_tasks);
74TEST(LinearAssignmentTest, NullMatrix) {
75 std::vector<std::vector<double>>
cost;
76 const int* expected_agents =
nullptr;
77 const int* expected_tasks =
nullptr;
83TEST(LinearAssignmentTest, InvalidMatrix) {
84 const std::vector<std::vector<double>> cost_nan = {{1, 2},
86 const int* expected_agents =
nullptr;
87 const int* expected_tasks =
nullptr;
94 std::vector<std::vector<double>> cost(kMatrixHeight); \
95 for (int row = 0; row < kMatrixHeight; ++row) { \
96 cost[row].resize(kMatrixWidth); \
97 for (int col = 0; col < kMatrixWidth; ++col) { \
98 cost[row][col] = kCost[row][col]; \
101 EXPECT_EQ(arraysize(expected_agents_for_min), \
102 arraysize(expected_tasks_for_min)); \
103 EXPECT_EQ(arraysize(expected_agents_for_max), \
104 arraysize(expected_tasks_for_max)); \
105 const int assignment_size = arraysize(expected_agents_for_max); \
106 TestMinimization(cost, assignment_size, expected_agents_for_min, \
107 expected_tasks_for_min); \
108 TestMaximization(cost, assignment_size, expected_agents_for_max, \
109 expected_tasks_for_max); \
114TEST(LinearAssignmentTest, SizeOneMatrix) {
115 const int kMatrixHeight = 1;
116 const int kMatrixWidth = 1;
117 const double kCost[kMatrixHeight][kMatrixWidth] = {{4}};
118 const int expected_agents_for_min[] = {0};
119 const int expected_tasks_for_min[] = {0};
120 const int expected_agents_for_max[] = {0};
121 const int expected_tasks_for_max[] = {0};
127TEST(LinearAssignmentTest, Small4x4Matrix) {
128 const int kMatrixHeight = 4;
129 const int kMatrixWidth = 4;
130 const double kCost[kMatrixHeight][kMatrixWidth] = {{90, 75, 75, 80},
134 const int expected_agents_for_min[] = {0, 1, 2, 3};
135 const int expected_tasks_for_min[] = {3, 2, 1, 0};
136 const int expected_agents_for_max[] = {0, 1, 2, 3};
137 const int expected_tasks_for_max[] = {2, 1, 0, 3};
142TEST(LinearAssignmentTest, Small3x4Matrix) {
143 const int kMatrixHeight = 3;
144 const int kMatrixWidth = 4;
145 const double kCost[kMatrixHeight][kMatrixWidth] = {
146 {90, 75, 75, 80}, {35, 85, 55, 65}, {125, 95, 90, 105}};
147 const int expected_agents_for_min[] = {0, 1, 2};
148 const int expected_tasks_for_min[] = {1, 0, 2};
149 const int expected_agents_for_max[] = {0, 1, 2};
150 const int expected_tasks_for_max[] = {3, 1, 0};
155TEST(LinearAssignmentTest, Small4x3Matrix) {
156 const int kMatrixHeight = 4;
157 const int kMatrixWidth = 3;
158 const double kCost[kMatrixHeight][kMatrixWidth] = {
159 {90, 75, 75}, {35, 85, 55}, {125, 95, 90}, {45, 110, 95}};
160 const int expected_agents_for_min[] = {0, 1, 3};
161 const int expected_tasks_for_min[] = {1, 2, 0};
162 const int expected_agents_for_max[] = {0, 2, 3};
163 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 MinimizeLinearAssignment(const std::vector< std::vector< double > > &cost, absl::flat_hash_map< int, int > *direct_assignment, absl::flat_hash_map< int, int > *reverse_assignment)
void TestMaximization(const std::vector< std::vector< double > > &cost, const int expected_assignment_size, const int expected_agents[], const int expected_tasks[])
TEST(LinearAssignmentTest, NullMatrix)
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[])
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)