Advertisement
Guest User

Untitled

a guest
May 25th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.Random;
  5.  
  6. /**
  7. * Created by micha on 06.04.2017.
  8. */
  9. public class Sieci_Hopfielda {
  10. private double[] xs;
  11. private double[] x;
  12. private double[][] c;
  13. private double[][] w;
  14. private double[] theta;
  15.  
  16. public Sieci_Hopfielda(){
  17. initXS();
  18. initC();
  19. initW();
  20. initTheta();
  21. initX();
  22. }
  23.  
  24. private void initXS(){
  25. this.xs = new double[]{
  26. 0,0,0,0,0,
  27. 0,1,1,0,0,
  28. 0,0,1,0,0,
  29. 0,0,1,0,0,
  30. 0,0,1,0,0};
  31.  
  32. }
  33.  
  34. private void initC(){
  35. this.c = new double[25][25];
  36. for(int i=0;i<25;i++){
  37. for(int j=0;j<25;j++){
  38. if(i==j){
  39. c[i][j]=0;
  40. }
  41. else{
  42. c[i][j]=(xs[i]-0.5)*(xs[j]-0.5);
  43. }
  44. }
  45. }
  46. }
  47.  
  48. private void initW(){
  49. this.w = new double[25][25];
  50. for(int i=0;i<25;i++){
  51. for(int j=0;j<25;j++){
  52. w[i][j]=2*c[i][j];
  53. }
  54. }
  55. }
  56.  
  57.  
  58. //pytanie czy ma iterować tylko po j w C
  59. private void initTheta(){
  60. this.theta = new double[25];
  61. for(int i=0;i<25;i++){
  62. for(int j=0;j<25;j++){
  63. theta[i]=theta[i]+c[i][j];
  64. }
  65. }
  66. }
  67.  
  68. private void initX(){
  69. Random generator = new Random();
  70. this.x = new double[25];
  71. for(int i=0;i<25;i++){
  72. x[i] = Math.floorMod(generator.nextInt(),2);
  73. }
  74. }
  75.  
  76. private void nextX(){
  77.  
  78. double[] u = getU();
  79.  
  80. for(int i=0;i<25;i++){
  81. if(u[i]>0.0){
  82. x[i]=1.0;}
  83. if(u[i]<0.0){
  84. x[i]=0.0;}
  85.  
  86. }
  87. }
  88.  
  89. private double[] getU(){
  90. double[] u = new double[25];
  91. for(int i=0;i<25;i++){
  92. for(int j=0;j<25;j++){
  93. u[i]=u[i]+w[i][j]*x[j];
  94. }
  95. u[i]=u[i]-theta[i];
  96. }
  97.  
  98. return u;
  99. }
  100.  
  101. public void algorithm() throws IOException {
  102. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  103.  
  104. initX();
  105. display(x);
  106.  
  107. while(!br.readLine().equals("exit")){
  108. nextX();
  109. display(this.x);
  110. }
  111. }
  112.  
  113. private void display(double[] vector){
  114. for(int i=0;i<25;i++){
  115.  
  116. if(i % 5 == 0){
  117. System.out.print("\n");}
  118. if(vector[i]==0.0){
  119. System.out.print("-");
  120. }
  121. else if(vector[i]==1.0){
  122. System.out.print("*");
  123. }
  124.  
  125. }
  126. }
  127.  
  128.  
  129.  
  130.  
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement