Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Last update: 22.08.2016, 17.40
- #include "LSN.cc"
- #include "LSN_CODE/runner.cc"
- #include "C:\genn-team-genn-8cacb78\lib\include\hr_time.cpp"
- #include <stdlib.h>
- #include <math.h>
- #define DT 0.1
- #define SIM_TIME 1000
- // -------------------------------------------- Prototypes ----------------------------------------------------
- typedef struct arrayItem{
- /*
- population - integer value referred to the population neuron belongs to (0 = EXC, 1 = INH)
- indexInPopulation - integer value referred to the index of the neuron (starting from 0)
- */
- int population;
- int indexInPopulation;
- } t_extendedArray;
- void printArray(int *,int,int);
- void setArrayItemAtXY(int **,int,int,int,int);
- int * getGrid(int,int);
- int * scrambleGrid(int *,int,int);
- int * getCoordinates(int *,int,int,int);
- int getArrayItemAtXY(int *,int,int,int);
- double getDistance(int *,int *);
- double getK(int,int);
- double getC(int *,int,int,int,t_extendedArray);
- double getP(int *,int,int,int,int,t_extendedArray);
- t_extendedArray * getExtendedArray(int,int);
- // -------------------------------------------- main ----------------------------------------------------------
- int main(){
- CStopWatch timer;
- const double PERCENTAGE_INH = 0.25;
- const double PERCENTAGE_EXC = 1 - PERCENTAGE_INH;
- const int NO_ROWS = 8;
- const int NO_COLS = 8;
- const int dim_EXC = PERCENTAGE_EXC * NO_COLS * NO_ROWS;
- const int dim_INH = PERCENTAGE_INH * NO_COLS * NO_ROWS;
- timer.startTimer();
- allocateMem();
- initialize();
- system("cls");
- printf("--------------\n");
- printf("LSN SIMULATION\n");
- printf("--------------\n\n");
- printf("About the simulation\n");
- printf("--------------------\n");
- printf("1. SIM_TIME = %d\n",SIM_TIME);
- printf("2. DT = %f\n",DT);
- printf("--------------------\n\n");
- /*
- Because we're working with two populations with different sizes, we want
- to create an unique vector that provides all the available indexes for
- referring the neurons of both populations in a proper way. Here,
- we are supposing that excitatory neurons are placed in the first places.
- Each element of extendedArray is a struct composed of two integer values,
- that inform us about both the nature of population the neuron belongs to
- and which is its position inside it. Hence, if N1 and N2 are the sizes
- of these two populations, extendedArray has N1+N2 structs, but each
- group of neurons corresponds to a given index interval that starts always from 0.
- With such approach, for example, accessing to the 5th neuron corresponds to get
- the element extendedArray[getArrayItemAtXY(grid,8,coordinates[0],coordinates[1])],
- where coordinates = getCoordinates(grid,8,8,5);
- */
- t_extendedArray * extendedArray;
- /*
- Following grid contains all the indexes used to refer to each
- item within the extendedArray array, which comprises a number
- of structs equal to the sum of the dimensions of excitatory and
- inhibitory populations. It has to be defined because we want
- an unique structure thanks to which we can establish the nature
- of involved neurons and to process data easily.
- The mechanism behind such structure is the following. Once each
- entry of 'grid' has been determined each value works as an index.
- By specifying the position (i,j) within the lattice, we can retrieve
- the corresponding element of extendedArray.
- */
- int * grid;
- printf("1. Two 1D arrays: one is related to the excitatory sub-population and the other is related to the inhibitory sub-population.\n");
- printf(" First array comprises %d neurons, second array comprises %d neurons.\n",dim_EXC,dim_INH);
- extendedArray = getExtendedArray(dim_EXC,dim_INH);
- printf("\n Extended array has been created.\n\n");
- printf("2. New extended array has to be transformed in a %d-by-%d grid.\n\n",NO_ROWS,NO_COLS);
- grid = getGrid(NO_ROWS,NO_COLS);
- printArray(grid,NO_ROWS,NO_COLS);
- printf("\n\nExample of distances calculation\n");
- printf("d(4,2) = %f\n",getDistance(getCoordinates(grid,NO_ROWS,NO_COLS,4),getCoordinates(grid,NO_ROWS,NO_COLS,2)));
- printf("d(25,38) = %f\n",getDistance(getCoordinates(grid,NO_ROWS,NO_COLS,25),getCoordinates(grid,NO_ROWS,NO_COLS,38)));
- /* for(double t = 0.0; t < SIM_TIME; t += dt){
- } */
- printf("\n\nTime = %f\n",timer.getElapsedTime());
- free(extendedArray);
- free(grid);
- extendedArray = NULL;
- grid = NULL;
- return 0;
- }
- // -------------------------------------------- printArray ----------------------------------------------------
- void printArray(int * array, int rows, int cols){
- for(int row = 0; row < rows; row++){
- for(int column = 0; column < cols; column++) printf("%d\t",getArrayItemAtXY(array,cols,row,column));
- printf("\n");
- }
- }
- // -------------------------------------------- setArrayItemAtXY ----------------------------------------------
- void setArrayItemAtXY(int ** array, int cols, int x, int y, int value){ *array[x * cols + y] = value; }
- // -------------------------------------------- getGrid -------------------------------------------------------
- int * getGrid(int rows, int cols){
- int * grid = (int *)malloc(rows * cols * sizeof(int));
- if (!grid){
- perror("MALLOC FAILED\n\n");
- exit(ENOMEM);
- }
- return scrambleGrid(grid,rows,cols);
- }
- // -------------------------------------------- scrambleGrid ---------------------------------------------------
- int * scrambleGrid(int * grid, int rows, int cols){
- int * checkDuplicates = (int *)malloc(rows * cols * 2 * sizeof(int));
- int row;
- int column;
- int randomNumber;
- // Initializes matrix checkDuplicates
- for(row = 0; row < cols * rows; row++){
- setArrayItemAtXY(&checkDuplicates,2,row,0,row);
- setArrayItemAtXY(&checkDuplicates,2,row,1,0);
- }
- // Generates grid
- srand(time(NULL));
- for(row = 0; row < rows; row++){
- for(column = 0; column < cols; column++){
- do{
- randomNumber = rand() % (cols * rows);
- } while(getArrayItemAtXY(checkDuplicates,2,randomNumber,1) == 1);
- setArrayItemAtXY(&checkDuplicates,2,randomNumber,1,1);
- setArrayItemAtXY(&grid,cols,row,column,randomNumber);
- }
- }
- return grid;
- }
- // -------------------------------------------- getCoordinates ------------------------------------------------
- int * getCoordinates(int * grid, int rows, int cols, int i){
- for(int row = 0; row < rows; row++){
- for(int column = 0; column < cols; column++){
- if(getArrayItemAtXY(grid,cols,row,column) == i){
- int coordinates[2] = {row,column};
- return coordinates;
- }
- }
- }
- return NULL;
- }
- // -------------------------------------------- getArrayItemAtXY ----------------------------------------------
- int getArrayItemAtXY(int * array, int cols, int x, int y){ return array[x * cols + y]; }
- // -------------------------------------------- getDistance ---------------------------------------------------
- double getDistance(int * coordinates_neuron_i, int * coordinates_neuron_j){
- /*
- coordinates[0] is the X component;
- coordinates[1] is the Y component;
- */
- return sqrt(pow(coordinates_neuron_i[0] - coordinates_neuron_j[0],2) + pow(coordinates_neuron_i[1] - coordinates_neuron_j[1],2));
- }
- // -------------------------------------------- getK ----------------------------------------------------------
- double getK(int * coordinates_neuron_i, int * coordinates_neuron_j){
- double distance = getDistance(coordinates_neuron_i,coordinates_neuron_j);
- if(distance <= 1) return 1;
- if((distance > 1) && (distance <= 2)) return 0.5;
- return 0;
- }
- // -------------------------------------------- getC ----------------------------------------------------------
- double getC(int* grid, int * coordinates_neuron_i, int * coordinates_neuron_j,int cols, t_extendedArray * array){
- const double C_INH_EXC = 0.8;
- const double C_EXC_EXC = 0.6;
- const double C_EXC_INH = 0.4;
- const double C_INH_INH = 0.2;
- t_extendedArray item_i = array[getArrayItemAtXY(grid,cols,coordinates_neuron_i[0],coordinates_neuron_i[1])];
- t_extendedArray item_j = array[getArrayItemAtXY(grid,cols,coordinates_neuron_j[0],coordinates_neuron_j[1])];
- if((item_i.population == 0) && (item_j.population == 0)) return C_EXC_EXC;
- if((item_i.population == 0) && (item_j.population == 1)) return C_EXC_INH;
- if((item_i.population == 1) && (item_j.population == 0)) return C_INH_EXC;
- return C_INH_INH;
- }
- // -------------------------------------------- getP ----------------------------------------------------------
- double getP(int * grid, int neuron_i, int neuron_j, int rows, int cols, t_extendedArray * array){
- int * coordinates_neuron_i = getCoordinates(grid,rows,cols,neuron_i);
- int * coordinates_neuron_j = getCoordinates(grid,rows,cols,neuron_j);
- return getK(coordinates_neuron_i,coordinates_neuron_j) * getC(grid,coordinates_neuron_i,coordinates_neuron_j,cols,array);
- }
- // -------------------------------------------- getExtendedArray ----------------------------------------------
- t_extendedArray * getExtendedArray(int dim_EXC, int dim_INH){
- int index;
- int dim = dim_EXC + dim_INH;
- t_extendedArray * array = (t_extendedArray *)malloc(dim * sizeof(t_extendedArray));
- for(index = 0; index < dim_EXC; index++){
- array[index].population = 0;
- array[index].indexInPopulation = index;
- }
- while(index < dim){
- array[index].population = 1;
- array[index].indexInPopulation = index - dim_EXC;
- index++;
- }
- return array;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement