Visual Servoing Platform  version 3.6.1 under development (2024-03-29)
vpMunkres.h
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
4  *
5  * This software is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See https://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Class for Munkres Assignment Algorithm.
32  */
33 
34 #pragma once
35 
36 #include <visp3/core/vpConfig.h>
37 
38 // Check if std:c++17 or higher.
39 // Here we cannot use (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_17) in the declaration of the class
40 #if ((__cplusplus >= 201703L) || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201703L)))
41 
42 // System
43 #include <optional>
44 #include <tuple>
45 
46 // Internal
47 #include "vpMath.h"
48 
57 class VISP_EXPORT vpMunkres
58 {
59 public:
60  template <typename Type>
61  static std::vector<std::pair<unsigned int, unsigned int> > run(std::vector<std::vector<Type> > costs);
62 
63 private:
64  enum ZERO_T : unsigned int;
65  enum STEP_T : unsigned int;
66 
67  // Init
68  template <typename Type> static void padCostMatrix(std::vector<std::vector<Type> > &costs);
69 
70  // Global helpers
71  template <typename Type>
72  static std::optional<std::pair<unsigned int, unsigned int> > findAZero(const std::vector<std::vector<Type> > &costs,
73  const std::vector<bool> &row_cover,
74  const std::vector<bool> &col_cover);
75  static std::optional<unsigned int> findStarInRow(const std::vector<std::vector<ZERO_T> > &mask,
76  const unsigned int &row);
77  static std::optional<unsigned int> findStarInCol(const std::vector<std::vector<ZERO_T> > &mask,
78  const unsigned int &col);
79  static std::optional<unsigned int> findPrimeInRow(const std::vector<std::vector<ZERO_T> > &mask,
80  const unsigned int &row);
81  template <typename Type>
82  static Type findSmallest(const std::vector<std::vector<Type> > &costs, const std::vector<bool> &row_cover,
83  const std::vector<bool> &col_cover);
84 
85  // FSM helpers
86  static void augmentPath(std::vector<std::vector<ZERO_T> > &mask,
87  const std::vector<std::pair<unsigned int, unsigned int> > &path);
88  static void clearCovers(std::vector<bool> &row_cover, std::vector<bool> &col_cover);
89  static void erasePrimes(std::vector<std::vector<ZERO_T> > &mask);
90 
91  // FSM
92  template <typename Type> static STEP_T stepOne(std::vector<std::vector<Type> > &costs);
93  template <typename Type>
94  static STEP_T stepTwo(std::vector<std::vector<Type> > &costs, std::vector<std::vector<ZERO_T> > &mask,
95  std::vector<bool> &row_cover, std::vector<bool> &col_cover);
96  static STEP_T stepThree(const std::vector<std::vector<ZERO_T> > &mask, std::vector<bool> &col_cover);
97  template <typename Type>
98  static std::tuple<STEP_T, std::optional<std::pair<unsigned int, unsigned int> > >
99  stepFour(const std::vector<std::vector<Type> > &costs, std::vector<std::vector<ZERO_T> > &mask,
100  std::vector<bool> &row_cover, std::vector<bool> &col_cover);
101  static STEP_T stepFive(std::vector<std::vector<ZERO_T> > &mask, const std::pair<unsigned int, unsigned int> &path_0,
102  std::vector<bool> &row_cover, std::vector<bool> &col_cover);
103  template <typename Type>
104  static STEP_T stepSix(std::vector<std::vector<Type> > &costs, const std::vector<bool> &row_cover,
105  const std::vector<bool> &col_cover);
106 
107 private:
108  static constexpr auto ZeroEpsilon { 1e-6 };
109 };
110 
111 enum vpMunkres::ZERO_T : unsigned int { NA = 0, STARRED = 1, PRIMED = 2 };
112 
113 enum vpMunkres::STEP_T : unsigned int { ENTRY = 0, ONE = 1, TWO = 2, THREE = 3, FOUR = 4, FIVE = 5, SIX = 6, DONE };
114 
120 template <typename Type> inline void vpMunkres::padCostMatrix(std::vector<std::vector<Type> > &costs)
121 {
122  const auto row_input_size = costs.size();
123  const auto col_input_size = costs.at(0).size();
124 
125  if (row_input_size > col_input_size) {
126  for (auto &vec : costs)
127  vec.resize(row_input_size, 0);
128  }
129 
130  while (costs.size() < col_input_size) {
131  costs.emplace_back(col_input_size, 0);
132  }
133 }
134 
143 template <typename Type>
144 inline std::optional<std::pair<unsigned int, unsigned int> >
145 vpMunkres::findAZero(const std::vector<std::vector<Type> > &costs, const std::vector<bool> &row_cover,
146  const std::vector<bool> &col_cover)
147 {
148  for (auto row = 0u; row < costs.size(); row++)
149  for (auto col = 0u; col < costs.size(); col++)
150  if (vpMath::equal(costs.at(row).at(col), static_cast<Type>(vpMunkres::ZeroEpsilon)) && !row_cover.at(row) &&
151  !col_cover.at(col)) {
152  return std::make_optional<std::pair<unsigned int, unsigned int> >(row, col);
153  }
154 
155  return std::nullopt;
156 }
157 
166 template <typename Type>
167 inline Type vpMunkres::findSmallest(const std::vector<std::vector<Type> > &costs, const std::vector<bool> &row_cover,
168  const std::vector<bool> &col_cover)
169 {
170  auto minval = std::numeric_limits<Type>::max();
171  for (auto row = 0u; row < costs.size(); row++)
172  for (auto col = 0u; col < costs.size(); col++)
173  if (minval > costs.at(row).at(col) && !row_cover.at(row) && !col_cover.at(col)) {
174  minval = costs.at(row).at(col);
175  }
176 
177  return minval;
178 }
179 
188 template <typename Type> inline vpMunkres::STEP_T vpMunkres::stepOne(std::vector<std::vector<Type> > &costs)
189 {
190  // process rows
191  std::for_each(begin(costs), end(costs), [](auto &cost_row) {
192  const auto min_in_row = *std::min_element(begin(cost_row), end(cost_row));
193  std::transform(begin(cost_row), end(cost_row), begin(cost_row),
194  [&min_in_row](auto &cost) { return cost - min_in_row; });
195  });
196 
197  // process cols
198  for (auto col = 0u; col < costs.size(); ++col) {
199  auto minval = std::numeric_limits<Type>::max();
200  for (const auto &cost_row : costs) {
201  minval = std::min<Type>(minval, cost_row.at(col));
202  }
203 
204  for (auto &cost_row : costs) {
205  cost_row.at(col) -= minval;
206  }
207  }
208 
209  return vpMunkres::STEP_T(2);
210 }
211 
223 template <typename Type>
224 inline vpMunkres::STEP_T vpMunkres::stepTwo(std::vector<std::vector<Type> > &costs,
225  std::vector<std::vector<vpMunkres::ZERO_T> > &mask,
226  std::vector<bool> &row_cover, std::vector<bool> &col_cover)
227 {
228  for (auto row = 0u; row < costs.size(); row++) {
229  for (auto col = 0u; col < costs.size(); col++) {
230  if (vpMath::equal(costs.at(row).at(col), static_cast<Type>(vpMunkres::ZeroEpsilon)) && !row_cover.at(row) &&
231  !col_cover.at(col)) {
232  mask.at(row).at(col) = vpMunkres::ZERO_T::STARRED;
233  row_cover.at(row) = true;
234  col_cover.at(col) = true;
235  break;
236  }
237  }
238  }
239 
240  clearCovers(row_cover, col_cover);
241  return vpMunkres::STEP_T(3);
242 }
243 
256 template <typename Type>
257 inline std::tuple<vpMunkres::STEP_T, std::optional<std::pair<unsigned int, unsigned int> > >
258 vpMunkres::stepFour(const std::vector<std::vector<Type> > &costs, std::vector<std::vector<vpMunkres::ZERO_T> > &mask,
259  std::vector<bool> &row_cover, std::vector<bool> &col_cover)
260 {
261  if (const auto zero = findAZero(costs, row_cover, col_cover)) {
262  const auto [row, col] = *zero; // convenient zero.value() is not working on iOS
263  mask.at(row).at(col) = vpMunkres::ZERO_T::PRIMED;
264 
265  if (const auto star_in_row = findStarInRow(mask, row)) {
266  row_cover.at(row) = true;
267  col_cover.at(*star_in_row) = false;
268  return { vpMunkres::STEP_T(4), std::nullopt }; // Repeat
269  }
270  else {
271  return { vpMunkres::STEP_T(5), std::make_optional<std::pair<unsigned int, unsigned int> >(row, col) };
272  }
273  }
274  else {
275  return { vpMunkres::STEP_T(6), std::nullopt };
276  }
277 }
278 
288 template <typename Type>
289 inline vpMunkres::STEP_T vpMunkres::stepSix(std::vector<std::vector<Type> > &costs, const std::vector<bool> &row_cover,
290  const std::vector<bool> &col_cover)
291 {
292  const auto minval = findSmallest(costs, row_cover, col_cover);
293  for (auto row = 0u; row < costs.size(); row++) {
294  for (auto col = 0u; col < costs.size(); col++) {
295  if (row_cover.at(row)) {
296  costs.at(row).at(col) += minval;
297  }
298 
299  if (!col_cover.at(col)) {
300  costs.at(row).at(col) -= minval;
301  }
302  }
303  }
304 
305  return vpMunkres::STEP_T(4);
306 }
307 
314 template <typename Type>
315 inline std::vector<std::pair<unsigned int, unsigned int> > vpMunkres::run(std::vector<std::vector<Type> > costs)
316 {
317  const auto original_row_size = static_cast<Type>(costs.size());
318  const auto original_col_size = static_cast<Type>(costs.front().size());
319  const auto sq_size = std::max<Type>(original_row_size, original_col_size);
320 
321  auto mask = std::vector<std::vector<vpMunkres::ZERO_T> >(sq_size, std::vector<vpMunkres::ZERO_T>(sq_size, vpMunkres::ZERO_T::NA));
322  auto row_cover = std::vector<bool>(sq_size, false);
323  auto col_cover = std::vector<bool>(sq_size, false);
324 
325  std::optional<std::pair<unsigned int, unsigned int> > path_0 { std::nullopt };
326 
327  auto step { vpMunkres::STEP_T::ENTRY };
328  while (step != vpMunkres::STEP_T::DONE) {
329  switch (step) {
330  case vpMunkres::STEP_T::ENTRY:
331  padCostMatrix(costs);
332  step = vpMunkres::STEP_T(1);
333  break;
334  case 1:
335  step = stepOne(costs);
336  break;
337  case 2:
338  step = stepTwo(costs, mask, row_cover, col_cover);
339  break;
340  case 3:
341  step = stepThree(mask, col_cover);
342  break;
343  case 4:
344  std::tie(step, path_0) = stepFour(costs, mask, row_cover, col_cover);
345  break;
346  case 5:
347  step = stepFive(mask, *path_0, row_cover, col_cover);
348  break;
349  case 6:
350  step = stepSix(costs, row_cover, col_cover);
351  break;
352  case vpMunkres::STEP_T::DONE:
353  default:
354  break;
355  }
356  }
357 
358  // Compute the pairs
359  std::vector<std::pair<unsigned int, unsigned int> > ret {};
360  for (auto i = 0u; i < original_row_size; i++) {
361  if (const auto it = std::find(begin(mask.at(i)), end(mask.at(i)), vpMunkres::ZERO_T::STARRED);
362  it != end(mask.at(i))) {
363  if (const unsigned int j = static_cast<unsigned int>(std::distance(begin(mask.at(i)), it));
364  j < original_col_size) {
365  ret.emplace_back(i, j);
366  }
367  }
368  }
369 
370  return ret;
371 }
372 
373 #endif
static bool equal(double x, double y, double threshold=0.001)
Definition: vpMath.h:449