You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

169 lines
6.1 KiB

  1. /*
  2. Copyright 2005-2014 Intel Corporation. All Rights Reserved.
  3. This file is part of Threading Building Blocks.
  4. Threading Building Blocks is free software; you can redistribute it
  5. and/or modify it under the terms of the GNU General Public License
  6. version 2 as published by the Free Software Foundation.
  7. Threading Building Blocks is distributed in the hope that it will be
  8. useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  9. of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with Threading Building Blocks; if not, write to the Free Software
  13. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  14. As a special exception, you may use this file as part of a free software
  15. library without restriction. Specifically, if other files instantiate
  16. templates or use macros or inline functions from this file, or you compile
  17. this file and link it with other files to produce an executable, this
  18. file does not by itself cause the resulting executable to be covered by
  19. the GNU General Public License. This exception does not however
  20. invalidate any other reasons why the executable file might be covered by
  21. the GNU General Public License.
  22. */
  23. //
  24. // Self-organizing map
  25. //
  26. // support for self-ordering maps
  27. #ifndef __SOM_H__
  28. #define __SOM_H__
  29. #include <vector>
  30. #include <cstdlib>
  31. #include <cmath>
  32. #include <cfloat>
  33. #include <iostream>
  34. #include <cstdio>
  35. #include "tbb/flow_graph.h"
  36. #include "tbb/blocked_range2d.h"
  37. using namespace tbb;
  38. using namespace tbb::flow;
  39. typedef blocked_range2d<int> subsquare_type;
  40. typedef tuple<double,int,int> search_result_type;
  41. std::ostream& operator<<( std::ostream &out, const search_result_type &s);
  42. #define RADIUS 0 // for the std::gets
  43. #define XV 1
  44. #define YV 2
  45. // to have single definitions of static variables, define _MAIN_C_ in the main program
  46. //
  47. #ifdef _MAIN_C_
  48. #define DEFINE // nothing
  49. #define INIT(n) = n
  50. #else // not in main file
  51. #define DEFINE extern
  52. #define INIT(n) // nothing
  53. #endif // _MAIN_C_
  54. DEFINE int nElements INIT(3); // length of input vectors, matching vector in map
  55. DEFINE double max_learning_rate INIT(0.8); // decays exponentially
  56. DEFINE double radius_decay_rate;
  57. DEFINE double learning_decay_rate INIT(0.005);
  58. DEFINE double max_radius;
  59. DEFINE bool extra_debug INIT(false);
  60. DEFINE bool cancel_test INIT(false);
  61. DEFINE int xMax INIT(100);
  62. DEFINE int yMax INIT(100);
  63. DEFINE int nPasses INIT(100);
  64. enum InitializeType { InitializeRandom, InitializeGradient };
  65. #define RED 0
  66. #define GREEN 1
  67. #define BLUE 2
  68. class SOM_element;
  69. void remark_SOM_element(const SOM_element &s);
  70. // all SOM_element vectors are the same length (nElements), so we do not have
  71. // to range-check the vector accesses.
  72. class SOM_element {
  73. std::vector<double> w;
  74. public:
  75. friend std::ostream& operator<<( std::ostream &out, const SOM_element &s);
  76. friend void remark_SOM_element(const SOM_element &s);
  77. SOM_element() : w(nElements,0.0) {}
  78. double &operator[](int indx) { return w.at(indx); }
  79. const double &operator[](int indx) const { return w.at(indx); }
  80. bool operator==(SOM_element const &other) const {
  81. for(size_t i=0;i<size();++i) {
  82. if(w[i] != other.w[i]) {
  83. return false;
  84. }
  85. }
  86. return true;
  87. }
  88. bool operator!=(SOM_element const &other) const { return !operator==(other); }
  89. void elementwise_max(SOM_element const &other) {
  90. for(size_t i = 0; i < w.size(); ++i) if(w[i] < other.w[i]) w[i] = other.w[i];
  91. }
  92. void elementwise_min(SOM_element const &other) {
  93. for(size_t i = 0; i < w.size(); ++i) if(w[i] > other.w[i]) w[i] = other.w[i];
  94. }
  95. size_t size() const { return w.size(); }
  96. };
  97. typedef std::vector<SOM_element> teaching_vector_type;
  98. DEFINE SOM_element max_range;
  99. DEFINE SOM_element min_range;
  100. extern double randval( double lowlimit, double highlimit);
  101. extern void find_data_ranges(teaching_vector_type &teaching, SOM_element &max_range, SOM_element &min_range );
  102. extern void add_fraction_of_difference( SOM_element &to, SOM_element &from, double frac);
  103. DEFINE teaching_vector_type my_teaching;
  104. class SOMap {
  105. std::vector< std::vector< SOM_element > > my_map;
  106. public:
  107. SOMap(int xSize, int ySize) {
  108. my_map.reserve(xSize);
  109. for(int i = 0; i < xSize; ++i) {
  110. my_map.push_back(teaching_vector_type());
  111. my_map[i].reserve(ySize);
  112. for(int j = 0; j < ySize;++j) {
  113. my_map[i].push_back(SOM_element());
  114. }
  115. }
  116. }
  117. size_t size() { return my_map.size(); }
  118. void initialize(InitializeType it, SOM_element &max_range, SOM_element &min_range);
  119. teaching_vector_type &operator[](int indx) { return my_map[indx]; }
  120. SOM_element &at(int xVal, int yVal) { return my_map[xVal][yVal]; }
  121. SOM_element &at(search_result_type const &s) { return my_map[flow::get<1>(s)][flow::get<2>(s)]; }
  122. void epoch_update( SOM_element const &s, int epoch, int min_x, int min_y, double radius, double learning_rate) {
  123. int min_xiter = (int)((double)min_x - radius);
  124. if(min_xiter < 0) min_xiter = 0;
  125. int max_xiter = (int)((double)min_x + radius);
  126. if(max_xiter > (int)my_map.size()-1) max_xiter = (int)(my_map.size()-1);
  127. blocked_range<int> br1(min_xiter, max_xiter, 1);
  128. epoch_update_range(s, epoch, min_x, min_y, radius, learning_rate, br1);
  129. }
  130. void epoch_update_range( SOM_element const &s, int epoch, int min_x, int min_y, double radius, double learning_rate, blocked_range<int> &r);
  131. void teach( teaching_vector_type &id);
  132. void debug_output();
  133. // find BMU given an input, returns distance
  134. double BMU_range(const SOM_element &s, int &xval, int &yval, subsquare_type &r);
  135. double BMU(const SOM_element &s, int &xval, int &yval) {
  136. subsquare_type br(0,(int)my_map.size(),1,0,(int)my_map[0].size(),1);
  137. return BMU_range(s, xval, yval, br);
  138. }
  139. };
  140. extern double distance_squared(SOM_element x, SOM_element y);
  141. void remark_SOM_element(const SOM_element &s);
  142. extern void readInputData();
  143. #endif // __SOM_H__