Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
testTukeyEstimator.cpp
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2024 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 * Test Tukey M-Estimator.
32 */
33
39
40#include <cstdlib>
41#include <iostream>
42#include <time.h>
43#include <visp3/core/vpConfig.h>
44#include <visp3/core/vpGaussRand.h>
45#include <visp3/core/vpRobust.h>
46#include <visp3/mbt/vpMbtTukeyEstimator.h>
47
48int main()
49{
50#ifdef ENABLE_VISP_NAMESPACE
51 using namespace VISP_NAMESPACE_NAME;
52#endif
53 size_t nb_elements = 1000;
54 int nb_iterations = 100;
55 double stdev = 0.5, mean = 0.0, noise_threshold = 1e-3;
56
57 vpGaussRand noise(stdev, mean);
58 noise.seed(static_cast<unsigned int>(time(nullptr)));
59
60 vpColVector residues_col(static_cast<unsigned int>(nb_elements));
61 vpColVector weights_col, weights_col_save;
62 for (size_t i = 0; i < nb_elements; i++) {
63 residues_col[static_cast<unsigned int>(i)] = noise();
64 }
65
66 vpRobust robust;
67 robust.setMinMedianAbsoluteDeviation(noise_threshold);
68 double t_robust = vpTime::measureTimeMs();
69 for (int i = 0; i < nb_iterations; i++) {
70 robust.MEstimator(vpRobust::TUKEY, residues_col, weights_col);
71 }
72 t_robust = vpTime::measureTimeMs() - t_robust;
73 {
74 vpMbtTukeyEstimator<double> tukey_estimator;
75 std::vector<double> residues(nb_elements);
76 for (size_t i = 0; i < residues.size(); i++) {
77 residues[i] = residues_col[static_cast<unsigned int>(i)];
78 }
79
80 std::vector<double> weights;
81 double t = vpTime::measureTimeMs();
82 for (int i = 0; i < nb_iterations; i++) {
83 tukey_estimator.MEstimator(residues, weights, noise_threshold);
84 }
86
87 std::cout << "t_robust=" << t_robust << " ms ; t (double)=" << t << " ; ratio=" << (t_robust / t) << std::endl;
88
89 for (size_t i = 0; i < weights.size(); i++) {
90 if (!vpMath::equal(weights[i], weights_col[static_cast<unsigned int>(i)], noise_threshold)) {
91 std::cerr << "Difference between vpRobust::TUKEY and vpMbtTukeyEstimator (double)!" << std::endl;
92 std::cerr << "weights_col[" << i << "]=" << weights_col[static_cast<unsigned int>(i)] << std::endl;
93 std::cerr << "weights[" << i << "]=" << weights[i] << std::endl;
94 return EXIT_FAILURE;
95 }
96 }
97 }
98
99 // Generate again for weights != 1
100 for (size_t i = 0; i < nb_elements; i++) {
101 residues_col[static_cast<unsigned int>(i)] = noise();
102 }
103 weights_col_save = weights_col;
104 t_robust = vpTime::measureTimeMs();
105 for (int i = 0; i < nb_iterations; i++) {
106 robust.MEstimator(vpRobust::TUKEY, residues_col, weights_col);
107 }
108 t_robust = vpTime::measureTimeMs() - t_robust;
109
110 {
111 vpMbtTukeyEstimator<float> tukey_estimator;
112 std::vector<float> residues(nb_elements);
113 std::vector<float> weights(nb_elements);
114 for (size_t i = 0; i < residues.size(); i++) {
115 residues[i] = static_cast<float>(residues_col[static_cast<unsigned int>(i)]);
116 weights[i] = static_cast<float>(weights_col_save[static_cast<unsigned int>(i)]);
117 }
118
119 double t = vpTime::measureTimeMs();
120 for (int i = 0; i < nb_iterations; i++) {
121 tukey_estimator.MEstimator(residues, weights, static_cast<float>(noise_threshold));
122 }
124
125 std::cout << "t_robust=" << t_robust << " ms ; t (float)=" << t << " ; ratio=" << (t_robust / t) << std::endl;
126
127 for (size_t i = 0; i < weights.size(); i++) {
128 if (!vpMath::equal(weights[i], static_cast<float>(weights_col[static_cast<unsigned int>(i)]), static_cast<float>(noise_threshold))) {
129 std::cerr << "Difference between vpRobust::TUKEY and vpMbtTukeyEstimator (float)!" << std::endl;
130 std::cerr << "weights_col[" << i << "]=" << weights_col[static_cast<unsigned int>(i)] << std::endl;
131 std::cerr << "weights[" << i << "]=" << weights[i] << std::endl;
132 return EXIT_FAILURE;
133 }
134 }
135 }
136
137 // Generate again for weights != 1 and vpColVector type
138 for (size_t i = 0; i < nb_elements; i++) {
139 residues_col[static_cast<unsigned int>(i)] = noise();
140 }
141 weights_col_save = weights_col;
142 t_robust = vpTime::measureTimeMs();
143 for (int i = 0; i < nb_iterations; i++) {
144 robust.MEstimator(vpRobust::TUKEY, residues_col, weights_col);
145 }
146 t_robust = vpTime::measureTimeMs() - t_robust;
147
148 {
149 vpMbtTukeyEstimator<double> tukey_estimator;
150 vpColVector residues = residues_col;
151 vpColVector weights = weights_col_save;
152
153 double t = vpTime::measureTimeMs();
154 for (int i = 0; i < nb_iterations; i++) {
155 tukey_estimator.MEstimator(residues, weights, noise_threshold);
156 }
158
159 std::cout << "t_robust=" << t_robust << " ms ; t (vpColVector)=" << t << " ; ratio=" << (t_robust / t) << std::endl;
160
161 for (size_t i = 0; i < weights.size(); i++) {
162 if (!vpMath::equal(weights[static_cast<unsigned int>(i)], weights_col[static_cast<unsigned int>(i)], noise_threshold)) {
163 std::cerr << "Difference between vpRobust::TUKEY and vpMbtTukeyEstimator (float)!" << std::endl;
164 std::cerr << "weights_col[" << i << "]=" << weights_col[static_cast<unsigned int>(i)] << std::endl;
165 std::cerr << "weights[" << i << "]=" << weights[static_cast<unsigned int>(i)] << std::endl;
166 return EXIT_FAILURE;
167 }
168 }
169 }
170
171 std::cout << "vpMbtTukeyEstimator returns the same values than vpRobust::TUKEY." << std::endl;
172 return EXIT_SUCCESS;
173}
unsigned int size() const
Return the number of elements of the 2D array.
Definition vpArray2D.h:435
Implementation of column vector and the associated operations.
Class for generating random number with normal probability density.
static bool equal(double x, double y, double threshold=0.001)
Definition vpMath.h:470
Contains an M-estimator and various influence function.
Definition vpRobust.h:84
@ TUKEY
Tukey influence function.
Definition vpRobust.h:89
void MEstimator(const vpRobustEstimatorType method, const vpColVector &residues, vpColVector &weights)
Definition vpRobust.cpp:130
void setMinMedianAbsoluteDeviation(double mad_min)
Definition vpRobust.h:136
VISP_EXPORT double measureTimeMs()