using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Diagnostics; using System.Threading; using BCMToolbox; namespace VisualBCM { public partial class MainForm : Form { TraceableNetwork net = null; TimeSpan __lastElapsed = TimeSpan.Zero; List __graphs = new List(); public MainForm() { InitializeComponent(); this.weightsVisualiser1.AutoRefresh = false; this.weightsVisualiser1.SetManualScale(0, 1.00); ///this.weightsVisualiser1.SetAutoScale(); /* for (int i = 0; i < 20; i++) System.Diagnostics.Debug.WriteLine(BCMToolbox.MathToolbox.GetRandomGaussian()); */ /* double tot = 0; for (int i = 0; i < 20; i++) { double e = Math.Exp(-i / 10.0); tot += e; System.Diagnostics.Debug.Write(String.Format("{0}, ", e)); } System.Diagnostics.Debug.WriteLine(""); System.Diagnostics.Debug.WriteLine(tot); */ } private void MainForm_Load(object sender, EventArgs e) { Random r = new Random(); //net = BCMFactory.BuildSyncSigmoidActivationNetwork(100); //net = BCMFactory.BuildSyncSigmoidActivationNetworkSparse(100, (i, j) => 5.0/Math.Pow(Math.Abs(i - j),2) >= r.NextDouble()); //net = BCMFactory.BuildOja(100); net = BCMFactory.BuildModel01(100); BCMFactory.Reduce(net, (i, j) => 5.0 / Math.Pow(Math.Abs(i - j), 2) >= r.NextDouble()); net.Initialize(0.05,0.3); /*double[] de = new double[100]; for (int i = 0; i < 10; i++) de[i] = 1; net.ApplyInputs(de);*/ this.weightsVisualiser1.BoundNetwork = net; RefreshVisualizer(); //GraphForm.PlotFunction(x => { double e1 = Math.Exp(-x); return e1 / ((1.0 + e1) * (1.0 + e1)); }, -5, 5); //GraphForm.PlotFunction(x => (1 / (1 + Math.Exp(-x))), -5, 5); } void propagateBcknd() { Stopwatch st1 = new Stopwatch(); while (true) { st1.Reset(); st1.Start(); net.Propagate(); st1.Stop(); __lastElapsed = st1.Elapsed; } } public void RefreshVisualizer() { weightsVisualiser1.RefreshNetwork(); if (weightsVisualiser1.LastNetworkState != null) { BCMToolbox.Statistics.BasicStats stats = new BCMToolbox.Statistics.BasicStats(weightsVisualiser1.LastNetworkState); this.label1.Text = stats.ToString(); } } private void testToolStripMenuItem_Click(object sender, EventArgs e) { if (net != null) { BCMToolbox.Statistics.StaticBoundessTest sbt = new BCMToolbox.Statistics.StaticBoundessTest(net); this.label1.Text = sbt.ToString(); net.Initialize(1.0, 1.0); } } private void simpleStatsToolStripMenuItem_Click(object sender, EventArgs e) { GraphForm g1 = new GraphForm( net, n1 => { BCMToolbox.Statistics.BasicStats bs = new BCMToolbox.Statistics.BasicStats(n1); return new double[] { bs.AveragePotential, bs.AverageWeight }; }, "ave out | ave wt"); __graphs.Add(g1); g1.Show(); } private void midNeuronToolStripMenuItem_Click(object sender, EventArgs e) { GraphForm g1 = new GraphForm( net, n1 => { int idx = net.Count / 2; double ave = 0.0; double ave2 = 0.0; for (int i = 0; i < net.Count; i++) { ave += net[i, idx]; ave2 += net[idx, i]; } ave /= net.Count; ave2 /= net.Count; return new double[] { net[idx], ave, ave2 }; }, "mid out | mid ave wt in | mid ave wt out"); __graphs.Add(g1); g1.Show(); } private void constantToolStripMenuItem_Click(object sender, EventArgs e) { if (net == null) return; BCMToolbox.Stimuli.SimpleExcitator se = new BCMToolbox.Stimuli.SimpleExcitator(); StimulusForm sf = new StimulusForm(se); sf.ApplyStimulus += new EventHandler(sf_ApplyStimulus); sf.Show(); sf.BringToFront(); } private void consistentToolStripMenuItem_Click(object sender, EventArgs e) { if (net == null) return; BCMToolbox.Stimuli.ConsistentExcitator ce = new BCMToolbox.Stimuli.ConsistentExcitator(); StimulusForm sf = new StimulusForm(ce); sf.ApplyStimulus += new EventHandler(sf_ApplyStimulus); sf.Show(); sf.BringToFront(); } void sf_ApplyStimulus(object sender, EventArgs e) { StimulusForm sf = sender as StimulusForm; sf.Stimulator.BindToNetwork(net); } private void MainForm_FormClosing(object sender, FormClosingEventArgs e) { System.Diagnostics.Process.GetCurrentProcess().Kill(); } void RefreshAll() { RefreshVisualizer(); this.label2.Text = String.Format("tick: {1} | inst tps: {0}", (1.0 / __lastElapsed.TotalSeconds).ToString("#0.00"), net.Ticks); foreach (GraphForm gf in __graphs) gf.RefreshGraph(); } private void indefinitelyToolStripMenuItem_Click(object sender, EventArgs e) { propagateToolStripMenuItem.Enabled = false; testToolStripMenuItem.Enabled = false; Thread th1 = new Thread(new ThreadStart(this.propagateBcknd)); th1.IsBackground = true; th1.Start(); th1.Priority = ThreadPriority.Highest; while (true) { RefreshAll(); Application.DoEvents(); } } private void toolStripMenuItem2_Click(object sender, EventArgs e) { Stopwatch st1 = new Stopwatch(); for (int i = 0; i < 25; i++) { st1.Reset(); st1.Start(); net.Propagate(); st1.Stop(); __lastElapsed = st1.Elapsed; } RefreshAll(); } private void ticksToolStripMenuItem_Click(object sender, EventArgs e) { Stopwatch st1 = new Stopwatch(); for (int i = 0; i < 250; i++) { st1.Reset(); st1.Start(); net.Propagate(); st1.Stop(); __lastElapsed = st1.Elapsed; } RefreshAll(); } } }