1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
|
/****************************************************************************
*
* ViSP, open source Visual Servoing Platform software.
* Copyright (C) 2005 - 2023 by Inria. All rights reserved.
*
* This software is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* See the file LICENSE.txt at the root directory of this source
* distribution for additional information about the GNU GPL.
*
* For using ViSP with software that can not be combined with the GNU
* GPL, please contact Inria about acquiring a ViSP Professional
* Edition License.
*
* See https://visp.inria.fr for more information.
*
* This software was developed at:
* Inria Rennes - Bretagne Atlantique
* Campus Universitaire de Beaulieu
* 35042 Rennes Cedex
* France
*
* If you have questions regarding the use of this file, please contact
* Inria at visp@inria.fr
*
* This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
* WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*
* Description:
* Test Munkres assignment algorithm.
*
*****************************************************************************/
/*!
\example testMunkres.cpp
\brief Test Munkres assignment algorithm.
*/
#include <visp3/core/vpMunkres.h>
#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_17) && \
(!defined(_MSC_VER) || ((VISP_CXX_STANDARD >= VISP_CXX_STANDARD_17) && (_MSC_VER >= 1911)))
// System
#include <iostream>
#ifndef DOXYGEN_SHOULD_SKIP_THIS
namespace std
{
// Helper to output a Munkres pair
ostream &operator<<(ostream &os, const pair<unsigned int, unsigned int> &val)
{
os << "[" << val.first << "," << val.second << "]";
return os;
}
} // namespace std
#endif // DOXYGEN_SHOULD_SKIP_THIS
#ifdef VISP_HAVE_CATCH2
#define CATCH_CONFIG_RUNNER
#include <catch.hpp>
TEST_CASE("Check Munkres-based assignment", "[visp_munkres]")
{
auto testMunkres = [](const std::vector<std::vector<double> > &cost_matrix,
const std::vector<std::pair<unsigned int, unsigned int> > &expected_pairs) {
const auto munkres_pairs = vpMunkres::run(cost_matrix);
REQUIRE(expected_pairs.size() == munkres_pairs.size());
for (auto i = 0u; i < munkres_pairs.size(); i++) {
REQUIRE(expected_pairs.at(i) == munkres_pairs.at(i));
}
};
SECTION("Square cost matrix")
{
std::vector<std::vector<double> > costs{};
costs.push_back(std::vector<double>{3, 1, 2});
costs.push_back(std::vector<double>{2, 3, 1});
costs.push_back(std::vector<double>{1, 2, 3});
std::vector<std::pair<unsigned int, unsigned int> > expected_pairs{};
expected_pairs.emplace_back(0, 1);
expected_pairs.emplace_back(1, 2);
expected_pairs.emplace_back(2, 0);
testMunkres(costs, expected_pairs);
}
SECTION("Horizontal cost matrix")
{
std::vector<std::vector<double> > costs{};
costs.push_back(std::vector<double>{4, 1, 2, 3});
costs.push_back(std::vector<double>{3, 4, 1, 2});
costs.push_back(std::vector<double>{2, 3, 4, 1});
std::vector<std::pair<unsigned int, unsigned int> > expected_pairs{};
expected_pairs.emplace_back(0, 1);
expected_pairs.emplace_back(1, 2);
expected_pairs.emplace_back(2, 3);
testMunkres(costs, expected_pairs);
}
SECTION("Vertical cost matrix")
{
std::vector<std::vector<double> > costs{};
costs.push_back(std::vector<double>{4, 1, 2});
costs.push_back(std::vector<double>{3, 4, 1});
costs.push_back(std::vector<double>{2, 3, 4});
costs.push_back(std::vector<double>{1, 2, 3});
std::vector<std::pair<unsigned int, unsigned int> > expected_pairs{};
expected_pairs.emplace_back(0, 1);
expected_pairs.emplace_back(1, 2);
expected_pairs.emplace_back(3, 0);
testMunkres(costs, expected_pairs);
}
}
int main(int argc, char *argv[])
{
Catch::Session session;
session.applyCommandLine(argc, argv);
return session.run();
}
#else
// Fallback to classic tests
bool testMunkres(const std::vector<std::vector<double> > &costs_matrix,
const std::vector<std::pair<unsigned int, unsigned int> > &expected_pairs)
{
const auto pairs = vpMunkres::run(costs_matrix);
if (pairs.size() != expected_pairs.size()) {
// clang-format off
std::cerr << "Expected nb of association | Munkres nb of association: "
<< expected_pairs.size() << " | " << pairs.size()
<< std::endl;
// clang-format on
return false;
}
for (auto i = 0u; i < pairs.size(); i++) {
if (expected_pairs.at(i) != pairs.at(i)) {
// Output the cost matrix
std::cout << "Cost matrix:" << std::endl;
for (const auto &cost_row : costs_matrix) {
std::cout << "| ";
for (const auto &cost : cost_row) {
std::cout << cost << " | ";
}
std::cout << std::endl;
}
std::cout << std::endl;
// Output the pair which fails
std::cerr << "FAIL: "
<< "Expected association | Munkres association: " << expected_pairs.at(i) << " | " << pairs.at(i)
<< std::endl;
return false;
}
}
return true;
}
bool testSquareMat()
{
std::vector<std::vector<double> > costs{};
costs.push_back(std::vector<double>{3, 4, 1, 2});
costs.push_back(std::vector<double>{3, 4, 2, 1});
costs.push_back(std::vector<double>{1, 2, 3, 4});
costs.push_back(std::vector<double>{2, 1, 4, 3});
std::vector<std::pair<unsigned int, unsigned int> > pairs{};
pairs.emplace_back(0, 2);
pairs.emplace_back(1, 3);
pairs.emplace_back(2, 0);
pairs.emplace_back(3, 1);
return testMunkres(costs, pairs);
}
bool testVertMat()
{
std::vector<std::vector<double> > costs{};
costs.push_back(std::vector<double>{3, 2, 1});
costs.push_back(std::vector<double>{4, 3, 2});
costs.push_back(std::vector<double>{1, 4, 3});
costs.push_back(std::vector<double>{2, 1, 4});
std::vector<std::pair<unsigned int, unsigned int> > pairs{};
pairs.emplace_back(0, 2);
pairs.emplace_back(2, 0);
pairs.emplace_back(3, 1);
return testMunkres(costs, pairs);
}
bool testHorMat()
{
std::vector<std::vector<double> > costs{};
costs.push_back(std::vector<double>{2, 3, 4, 1});
costs.push_back(std::vector<double>{4, 1, 2, 3});
costs.push_back(std::vector<double>{1, 2, 3, 4});
std::vector<std::pair<unsigned int, unsigned int> > pairs{};
pairs.emplace_back(0, 3);
pairs.emplace_back(1, 1);
pairs.emplace_back(2, 0);
return testMunkres(costs, pairs);
}
int main()
{
if (not testSquareMat()) {
return EXIT_FAILURE;
}
if (not testVertMat()) {
return EXIT_FAILURE;
}
if (not testHorMat()) {
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
#endif
#else
int main() { return EXIT_SUCCESS; }
#endif
|