Advertisement
Guest User

LSN_SIM

a guest
Aug 23rd, 2016
355
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.30 KB | None | 0 0
  1. // Last update: 22.08.2016, 17.40
  2.  
  3. #include "LSN.cc"
  4. #include "LSN_CODE/runner.cc"
  5. #include "C:\genn-team-genn-8cacb78\lib\include\hr_time.cpp"
  6. #include <stdlib.h>
  7. #include <math.h>
  8.  
  9. #define DT 0.1
  10. #define SIM_TIME 1000
  11.  
  12. // -------------------------------------------- Prototypes ----------------------------------------------------
  13. typedef struct arrayItem{
  14.     /*
  15.         population - integer value referred to the population neuron belongs to (0 = EXC, 1 = INH)
  16.         indexInPopulation - integer value referred to the index of the neuron (starting from 0)
  17.     */
  18.     int population;
  19.     int indexInPopulation;
  20. } t_extendedArray;
  21.  
  22. void printArray(int *,int,int);
  23. void setArrayItemAtXY(int **,int,int,int,int);
  24.  
  25. int * getGrid(int,int);
  26. int * scrambleGrid(int *,int,int);
  27. int * getCoordinates(int *,int,int,int);
  28.  
  29. int getArrayItemAtXY(int *,int,int,int);
  30.  
  31. double getDistance(int *,int *);
  32. double getK(int,int);
  33. double getC(int *,int,int,int,t_extendedArray);
  34. double getP(int *,int,int,int,int,t_extendedArray);
  35.  
  36. t_extendedArray * getExtendedArray(int,int);
  37. // -------------------------------------------- main ----------------------------------------------------------
  38. int main(){
  39.     CStopWatch timer;
  40.    
  41.     const double PERCENTAGE_INH = 0.25;
  42.     const double PERCENTAGE_EXC = 1 - PERCENTAGE_INH;
  43.     const int NO_ROWS = 8;
  44.     const int NO_COLS = 8;
  45.     const int dim_EXC = PERCENTAGE_EXC * NO_COLS * NO_ROWS;
  46.     const int dim_INH = PERCENTAGE_INH * NO_COLS * NO_ROWS;
  47.  
  48.     timer.startTimer();
  49.     allocateMem();
  50.     initialize();
  51.    
  52.     system("cls");
  53.    
  54.     printf("--------------\n");
  55.     printf("LSN SIMULATION\n");
  56.     printf("--------------\n\n");
  57.    
  58.     printf("About the simulation\n");
  59.     printf("--------------------\n");
  60.     printf("1. SIM_TIME = %d\n",SIM_TIME);
  61.     printf("2. DT = %f\n",DT);
  62.     printf("--------------------\n\n");
  63.    
  64.     /*
  65.         Because we're working with two populations with different sizes, we want
  66.         to create an unique vector that provides all the available indexes for
  67.         referring the neurons of both populations in a proper way. Here,
  68.         we are supposing that excitatory neurons are placed in the first places.
  69.        
  70.         Each element of extendedArray is a struct composed of two integer values,
  71.         that inform us about both the nature of population the neuron belongs to
  72.         and which is its position inside it. Hence, if N1 and N2 are the sizes
  73.         of these two populations, extendedArray has N1+N2 structs, but each
  74.         group of neurons corresponds to a given index interval that starts always from 0.
  75.        
  76.         With such approach, for example, accessing to the 5th neuron corresponds to get
  77.         the element extendedArray[getArrayItemAtXY(grid,8,coordinates[0],coordinates[1])],
  78.         where coordinates = getCoordinates(grid,8,8,5);
  79.     */ 
  80.     t_extendedArray * extendedArray;
  81.    
  82.     /*
  83.         Following grid contains all the indexes used to refer to each
  84.         item within the extendedArray array, which comprises a number
  85.         of structs equal to the sum of the dimensions of excitatory and
  86.         inhibitory populations. It has to be defined because we want
  87.         an unique structure thanks to which we can establish the nature
  88.         of involved neurons and to process data easily.
  89.        
  90.         The mechanism behind such structure is the following. Once each
  91.         entry of 'grid' has been determined each value works as an index.
  92.         By specifying the position (i,j) within the lattice, we can retrieve
  93.         the corresponding element of extendedArray.
  94.     */
  95.     int * grid;
  96.    
  97.     printf("1. Two 1D arrays: one is related to the excitatory sub-population and the other is related to the inhibitory sub-population.\n");
  98.     printf("   First array comprises %d neurons, second array comprises %d neurons.\n",dim_EXC,dim_INH);
  99.    
  100.     extendedArray = getExtendedArray(dim_EXC,dim_INH);
  101.    
  102.     printf("\n   Extended array has been created.\n\n");
  103.    
  104.     printf("2. New extended array has to be transformed in a %d-by-%d grid.\n\n",NO_ROWS,NO_COLS);
  105.     grid =  getGrid(NO_ROWS,NO_COLS);
  106.     printArray(grid,NO_ROWS,NO_COLS);
  107.    
  108.     printf("\n\nExample of distances calculation\n");
  109.     printf("d(4,2) = %f\n",getDistance(getCoordinates(grid,NO_ROWS,NO_COLS,4),getCoordinates(grid,NO_ROWS,NO_COLS,2)));
  110.     printf("d(25,38) = %f\n",getDistance(getCoordinates(grid,NO_ROWS,NO_COLS,25),getCoordinates(grid,NO_ROWS,NO_COLS,38)));
  111.        
  112. /*  for(double t = 0.0; t < SIM_TIME; t += dt){
  113.        
  114.     } */
  115.    
  116.     printf("\n\nTime = %f\n",timer.getElapsedTime());
  117.    
  118.     free(extendedArray);
  119.     free(grid);
  120.    
  121.     extendedArray = NULL;
  122.     grid = NULL;
  123.    
  124.     return 0;
  125. }
  126. // -------------------------------------------- printArray ----------------------------------------------------
  127. void printArray(int * array, int rows, int cols){
  128.     for(int row = 0; row < rows; row++){
  129.         for(int column = 0; column < cols; column++) printf("%d\t",getArrayItemAtXY(array,cols,row,column));
  130.         printf("\n");
  131.     }
  132. }
  133. // -------------------------------------------- setArrayItemAtXY ----------------------------------------------
  134. void setArrayItemAtXY(int ** array, int cols, int x, int y, int value){ *array[x * cols + y] = value; }
  135. // -------------------------------------------- getGrid -------------------------------------------------------
  136. int * getGrid(int rows, int cols){ 
  137.     int * grid = (int *)malloc(rows * cols * sizeof(int));
  138.     if (!grid){
  139.         perror("MALLOC FAILED\n\n");
  140.         exit(ENOMEM);
  141.     }
  142.     return scrambleGrid(grid,rows,cols);
  143. }
  144. // -------------------------------------------- scrambleGrid ---------------------------------------------------
  145. int * scrambleGrid(int * grid, int rows, int cols){
  146.     int * checkDuplicates = (int *)malloc(rows * cols * 2 * sizeof(int));
  147.     int row;
  148.     int column;
  149.     int randomNumber;
  150.    
  151.     // Initializes matrix checkDuplicates
  152.     for(row = 0; row < cols * rows; row++){
  153.         setArrayItemAtXY(&checkDuplicates,2,row,0,row);
  154.         setArrayItemAtXY(&checkDuplicates,2,row,1,0);
  155.     }
  156.    
  157.     // Generates grid
  158.     srand(time(NULL));
  159.     for(row = 0; row < rows; row++){
  160.         for(column = 0; column < cols; column++){
  161.             do{
  162.                 randomNumber = rand() % (cols * rows);
  163.             } while(getArrayItemAtXY(checkDuplicates,2,randomNumber,1) == 1);
  164.             setArrayItemAtXY(&checkDuplicates,2,randomNumber,1,1);
  165.             setArrayItemAtXY(&grid,cols,row,column,randomNumber);
  166.         }
  167.     }
  168.    
  169.     return grid;
  170. }
  171. // -------------------------------------------- getCoordinates ------------------------------------------------
  172. int * getCoordinates(int * grid, int rows, int cols, int i){
  173.    
  174.     for(int row = 0; row < rows; row++){
  175.         for(int column = 0; column < cols; column++){
  176.             if(getArrayItemAtXY(grid,cols,row,column) == i){
  177.                 int coordinates[2] = {row,column};
  178.                 return coordinates;
  179.             }
  180.         }
  181.     }
  182.    
  183.     return NULL;
  184. }
  185. // -------------------------------------------- getArrayItemAtXY ----------------------------------------------
  186. int getArrayItemAtXY(int * array, int cols, int x, int y){ return array[x * cols + y]; }
  187. // -------------------------------------------- getDistance ---------------------------------------------------
  188. double getDistance(int * coordinates_neuron_i, int * coordinates_neuron_j){
  189.     /*
  190.         coordinates[0] is the X component;
  191.         coordinates[1] is the Y component;
  192.     */
  193.     return sqrt(pow(coordinates_neuron_i[0] - coordinates_neuron_j[0],2) + pow(coordinates_neuron_i[1] - coordinates_neuron_j[1],2));
  194. }
  195. // -------------------------------------------- getK ----------------------------------------------------------
  196. double getK(int * coordinates_neuron_i, int * coordinates_neuron_j){
  197.     double distance = getDistance(coordinates_neuron_i,coordinates_neuron_j);
  198.    
  199.     if(distance <= 1) return 1;
  200.     if((distance > 1) && (distance <= 2)) return 0.5;
  201.     return 0;
  202. }
  203. // -------------------------------------------- getC ----------------------------------------------------------
  204. double getC(int* grid, int * coordinates_neuron_i, int * coordinates_neuron_j,int cols, t_extendedArray * array){
  205.     const double C_INH_EXC = 0.8;
  206.     const double C_EXC_EXC = 0.6;
  207.     const double C_EXC_INH = 0.4;
  208.     const double C_INH_INH = 0.2;
  209.    
  210.     t_extendedArray item_i = array[getArrayItemAtXY(grid,cols,coordinates_neuron_i[0],coordinates_neuron_i[1])];
  211.     t_extendedArray item_j = array[getArrayItemAtXY(grid,cols,coordinates_neuron_j[0],coordinates_neuron_j[1])];
  212.    
  213.     if((item_i.population == 0) && (item_j.population == 0)) return C_EXC_EXC;
  214.     if((item_i.population == 0) && (item_j.population == 1)) return C_EXC_INH;
  215.     if((item_i.population == 1) && (item_j.population == 0)) return C_INH_EXC;
  216.     return C_INH_INH;
  217. }
  218. // -------------------------------------------- getP ----------------------------------------------------------
  219. double getP(int * grid, int neuron_i, int neuron_j, int rows, int cols, t_extendedArray * array){
  220.     int * coordinates_neuron_i = getCoordinates(grid,rows,cols,neuron_i);
  221.     int * coordinates_neuron_j = getCoordinates(grid,rows,cols,neuron_j);
  222.    
  223.     return getK(coordinates_neuron_i,coordinates_neuron_j) * getC(grid,coordinates_neuron_i,coordinates_neuron_j,cols,array);
  224. }
  225. // -------------------------------------------- getExtendedArray ----------------------------------------------
  226. t_extendedArray * getExtendedArray(int dim_EXC, int dim_INH){  
  227.     int index;
  228.     int dim = dim_EXC + dim_INH;
  229.     t_extendedArray * array = (t_extendedArray *)malloc(dim * sizeof(t_extendedArray));
  230.    
  231.     for(index = 0; index < dim_EXC; index++){
  232.         array[index].population = 0;
  233.         array[index].indexInPopulation = index;
  234.     }
  235.     while(index < dim){
  236.         array[index].population = 1;
  237.         array[index].indexInPopulation = index - dim_EXC;
  238.         index++;
  239.     }
  240.    
  241.     return array;
  242. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement