|
| 1 | +// |
| 2 | +// Copyright (c) [Name]. All rights reserved. |
| 3 | +// Licensed under the MIT License. See LICENSE file in the project root for full license information. |
| 4 | +// |
| 5 | + |
| 6 | +using System; |
| 7 | +using System.Collections.Generic; |
| 8 | +using System.Linq; |
| 9 | +using System.Text; |
| 10 | +using System.Threading.Tasks; |
| 11 | +using System.Windows; |
| 12 | +using System.Windows.Controls; |
| 13 | +using System.Windows.Media; |
| 14 | +using System.Xml.Serialization; |
| 15 | + |
| 16 | +namespace BrainSimulator.Modules |
| 17 | +{ |
| 18 | + public class ModuleCorticalColumn : ModuleBase |
| 19 | + { |
| 20 | + //any public variable you create here will automatically be saved and restored with the network |
| 21 | + //unless you precede it with the [XmlIgnore] directive |
| 22 | + //[XlmIgnore] |
| 23 | + //public theStatus = 1; |
| 24 | + |
| 25 | + |
| 26 | + //set size parameters as needed in the constructor |
| 27 | + //set max to be -1 if unlimited |
| 28 | + public ModuleCorticalColumn() |
| 29 | + { |
| 30 | + minHeight = 15; |
| 31 | + maxHeight = 20; |
| 32 | + minWidth = 1; |
| 33 | + maxWidth = 1; |
| 34 | + } |
| 35 | + |
| 36 | + |
| 37 | + //fill this method in with code which will execute |
| 38 | + //once for each cycle of the engine |
| 39 | + public override void Fire() |
| 40 | + { |
| 41 | + Init(); //be sure to leave this here |
| 42 | + |
| 43 | + //if you want the dlg to update, use the following code whenever any parameter changes |
| 44 | + // UpdateDialog(); |
| 45 | + } |
| 46 | + |
| 47 | + //fill this method in with code which will execute once |
| 48 | + //when the module is added, when "initialize" is selected from the context menu, |
| 49 | + //or when the engine restart button is pressed |
| 50 | + public override void Initialize() |
| 51 | + { |
| 52 | + AddSynapses(); |
| 53 | + } |
| 54 | + |
| 55 | + |
| 56 | + private void AddSynapses() |
| 57 | + { |
| 58 | + //rows with hard-coded functionality |
| 59 | + int inputRow = 6; |
| 60 | + int outputRow = 8; |
| 61 | + int thisCol = 9; |
| 62 | + int recur = 10; |
| 63 | + int isa = 11; |
| 64 | + int has_inst = 12; |
| 65 | + int has_a = 13; |
| 66 | + int part_of = 14; |
| 67 | + |
| 68 | + Init(); |
| 69 | + //ClearNeurons(false); |
| 70 | + |
| 71 | + mv.Color = Utils.ColorToInt(Colors.DarkGray); |
| 72 | + int colNum = 0; |
| 73 | + foreach (var module in theNeuronArray.modules) |
| 74 | + { |
| 75 | + if (module.Label=="CorticalColumn" && module.FirstNeuron < mv.FirstNeuron) |
| 76 | + colNum++; |
| 77 | + } |
| 78 | + |
| 79 | + mv.GetNeuronAt(0, 0).Label = "Col" + colNum; |
| 80 | + Neuron n = theNeuronArray.GetNeuron("Request"); |
| 81 | + if (n != null) |
| 82 | + n.AddSynapse(mv.GetNeuronAt(0, 0).id, 1); |
| 83 | + |
| 84 | + mv.GetNeuronAt(0, 0).AddSynapse(mv.GetNeuronAt(0, 1).id, .2f, Synapse.modelType.Hebbian3); |
| 85 | + mv.GetNeuronAt(0, 0).AddSynapse(mv.GetNeuronAt(0, 2).id, 1); |
| 86 | + mv.GetNeuronAt(0, 1).Model = Neuron.modelType.LIF; |
| 87 | + mv.GetNeuronAt(0, 1).LeakRate = 0.3f; |
| 88 | + mv.GetNeuronAt(0, 1).AddSynapse(mv.GetNeuronAt(0, 2).id, -1); |
| 89 | + mv.GetNeuronAt(0, 1).AddSynapse(mv.GetNeuronAt(0, 3).id, -1); |
| 90 | + mv.GetNeuronAt(0, 2).AddSynapse(mv.GetNeuronAt(0, 3).id, 1); |
| 91 | + mv.GetNeuronAt(0, 3).AddSynapse(mv.GetNeuronAt(0, 4).id, 1); |
| 92 | + |
| 93 | + foreach (var module in theNeuronArray.modules) |
| 94 | + { |
| 95 | + if (module.ModuleTypeStr.Contains("CorticalColumn") && module.FirstNeuron > mv.FirstNeuron) |
| 96 | + { |
| 97 | + mv.GetNeuronAt(0, 3).AddSynapse(module.FirstNeuron + 4, -1); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + mv.GetNeuronAt(0, 4).Label = "Act" + colNum; |
| 102 | + mv.GetNeuronAt(0, 4).Model = Neuron.modelType.Burst; |
| 103 | + mv.GetNeuronAt(0, 4).AxonDelay = 7; |
| 104 | + mv.GetNeuronAt(0, 4).LeakRate = 4; |
| 105 | + |
| 106 | + mv.GetNeuronAt(0, 4).AddSynapse(mv.GetNeuronAt(0, 0).id, 1); |
| 107 | + mv.GetNeuronAt(0, 4).AddSynapse(mv.GetNeuronAt(0, 1).id, 1); |
| 108 | + mv.GetNeuronAt(0, 4).AddSynapse(mv.GetNeuronAt(0, 6).id, 1, Synapse.modelType.Learn); |
| 109 | + mv.GetNeuronAt(0, 4).AddSynapse(mv.GetNeuronAt(0, 7).id, 1, Synapse.modelType.Learn); |
| 110 | + |
| 111 | + mv.GetNeuronAt(5).Label = "De" + colNum; |
| 112 | + mv.GetNeuronAt(5).Model = Neuron.modelType.Burst; |
| 113 | + mv.GetNeuronAt(5).AxonDelay = 5; |
| 114 | + mv.GetNeuronAt(5).LeakRate = 4; |
| 115 | + mv.GetNeuronAt(5).AddSynapse(mv.GetNeuronAt(1).id, -1, Synapse.modelType.Learn); |
| 116 | + mv.GetNeuronAt(5).AddSynapse(mv.GetNeuronAt(6).id, -1, Synapse.modelType.Learn); |
| 117 | + mv.GetNeuronAt(5).AddSynapse(mv.GetNeuronAt(7).id, -1, Synapse.modelType.Learn); |
| 118 | + mv.GetNeuronAt(5).AddSynapse(mv.GetNeuronAt(8).id, -1, Synapse.modelType.Learn); |
| 119 | + |
| 120 | + mv.GetNeuronAt(6).Label = "In" + colNum; |
| 121 | + mv.GetNeuronAt(6).Model = Neuron.modelType.LIF; |
| 122 | + mv.GetNeuronAt(6).LeakRate = .3f; |
| 123 | + n = theNeuronArray.GetNeuron("in-fired"); |
| 124 | + if (n != null) |
| 125 | + mv.GetNeuronAt(6).AddSynapse(n.id, 1); |
| 126 | + for (int i = thisCol; i < part_of; i++) |
| 127 | + if (i != recur) |
| 128 | + mv.GetNeuronAt(inputRow).AddSynapse(mv.GetNeuronAt(i).id, 1); |
| 129 | + mv.GetNeuronAt(7).AddSynapse(mv.GetNeuronAt(recur).id, 1); |
| 130 | + mv.GetNeuronAt(8).AddSynapse(mv.GetNeuronAt(recur).id, 1); |
| 131 | + |
| 132 | + mv.GetNeuronAt(7).Label = "In" + colNum+"*"; |
| 133 | + mv.GetNeuronAt(7).Model = Neuron.modelType.LIF; |
| 134 | + mv.GetNeuronAt(7).LeakRate = .3f; |
| 135 | + mv.GetNeuronAt(7).AddSynapse(mv.GetNeuronAt(8).id, 1, Synapse.modelType.Learn); |
| 136 | + |
| 137 | + mv.GetNeuronAt(8).Label = "Out" + colNum; |
| 138 | + mv.GetNeuronAt(8).Model = Neuron.modelType.LIF; |
| 139 | + mv.GetNeuronAt(8).LeakRate = .3f; |
| 140 | + n = theNeuronArray.GetNeuron("out-fired"); |
| 141 | + if (n != null) |
| 142 | + mv.GetNeuronAt(8).AddSynapse(n.id, 1); |
| 143 | + |
| 144 | + //find all neurons in theNeuronArray with labels starting with " |
| 145 | + string labelPrefix = "\""; // Replace with desired prefix |
| 146 | + |
| 147 | + for (int i = 0; i < theNeuronArray.arraySize; i++) |
| 148 | + { |
| 149 | + Neuron neuron = theNeuronArray.GetNeuron(i); |
| 150 | + if (neuron != null && neuron.Label != null && neuron.Label.StartsWith(labelPrefix)) |
| 151 | + { |
| 152 | + neuron.AddSynapse(mv.GetNeuronAt(6).id, .2f, Synapse.modelType.Hebbian3); |
| 153 | + Neuron neuron2 = theNeuronArray.GetNeuron(i + 2*theNeuronArray.rows); |
| 154 | + neuron2.AddSynapse(mv.GetNeuronAt(7).id, .2f, Synapse.modelType.Hebbian3); |
| 155 | + Neuron neuron3 = theNeuronArray.GetNeuron(i + 4 * theNeuronArray.rows); |
| 156 | + mv.GetNeuronAt(8).AddSynapse(neuron3.id, .2f, Synapse.modelType.Hebbian3); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + |
| 161 | + //this relationship...transfer input to output |
| 162 | + Neuron nThis = theNeuronArray.GetNeuron("this"); |
| 163 | + if (nThis == null) return; |
| 164 | + nThis.AddSynapse(mv.GetNeuronAt(thisCol).id, 1, Synapse.modelType.Gate); |
| 165 | + mv.GetNeuronAt(thisCol).AddSynapse(mv.GetNeuronAt(8).id, 1); |
| 166 | + |
| 167 | + //recur relationship...transfer output to input |
| 168 | + Neuron nRecur = theNeuronArray.GetNeuron("recur"); |
| 169 | + if (nRecur == null) return; |
| 170 | + nRecur.AddSynapse(mv.GetNeuronAt(recur).id, 1, Synapse.modelType.Gate); |
| 171 | + mv.GetNeuronAt(recur).AddSynapse(mv.GetNeuronAt(6).id, 1); |
| 172 | + |
| 173 | + //Isa Relationships |
| 174 | + bool flowControl = AddFixedRelationshipRow(outputRow, has_inst, "has-inst"); |
| 175 | + if (!flowControl) |
| 176 | + return; |
| 177 | + flowControl = AddFixedRelationshipRow(outputRow, has_a, "has-a"); |
| 178 | + if (!flowControl) |
| 179 | + return; |
| 180 | + flowControl = AddFixedRelationshipRow(outputRow, isa, "is-a"); |
| 181 | + if (!flowControl) |
| 182 | + return; |
| 183 | + |
| 184 | + //handle part-of relationships |
| 185 | + Neuron nSource = theNeuronArray.GetNeuron("part-of"); |
| 186 | + if (nSource is null) return; |
| 187 | + nSource.AddSynapse(mv.GetNeuronAt(part_of).id, 1, Synapse.modelType.Gate); |
| 188 | + mv.GetNeuronAt(part_of).AddSynapse(mv.GetNeuronAt(outputRow).id, 1); |
| 189 | + |
| 190 | + foreach (var module in theNeuronArray.modules) |
| 191 | + { |
| 192 | + if (module.Label== "CorticalColumn" && module.FirstNeuron != mv.FirstNeuron) |
| 193 | + { |
| 194 | + Neuron nTest = module.GetNeuronAt(inputRow); |
| 195 | + nTest.AddSynapse(mv.GetNeuronAt(part_of).id,.2f, Synapse.modelType.Hebbian2); |
| 196 | + } |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + private bool AddFixedRelationshipRow(int thisOut, int row, string sourceLabel) |
| 201 | + { |
| 202 | + Neuron nSource = theNeuronArray.GetNeuron(sourceLabel); |
| 203 | + if (nSource is null) return false; |
| 204 | + nSource.AddSynapse(mv.GetNeuronAt(row).id, 1, Synapse.modelType.Gate); |
| 205 | + |
| 206 | + foreach (var module in theNeuronArray.modules) |
| 207 | + { |
| 208 | + if (module.Label =="CorticalColumn" && module.FirstNeuron != mv.FirstNeuron) |
| 209 | + { |
| 210 | + mv.GetNeuronAt(row).AddSynapse(module.GetNeuronAt(thisOut).id, .2f, Synapse.modelType.Hebbian3); |
| 211 | + } |
| 212 | + } |
| 213 | + |
| 214 | + return true; |
| 215 | + } |
| 216 | + |
| 217 | + |
| 218 | + |
| 219 | + //the following can be used to massage public data to be different in the xml file |
| 220 | + //delete if not needed |
| 221 | + public override void SetUpBeforeSave() |
| 222 | + { |
| 223 | + } |
| 224 | + public override void SetUpAfterLoad() |
| 225 | + { |
| 226 | + } |
| 227 | + |
| 228 | + public override MenuItem CustomContextMenuItems() |
| 229 | + { |
| 230 | + Button clearButton = new Button { Content = "Clear Neurons", }; |
| 231 | + clearButton.Click += ClrButton_Click; |
| 232 | + MenuItem mi = new MenuItem { Header = clearButton }; |
| 233 | + return mi; |
| 234 | + } |
| 235 | + |
| 236 | + private void ClrButton_Click(object sender, RoutedEventArgs e) |
| 237 | + { |
| 238 | + foreach (Neuron n in mv.Neurons) |
| 239 | + { |
| 240 | + foreach (Synapse s in n.synapses) |
| 241 | + n.DeleteSynapse(s.targetNeuron); |
| 242 | + } |
| 243 | + MainWindow.arrayView.Update(); |
| 244 | + } |
| 245 | + |
| 246 | + |
| 247 | + //called whenever the size of the module rectangle changes |
| 248 | + //for example, you may choose to reinitialize whenever size changes |
| 249 | + //delete if not needed |
| 250 | + public override void SizeChanged() |
| 251 | + { |
| 252 | + if (mv == null) return; |
| 253 | + AddSynapses(); |
| 254 | + } |
| 255 | + } |
| 256 | +} |
0 commit comments