This repository was archived by the owner on Feb 18, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathNeuralNetwork.cs
More file actions
428 lines (415 loc) · 15.8 KB
/
NeuralNetwork.cs
File metadata and controls
428 lines (415 loc) · 15.8 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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
using System;
using System.Collections.Generic;
namespace Gerbil
{
namespace NeuralNetwork
{
/// <summary>
/// Base class for network nodes.
/// </summary>
class Node
{
public delegate void NodeFiredHandler(object sender, NetPathEventArgs e);
public event NodeFiredHandler Fired;
private string nodeName;
/// <summary>
/// Constructor method for Node class.
/// </summary>
public Node()
{
nodeName = "";
}
/// <summary>
/// Constructor method for Node class
/// </summary>
/// <param name="nName">Name of node</param>
public Node(string nName)
{
nodeName = nName;
}
/// <summary>
/// Constructor method for Node class.
/// </summary>
/// <param name="nName">Name of node</param>
/// <param name="input">Pointer to a list of active network connections</param>
/// <param name="connectorSelector">Name of connector to trigger new node</param>
public Node(string nName, ref Dictionary<string, Connection> input, string connectorSelector)
{
nodeName = nName;
input[connectorSelector].Fired += new Connection.ConnectionFiredHandler(Fire);
}
/// <summary>
/// Event handler for Node object.
/// </summary>
/// <param name="sender">Sender the fired the event (Type:InputNode)</param>
/// <param name="e">EventArg carrying fire value</param>
public virtual void Fire(object sender, NetPathEventArgs e)
{
Fired(sender, e);
}
/// <summary>
/// Gets the name of the specified node
/// </summary>
/// <returns>Name of referenced node</returns>
public string getName()
{
return nodeName;
}
/// <summary>
/// Gets the name of the specified node
/// </summary>
/// <param name="nd">Node to examine</param>
/// <returns>Name of referenced node</returns>
public static string getName(Node nd)
{
return nd.getName();
}
/// <summary>
/// Adds a connector fire event to the specified node
/// </summary>
/// <param name="input">Pointer to list of active connection list</param>
/// <param name="connectorSelector">Name of connector to bind to Fired() event</param>
public void addConnection(ref Dictionary<string, Connection> input, string connectorSelector)
{
input[connectorSelector].Fired += new Connection.ConnectionFiredHandler(Fire);
}
}
/// <summary>
/// Node that does not recieve input, but can be fired using an outside method call.
/// </summary>
class InputNode : Node
{
/// <summary>
/// Constructor class for InputNode
/// </summary>
public InputNode()
: base()
{
}
/// <summary>
/// Constructor class for InputNode
/// </summary>
/// <param name="nName">Name of node to create</param>
public InputNode(string nName)
: base(nName)
{
}
/// <summary>
/// Trigger to fire an event through the neural network.
/// </summary>
public void Fire()
{
NetPathEventArgs e = new NetPathEventArgs();
e.value = 1.0f;
e.tag = new Random().Next(100000000, 999999999).ToString();
base.Fire(this, e);
}
}
/// <summary>
/// Node that stores its value and cannot be tagged as an input to another connection.
/// </summary>
class OutputNode : Node
{
private float result;
/// <summary>
/// Constructor for OutputNode class
/// </summary>
/// <param name="nName">Name of node to create</param>
/// <param name="input">Pointer to list of active connections in the network</param>
/// <param name="connectorSelector">Name of connection to bind to Fired() event</param>
public OutputNode(string nName, ref Dictionary<string, Connection> input, string connectorSelector)
: base(nName, ref input, connectorSelector)
{
result = 1.0f;
}
/// <summary>
/// Event handler for Fired() event. Stores input value.
/// </summary>
/// <param name="sender">Event trigger</param>
/// <param name="e">EventArg containing neural trigger value</param>
public override void Fire(object sender, NetPathEventArgs e)
{
result += (e.value + 1);
}
/// <summary>
/// Gets value of OutputNode
/// </summary>
/// <returns>Value of node</returns>
public float getResult()
{
return result;
}
}
/// <summary>
/// Node that holds its value for checkpoint lookups
/// </summary>
class CheckpointNode : Node
{
private float result;
/// <summary>
/// Constructor for CheckpointNode class
/// </summary>
/// <param name="nName">Name of node</param>
/// <param name="input">Pointer to list of active connections in network</param>
/// <param name="connectorSelector">Name of connector to bind Fired() event to</param>
public CheckpointNode(string nName, ref Dictionary<string, Connection> input, string connectorSelector)
: base(nName, ref input, connectorSelector)
{
result = 1.0f;
}
/// <summary>
/// Event handler for Connection Fired() event
/// </summary>
/// <param name="sender">InputNode event sender</param>
/// <param name="e">Tag containing neural value</param>
public override void Fire(object sender, NetPathEventArgs e)
{
result += (e.value + 1);
base.Fire(sender, e);
}
/// <summary>
/// Get checkpoint value of node
/// </summary>
/// <returns>Value of node</returns>
public float getResult()
{
return result;
}
}
/// <summary>
/// Connector to bind two nodes together
/// </summary>
class Connection
{
public delegate void ConnectionFiredHandler(object sender, NetPathEventArgs e);
public event ConnectionFiredHandler Fired;
/// <summary>
/// Holds weight to be applied to EventArg tag
/// </summary>
public float Weight
{
get
{
return weight;
}
set
{
if (verifyWeight(value))
{
weight = value;
}
}
}
private float weight;
/// <summary>
/// Constructor for Connection class
/// </summary>
/// <param name="cWeight">Weight of connection</param>
/// <param name="input">Node output Fire() event to bind to</param>
public Connection(float cWeight, ref Node input)
{
input.Fired += new Node.NodeFiredHandler(Fire);
Weight = cWeight;
}
/// <summary>
/// Constructor for Connection class
/// </summary>
/// <param name="cWeight">Weight of connection</param>
/// <param name="input">Pointer to list of active nodes</param>
/// <param name="nodeSelector">Name of node to bind Fire() output event to</param>
public Connection(float cWeight, ref Dictionary<string, Node> input, string nodeSelector)
{
input[nodeSelector].Fired += new Node.NodeFiredHandler(Fire);
Weight = cWeight;
}
/// <summary>
/// Constructor for Connection class
/// </summary>
/// <param name="cWeight">Weight of connection</param>
/// <param name="input">Pointer to list of active nodes</param>
/// <param name="nodeSelector">Name of InputNode to bind Fire() output event to</param>
public Connection(float cWeight, ref Dictionary<string, InputNode> input, string nodeSelector)
{
input[nodeSelector].Fired += new Node.NodeFiredHandler(Fire);
Weight = cWeight;
}
/// <summary>
/// Event handler on node Fire()
/// </summary>
/// <param name="sender">InputNode sender</param>
/// <param name="e">Tag containing neural value</param>
public void Fire(object sender, NetPathEventArgs e)
{
e.value = e.value * (Weight + 1);
Fired(sender, e);
}
/// <summary>
/// Verifies input weight of a Connection
/// </summary>
/// <param name="input">Input weight</param>
/// <returns>Weight is valid</returns>
public static bool verifyWeight(float input)
{
if (input.GetType() == typeof(float) && input >= 0.0f)
{
return true;
}
else
{
return false;
}
}
}
/// <summary>
/// Neural fire tag class
/// </summary>
class NetPathEventArgs : EventArgs
{
public float value { get; set; }
public string tag { get; set; }
}
/// <summary>
/// Base network class
/// </summary>
public class Network
{
public static Random rd = new Random();
private Dictionary<string, Node> nodes;
private Dictionary<string, InputNode> inputs;
private Dictionary<string, OutputNode> outputs;
private Dictionary<string, Connection> connectors;
/// <summary>
/// Constructor for Network class
/// </summary>
public Network()
{
nodes = new Dictionary<string, Node>();
inputs = new Dictionary<string, InputNode>();
outputs = new Dictionary<string, OutputNode>();
connectors = new Dictionary<string, Connection>();
}
/// <summary>
/// Adds a node to the network
/// </summary>
/// <param name="nName">Name of node to add</param>
/// <param name="cName">Name of connector to connect to node</param>
/// <param name="weight">Weight of connector</param>
/// <param name="inNode">Node feeding into connector/node</param>
public void addNode(string nName, string cName, float weight, string inNode)
{
string cName2 = addConnector(cName, weight, inNode);
nodes.Add(nName, new Node(nName, ref connectors, cName2));
}
/// <summary>
/// Adds a connector to the network
/// </summary>
/// <param name="cName">Name of connector to add</param>
/// <param name="weight">Weight of connector</param>
/// <param name="inNode">Node feeding into connector</param>
/// <returns>Name of connector created</returns>
public string addConnector(string cName, float weight, string inNode)
{
string rcName = cName;
if (connectors.ContainsKey(cName))
{
rcName = rcName + rd.Next(100000, 999999).ToString();
}
if (inputs.ContainsKey(inNode))
{
connectors.Add(rcName, new Connection(weight, ref inputs, inNode));
}
else if (nodes.ContainsKey(inNode))
{
connectors.Add(rcName, new Connection(weight, ref nodes, inNode));
}
else
{
throw new NodeNotFoundException();
}
return rcName;
}
/// <summary>
/// Adds an output node to the network
/// </summary>
/// <param name="oName">Name of output node</param>
/// <param name="cName">Name of connector</param>
/// <param name="weight">Weight of connector</param>
/// <param name="inNode">Name of node to bind connector/node to</param>
public void addOutput(string oName, string cName, float weight, string inNode)
{
string rcName = addConnector(cName, weight, inNode);
if (!outputs.ContainsKey(oName))
{
outputs.Add(oName, new OutputNode(oName, ref connectors, rcName));
}
else
{
outputs[oName].addConnection(ref connectors, rcName);
}
}
/// <summary>
/// Adds an InputNode to the network
/// </summary>
/// <param name="name">Name of node to create</param>
public void addInput(string name)
{
if (inputs.ContainsKey(name))
{
// Do nothing, input key already exists
}
else
{
inputs.Add(name, new InputNode(name));
}
}
/// <summary>
/// Gets the results of a neural network computation
/// </summary>
/// <returns>List of values of all OutputNodes</returns>
public Dictionary<string, float> getResults()
{
if (inputs.Count == 0 || outputs.Count == 0)
{
throw new EmptyNodeGroupException();
}
Dictionary<string, float> results = new Dictionary<string, float>();
foreach (KeyValuePair<string, OutputNode> i in outputs)
{
results.Add(i.Key, i.Value.getResult());
}
return results;
}
/// <summary>
/// Trigger to start a neural computation
/// </summary>
/// <param name="fireNode">Name of InputNode to fire</param>
public void fireInput(string fireNode)
{
if (inputs.Count == 0 || outputs.Count == 0)
{
throw new EmptyNodeGroupException();
}
if (inputs.ContainsKey(fireNode))
{
inputs[fireNode].Fire();
}
else
{
throw new NodeNotFoundException();
}
}
}
/// <summary>
/// Exception type for missing node parameter
/// </summary>
class NodeNotFoundException : Exception
{
}
/// <summary>
/// Exception for performing operator on empty node group
/// </summary>
class EmptyNodeGroupException : Exception
{
}
}
}