-
Notifications
You must be signed in to change notification settings - Fork 0
/
Neat.cs
184 lines (171 loc) · 6.45 KB
/
Neat.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
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
[Serializable]
internal class NEAT
{
public List<Neuron> neurons = new List<Neuron>();
public List<Synapse> synapses = new List<Synapse>();
public List<Neuron> inputNeurons = new List<Neuron>();
public List<Neuron> outputNeurons = new List<Neuron>();
public List<List<Neuron>> layers = new List<List<Neuron>>();
public NEAT(Random random, int inputNeuronCount, int outputNeuronCount)
{
initNeurons(random, inputNeuronCount, outputNeuronCount);
initWeights(random);
}
public NEAT(Random random, NEAT copyFrom, int inputNeuronCount, int outputNeuronCount)
{
if (copyFrom == null)
{
initNeurons(random, inputNeuronCount, outputNeuronCount);
initWeights(random);
return;
}
// serialize and deserialize to copy
using (MemoryStream memoryStream = new MemoryStream())
{
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter binaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
binaryFormatter.Serialize(memoryStream, copyFrom);
memoryStream.Position = 0;
NEAT copy = (NEAT)binaryFormatter.Deserialize(memoryStream);
neurons = copy.neurons;
synapses = copy.synapses;
inputNeurons = copy.inputNeurons;
outputNeurons = copy.outputNeurons;
layers = copy.layers;
}
}
public void Save()
{
using (FileStream fileStream = new FileStream("neat.dat", FileMode.Create))
{
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter binaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
binaryFormatter.Serialize(fileStream, this);
}
}
public void Load()
{
using (FileStream fileStream = new FileStream("neat.dat", FileMode.Open))
{
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter binaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
NEAT copy = (NEAT)binaryFormatter.Deserialize(fileStream);
neurons = copy.neurons;
synapses = copy.synapses;
inputNeurons = copy.inputNeurons;
outputNeurons = copy.outputNeurons;
layers = copy.layers;
}
}
public void Mutate(Random random)
{
double chance = random.NextDouble();
if (chance < 0.8)
{
MutateWeights(random);
}
else if (chance < 0.9)
{
MutateAddNeuron(random);
}
else
{
MutateAddSynapse(random);
}
}
private void MutateAddSynapse(Random random)
{
Neuron neuron1 = neurons[random.Next(neurons.Count)];
Neuron neuron2 = neurons[random.Next(neurons.Count)];
if (neuron1 == neuron2)
{
return;
}
Synapse synapse = new Synapse(random, neuron1, neuron2);
}
private void MutateAddNeuron(Random random)
{
Synapse synapse = synapses[random.Next(synapses.Count)];
int layerIndex = 0;
for (int i = 0; i < layers.Count; i++)
{
if (layers[i].Contains(synapse.outputNeuron))
{
layerIndex = i;
break;
}
}
List<Neuron> newLayer = new List<Neuron>();
Neuron neuron = new Neuron(random);
newLayer.Add(neuron);
Synapse synapse1 = new Synapse(random, synapse.inputNeuron, neuron);
Synapse synapse2 = new Synapse(random, neuron, synapse.outputNeuron);
neurons.Add(neuron);
synapses.Add(synapse1);
synapses.Add(synapse2);
layers.Insert(layerIndex, newLayer);
synapses.Remove(synapse);
}
private void MutateWeights(Random random)
{
foreach (Synapse synapse in synapses)
{
synapse.weight += random.NextDouble() * 2 - 1;
}
}
private void initNeurons(Random random, int inputNeuronCount, int outputNeuronCount)
{
for (int i = 0; i < inputNeuronCount; i++)
{
Neuron neuron = new Neuron(random);
inputNeurons.Add(neuron);
neurons.Add(neuron);
}
for (int i = 0; i < outputNeuronCount; i++)
{
Neuron neuron = new Neuron(random);
outputNeurons.Add(neuron);
neurons.Add(neuron);
}
layers.Add(inputNeurons);
layers.Add(outputNeurons);
}
private void initWeights(Random random)
{
for (int i = 0; i < inputNeurons.Count; i++)
{
for (int j = 0; j < outputNeurons.Count; j++)
{
Synapse synapse = new Synapse(random, inputNeurons[i], outputNeurons[j]);
synapses.Add(synapse);
}
}
}
public List<double> getOutput(List<double> inputValues)
{
// if not same input count as neuron count
if (inputValues.Count != layers[0].Count)
{
throw new Exception("Input values must match input neuron count");
}
// set input values
for (int i = 0; i < inputValues.Count; i++)
{
layers[0][i].output = inputValues[i];
}
// activate neurons
foreach (List<Neuron> layer in layers)
{
foreach (Neuron neuron in layer)
{
neuron.Activate();
}
}
// get output values
List<double> outputValues = new List<double>();
// for each output neuron
foreach (Neuron neuron in layers[layers.Count - 1])
{
outputValues.Add(neuron.output);
}
// return output values
return outputValues;
}
}