-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix.cs
More file actions
333 lines (288 loc) · 8.87 KB
/
Matrix.cs
File metadata and controls
333 lines (288 loc) · 8.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SnakeEvo
{
public class Matrix
{
public int rows;
public int cols;
public float[,] matrix;
public Matrix(int r, int c)
{
rows = r;
cols = c;
matrix = new float[rows, cols];
}
public Matrix(float [,] m)
{
matrix = m;
rows = m.GetLength(0);
cols = m.GetLength(1);
}
public void Multiply(float n)
{
for(int i = 0; i <rows; i++)
{
for(int j = 0; j<cols; j++)
{
matrix[i,j] *= n;
}
}
}
//mnozenje jednodim. vektora "Dot product in Matrix notation
public Matrix Dot(Matrix n)
{
Matrix result = new Matrix(rows, n.cols);
if(cols == n.rows)
{
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < n.cols; j++)
{
float sum = 0;
for (int k=0 ; k < cols ; k++)
{
sum += matrix[i, k] * n.matrix[k, j];
}
result.matrix[i, j] = sum;
}
}
}
return result;
}
public void Randomize()
{
int max = 1;
int min = -1;
for(int i= 0; i<rows; i++)
{
for(int j=0; j<cols; j++)
{
matrix[i, j] = (float)(Rnd.RandomDouble() * Math.Abs(max - min) + min);
}
}
}
public void Add(float n)
{
for(int i = 0; i <rows; i++)
{
for(int j = 0; j< cols; j++)
{
matrix[i, j] += n;
}
}
}
public Matrix Add (Matrix n)
{
Matrix newMatrix = new Matrix(rows, cols);
if(cols == n.cols && rows == n.rows)
{
for(int i = 0; i<rows; i++)
{
for(int j=0; j<cols; j++)
{
newMatrix.matrix[i, j] = matrix[i, j] + n.matrix[i, j];
}
}
}
return newMatrix;
}
public Matrix Substract(Matrix n)
{
Matrix newMatrix = new Matrix(rows, cols);
if (cols == n.cols && rows == n.rows)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
newMatrix.matrix[i, j] = matrix[i, j] - n.matrix[i, j];
}
}
}
return newMatrix;
}
public Matrix Multiply(Matrix n)
{
Matrix newMatrix = new Matrix(rows, cols);
if (cols == n.cols && rows == n.rows)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
newMatrix.matrix[i, j] = matrix[i, j] * n.matrix[i, j];
}
}
}
return newMatrix;
}
public Matrix Transpose()
{
Matrix n = new Matrix(cols, rows);
for(int i = 0; i<rows; i++)
{
for(int j = 0; j < cols; j++)
{
n.matrix[j, i] = matrix[i, j];
}
}
return n;
}
public Matrix SingleColumnMatrixFromArray(float[] arr)
{
Matrix n = new Matrix(arr.Length, 1);
for(int i = 0; i < arr.Length; i++)
{
n.matrix[i, 0] = arr[i];
}
return n;
}
public void FromArray(float[] arr)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
matrix[i,j] = arr[j + i * cols];
}
}
}
public float[] ToArray()
{
float[] arr = new float[rows * cols];
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
arr[j + i * cols] = matrix[i,j];
}
}
return arr;
}
//još jedan red i popuni ga sa 1
public Matrix AddBias()
{
Matrix n = new Matrix(rows + 1, 1);
for (int i = 0; i < rows; i++)
{
n.matrix[i,0] = matrix[i,0];
}
n.matrix[rows,0] = 1;
return n;
}
//prođi kroz aktivacijsku funkciju - sigmoid
public Matrix Activate()
{
Matrix n = new Matrix(rows, cols);
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
n.matrix[i,j] = Sigmoid(matrix[i,j]);
}
}
return n;
}
//sigmoid ( ovo zamjeniti za neku drugu aktiv. funkc)
public float Sigmoid(float x)
{
float y = 1 / (1 + (float)Math.Pow((float)Math.E, -x));
return y;
}
//dobivena matrica nakon aktivacije
public Matrix SigmoidDerived()
{
Matrix n = new Matrix(rows, cols);
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
n.matrix[i,j] = (matrix[i,j] * (1 - matrix[i,j]));
}
}
return n;
}
public Matrix RemoveBottomLayer()
{
Matrix n = new Matrix(rows - 1, cols);
for (int i = 0; i < n.rows; i++)
{
for (int j = 0; j < cols; j++)
{
n.matrix[i,j] = matrix[i,j];
}
}
return n;
}
public void Mutate(float mutationRate)
{
double mean = 0; //parametriziranje
double stdDev = 1; //parametriziranje
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
float rand = (float)Rnd.RandomDouble();
if (rand < mutationRate) //ako je izabran za mutaciju
{
//Gaussian rand - Box-Muller
double u1 = 1.0 - Rnd.RandomDouble(); //(0,1] random double
double u2 = 1.0 - Rnd.RandomDouble();
double randStdNormal = Math.Sqrt(-2.0 * Math.Log(u1)) *
Math.Sin(2.0 * Math.PI * u2); //random normal(0,1)
double randNormal = mean + stdDev * randStdNormal; //random normal(mean,stdDev^2)
matrix[i,j] += (float)randNormal / 5; //dodaj random vrijednost, +,-
//ispravi
if (matrix[i,j] > 1)
{
matrix[i,j] = 1;
}
if (matrix[i,j] < -1)
{
matrix[i,j] = -1;
}
}
}
}
}
public Matrix Crossover(Matrix partner)
{
Matrix child = new Matrix(rows, cols);
double tempRandC = Rnd.RandomDouble() * cols;
double tempRandR = Rnd.RandomDouble() * rows;
int randC = (int)Math.Floor(tempRandC);
int randR = (int)Math.Floor(tempRandR);
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
if ((i < randR) || (i == randR && j <= randC)) //ispod i poviše nasumične točke
{
child.matrix[i,j] = matrix[i,j]; //prvi roditelj
}
else
{
child.matrix[i,j] = partner.matrix[i,j]; //drugi roditelj
}
}
}
return child;
}
public Matrix Clone()
{
Matrix clone = new Matrix(rows, cols);
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
clone.matrix[i,j] = matrix[i,j];
}
}
return clone;
}
}
}