Advertisement
SDL2

commit#830211

Jul 3rd, 2020
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3. class Matrix {
  4.  
  5.   //local variables
  6.   int rows;
  7.   int cols;
  8.   float[][] matrix;
  9.  
  10.   //---------------------------------------------------------------------------------------------------------------------------------------------------------  
  11.   //constructor
  12.   Matrix(int r, int c) {
  13.     rows = r;
  14.     cols = c;
  15.     matrix = new float[rows][cols];
  16.   }
  17.  
  18.   //---------------------------------------------------------------------------------------------------------------------------------------------------------  
  19.   //constructor from 2D array
  20.   Matrix(float[][] m) {
  21.     matrix = m;
  22.     cols = m.length;
  23.     rows = m[0].length;
  24.   }
  25.  
  26.   //---------------------------------------------------------------------------------------------------------------------------------------------------------  
  27.   //print matrix
  28.   void output() {
  29.     for (int i =0; i<rows; i++) {
  30.       for (int j = 0; j<cols; j++) {
  31.         print(matrix[i][j] + "  ");
  32.       }
  33.       println(" ");
  34.     }
  35.     println();
  36.   }
  37.   //---------------------------------------------------------------------------------------------------------------------------------------------------------  
  38.  
  39.   //multiply by scalar
  40.   void multiply(float n ) {
  41.  
  42.     for (int i =0; i<rows; i++) {
  43.       for (int j = 0; j<cols; j++) {
  44.         matrix[i][j] *= n;
  45.       }
  46.     }
  47.   }
  48.  
  49. //---------------------------------------------------------------------------------------------------------------------------------------------------------  
  50.   //return a matrix which is this matrix dot product parameter matrix
  51.   Matrix dot(Matrix n) {
  52.     Matrix result = new Matrix(rows, n.cols);
  53.    
  54.     if (cols == n.rows) {
  55.       //for each spot in the new matrix
  56.       for (int i =0; i<rows; i++) {
  57.         for (int j = 0; j<n.cols; j++) {
  58.           float sum = 0;
  59.           for (int k = 0; k<cols; k++) {
  60.             sum+= matrix[i][k]*n.matrix[k][j];
  61.           }
  62.           result.matrix[i][j] = sum;
  63.         }
  64.       }
  65.     }
  66.  
  67.     return result;
  68.   }
  69. //---------------------------------------------------------------------------------------------------------------------------------------------------------  
  70.   //set the matrix to random ints between -1 and 1
  71.   void randomize() {
  72.     for (int i =0; i<rows; i++) {
  73.       for (int j = 0; j<cols; j++) {
  74.         matrix[i][j] = random(-1, 1);
  75.       }
  76.     }
  77.   }
  78.  
  79. //---------------------------------------------------------------------------------------------------------------------------------------------------------  
  80.   //add a scalar to the matrix
  81.   void Add(float n ) {
  82.     for (int i =0; i<rows; i++) {
  83.       for (int j = 0; j<cols; j++) {
  84.         matrix[i][j] += n;
  85.       }
  86.     }
  87.   }
  88. //---------------------------------------------------------------------------------------------------------------------------------------------------------  
  89.   ///return a matrix which is this matrix + parameter matrix
  90.   Matrix add(Matrix n ) {
  91.     Matrix newMatrix = new Matrix(rows, cols);
  92.     if (cols == n.cols && rows == n.rows) {
  93.       for (int i =0; i<rows; i++) {
  94.         for (int j = 0; j<cols; j++) {
  95.           newMatrix.matrix[i][j] = matrix[i][j] + n.matrix[i][j];
  96.         }
  97.       }
  98.     }
  99.     return newMatrix;
  100.   }
  101. //---------------------------------------------------------------------------------------------------------------------------------------------------------  
  102.   //return a matrix which is this matrix - parameter matrix
  103.   Matrix subtract(Matrix n ) {
  104.     Matrix newMatrix = new Matrix(cols, rows);
  105.     if (cols == n.cols && rows == n.rows) {
  106.       for (int i =0; i<rows; i++) {
  107.         for (int j = 0; j<cols; j++) {
  108.           newMatrix.matrix[i][j] = matrix[i][j] - n.matrix[i][j];
  109.         }
  110.       }
  111.     }
  112.     return newMatrix;
  113.   }
  114. //---------------------------------------------------------------------------------------------------------------------------------------------------------  
  115.   //return a matrix which is this matrix * parameter matrix (element wise multiplication)
  116.   Matrix multiply(Matrix n ) {
  117.     Matrix newMatrix = new Matrix(rows, cols);
  118.     if (cols == n.cols && rows == n.rows) {
  119.       for (int i =0; i<rows; i++) {
  120.         for (int j = 0; j<cols; j++) {
  121.           newMatrix.matrix[i][j] = matrix[i][j] * n.matrix[i][j];
  122.         }
  123.       }
  124.     }
  125.     return newMatrix;
  126.   }
  127. //---------------------------------------------------------------------------------------------------------------------------------------------------------  
  128.   //return a matrix which is the transpose of this matrix
  129.   Matrix transpose() {
  130.     Matrix n = new Matrix(cols, rows);
  131.     for (int i =0; i<rows; i++) {
  132.       for (int j = 0; j<cols; j++) {
  133.         n.matrix[j][i] = matrix[i][j];
  134.       }
  135.     }
  136.     return n;
  137.   }
  138. //---------------------------------------------------------------------------------------------------------------------------------------------------------  
  139.   //Creates a single column array from the parameter array
  140.   Matrix singleColumnMatrixFromArray(float[] arr) {
  141.     Matrix n = new Matrix(arr.length, 1);
  142.     for (int i = 0; i< arr.length; i++) {
  143.       n.matrix[i][0] = arr[i];
  144.     }
  145.     return n;
  146.   }
  147.   //---------------------------------------------------------------------------------------------------------------------------------------------------------  
  148.   //sets this matrix from an array
  149.   void fromArray(float[] arr) {
  150.     for (int i = 0; i< rows; i++) {
  151.       for (int j = 0; j< cols; j++) {
  152.         matrix[i][j] =  arr[j+i*cols];
  153.       }
  154.     }
  155.   }
  156. //---------------------------------------------------------------------------------------------------------------------------------------------------------    
  157.   //returns an array which represents this matrix
  158.   float[] toArray() {
  159.     float[] arr = new float[rows*cols];
  160.     for (int i = 0; i< rows; i++) {
  161.       for (int j = 0; j< cols; j++) {
  162.         arr[j+i*cols] = matrix[i][j];
  163.       }
  164.     }
  165.     return arr;
  166.   }
  167.  
  168. //---------------------------------------------------------------------------------------------------------------------------------------------------------  
  169.   //for ix1 matrixes adds one to the bottom
  170.   Matrix addBias() {
  171.     Matrix n = new Matrix(rows+1, 1);
  172.     for (int i =0; i<rows; i++) {
  173.       n.matrix[i][0] = matrix[i][0];
  174.     }
  175.     n.matrix[rows][0] = 1;
  176.     return n;
  177.   }
  178. //---------------------------------------------------------------------------------------------------------------------------------------------------------  
  179.   //applies the activation function(sigmoid) to each element of the matrix
  180.   Matrix activate() {
  181.     Matrix n = new Matrix(rows, cols);
  182.     for (int i =0; i<rows; i++) {
  183.       for (int j = 0; j<cols; j++) {
  184.         n.matrix[i][j] = sigmoid(matrix[i][j]);
  185.       }
  186.     }
  187.     return n;
  188.   }
  189.  
  190. //---------------------------------------------------------------------------------------------------------------------------------------------------------    
  191.   //sigmoid activation function
  192.   float sigmoid(float x) {
  193.     float y = 1 / (1 + pow((float)Math.E, -x));
  194.     return y;
  195.   }
  196.   //returns the matrix that is the derived sigmoid function of the current matrix
  197.   Matrix sigmoidDerived() {
  198.     Matrix n = new Matrix(rows, cols);
  199.     for (int i =0; i<rows; i++) {
  200.       for (int j = 0; j<cols; j++) {
  201.         n.matrix[i][j] = (matrix[i][j] * (1- matrix[i][j]));
  202.       }
  203.     }
  204.     return n;
  205.   }
  206.  
  207. //---------------------------------------------------------------------------------------------------------------------------------------------------------  
  208.   //returns the matrix which is this matrix with the bottom layer removed
  209.   Matrix removeBottomLayer() {
  210.     Matrix n = new Matrix(rows-1, cols);      
  211.     for (int i =0; i<n.rows; i++) {
  212.       for (int j = 0; j<cols; j++) {
  213.         n.matrix[i][j] = matrix[i][j];
  214.       }
  215.     }
  216.     return n;
  217.   }
  218. //---------------------------------------------------------------------------------------------------------------------------------------------------------  
  219.   //Mutation function for genetic algorithm
  220.  
  221.   void mutate(float mutationRate) {
  222.    
  223.     //for each element in the matrix
  224.     for (int i =0; i<rows; i++) {
  225.       for (int j = 0; j<cols; j++) {
  226.         float rand = random(1);
  227.         if (rand<mutationRate) {//if chosen to be mutated
  228.           matrix[i][j] += randomGaussian()/5;//add a random value to it(can be negative)
  229.          
  230.           //set the boundaries to 1 and -1
  231.           if (matrix[i][j]>1) {
  232.             matrix[i][j] = 1;
  233.           }
  234.           if (matrix[i][j] <-1) {
  235.             matrix[i][j] = -1;
  236.           }
  237.         }
  238.       }
  239.     }
  240.   }
  241. //---------------------------------------------------------------------------------------------------------------------------------------------------------  
  242.   //returns a matrix which has a random number of values from this matrix and the rest from the parameter matrix
  243.   Matrix crossover(Matrix partner) {
  244.     Matrix child = new Matrix(rows, cols);
  245.    
  246.     //pick a random point in the matrix
  247.     int randC = floor(random(cols));
  248.     int randR = floor(random(rows));
  249.     for (int i =0; i<rows; i++) {
  250.       for (int j = 0; j<cols; j++) {
  251.  
  252.         if ((i< randR)|| (i==randR && j<=randC)) { //if before the random point then copy from this matric
  253.           child.matrix[i][j] = matrix[i][j];
  254.         } else { //if after the random point then copy from the parameter array
  255.           child.matrix[i][j] = partner.matrix[i][j];
  256.         }
  257.       }
  258.     }
  259.     return child;
  260.   }
  261. //---------------------------------------------------------------------------------------------------------------------------------------------------------  
  262.   //return a copy of this matrix
  263.   Matrix clone() {
  264.     Matrix clone = new  Matrix(rows, cols);
  265.     for (int i =0; i<rows; i++) {
  266.       for (int j = 0; j<cols; j++) {
  267.         clone.matrix[i][j] = matrix[i][j];
  268.       }
  269.     }
  270.     return clone;
  271.   }
  272. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement