Guest User

RandomInhibitoryConnectionsEffect_V20.pde

a guest
Nov 16th, 2018
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.41 KB | None | 0 0
  1. int[][] neurons;
  2. float[] activity;
  3. int testCount = 1000, neuronCount = 100, inhibited = 12;
  4. void setup()
  5. {
  6.   background(255);
  7.   size(805,400);
  8.   frameRate(100);
  9.   neurons = GetNeurons(neuronCount, inhibited);
  10.   activity = new float[neuronCount];
  11.   RunTest(testCount, false);
  12. }
  13.  
  14. void draw()
  15. {
  16. }
  17.  
  18. void keyPressed()
  19. {
  20.   if(key == 'a')
  21.   {
  22.     neurons = GetNeurons(neuronCount, inhibited);
  23.     activity = new float[neuronCount];
  24.     RunTest(testCount, false);
  25.   }
  26.  
  27.   if(key == 's')
  28.   {
  29.     neurons = GetNeurons(neuronCount, inhibited);
  30.     activity = new float[neuronCount];
  31.     RunTest(testCount, true);
  32.   }
  33. }
  34.  
  35. void RunTest(int count, boolean radial)
  36. {
  37.   background(0);
  38.  
  39.   if(!radial)
  40.   {
  41.     for(int c = 0; c < count; c++)
  42.     {
  43.       activity = AddScores(ActivateNeurons(), activity);
  44.     }
  45.   }
  46.   else
  47.   {
  48.     for(int c = 0; c < count; c++)
  49.     {
  50.       activity = AddScores(ActivateRadialNeurons(), activity);
  51.     }
  52.   }
  53.  
  54.   activity = Grade(activity,count);
  55.  
  56.   int rW = floor(width/activity.length);
  57.  
  58.   for(int a = 0; a < activity.length; a++)
  59.   {  
  60.     color col = color(255*activity[a]);
  61.     stroke(col);
  62.     fill(col);
  63.     rect(rW*a,0,rW,height);
  64.     print(activity[a] + " ");
  65.   }
  66. }
  67.  
  68.  
  69.  
  70. int[][] GetNeurons(int count, int inhibited)
  71. {
  72.   int[][] neuroSample = new int[count][inhibited];
  73.  
  74.   for(int n = 0; n < count; n++)
  75.   {
  76.     for(int i = 0; i < inhibited; i++)
  77.     {
  78.       int selection = round(random(count-1));
  79.      
  80.       while(selection == n)
  81.       {
  82.         selection = round(random(count-1));
  83.       }
  84.      
  85.       neuroSample[n][i] = selection;
  86.     }
  87.   }
  88.  
  89.   return neuroSample;
  90. }
  91.  
  92. float[] AddScores(int[][] activations, float[] scores)
  93. {
  94.   for(int a = 0; a < activations.length; a++)
  95.   {
  96.     if(activations[a][0] >= 0)
  97.     {
  98.       scores[a] += 1.0f;
  99.     }
  100.   }
  101.   return scores;
  102. }
  103.  
  104. float[] AddScores(int[] activations, float[] scores)
  105. {
  106.   for(int a = 0; a < activations.length; a++)
  107.   {
  108.     if(activations[a] >= 0)
  109.     {
  110.       scores[a] += 1.0f;
  111.     }
  112.   }
  113.   return scores;
  114. }
  115.  
  116. float[] Grade(float[] scores, int totalRuns)
  117. {
  118.   for(int s = 0; s < scores.length; s++)
  119.   {
  120.     scores[s] = scores[s]/totalRuns;
  121.   }
  122.  
  123.   return scores;
  124. }
  125.  
  126. int[][] ActivateNeurons()
  127. {
  128.   int[][] activations = neurons;
  129.  
  130.   for(int n = 0; n < neurons.length; n++)
  131.   {
  132.     int neuron = round(random(neurons.length-1));
  133.    
  134.     if(activations[neuron][0] != -1)
  135.     {
  136.       for(int i = 0; i < activations[neuron].length; i++)
  137.       {
  138.         if(neurons[neuron][i] < neurons.length+1)
  139.         {
  140.           activations[neurons[neuron][i]][0] = -1;
  141.         }
  142.        
  143.         activations[neuron][0] = neurons.length+1;
  144.       }
  145.     }
  146.   }
  147.  
  148.   return activations;
  149. }
  150.  
  151. int[] ActivateRadialNeurons()
  152. {
  153.   int[] activations = new int[neuronCount];
  154.  
  155.   for(int n = 0; n < neuronCount; n++)
  156.   {
  157.     int rand = round(random(neuronCount-1));
  158.     if(activations[rand] == 0)
  159.     {
  160.       activations[rand] = 1;
  161.      
  162.       for(int r = 0; r < inhibited; r++)
  163.       {
  164.         if(rand - r >= 0)
  165.         {
  166.           if(activations[rand - r] < 1)
  167.           {
  168.             activations[rand-r] = -1;
  169.           }
  170.         }
  171.        
  172.         if(rand + r < neuronCount)
  173.         {
  174.           if(activations[rand + r] < 1)
  175.           {
  176.             activations[rand+r] = -1;
  177.           }
  178.         }
  179.       }
  180.     }
  181.   }
  182.  
  183.   return activations;
  184.  
  185. }
Advertisement
Add Comment
Please, Sign In to add comment