Guest User

sim

a guest
Oct 5th, 2016
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 21.26 KB | None | 0 0
  1. // Last update: 05.10.2016, 16.00
  2.  
  3. #include "CODE.cc"
  4. #include "Classifier_CODE/runner.cc"
  5. #include "C:\genn-team-genn-8cacb78\lib\include\hr_time.cpp"
  6. #include "Eigen/Dense"
  7. #include <string>
  8.  
  9. #define LENGTH(x)               (sizeof(x)/sizeof((x)[0]))
  10. #define MAX_LENGTH_STRING       20
  11. #define AUTOFEEDBACK_REMOVAL    0.001
  12. #define SIM_TIME                2000
  13. #define STOP_TIME               800
  14.  
  15. using namespace Eigen;
  16.  
  17. typedef struct coordinatesItem{
  18.     int x;
  19.     int y;
  20. } t_coordinates;
  21.  
  22. typedef struct arrayItem{
  23.     int population;
  24.     int indexInPopulation;
  25.     t_coordinates coordinates;
  26. } t_extendedArray;
  27.  
  28. typedef struct rowDataset{
  29.     double * features;
  30.     char * Class;
  31. } t_row;
  32.  
  33. // Prototypes
  34. void resetNeuronInputLayer(void);
  35. void LSNconnectivitySetup(t_extendedArray * array, int DIM_EXC, int DIM_INH);
  36. void associateExtendedArrayToGrid(int * grid, int rows, int cols, t_extendedArray * array);
  37. t_extendedArray * getExtendedArray(int DIM_EXC, int DIM_INH);
  38. t_extendedArray getItemFromExtendedArray(t_extendedArray * array, int indexNeuron, int length);
  39. int * getGrid(int rows, int cols);
  40. int * scrambleGrid(int * grid, int rows, int cols);
  41. int getRows(void);
  42. double * getCurrents(t_row row);
  43. double getDistance(t_coordinates coordinates_neuron_i, t_coordinates coordinates_neuron_j);
  44. double getK(t_extendedArray neuron_i, t_extendedArray neuron_j);
  45. double getC(t_extendedArray neuron_i, t_extendedArray neuron_j);
  46. double getP(t_extendedArray neuron_i, t_extendedArray neuron_j);
  47. double getMax(t_row row);
  48. double getMin(t_row row);
  49.  
  50. FILE * IrisDataset = fopen("iris.dat","r");
  51.  
  52. // Files for tracing the information about Input Layer
  53. ofstream I_InputLayer("I_InputLayer.dat");
  54.  
  55. // Files for tracing the information about LSN
  56. ofstream V_LSN_EXC("V_LSN_EXC.dat");
  57. ofstream V_LSN_INH("V_LSN_INH.dat");
  58. ofstream I_LSN_EXC("I_LSN_EXC.dat");                                                    // Synaptic current, because no external current is applied!
  59. ofstream I_LSN_INH("I_LSN_INH.dat");                                                    // Synaptic current, because no external current is applied!
  60. ofstream K("K.dat");
  61. ofstream C("C.dat");
  62. ofstream P("P.dat");
  63. ofstream Grid("Grid.dat");
  64. ofstream W_LSN_EXC_EXC("W_LSN_EXC_EXC.dat");
  65. ofstream W_LSN_EXC_INH("W_LSN_EXC_INH.dat");
  66. ofstream W_LSN_INH_EXC("W_LSN_INH_EXC.dat");
  67. ofstream W_LSN_INH_INH("W_LSN_INH_INH.dat");
  68.  
  69. // Files for tracing the information about SNs
  70. ofstream V_SNs("V_SNs.dat");
  71. ofstream ISyn_SNs("ISyn_SNs.dat");
  72.  
  73. // Files for tracing the information about connections from Input Layer to LSN
  74. ofstream W_INPUT_LSN_EXC("W_INPUT_LSN_EXC.dat");
  75. ofstream W_INPUT_LSN_INH("W_INPUT_LSN_INH.dat");
  76.  
  77. // Files for tracing the information about connections from LSN to SNs
  78. ofstream W_LSN_EXC_SNs("W_LSN_EXC_SNs.dat");
  79. ofstream W_LSN_INH_SNs("W_LSN_INH_SNs.dat");
  80.  
  81. // Matrices
  82. ofstream VectorT_SLOW("T_SLOW.dat");
  83. ofstream VectorT_FAST("T_FAST.dat");
  84. ofstream MatrixZ("Z.dat");
  85. ofstream MatrixW("W.dat");
  86.  
  87. int main(){
  88.     const int NO_PATTERNS = getRows(); 
  89.     const int T = (int)(SIM_TIME/DT);
  90.     const int D = (int)(N_NEURONS_LSN*NO_PATTERNS);
  91.     VectorXd T_SLOW(T);
  92.     VectorXd T_FAST((T);
  93.     MatrixXd Z(T,D);
  94.     VectorXd W0(D);
  95.     VectorXd W1(D);
  96.     VectorXd W2(D);
  97.     t_extendedArray * extendedArray = getExtendedArray(DIM_EXC,DIM_INH);
  98.     CStopWatch timer;
  99.    
  100.     timer.startTimer();
  101.     allocateMem();
  102.     initialize();
  103.    
  104.     printf("--------------\n\n");
  105.     printf("CLASSIFICATION\n");
  106.     printf("--------------\n\n");
  107.     printf("About the simulation\n");
  108.     printf("- Number of patterns = %d\n",NO_PATTERNS);
  109.     printf("- Input layer dimensions = %d\n",NEURONS_PER_PATTERN);
  110.     printf("- LSN dimensions = %d-by-%d\n",R_LSN,C_LSN);
  111.     printf("- LSN excitatory subpopulation size = %d\n",DIM_EXC);
  112.     printf("- LSN inhibitory subpopulation size = %d\n",DIM_INH);
  113.     printf("------------------------------------------------\n\n");
  114.    
  115.     // Dataset structure generation
  116.     t_row * Dataset = (t_row *)malloc(NO_PATTERNS*sizeof(t_row));
  117.     for(int indexPattern = 0; indexPattern < NO_PATTERNS; indexPattern++){
  118.         Dataset[indexPattern].features = (double *)malloc(NEURONS_PER_PATTERN * sizeof(double));
  119.         Dataset[indexPattern].Class = (char *)malloc(MAX_LENGTH_STRING * sizeof(char));
  120.     }
  121.     // ---
  122.     // Target signals generation
  123.     const double timeConstant_fast = 800.0; // ms
  124.     const double timeConstant_slow = 8.0;   // ms
  125.     for(double t = 0.0; t < SIM_TIME; t += DT){
  126.         T_SLOW((int)(t/DT)) = 1 - exp((-t)/timeConstant_slow);
  127.         T_FAST((int)(t/DT)) = 1 - exp((-t)/timeConstant_fast);
  128.     }
  129.     // ---
  130.     // LSN setup
  131.     printf("LSN neurons spatial organization:\n\n");
  132.     int * grid = getGrid(R_LSN,C_LSN);
  133.    
  134.     for(int row = 0; row < R_LSN; row++){
  135.         for(int column = 0; column < C_LSN; column++){
  136.             Grid << grid[row * C_LSN + column] << "\t";
  137.             printf("%d\t",grid[row * C_LSN + column]);
  138.         }
  139.         printf("\n");
  140.         Grid << endl;
  141.     }
  142.     associateExtendedArrayToGrid(grid,R_LSN,C_LSN,extendedArray);
  143.     LSNconnectivitySetup(extendedArray,DIM_EXC,DIM_INH);
  144.     free(grid);
  145.     grid = NULL;
  146.     printf("LSN configuration = COMPLETE\n");
  147.     // ---
  148.     // Dataset acquisition
  149.     const char * delimiter = "-";
  150.     char * token;
  151.     if(IrisDataset != NULL){
  152.         char row[150];
  153.         int indexPattern = 0;
  154.         int indexFeature;
  155.         while(fgets(row,sizeof(row),IrisDataset) != NULL){
  156.             indexFeature = 0;
  157.             token = strtok(row,delimiter);
  158.             while(token != NULL){
  159.                 if(indexFeature < NEURONS_PER_PATTERN) Dataset[indexPattern].features[indexFeature] = atof(token);
  160.                 else strcpy(Dataset[indexPattern].Class,token); // This string comprises carriage return too.
  161.                 token = strtok(NULL,delimiter);
  162.                 indexFeature++;
  163.             }
  164.         }
  165.         printf("Iris dataset acquisition = COMPLETE\n");
  166.     }
  167.     // ---
  168.     // Learning phase
  169.     printf("\n--- LEARNING PHASE ---\n");
  170.     for(int indexPattern = 0; indexPattern < NO_PATTERNS; indexPattern++){
  171.         printf("\tSimulation no. %d ---\n",indexPattern + 1);
  172.        
  173.         resetNeuronInputLayer();
  174.         printf("\tReset of all state variables of Input Layer = COMPLETE\n");
  175.        
  176.         double * Iext_Amplitudes = getCurrents(Dataset[indexPattern]);
  177.        
  178.         printf("\tExecution...\n");
  179.         for(double t = 0.0; t <= SIM_TIME; t += DT){
  180.             I_InputLayer << indexPattern << "\t" << t << "\t";
  181.             // Applying currents
  182.             for(int indexNeuron = 0; indexNeuron < NEURONS_PER_PATTERN; indexNeuron++){
  183.                 IextINPUT[indexNeuron] = Iext_Amplitudes[indexNeuron];
  184.                 I_InputLayer << Iext_Amplitudes[indexNeuron] << "\t";
  185.             }
  186.             I_InputLayer << endl;
  187.             pushINPUTStateToDevice();
  188.             // ---
  189.             // Execution
  190.             stepTimeGPU(t);
  191.             // ---
  192.             // Data acquisition from memory
  193.             pullINPUTStateFromDevice();
  194.             pullLSN_EXCStateFromDevice();
  195.             pullLSN_INHStateFromDevice();
  196.             pullLSN_EXC_EXCStateFromDevice();
  197.             pullLSN_EXC_INHStateFromDevice();
  198.             pullLSN_INH_EXCStateFromDevice();
  199.             pullLSN_INH_INHStateFromDevice();
  200.             pullSNStateFromDevice();
  201.             pullINPUT_LSN_EXCStateFromDevice();
  202.             pullINPUT_LSN_INHStateFromDevice();
  203.             pullLSN_INH_SNsStateFromDevice();
  204.             pullLSN_EXC_SNsStateFromDevice();
  205.             // ---         
  206.             // Saving LSN data
  207.             V_LSN_EXC << indexPattern << t << "\t";
  208.             I_LSN_EXC << indexPattern << t << "\t";
  209.             for(int indexNeuron = 0; indexNeuron < DIM_EXC; indexNeuron++){
  210.                 V_LSN_EXC << VLSN_EXC[indexNeuron] << "\t";
  211.                 I_LSN_EXC << yLSN_EXC[indexNeuron] << "\t";
  212.             }
  213.             V_LSN_EXC << endl;
  214.             I_LSN_EXC << endl;
  215.             V_LSN_INH << indexPattern << "\t" << t << "\t";
  216.             I_LSN_INH << indexPattern << "\t" << t << "\t";
  217.             for(int indexNeuron = 0; indexNeuron < DIM_INH; indexNeuron++){
  218.                 V_LSN_INH << VLSN_INH[indexNeuron] << "\t";
  219.                 I_LSN_INH << yLSN_INH[indexNeuron] << "\t";
  220.             }
  221.             V_LSN_INH << endl;
  222.             I_LSN_INH << endl;
  223.             for(int indexNeuronPre = 0; indexNeuronPre < N_NEURONS_LSN; indexNeuronPre++){
  224.                 for(int indexNeuronPost = 0; indexNeuronPost < N_NEURONS_LSN; indexNeuronPost++){
  225.                     int indexSynapse;
  226.                     t_extendedArray pre = getItemFromExtendedArray(extendedArray,indexNeuronPre,DIM_EXC + DIM_INH);
  227.                     t_extendedArray post = getItemFromExtendedArray(extendedArray,indexNeuronPost,DIM_EXC + DIM_INH);
  228.                     if((pre.population == 0) && (post.population == 0)){
  229.                         indexSynapse = pre.indexInPopulation * DIM_EXC + post.indexInPopulation;
  230.                         W_LSN_EXC_EXC << indexPattern << "\t" << t << "\t" << pre.indexInPopulation << "\t" << post.indexInPopulation << "\t" << wLSN_EXC_EXC[indexSynapse] << endl;
  231.                     }
  232.                     else{
  233.                         if((pre.population == 0) && (post.population == 1)){
  234.                             indexSynapse = pre.indexInPopulation * DIM_INH + post.indexInPopulation;
  235.                             W_LSN_EXC_INH << indexPattern << "\t" << t << "\t" << pre.indexInPopulation << "\t" << post.indexInPopulation << "\t" << wLSN_EXC_INH[indexSynapse] << endl;
  236.                         }
  237.                         else{
  238.                             if((pre.population == 1) && (post.population == 0)){
  239.                                 indexSynapse = pre.indexInPopulation * DIM_EXC + post.indexInPopulation;
  240.                                 W_LSN_INH_EXC << indexPattern << "\t" << t << "\t" << pre.indexInPopulation << "\t" << post.indexInPopulation << "\t" << wLSN_INH_EXC[indexSynapse] << endl;
  241.                             }
  242.                             else{
  243.                                 indexSynapse = pre.indexInPopulation * DIM_INH + post.indexInPopulation;
  244.                                 W_LSN_INH_INH << indexPattern << "\t" << t << "\t" << pre.indexInPopulation << "\t" << post.indexInPopulation << "\t" << wLSN_INH_INH[indexSynapse] << endl;
  245.                             }
  246.                         }
  247.                     }
  248.                 }
  249.             }
  250.             // ---
  251.             // Saving SNs data
  252.             V_SNs << indexPattern << "\t" << t << "\t";
  253.             ISyn_SNs << indexPattern << "\t" << t << "\t";
  254.             for(int indexNeuron = 0; indexNeuron < N_CLASSES; indexNeuron++){
  255.                 V_SNs << VSN[indexNeuron] << "\t";
  256.                 ISyn_SNs << ISynSN[indexNeuron] << "\t";
  257.             }
  258.             V_SNs << endl;
  259.             ISyn_SNs << endl;
  260.             // ---
  261.             // Saving weights from INPUT to LSN_EXC/LSN_INH
  262.             for(int indexPre = 0; indexPre < NEURONS_PER_PATTERN; indexPre++){
  263.                 for(int indexPost = 0; indexPost < N_NEURONS_LSN; indexPost++){
  264.                     int idPost = extendedArray[indexPost].indexInPopulation;
  265.                     if(extendedArray[indexPost].population == 0) W_INPUT_LSN_EXC << indexPattern << "\t" << t << "\t" << indexPre << "\t" << idPost << "\t" << wINPUT_LSN_EXC[indexPre * DIM_EXC + idPost] << endl;
  266.                     else W_INPUT_LSN_EXC << indexPattern << "\t" << t << "\t" << indexPre << "\t" << idPost << "\t" << wINPUT_LSN_INH[indexPre * DIM_INH + idPost] << endl;
  267.                 }
  268.             }
  269.             // --- 
  270.             // Saving weights from LSN_EXC/LSN_INH to SNs
  271.             for(int indexPre = 0; indexPre < N_NEURONS_LSN; indexPre++){
  272.                 int idPre = extendedArray[indexPre].indexInPopulation;
  273.                 for(int indexPost = 0; indexPost < N_CLASSES; indexPost++){
  274.                     if(extendedArray[indexPre].population == 0) W_LSN_EXC_SNs << indexPattern << "\t" << t << "\t" << idPre << "\t" << indexPost << "\t" << wLSN_EXC_SNs[idPre * N_CLASSES + indexPost] << endl;
  275.                     else W_LSN_INH_SNs << indexPattern << "\t" << t << "\t" << idPre << "\t" << indexPost << "\t" << wLSN_INH_SNs[idPre * N_CLASSES + indexPost] << endl;
  276.                 }
  277.             }
  278.             // --- 
  279.             // Definition of Z for the given pattern
  280.             for(int indexNeuron = 0; indexNeuron < N_NEURONS_LSN; indexNeuron++) Z((int)t/DT,indexPattern * N_NEURONS_LSN + indexNeuron) = (extendedArray[indexNeuron].population == 0) ? jLSN_EXC_SNs[indexNeuron * N_CLASSES] : jLSN_INH_SNs[indexNeuron * N_CLASSES];
  281.             // ---
  282.         }
  283.         printf(" COMPLETE\n");
  284.     }
  285.     // Matrix computations and saving on file
  286.     MatrixXd Pseudoinverse((int)(N_NEURONS_LSN*NO_PATTERNS),(int)(SIM_TIME/DT));
  287.     Pseudoinverse = (Z.transpose()*Z).inverse()*Z.transpose();
  288.    
  289.     VectorT_FAST <<  T_FAST;
  290.     VectorT_SLOW <<  T_SLOW;
  291.     MatrixZ << Z;  
  292.    
  293.     const char * C1 = "Iris-setosa\n";
  294.     const char * C2 = "Iris-versicolor\n";
  295.     const char * C3 = "Iris-virginica\n";
  296.     char * Class;
  297.     for(int indexPattern = 0; indexPattern < NO_PATTERNS; indexPattern++){
  298.         strcpy(Class,Dataset[indexPattern].Class);
  299.        
  300.         if(strcmp(Class,C1) == 0){
  301.             W0 = Pseudoinverse * T_FAST;
  302.             W1 = Pseudoinverse * T_SLOW;
  303.             W2 = Pseudoinverse * T_SLOW;
  304.         }
  305.         else{
  306.             if(strcmp(Class,C2) == 0){
  307.                 W0 = Pseudoinverse * T_SLOW;
  308.                 W1 = Pseudoinverse * T_FAST;
  309.                 W2 = Pseudoinverse * T_SLOW;
  310.             }
  311.             else{
  312.                 if(strcmp(Class,C3) == 0){
  313.                     W0 = Pseudoinverse * T_SLOW;
  314.                     W1 = Pseudoinverse * T_SLOW;
  315.                     W2 = Pseudoinverse * T_FAST;
  316.                 }
  317.             }
  318.         }
  319.     }
  320.     // ---
  321.     printf("\nLEARNING PHASE COMPLETED\n\n");
  322.     // ---
  323.    
  324.     // ---
  325.     // printf("TESTING PHASE IS STARTED\n\n");
  326.    
  327.     // Closing files
  328.     IrisDataset.fclose();
  329.     I_InputLayer.close();
  330.     V_LSN_EXC.close();
  331.     V_LSN_INH.close();
  332.     I_LSN_EXC.close();
  333.     I_LSN_INH.close();
  334.     K.close();
  335.     C.close();
  336.     P.close();
  337.     Grid.close();
  338.     W_LSN_EXC_EXC.close();
  339.     W_LSN_EXC_INH.close();
  340.     W_LSN_INH_EXC.close();
  341.     W_LSN_INH_INH.close();
  342.     V_SNs.close();
  343.     ISyn_SNs.close();
  344.     W_INPUT_LSN_EXC.close();
  345.     W_INPUT_LSN_INH.close();
  346.     W_LSN_EXC_SNs.close();
  347.     W_LSN_INH_SNs.close();
  348.     VectorT_SLOW.close();
  349.     VectorT_FAST.close();
  350.     MatrixZ.close();
  351.     MatrixW.close();
  352.    
  353.     timer.stopTimer();
  354.    
  355.     return EXIT_SUCCESS;
  356. }
  357. // ---------------------- associateExtendedArrayToGrid -----------------------------
  358. void associateExtendedArrayToGrid(int * grid, int rows, int cols, t_extendedArray * array){
  359.     int index;
  360.    
  361.     for(int row = 0; row < rows; row++){
  362.         for(int col = 0; col < cols; col++){
  363.             index = grid[row * cols + col];
  364.             array[index].coordinates.x = row;
  365.             array[index].coordinates.y = col;
  366.         }
  367.     }
  368. }
  369. // ---------------------------- LSNconnectivitySetup -------------------------------
  370. void LSNconnectivitySetup(t_extendedArray * array, int DIM_EXC, int DIM_INH){
  371.     int dim = DIM_EXC + DIM_INH;
  372.     int indexSynapse;
  373.     double p;
  374.     double randomNumber;
  375.     double synapticWeight;
  376.     t_extendedArray neuron_i;
  377.     t_extendedArray neuron_j;
  378.    
  379.     srand(time(NULL));
  380.     for(int index_i = 0; index_i < dim; index_i++){
  381.         neuron_i = getItemFromExtendedArray(array,dim,index_i);
  382.        
  383.         for(int index_j = 0; index_j < dim; index_j++){
  384.             if(index_i != index_j){
  385.                 neuron_j = getItemFromExtendedArray(array,dim,index_j);
  386.                 K << neuron_i.indexInPopulation << "\t" << neuron_j.indexInPopulation << "\t" << neuron_i.population << "\t" << neuron_j.population << "\t";
  387.                 C << neuron_i.indexInPopulation << "\t" << neuron_j.indexInPopulation << "\t" << neuron_i.population << "\t" << neuron_j.population << "\t";
  388.                 P << neuron_i.indexInPopulation << "\t" << neuron_j.indexInPopulation << "\t" << neuron_i.population << "\t" << neuron_j.population << "\t";
  389.                
  390.                 p = getP(neuron_i,neuron_j);
  391.                 P << p << endl;
  392.        
  393.                 randomNumber = (double)rand() / (double)RAND_MAX;
  394.                 if(randomNumber >= p) synapticWeight = AUTOFEEDBACK_REMOVAL;
  395.                 else synapticWeight = (neuron_i.population == 0) ? 10.0 : -10.0;               
  396.                
  397.                 if((neuron_i.population == 0) && (neuron_j.population == 0)){
  398.                     // Pre-synaptic neuron is excitatory, post-synaptic neuron is excitatory
  399.                     indexSynapse = neuron_i.indexInPopulation * DIM_EXC + neuron_j.indexInPopulation;
  400.                     wLSN_EXC_EXC[indexSynapse] = synapticWeight;
  401.                     pushLSN_EXC_EXCToDevice();
  402.                 }
  403.                 else{
  404.                     if((neuron_i.population == 0) && (neuron_j.population == 1)){
  405.                         // Pre-synaptic neuron is excitatory, post-synaptic neuron is inhibitory
  406.                         indexSynapse = neuron_i.indexInPopulation * DIM_INH + neuron_j.indexInPopulation;
  407.                         wLSN_EXC_INH[indexSynapse] = synapticWeight;
  408.                         pushLSN_EXC_INHToDevice();
  409.                     }
  410.                     else{
  411.                         if((neuron_i.population == 1) && (neuron_j.population == 0)){
  412.                             // Pre-synaptic neuron is inhibitory, post-synaptic neuron is excitatory
  413.                             indexSynapse = neuron_i.indexInPopulation * DIM_EXC + neuron_j.indexInPopulation;
  414.                             wLSN_INH_EXC[indexSynapse] = synapticWeight;
  415.                             pushLSN_INH_EXCToDevice();
  416.                         }
  417.                         else{
  418.                             // Pre-synaptic neuron is inhibitory, post-synaptic neuron is inhibitory
  419.                             indexSynapse = neuron_i.indexInPopulation * DIM_INH + neuron_j.indexInPopulation;
  420.                             wLSN_INH_INH[indexSynapse] = synapticWeight;
  421.                             pushLSN_INH_INHToDevice();
  422.                         }
  423.                     }
  424.                 }
  425.             }
  426.             else{
  427.                 // Autofeedback removal
  428.                 if(neuron_i.population == 0){
  429.                     wLSN_EXC_EXC[neuron_i.indexInPopulation * (int)(1 + DIM_EXC)] = AUTOFEEDBACK_REMOVAL;
  430.                     pushLSN_EXC_EXCToDevice();
  431.                 }
  432.                 else{
  433.                     wLSN_INH_INH[neuron_i.indexInPopulation * (int)(1 + DIM_INH)] = AUTOFEEDBACK_REMOVAL;
  434.                     pushLSN_INH_INHToDevice();
  435.                 }
  436.             }
  437.         }
  438.     }
  439. }
  440. // -------------------------- resetNeuronInputLayer ------------------------------
  441. void resetNeuronInputLayer(void){
  442.     //
  443. }
  444. // ------------------------------- getExtendedArray --------------------------------
  445. t_extendedArray * getExtendedArray(int DIM_EXC, int DIM_INH){  
  446.     int index;
  447.     int dim = DIM_EXC + DIM_INH;
  448.     t_extendedArray * array = (t_extendedArray *)malloc(dim * sizeof(t_extendedArray));
  449.     if (!array){
  450.         perror("getExtendedArray malloc : FAILED\n\n");
  451.         exit(ENOMEM);
  452.     }
  453.    
  454.     for(index = 0; index < DIM_EXC; index++){
  455.         array[index].population = 0;
  456.         array[index].indexInPopulation = index;
  457.     }
  458.     while(index < dim){
  459.         array[index].population = 1;
  460.         array[index].indexInPopulation = index - DIM_EXC;
  461.         index++;
  462.     }
  463.    
  464.     return array;
  465. }
  466. // ----------------------------------- getGrid -------------------------------------
  467. int * getGrid(int rows, int cols){ 
  468.     int * grid = (int *)malloc(rows * cols * sizeof(int));
  469.     if (!grid){
  470.         perror("getGrid malloc : FAILED\n\n");
  471.         exit(ENOMEM);
  472.     }
  473.     return scrambleGrid(grid,rows,cols);
  474. }
  475. // -------------------------------- scrambleGrid -----------------------------------
  476. int * scrambleGrid(int * grid, int rows, int cols){
  477.     int * checkDuplicates = (int *)malloc(rows * cols * 2 * sizeof(int));
  478.     if (!checkDuplicates){
  479.         perror("scrambledGrid malloc : FAILED\n\n");
  480.         exit(ENOMEM);
  481.     }
  482.     int row;
  483.     int column;
  484.     int randomNumber;
  485.    
  486.     // Initializes matrix checkDuplicates
  487.     for(row = 0; row < cols * rows; row++){
  488.         checkDuplicates[2 * row] = row;
  489.         checkDuplicates[2 * row + 1] = 0;
  490.     }
  491.    
  492.     // Generates grid
  493.     srand(time(NULL));
  494.     for(row = 0; row < rows; row++){
  495.         for(column = 0; column < cols; column++){
  496.             do{
  497.                 randomNumber = rand() % (cols * rows);
  498.             } while(checkDuplicates[2 * randomNumber + 1] == 1);
  499.             checkDuplicates[2 * randomNumber + 1] = 1;
  500.             grid[row * cols + column] = randomNumber;
  501.         }
  502.     }
  503.    
  504.     free(checkDuplicates);
  505.     checkDuplicates = NULL;
  506.    
  507.     return grid;
  508. }
  509. // -------------------------------- getRows -----------------------------------
  510. int getRows(void){
  511.     int ch;
  512.     int n = 0;
  513.    
  514.     do{
  515.         ch = fgetc(IrisDataset);
  516.         if(ch == '\n')  n++;
  517.     } while (ch != EOF);
  518.     if(ch != '\n' && n != 0) n++;
  519.    
  520.     return n;
  521. }
  522. // -------------------------------------------- getCurrents ---------------------------------------------------
  523. double * getCurrents(t_row row){
  524.     const double Iext_Minimum = 10;
  525.     double * currents = (double *)malloc(NEURONS_PER_PATTERN * sizeof(double));
  526.     double max = getMax(row);
  527.     double min = getMin(row);
  528.     for(int i = 0; i < NEURONS_PER_PATTERN; i++) currents[i] = Iext_Minimum * (0.5 + (row.features[i] - min)/(max - min));
  529.     return currents;
  530. }
  531.  
  532. // -------------------------------------------- getDistance ---------------------------------------------------
  533. double getDistance(t_coordinates coordinates_neuron_i, t_coordinates coordinates_neuron_j){
  534.     return sqrt(pow(coordinates_neuron_i.x - coordinates_neuron_j.x,2) + pow(coordinates_neuron_i.y - coordinates_neuron_j.y,2));
  535. }
  536.  
  537. // -------------------------------------------- getK ----------------------------------------------------------
  538. double getK(t_extendedArray neuron_i, t_extendedArray neuron_j){
  539.     double distance = getDistance(neuron_i.coordinates, neuron_j.coordinates);
  540.    
  541.     if(distance <= 1) return 1;
  542.     if((distance > 1) && (distance <= 2)) return 0.5;
  543.     return 0;
  544. }
  545. // -------------------------------------------- getC ----------------------------------------------------------
  546. double getC(t_extendedArray neuron_i, t_extendedArray neuron_j){
  547.     const double percentageNoise = 0.1;
  548.     const double C_INH_EXC = 0.8;
  549.     const double C_EXC_EXC = 0.6;
  550.     const double C_EXC_INH = 0.4;
  551.     const double C_INH_INH = 0.2;
  552.     double x;
  553.    
  554.     if((neuron_i.population == 0) && (neuron_j.population == 0)) x = C_EXC_EXC;
  555.     if((neuron_i.population == 0) && (neuron_j.population == 1)) x = C_EXC_INH;
  556.     if((neuron_i.population == 1) && (neuron_j.population == 0)) x = C_INH_EXC;
  557.     if((neuron_i.population == 1) && (neuron_j.population == 1)) x = C_INH_INH;
  558.    
  559.     return (1-percentageNoise)*x+(2*percentageNoise*x)*((double)rand()/(double)RAND_MAX);
  560. }
  561. // -------------------------------------------- getP ----------------------------------------------------------
  562. double getP(t_extendedArray neuron_i, t_extendedArray neuron_j){
  563.     double k = getK(neuron_i, neuron_j);
  564.     double c = getC(neuron_i, neuron_j);
  565.    
  566.     K << k << endl;
  567.     C << c << endl;
  568.    
  569.     return k * c;
  570. }
  571. // -------------------------------------------- getMax --------------------------------------------------------
  572. double getMax(t_row row){
  573.     double * features = row.features;
  574.     double max = features[0];
  575.     for(int i = 1; i < NEURONS_PER_PATTERN; i++) if(max < features[i]) max = features[i];
  576.     return max;
  577. }
  578. // -------------------------------------------- getMin --------------------------------------------------------
  579. double getMin(double * vector, int row, int size){
  580.     double * features = row.features;
  581.     double min = features[0];
  582.     for(int i = 1; i < NEURONS_PER_PATTERN; i++) if(min > features[i]) min = features[i];
  583.     return min;
  584. }
  585. // ---------------------------------------- getItemFromExtendedArray ---------------------------------
  586. t_extendedArray getItemFromExtendedArray(t_extendedArray * array, int indexNeuron, int length){
  587.     int index = 0;
  588.     while((index < length) && index != indexNeuron) index++;
  589.     return array[index];
  590. }
Add Comment
Please, Sign In to add comment