Alexandria  2.19
Please provide a description of the project.
NeighborhoodFunc.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2021 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 <cmath>
28 #include <functional>
29 
30 namespace Euclid {
31 namespace SOM {
32 namespace NeighborhoodFunc {
33 
35  std::size_t iteration, std::size_t total_iterations)>;
36 
37 Signature linearUnitDisk(double initial_radius) {
38  double r_square = initial_radius * initial_radius;
40  std::size_t total_iterations) -> double {
41  double iter_factor = 1.0 * (total_iterations - iteration) / total_iterations;
42  iter_factor = iter_factor * iter_factor; // We compare the squared distances
43  double x = bmu.first - cell.first;
44  double y = bmu.second - cell.second;
45  double dist_square = x * x + y * y;
46  if (dist_square < r_square * iter_factor) {
47  return 1.;
48  } else {
49  return 0.;
50  }
51  };
52 }
53 
54 Signature kohonen(std::size_t x_size, std::size_t y_size, double sigma_cutoff_mult = 1.) {
55 
56  double init_sigma = std::max(x_size, y_size) / 2.;
57  std::tuple<std::size_t, std::size_t, double> sigma_buffer{0, 0, 0.};
58  double cutoff_mult_square = sigma_cutoff_mult * sigma_cutoff_mult;
59 
60  return [init_sigma, sigma_buffer, cutoff_mult_square](std::pair<std::size_t, std::size_t> bmu,
62  std::size_t total_iterations) mutable -> double {
63  // If we have new iteration we recompute the sigma, otherwise we use the already
64  // calculated one
65  if (std::get<0>(sigma_buffer) != iteration || std::get<1>(sigma_buffer) != total_iterations) {
66  std::get<0>(sigma_buffer) = iteration;
67  std::get<1>(sigma_buffer) = total_iterations;
68  double time_constant = total_iterations / std::log(init_sigma);
69  std::get<2>(sigma_buffer) = init_sigma * std::exp(-1. * iteration / time_constant);
70  }
71  double sigma_square = std::get<2>(sigma_buffer) * std::get<2>(sigma_buffer);
72 
73  double x = static_cast<double>(bmu.first) - cell.first;
74  double y = static_cast<double>(bmu.second) - cell.second;
75  double dist_square = x * x + y * y;
76 
77  if (dist_square < cutoff_mult_square * sigma_square) {
78  return std::exp(-1. * dist_square / (2. * sigma_square));
79  } else {
80  return 0.;
81  }
82  };
83 }
84 
85 } // namespace NeighborhoodFunc
86 } // namespace SOM
87 } // namespace Euclid
88 
89 #endif /* SOM_NEIGHBORHOODFUNC_H */
T exp(T... args)
T log(T... args)
T max(T... args)
Signature linearUnitDisk(double initial_radius)
Signature kohonen(std::size_t x_size, std::size_t y_size, double sigma_cutoff_mult=1.)