source: proiecte/ParallelANN/BCMToolbox/Base.cs @ 171

Last change on this file since 171 was 171, checked in by (none), 14 years ago
File size: 12.2 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Collections;
4using System.Linq;
5using System.Text;
6
7namespace BCMToolbox
8{
9    public interface INetwork : IEnumerable
10    {
11        int Count
12        {
13            get;
14        }
15
16        object SyncRoot
17        {
18            get;
19        }
20
21        double this[int indexer]
22        {
23            get;
24        }
25
26        double this[int from, int to]
27        {
28            get;
29        }
30
31        string Identification
32        {
33            get;
34        }
35
36        int UID
37        {
38            get;
39        }
40
41        int Ticks
42        {
43            get;
44        }
45
46        void Initialize(Func<int, int, double> initializerWeights, Func<int, double> initializerPotentials);
47        void Initialize(double hiBoundaryWeights, double hiBoundaryPotentials);
48        void ApplyInputs(double[] inputs);
49        void Propagate();
50
51        event EventHandler WeightsChanged;
52        event EventHandler PotentialsChanged;
53        event EventHandler TickProgress;
54    }
55
56    public interface INetworkMonitor
57    {
58        Func<INetwork, double[]> KernelFunction
59        {
60            get;
61        }
62
63        void RegisterNorm(double[] newNorm);
64    }
65
66
67    [Serializable]
68    public class NetworkState : INetwork
69    {
70        double[,] __weights = null;
71        double[] __outputs = null;
72        object __syncRoot = new object();
73        int __uid = 0;
74        int __ticks = 0;
75        string __ident = null;
76
77        public NetworkState(INetwork copyFrom)
78        {
79            __weights = new double[copyFrom.Count, copyFrom.Count];
80            __outputs = new double[copyFrom.Count];
81            __uid = copyFrom.UID;
82            __ident = copyFrom.Identification;
83            lock (copyFrom.SyncRoot)
84            {
85                __ticks = copyFrom.Ticks;
86                for (int i = 0; i < copyFrom.Count; i++)
87                {
88                    __outputs[i] = copyFrom[i];
89                    for (int j = 0; j < copyFrom.Count; j++)
90                        __weights[i, j] = copyFrom[i, j];
91                }
92            }
93
94
95        }
96
97        #region INetwork Members
98
99        public int Count
100        {
101            get
102            {
103                return __outputs.Length;
104            }
105        }
106
107        public object SyncRoot
108        {
109            get
110            {
111                return __syncRoot;
112            }
113        }
114
115        public double this[int indexer]
116        {
117            get
118            {
119                return __outputs[indexer];
120            }
121        }
122
123        public double this[int from, int to]
124        {
125            get
126            {
127                return __weights[from, to];
128            }
129        }
130
131        public string Identification
132        {
133            get
134            {
135                return __ident;
136            }
137        }
138
139        public int UID
140        {
141            get
142            {
143                return __uid;
144            }
145        }
146
147        public int Ticks
148        {
149            get
150            {
151                return __ticks;
152            }
153        }
154
155        public void Initialize(Func<int, int, double> initializerWeights, Func<int, double> initializerPotentials)
156        {
157            throw new NotSupportedException("This is but a lifeless clone.");
158        }
159
160        public void Initialize(double hiBoundaryWeights, double hiBoundaryPotentials)
161        {
162            throw new NotSupportedException("This is but a lifeless clone.");
163        }
164
165        public void ApplyInputs(double[] inputs)
166        {
167            throw new NotSupportedException("This is but a lifeless clone.");
168        }
169
170        public void Propagate()
171        {
172            throw new NotSupportedException("This is but a lifeless clone.");
173        }
174
175        public event EventHandler WeightsChanged;
176
177        public event EventHandler PotentialsChanged;
178
179        public event EventHandler TickProgress;
180
181       
182        #endregion
183
184        #region IEnumerable Members
185
186        public IEnumerator GetEnumerator()
187        {
188            return __outputs.GetEnumerator();
189        }
190
191        #endregion
192    }
193
194    public abstract class TraceableNetwork : INetwork
195    {
196        object __syncRoot = new object();
197        int __uid = 0;
198        int __ticks = 0;
199
200        protected TraceableNetwork(int newUID)
201        {
202            __uid = newUID;
203        }
204
205        public override string ToString()
206        {
207            StringBuilder toRet = new StringBuilder();
208            for (int i = 0; i < this.Count; i++)
209            {
210                toRet.AppendLine("");
211                toRet.AppendFormat("{0}   ->    ", this[i].ToString("#0.0000"));
212                for (int j = 0; j < this.Count; j++)
213                    toRet.AppendFormat("{0} ", this[i, j].ToString("#0.0000"));
214            }
215            toRet.AppendLine("");
216
217            return toRet.ToString();
218        }
219
220
221        #region Network State Duplicates
222        NetworkState __lastState = null;
223        public NetworkState BuildNetworkState()
224        {
225            __lastState = new NetworkState(this);
226            if (this.NetworkStateRefreshed != null)
227                this.NetworkStateRefreshed(this, EventArgs.Empty);
228            return __lastState;
229        }
230        public NetworkState GetLastNetworkState()
231        {
232            return __lastState;
233        }
234
235        public event EventHandler NetworkStateRefreshed;
236        #endregion
237
238        #region INetwork Members
239
240        public abstract int Count
241        {
242            get;
243        }
244
245        public object SyncRoot
246        {
247            get
248            {
249                return __syncRoot;
250            }
251        }
252
253        public abstract double this[int indexer]
254        {
255            get;
256            internal set;
257        }
258
259        public abstract double this[int from, int to]
260        {
261            get;
262            internal set;
263        }
264
265        public abstract string Identification
266        {
267            get;
268        }
269
270        public int UID
271        {
272            get
273            {
274                return __uid;
275            }
276        }
277
278        public int Ticks
279        {
280            get
281            {
282                return __ticks;
283            }
284        }
285
286        public virtual void Initialize(Func<int, int, double> initializerWeights, Func<int, double> initializerPotentials)
287        {
288            lock (SyncRoot)
289            {
290                for (int i = 0; i < this.Count; i++)
291                    for (int j = 0; j < Count; j++)
292                        if (!double.IsNaN(this[i, j]))
293                            this[i, j] = initializerWeights(i, j);
294                for (int i = 0; i < this.Count; i++)
295                    this[i] = initializerPotentials(i);
296            }
297
298            __ticks = 0;
299            if (this.PotentialsChanged != null)
300                this.PotentialsChanged(this, EventArgs.Empty);
301            if (this.WeightsChanged != null)
302                this.WeightsChanged(this, EventArgs.Empty);
303        }
304
305                 
306        // Initializes using a uniform random number distribution with two boundaries
307        public void Initialize(double hiBoundaryWeights, double hiBoundaryPotentials)
308        {
309            Random r = new Random();
310            Initialize(
311               //   (i, j) => (r.NextDouble() * 2 * hiBoundaryWeights - hiBoundaryWeights), i => (r.NextDouble() * hiBoundaryPotentials));
312              (i, j) => (r.NextDouble() * hiBoundaryWeights), i => (r.NextDouble() * hiBoundaryPotentials));
313        }
314       
315
316        public virtual void ApplyInputs(double[] inputs)
317        {
318            this.OnPotentialsChanged();
319        }
320
321
322        public virtual void Propagate()
323        {
324            __ticks++;
325            this.OnPotentialsChanged();
326            this.OnWeightsChanged();
327            this.OnTickProgress();
328           
329
330            UpdateMonitors();
331        }
332
333        protected void OnPotentialsChanged()
334        {
335            if (this.PotentialsChanged != null)
336                this.PotentialsChanged(this, EventArgs.Empty);
337        }
338
339        protected void OnWeightsChanged()
340        {
341            if (this.WeightsChanged != null)
342                this.WeightsChanged(this, EventArgs.Empty);
343        }
344
345        protected void OnTickProgress()
346        {
347            if (this.TickProgress != null)
348                this.TickProgress(this, EventArgs.Empty);
349        }
350
351        public event EventHandler WeightsChanged;
352
353        public event EventHandler PotentialsChanged;
354
355        public event EventHandler TickProgress;
356
357        #endregion
358
359        #region IEnumerable Members
360
361        public abstract IEnumerator GetEnumerator();
362     
363
364        #endregion
365
366        #region Graphing With Kernels
367        List<INetworkMonitor> __monitors = new List<INetworkMonitor>();
368
369        public void RegisterMonitor(INetworkMonitor newMonitor)
370        {
371            lock (__syncRoot)
372                __monitors.Add(newMonitor);
373        }
374
375        void UpdateMonitors()
376        {
377            foreach (INetworkMonitor inm in __monitors)
378            {
379                double[] norm = inm.KernelFunction(this);
380                inm.RegisterNorm(norm);
381            }
382        }
383
384
385        #endregion
386
387        #region Utilities
388        public void NormalizeWeights(double degree)
389        {
390            double sum = 0.0;
391
392            if (degree != 2.0)
393            {
394                for (int i = 0; i < Count; i++)
395                    for (int j = 0; j < Count; j++)
396                        if ((i != j) && !double.IsNaN(this[i, j]))
397                            sum += Math.Pow(this[i, j], degree);
398
399                sum = Math.Pow(sum, 1 / degree);
400            }
401            else
402            {
403                for (int i = 0; i < Count; i++)
404                    for (int j = 0; j < Count; j++)
405                        if ((i != j) && !double.IsNaN(this[i, j]))
406                            sum += this[i,j]*this[i,j];
407
408                sum = Math.Sqrt(sum);
409            }
410
411            for (int i = 0; i < Count; i++)
412                for (int j = 0; j < Count; j++)
413                    if ((i != j) && !double.IsNaN(this[i, j]))
414                        this[i, j] = this[i, j] / sum;
415
416        }
417
418        public void NormalizePotentials(double degree)
419        {
420            double sum = 0.0;
421
422            if (degree != 2.0)
423            {
424                for (int i = 0; i < Count; i++)
425                    sum += Math.Pow(this[i], degree);
426
427                sum = Math.Pow(sum, 1 / degree);
428            }
429            else
430            {
431                for (int i = 0; i < Count; i++)
432                    sum += this[i] * this[i];
433
434                sum = Math.Sqrt(sum);
435            }
436
437            for (int i = 0; i < Count; i++)
438                this[i] = this[i] / sum;
439        }
440        #endregion
441    }
442
443    public abstract class FixedTraceableNetwork : TraceableNetwork
444    {
445        protected double[,] __weights = null;
446        protected double[] __potentials = null;
447        protected FixedTraceableNetwork(int neuronCount, int newUID)
448            : base(newUID)
449        {
450            __weights = new double[neuronCount, neuronCount];
451            __potentials = new double[neuronCount];
452        }
453
454        public override int Count
455        {
456            get
457            {
458                return __potentials.Length;
459            }
460        }
461
462        public override double this[int indexer]
463        {
464            get
465            {
466                return __potentials[indexer];
467            }
468            internal set
469            {
470                __potentials[indexer] = value;
471            }
472        }
473
474        public override double this[int from, int to]
475        {
476            get
477            {
478                return __weights[from, to];
479            }
480            internal set
481            {
482                __weights[from, to] = value;
483            }
484        }
485
486        public override System.Collections.IEnumerator GetEnumerator()
487        {
488            return __potentials.GetEnumerator();
489        }
490    }
491}
Note: See TracBrowser for help on using the repository browser.