Alexandria  2.27.0
SDC-CH common library for the Euclid project
NeighborhoodFunc.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2022 Euclid Science Ground Segment
3  *
4  * This library is free software; you can redistribute it and/or modify it under
5  * the terms of the GNU Lesser General Public License as published by the Free
6  * Software Foundation; either version 3.0 of the License, or (at your option)
7  * any later version.
8  *
9  * This library is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12  * details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this library; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 /*
20  * @file NeighborhoodFunc.h
21  * @author nikoapos
22  */
23 
24 #ifndef SOM_NEIGHBORHOODFUNC_H
25 #define SOM_NEIGHBORHOODFUNC_H
26 
27 #include <ElementsKernel/Export.h>
28 #include <cmath>
29 #include <functional>
30 
31 namespace Euclid {
32 namespace SOM {
33 namespace NeighborhoodFunc {
34 
36 public:
37  explicit LinearUnitDisk(double initial_radius) : m_r_square(initial_radius * initial_radius){};
38 
40  std::size_t iteration, std::size_t total_iterations);
41 
42 private:
43  double m_r_square;
44 };
45 
46 class Kohonen {
47 public:
48  Kohonen(std::size_t x_size, std::size_t y_size, double sigma_cutoff_mult);
49 
51  std::size_t iteration, std::size_t total_iterations) {
52  // If we have new iteration we recompute the sigma, otherwise we use the already
53  // calculated one
54  if (m_last_iteration != iteration || m_last_total != total_iterations) {
55  m_last_iteration = iteration;
56  m_last_total = total_iterations;
57  const double time_constant = static_cast<double>(total_iterations) / m_sigma_log;
58  const double sigma = m_init_sigma * std::exp(-1. * static_cast<double>(iteration) / time_constant);
59  m_sigma_square = sigma * sigma;
60  }
61 
62  const auto x = static_cast<double>(static_cast<int>(bmu.first) - static_cast<int>(cell.first));
63  const auto y = static_cast<double>(static_cast<int>(bmu.second) - static_cast<int>(cell.second));
64  const auto dist_square = x * x + y * y;
65 
66  if (dist_square < m_cutoff_mult_square * m_sigma_square) {
67  return std::exp(-1. * dist_square / (2. * m_sigma_square));
68  } else {
69  return 0.;
70  }
71  }
72 
73 private:
74  const double m_init_sigma;
75  const double m_sigma_log;
76  const double m_cutoff_mult_square;
79  double m_sigma_square = 0.;
80 };
81 
82 } // namespace NeighborhoodFunc
83 } // namespace SOM
84 } // namespace Euclid
85 
86 #endif /* SOM_NEIGHBORHOODFUNC_H */
double operator()(std::pair< std::size_t, std::size_t > bmu, std::pair< std::size_t, std::size_t > cell, std::size_t iteration, std::size_t total_iterations)
Kohonen(std::size_t x_size, std::size_t y_size, double sigma_cutoff_mult)
double operator()(std::pair< std::size_t, std::size_t > bmu, std::pair< std::size_t, std::size_t > cell, std::size_t iteration, std::size_t total_iterations)
T exp(T... args)