-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOutputLayerExternal.cs
More file actions
316 lines (261 loc) · 10.2 KB
/
OutputLayerExternal.cs
File metadata and controls
316 lines (261 loc) · 10.2 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
using System;
using System.Collections.Generic;
using System.Text;
namespace SLN
{
[Serializable]
class OutputLayerExternal
{
private LinkedList<Neuron> _neurons; //morris lecar
private LinkedList<SumNeuron> _neuronSum;
private LinkedList<SumNeuron> _neuronPersistance;
private LinkedList<SumNeuron> _neuronSameness;
private LinkedList<Synapse> _morrisToPersistance;
private LinkedList<Synapse> _morrisToSameness;
private int index = 0;
private double[,] matrixKI;
private double[] matrixf;
/* private double a = 0.03;
private double b = 0.2;
private double c = -65;
private double d = 2.5;
*/
private double a = 0.02;
private double b = 0.5;
private double c = -65;
private double d = 2;
/// <summary>
/// Constructor
/// </summary>
internal OutputLayerExternal()
{
this.init();
}
/// <summary>
/// initialization of the output layer
/// </summary>
private void init()
{
_neurons = new LinkedList<Neuron>();
_neuronSum = new LinkedList<SumNeuron>();
_neuronPersistance = new LinkedList<SumNeuron>();
_neuronSameness = new LinkedList<SumNeuron>();
matrixKI = new double[Constants.CLASSES, 2] { { 0.055, 61.5 },/* {0.067, 61.45}, {0.074, 61.4}, {0.082, 61.35}, {0.095, 61.25}, */{ 0.105, 61.2 }, { 0.155, 60.8 }, { 0.2, 60.5 } };
matrixf = new double[Constants.CLASSES] { 5, 10, 15, 20 }; //matrice delle frequenze
Neuron m = new Neuron();
m.A = a;
m.B = b;
m.C = c;
m.D = d;
m.setCoord(1, index, LayerNumbers.OUTPUT_LAYER);
m.i_const_morris = matrixf[index];
_neurons.AddLast(m);
SumNeuron n = new SumNeuron(0, matrixKI[index, 1]); //uso la variabile gain nel caso del sommatore per dire a che valore deve saturare l'ingresso (in pratico stabilisco il valore della saturazione della funzione di heaviside
n.setCoord(0, index, LayerNumbers.OUTPUT_LAYER);
_neuronSum.AddLast(n);
SumNeuron o = new SumNeuron();
o.setCoord(2, index, LayerNumbers.OUTPUT_LAYER);
_neuronPersistance.AddLast(o);
SumNeuron p = new SumNeuron(Constants.MORRIS_TO_SAMENESS_DECAY, Constants.MORRIS_TO_SAMENESS_GAIN);
p.setCoord(3, index, LayerNumbers.OUTPUT_LAYER);
_neuronSameness.AddLast(p);
_morrisToPersistance = new LinkedList<Synapse>();
_morrisToSameness = new LinkedList<Synapse>();
Synapse syn = new SynapseSameness(m, o, Constants.MORRIS_TO_SAMENESS_W / matrixf[index],
Constants.MORRIS_TO_SAMENESS_TAU,
Constants.MORRIS_TO_SAMENESS_DELAY_STEP,
Constants.MORRIS_TO_SAMENESS_SYNAPTIC_GAIN);
_morrisToPersistance.AddLast(syn);
Synapse syn2 = new SynapseSameness(m, p, Constants.MORRIS_TO_SAMENESS_W / matrixf[index],
Constants.MORRIS_TO_SAMENESS_TAU1,
Constants.MORRIS_TO_SAMENESS_DELAY_STEP,
Constants.MORRIS_TO_SAMENESS_SYNAPTIC_GAIN);
_morrisToSameness.AddLast(syn2);
index = index + 1;
}
/// <summary>
/// Finds the sameness neuron at the specific coordinates
/// </summary>
/// <returns>The neuron at the specified coordinates</returns>
internal SumNeuron getNeuronPersistance(int indexSameness)
{
foreach (SumNeuron a in _neuronPersistance)
{
if (a.COLUMN == indexSameness)
return a;
}
return null;
}
/// <summary>
/// Finds the sameness neuron at the specific coordinates
/// </summary>
/// <returns>The neuron at the specified coordinates</returns>
internal Neuron getNeuronMorris(int indexMorris)
{
foreach (Neuron a in _neurons)
{
if (a.COLUMN == indexMorris)
return a;
}
return null;
}
/// <summary>
/// Finds the neuron at the specific coordinates in the output of the Liquid
/// </summary>
/// <returns>The neuron at the specified coordinates</returns>
internal SumNeuron getOutNeuronFirst()
{
SumNeuron dest = new SumNeuron();
foreach (SumNeuron a in _neuronSum)
{
dest = a;
return dest;
}
return null;
}
public int getWinnerNeuron()
{
int indNeuron = -1;
foreach (Neuron n in _neurons)
{
double spikemax = getMorrisThreshold(n.COLUMN);
if (n.NSpikes > spikemax)
{
spikemax = n.NSpikes;
indNeuron = n.COLUMN;
}
}
return indNeuron;
}
/// <summary>
/// Simulate one Morris Lecar of the output layer
/// </summary>
/// <param name="step"></param>
/// <param name="target"></param>
/// <param name="Z"></param>
public int simulateMorrisLecar(int step, int target, double Z)
{
int cSpike = 0;
foreach (Neuron m in _neurons)
{
if (m.COLUMN == target)
{
m.I = Z;
m.simulate(step);
cSpike = m.NSpikes;
return cSpike;
}
}
return -1;
}
/// <summary>
/// Simulate the output layer
/// </summary>
/// <param name="step"></param>
/// <param name="log"></param>
public void simulate(int step, StateLogger log, int simNumberInternal, bool integration)
{
foreach (SumNeuron n in _neuronSum)
{
n.simulate(step); //scelgo questo o l'opzione sotto in base al fatto che voglio o no un neurone sommatore non lineare.
//n.simulateSquare(step);
if (log != null)
log.logNeuron(step, n);
foreach (Neuron m in _neurons)
{
if (m.COLUMN == n.COLUMN)
{
if (n.V > 0)
m.I = 10;
else
m.I = -10;
m.simulate(step);
if (log != null)
log.logNeuronMorrisLecar(step, m);
}
}
}
if (simNumberInternal == -1)
{
foreach (SynapseSameness s in _morrisToPersistance)
{
s.simulate(step);
if (log != null)
log.logSynapse(s, step);
}
foreach (SynapseSameness s in _morrisToSameness)
{
s.simulate(step);
if (log != null)
log.logSynapse(s, step);
}
foreach (SumNeuron n in _neuronPersistance)
{
n.simulateSameness(step, integration);
if (log != null)
log.logNeuron(step, n);
}
foreach (SumNeuron n in _neuronSameness)
{
n.simulateSameness(step, true);
if (log != null)
log.logNeuron(step, n);
}
}
}
public SumNeuron addNeuron()
{
Neuron m = new Neuron();
m.A = a;
m.B = b;
m.C = c;
m.D = d;
m.i_const_morris = matrixf[index];
m.setCoord(1, index, LayerNumbers.OUTPUT_LAYER);
SumNeuron n = new SumNeuron(0, matrixKI[index, 1]);
n.setCoord(0, index, LayerNumbers.OUTPUT_LAYER);
SumNeuron o = new SumNeuron();
o.setCoord(2, index, LayerNumbers.OUTPUT_LAYER);
SumNeuron p = new SumNeuron(Constants.MORRIS_TO_SAMENESS_DECAY, Constants.MORRIS_TO_SAMENESS_GAIN);
p.setCoord(3, index, LayerNumbers.OUTPUT_LAYER);
Synapse syn = new SynapseSameness(m, o, Constants.MORRIS_TO_SAMENESS_W / matrixf[index],
Constants.MORRIS_TO_SAMENESS_TAU,
Constants.MORRIS_TO_SAMENESS_DELAY_STEP,
Constants.MORRIS_TO_SAMENESS_SYNAPTIC_GAIN);
_morrisToPersistance.AddLast(syn);
Synapse syn2 = new SynapseSameness(m, p, Constants.MORRIS_TO_SAMENESS_W / matrixf[index],
Constants.MORRIS_TO_SAMENESS_TAU1,
Constants.MORRIS_TO_SAMENESS_DELAY_STEP,
Constants.MORRIS_TO_SAMENESS_SYNAPTIC_GAIN);
_morrisToSameness.AddLast(syn2);
index = index + 1;
_neurons.AddLast(m);
_neuronSum.AddLast(n);
_neuronSameness.AddLast(p);
_neuronPersistance.AddLast(o);
return n;
}
public void resetState()
{
foreach (Neuron n in _neurons)
n.resetState();
foreach (SumNeuron n in _neuronSum)
n.resetState();
}
public double getMorrisThreshold(int target)
{
//potremmo usare un vettore di treshold
foreach (Neuron m in _neurons)
if (m.COLUMN == target)
return matrixf[target];
return Double.MaxValue; //ritorna infinito se al target non corrisponde un neurone
}
public double getMatrixf(int index)
{
if (index > matrixf.Length - 1)
return -1;
else
return matrixf[index];
}
}
}