Advertisement
Guest User

Untitled

a guest
Jan 27th, 2015
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cmath>
  3. #include <ctime>
  4. #include <cassert>
  5. #include <stdint.h>
  6. #include <vector>
  7.  
  8. #include <boost/numeric/ublas/vector.hpp>
  9. #include <boost/numeric/ublas/matrix.hpp>
  10.  
  11. using namespace boost::numeric;
  12.  
  13. static const int M = 2;
  14. static const int N = 8;
  15.  
  16. int main()
  17. {
  18.  
  19. // patterns
  20.  
  21. ublas::vector<int> P[M];
  22.  
  23. ublas::vector<int> p(N);
  24.  
  25. p(0) = +1;
  26. p(1) = +1;
  27. p(2) = +1;
  28. p(3) = +1;
  29. p(4) = -1;
  30. p(5) = -1;
  31. p(6) = -1;
  32. p(7) = -1;
  33.  
  34. P[0] = p;
  35.  
  36. p(0) = +1;
  37. p(1) = +1;
  38. p(2) = -1;
  39. p(3) = -1;
  40. p(4) = +1;
  41. p(5) = +1;
  42. p(6) = -1;
  43. p(7) = -1;
  44.  
  45. P[1] = p;
  46.  
  47. // learning phase
  48.  
  49. ublas::matrix<int> W(N, N);
  50.  
  51. for(int i = 0; i < N; ++i)
  52. {
  53. for(int j = 0; j < N; ++j)
  54. {
  55. int sum = 0;
  56.  
  57. if (i != j)
  58. {
  59. for(int s = 0; s < M; ++s)
  60. {
  61. sum += P[s](i) * P[s](j);
  62. }
  63. }
  64.  
  65. W(i, j) = sum;
  66. }
  67. }
  68.  
  69.  
  70. // initial pattern
  71.  
  72. ublas::vector<int> u(N);
  73.  
  74. u(0) = +1;
  75. u(1) = +1;
  76. u(2) = -1;
  77. u(3) = -1;
  78. u(4) = +1;
  79. u(5) = -1;
  80. u(6) = +1;
  81. u(7) = +1;
  82.  
  83.  
  84. // recovering
  85.  
  86. bool isUnstable;
  87.  
  88. do
  89. {
  90. isUnstable = false;
  91.  
  92. ublas::vector<int> v = u;
  93.  
  94. for(int i = 0; i < N; ++i)
  95. {
  96. printf("u[%d] = %d\n", i, u(i));
  97. }
  98.  
  99. int E = 0;
  100.  
  101. for(int i = 0; i < N; ++i)
  102. for(int j = 0; j < N; ++j)
  103. E += - W(i,j) * u(i) * u(j);
  104.  
  105. printf("E = %d\n", E / 2);
  106.  
  107. printf("-------------------\n");
  108.  
  109. for(int i = 0; i < N; ++i)
  110. {
  111. int sum = 0;
  112.  
  113. for(int j = 0; j < N; ++j)
  114. {
  115. sum += W(i, j) * v(j);
  116. }
  117.  
  118. u(i) = (sum >= 0) ? +1 : -1;
  119.  
  120. if (u(i) != v(i))
  121. isUnstable = true;
  122. }
  123. }
  124. while(isUnstable);
  125.  
  126. return 0;
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement