-
Notifications
You must be signed in to change notification settings - Fork 0
/
Genome.cs
156 lines (132 loc) · 4.53 KB
/
Genome.cs
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NeuralNetwork
{
public class Genome
{
public int LayerCount { get; private set; }
public int[] NeuronsPerLayer { get; private set; }
public float[] Weights { get; private set; }
public float[] Biases { get; private set; }
public static readonly string SAVE_DIRECTORY = Environment.CurrentDirectory + "\\Data\\";
private const char seperator = ';';
public Genome(int layerCount, int[] neuronsPerLayer, float[] weights, float[] biases)
{
if (neuronsPerLayer == null)
throw new ArgumentNullException(nameof(neuronsPerLayer));
if (weights == null)
throw new ArgumentNullException(nameof(weights));
if (biases == null)
throw new ArgumentNullException(nameof(biases));
LayerCount = layerCount;
NeuronsPerLayer = neuronsPerLayer;
Weights = weights;
Biases = biases;
}
public Genome(NeuroNet neuroNet)
{
this.LayerCount = neuroNet.LayerCount;
int[] neuronsPerLayer = new int[LayerCount];
for (int i = 0; i < neuronsPerLayer.Length; i++)
{
neuronsPerLayer[i] = neuroNet.Neurons[i].Length;
}
this.NeuronsPerLayer = neuronsPerLayer;
List<float> Weights = new List<float>();
for (int i = 1; i < this.LayerCount; i++)
{
int index = i - 1;
for (int j = 0; j < neuroNet.Weights[index].Length; j++)
{
for (int k = 0; k < neuroNet.Weights[index][j].Length; k++)
{
Weights.Add(neuroNet.Weights[index][j][k]);
}
}
}
this.Weights = Weights.ToArray();
List<float> Biases = new List<float>();
for (int i = 1; i < this.LayerCount; i++)
{
int index = i - 1;
for (int j = 0; j < neuroNet.Biases[index].Length; j++)
{
Biases.Add(neuroNet.Biases[index][j]);
}
}
this.Biases = Biases.ToArray();
}
static Genome()
{
if (!Directory.Exists(SAVE_DIRECTORY))
{
Directory.CreateDirectory(SAVE_DIRECTORY);
}
}
public static void Save(NeuroNet neuroNet, string fileName)
{
Genome genome = new Genome(neuroNet);
string stringifiedGenome = "";
stringifiedGenome += genome.LayerCount + seperator.ToString();
stringifiedGenome += genome.NeuronsPerLayer.ToString('_') + seperator;
stringifiedGenome += genome.Weights.ToString('_') + seperator;
stringifiedGenome += genome.Biases.ToString('_') + seperator;
using (StreamWriter streamWriter = new StreamWriter(SAVE_DIRECTORY + fileName + ".genome"))
{
streamWriter.Write(stringifiedGenome);
streamWriter.Flush();
streamWriter.Close();
}
}
public static Genome Load(string fileName)
{
string stringifiedGenome = "";
using (StreamReader streamReader = new StreamReader(SAVE_DIRECTORY + fileName + ".genome"))
{
stringifiedGenome = streamReader.ReadToEnd();
streamReader.Close();
}
int layerCount = int.Parse(stringifiedGenome.Substring(0, stringifiedGenome.IndexOf(seperator)));
stringifiedGenome = stringifiedGenome.Substring(stringifiedGenome.IndexOf(seperator) + 1);
List<int> neuronsPerLayer = new List<int>();
stringifiedGenome = ExtractValues(stringifiedGenome, neuronsPerLayer);
List<float> weights = new List<float>();
stringifiedGenome = ExtractValues(stringifiedGenome, weights);
List<float> biases = new List<float>();
stringifiedGenome = ExtractValues(stringifiedGenome, biases);
return new Genome(layerCount, neuronsPerLayer.ToArray(), weights.ToArray(), biases.ToArray());
}
private static string ExtractValues(string stringifiedGenome, List<float> listToFill)
{
while (stringifiedGenome.IndexOf(seperator) > stringifiedGenome.IndexOf('_') && stringifiedGenome.IndexOf('_') != -1)
{
listToFill.Add(float.Parse(stringifiedGenome.Substring(0, stringifiedGenome.IndexOf('_'))));
stringifiedGenome = stringifiedGenome.Substring(stringifiedGenome.IndexOf('_') + 1);
}
if (stringifiedGenome.Contains(seperator))
{
listToFill.Add(float.Parse(stringifiedGenome.Substring(0, stringifiedGenome.IndexOf(seperator))));
stringifiedGenome = stringifiedGenome.Substring(stringifiedGenome.IndexOf(seperator) + 1);
}
else
{
listToFill.Add(float.Parse(stringifiedGenome.Substring(0)));
}
return stringifiedGenome;
}
private static string ExtractValues(string stringifiedGenome, List<int> listToFill)
{
List<float> lst = new List<float>();
stringifiedGenome = ExtractValues(stringifiedGenome, lst);
for (int i = 0; i < lst.Count; i++)
{
listToFill.Add((int)lst[i]);
}
return stringifiedGenome;
}
}
}