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