[171] | 1 | using System; |
---|
| 2 | using System.Collections.Generic; |
---|
| 3 | using System.ComponentModel; |
---|
| 4 | using System.Data; |
---|
| 5 | using System.Drawing; |
---|
| 6 | using System.Linq; |
---|
| 7 | using System.Text; |
---|
| 8 | using System.Windows.Forms; |
---|
| 9 | using System.Diagnostics; |
---|
| 10 | using System.Threading; |
---|
| 11 | |
---|
| 12 | using BCMToolbox; |
---|
| 13 | |
---|
| 14 | |
---|
| 15 | namespace VisualBCM |
---|
| 16 | { |
---|
| 17 | public partial class MainForm : Form |
---|
| 18 | { |
---|
| 19 | TraceableNetwork net = null; |
---|
| 20 | TimeSpan __lastElapsed = TimeSpan.Zero; |
---|
| 21 | List<GraphForm> __graphs = new List<GraphForm>(); |
---|
| 22 | |
---|
| 23 | public MainForm() |
---|
| 24 | { |
---|
| 25 | InitializeComponent(); |
---|
| 26 | |
---|
| 27 | this.weightsVisualiser1.AutoRefresh = false; |
---|
| 28 | this.weightsVisualiser1.SetManualScale(0, 1.00); |
---|
| 29 | ///this.weightsVisualiser1.SetAutoScale(); |
---|
| 30 | |
---|
| 31 | /* |
---|
| 32 | for (int i = 0; i < 20; i++) |
---|
| 33 | System.Diagnostics.Debug.WriteLine(BCMToolbox.MathToolbox.GetRandomGaussian()); */ |
---|
| 34 | |
---|
| 35 | /* |
---|
| 36 | double tot = 0; |
---|
| 37 | for (int i = 0; i < 20; i++) |
---|
| 38 | { |
---|
| 39 | double e = Math.Exp(-i / 10.0); |
---|
| 40 | tot += e; |
---|
| 41 | System.Diagnostics.Debug.Write(String.Format("{0}, ", e)); |
---|
| 42 | } |
---|
| 43 | System.Diagnostics.Debug.WriteLine(""); |
---|
| 44 | System.Diagnostics.Debug.WriteLine(tot); */ |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | private void MainForm_Load(object sender, EventArgs e) |
---|
| 48 | { |
---|
| 49 | Random r = new Random(); |
---|
| 50 | |
---|
| 51 | //net = BCMFactory.BuildSyncSigmoidActivationNetwork(100); |
---|
| 52 | |
---|
| 53 | //net = BCMFactory.BuildSyncSigmoidActivationNetworkSparse(100, (i, j) => 5.0/Math.Pow(Math.Abs(i - j),2) >= r.NextDouble()); |
---|
| 54 | //net = BCMFactory.BuildOja(100); |
---|
| 55 | net = BCMFactory.BuildModel01(100); |
---|
| 56 | |
---|
| 57 | BCMFactory.Reduce(net, (i, j) => 5.0 / Math.Pow(Math.Abs(i - j), 2) >= r.NextDouble()); |
---|
| 58 | |
---|
| 59 | net.Initialize(0.05,0.3); |
---|
| 60 | /*double[] de = new double[100]; |
---|
| 61 | for (int i = 0; i < 10; i++) |
---|
| 62 | de[i] = 1; |
---|
| 63 | net.ApplyInputs(de);*/ |
---|
| 64 | |
---|
| 65 | this.weightsVisualiser1.BoundNetwork = net; |
---|
| 66 | |
---|
| 67 | RefreshVisualizer(); |
---|
| 68 | |
---|
| 69 | //GraphForm.PlotFunction(x => { double e1 = Math.Exp(-x); return e1 / ((1.0 + e1) * (1.0 + e1)); }, -5, 5); |
---|
| 70 | //GraphForm.PlotFunction(x => (1 / (1 + Math.Exp(-x))), -5, 5); |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | |
---|
| 74 | void propagateBcknd() |
---|
| 75 | { |
---|
| 76 | Stopwatch st1 = new Stopwatch(); |
---|
| 77 | while (true) |
---|
| 78 | { |
---|
| 79 | st1.Reset(); |
---|
| 80 | st1.Start(); |
---|
| 81 | net.Propagate(); |
---|
| 82 | st1.Stop(); |
---|
| 83 | __lastElapsed = st1.Elapsed; |
---|
| 84 | } |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | |
---|
| 88 | public void RefreshVisualizer() |
---|
| 89 | { |
---|
| 90 | weightsVisualiser1.RefreshNetwork(); |
---|
| 91 | if (weightsVisualiser1.LastNetworkState != null) |
---|
| 92 | { |
---|
| 93 | BCMToolbox.Statistics.BasicStats stats = new BCMToolbox.Statistics.BasicStats(weightsVisualiser1.LastNetworkState); |
---|
| 94 | this.label1.Text = stats.ToString(); |
---|
| 95 | } |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | private void testToolStripMenuItem_Click(object sender, EventArgs e) |
---|
| 99 | { |
---|
| 100 | if (net != null) |
---|
| 101 | { |
---|
| 102 | BCMToolbox.Statistics.StaticBoundessTest sbt = new BCMToolbox.Statistics.StaticBoundessTest(net); |
---|
| 103 | this.label1.Text = sbt.ToString(); |
---|
| 104 | |
---|
| 105 | net.Initialize(1.0, 1.0); |
---|
| 106 | } |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | private void simpleStatsToolStripMenuItem_Click(object sender, EventArgs e) |
---|
| 110 | { |
---|
| 111 | GraphForm g1 = new GraphForm( |
---|
| 112 | net, |
---|
| 113 | n1 => { |
---|
| 114 | BCMToolbox.Statistics.BasicStats bs = new BCMToolbox.Statistics.BasicStats(n1); |
---|
| 115 | return new double[] { bs.AveragePotential, bs.AverageWeight }; |
---|
| 116 | }, |
---|
| 117 | "ave out | ave wt"); |
---|
| 118 | __graphs.Add(g1); |
---|
| 119 | g1.Show(); |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | private void midNeuronToolStripMenuItem_Click(object sender, EventArgs e) |
---|
| 123 | { |
---|
| 124 | GraphForm g1 = new GraphForm( |
---|
| 125 | net, |
---|
| 126 | n1 => |
---|
| 127 | { |
---|
| 128 | int idx = net.Count / 2; |
---|
| 129 | double ave = 0.0; |
---|
| 130 | double ave2 = 0.0; |
---|
| 131 | for (int i = 0; i < net.Count; i++) |
---|
| 132 | { |
---|
| 133 | ave += net[i, idx]; |
---|
| 134 | ave2 += net[idx, i]; |
---|
| 135 | } |
---|
| 136 | ave /= net.Count; |
---|
| 137 | ave2 /= net.Count; |
---|
| 138 | return new double[] { net[idx], ave, ave2 }; |
---|
| 139 | }, |
---|
| 140 | "mid out | mid ave wt in | mid ave wt out"); |
---|
| 141 | __graphs.Add(g1); |
---|
| 142 | g1.Show(); |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | private void constantToolStripMenuItem_Click(object sender, EventArgs e) |
---|
| 146 | { |
---|
| 147 | if (net == null) |
---|
| 148 | return; |
---|
| 149 | BCMToolbox.Stimuli.SimpleExcitator se = new BCMToolbox.Stimuli.SimpleExcitator(); |
---|
| 150 | StimulusForm sf = new StimulusForm(se); |
---|
| 151 | sf.ApplyStimulus += new EventHandler(sf_ApplyStimulus); |
---|
| 152 | sf.Show(); |
---|
| 153 | sf.BringToFront(); |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | private void consistentToolStripMenuItem_Click(object sender, EventArgs e) |
---|
| 157 | { |
---|
| 158 | if (net == null) |
---|
| 159 | return; |
---|
| 160 | BCMToolbox.Stimuli.ConsistentExcitator ce = new BCMToolbox.Stimuli.ConsistentExcitator(); |
---|
| 161 | StimulusForm sf = new StimulusForm(ce); |
---|
| 162 | sf.ApplyStimulus += new EventHandler(sf_ApplyStimulus); |
---|
| 163 | sf.Show(); |
---|
| 164 | sf.BringToFront(); |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | void sf_ApplyStimulus(object sender, EventArgs e) |
---|
| 168 | { |
---|
| 169 | StimulusForm sf = sender as StimulusForm; |
---|
| 170 | sf.Stimulator.BindToNetwork(net); |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | private void MainForm_FormClosing(object sender, FormClosingEventArgs e) |
---|
| 174 | { |
---|
| 175 | System.Diagnostics.Process.GetCurrentProcess().Kill(); |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | void RefreshAll() |
---|
| 179 | { |
---|
| 180 | RefreshVisualizer(); |
---|
| 181 | this.label2.Text = String.Format("tick: {1} | inst tps: {0}", (1.0 / __lastElapsed.TotalSeconds).ToString("#0.00"), net.Ticks); |
---|
| 182 | foreach (GraphForm gf in __graphs) |
---|
| 183 | gf.RefreshGraph(); |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | private void indefinitelyToolStripMenuItem_Click(object sender, EventArgs e) |
---|
| 187 | { |
---|
| 188 | propagateToolStripMenuItem.Enabled = false; |
---|
| 189 | testToolStripMenuItem.Enabled = false; |
---|
| 190 | |
---|
| 191 | Thread th1 = new Thread(new ThreadStart(this.propagateBcknd)); |
---|
| 192 | th1.IsBackground = true; |
---|
| 193 | th1.Start(); |
---|
| 194 | th1.Priority = ThreadPriority.Highest; |
---|
| 195 | |
---|
| 196 | while (true) |
---|
| 197 | { |
---|
| 198 | RefreshAll(); |
---|
| 199 | Application.DoEvents(); |
---|
| 200 | } |
---|
| 201 | } |
---|
| 202 | |
---|
| 203 | private void toolStripMenuItem2_Click(object sender, EventArgs e) |
---|
| 204 | { |
---|
| 205 | Stopwatch st1 = new Stopwatch(); |
---|
| 206 | for (int i = 0; i < 25; i++) |
---|
| 207 | { |
---|
| 208 | st1.Reset(); |
---|
| 209 | st1.Start(); |
---|
| 210 | net.Propagate(); |
---|
| 211 | st1.Stop(); |
---|
| 212 | __lastElapsed = st1.Elapsed; |
---|
| 213 | } |
---|
| 214 | RefreshAll(); |
---|
| 215 | } |
---|
| 216 | |
---|
| 217 | private void ticksToolStripMenuItem_Click(object sender, EventArgs e) |
---|
| 218 | { |
---|
| 219 | Stopwatch st1 = new Stopwatch(); |
---|
| 220 | for (int i = 0; i < 250; i++) |
---|
| 221 | { |
---|
| 222 | st1.Reset(); |
---|
| 223 | st1.Start(); |
---|
| 224 | net.Propagate(); |
---|
| 225 | st1.Stop(); |
---|
| 226 | __lastElapsed = st1.Elapsed; |
---|
| 227 | } |
---|
| 228 | RefreshAll(); |
---|
| 229 | } |
---|
| 230 | |
---|
| 231 | |
---|
| 232 | |
---|
| 233 | |
---|
| 234 | |
---|
| 235 | |
---|
| 236 | } |
---|
| 237 | } |
---|