24 #ifndef SOM_NEIGHBORHOODFUNC_H 25 #define SOM_NEIGHBORHOODFUNC_H 32 namespace NeighborhoodFunc {
34 using Signature = std::function<double(std::pair<std::size_t, std::size_t> bmu,
35 std::pair<std::size_t, std::size_t> cell,
36 std::size_t iteration, std::size_t total_iterations)>;
39 double r_square = initial_radius * initial_radius;
40 return [r_square](std::pair<std::size_t, std::size_t> bmu,
41 std::pair<std::size_t, std::size_t> cell,
42 std::size_t iteration, std::size_t total_iterations) ->
double {
43 double iter_factor = 1.0 * (total_iterations - iteration) / total_iterations;
44 iter_factor = iter_factor * iter_factor;
45 double x = bmu.first - cell.first;
46 double y = bmu.second - cell.second;
47 double dist_square = x * x + y * y;
48 if (dist_square < r_square * iter_factor) {
56 Signature kohonen(std::size_t x_size, std::size_t y_size,
double sigma_cutoff_mult=1.) {
58 double init_sigma = std::max(x_size, y_size) / 2.;
59 std::tuple<std::size_t, std::size_t, double> sigma_buffer {0, 0, 0.};
60 double cutoff_mult_square = sigma_cutoff_mult * sigma_cutoff_mult;
62 return [init_sigma, sigma_buffer, cutoff_mult_square](
63 std::pair<std::size_t, std::size_t> bmu,
64 std::pair<std::size_t, std::size_t> cell,
65 std::size_t iteration, std::size_t total_iterations)
mutable ->
double {
69 if (std::get<0>(sigma_buffer) != iteration || std::get<1>(sigma_buffer) != total_iterations) {
70 std::get<0>(sigma_buffer) = iteration;
71 std::get<1>(sigma_buffer) = total_iterations;
72 double time_constant = total_iterations / std::log(init_sigma);
73 std::get<2>(sigma_buffer) = init_sigma * std::exp(-1. * iteration / time_constant);
75 double sigma_square = std::get<2>(sigma_buffer) * std::get<2>(sigma_buffer);
77 double x =
static_cast<double>(bmu.first) - cell.first;
78 double y = static_cast<double>(bmu.second) - cell.second;
79 double dist_square = x * x + y * y;
81 if (dist_square < cutoff_mult_square * sigma_square) {
82 return std::exp(-1. * dist_square / (2. * sigma_square));
std::function< double(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)> Signature
Signature linearUnitDisk(double initial_radius)
Signature kohonen(std::size_t x_size, std::size_t y_size, double sigma_cutoff_mult=1.)