private void UndoSynapse() { if (synapseUndoInfo.Count == 0) { return; } SynapseUndo s = synapseUndoInfo.Last(); synapseUndoInfo.RemoveAt(synapseUndoInfo.Count - 1); Neuron n = GetNeuron(s.source); if (s.newSynapse) //the synapse was added so delete it { n.DeleteSynapse(s.target); } else if (s.delSynapse) //the synapse was deleted so add it back { n.AddSynapse(s.target, s.weight, s.model); } else //weight/type changed { n.AddSynapse(s.target, s.weight, s.model); } n.Update(); }
public void MoveOneNeuron(Neuron n, Neuron nNewLocation) { //copy the neuron attributes and delete them from the old neuron. n.Copy(nNewLocation); MainWindow.theNeuronArray.SetCompleteNeuron(nNewLocation); //for all the synapses going out this neuron, change to going from new location //don't use a foreach here because the body of the loop may delete a list entry for (int k = 0; k < n.Synapses.Count; k++) { Synapse s = n.Synapses[k]; nNewLocation.AddSynapse(s.targetNeuron, s.weight, s.isHebbian); } //for all the synapses coming into this neuron, change the synapse target to new location for (int k = 0; k < n.SynapsesFrom.Count; k++) { Synapse reverseSynapse = n.SynapsesFrom[k]; //(from synapses are sort-of backward if (reverseSynapse.targetNeuron != -1) { Neuron sourceNeuron = MainWindow.theNeuronArray.GetNeuron(reverseSynapse.targetNeuron); sourceNeuron.DeleteSynapse(n.id); sourceNeuron.AddSynapse(nNewLocation.id, reverseSynapse.weight, reverseSynapse.isHebbian); } } n.Clear(); }
public void StepAndRepeat(int source, int target, float weight) { int distance = target - source; theSelection.EnumSelectedNeurons(); for (Neuron n = theSelection.GetSelectedNeuron(); n != null; n = theSelection.GetSelectedNeuron()) { n.AddSynapse(theSelection.selectedNeuronIndex + distance, weight, MainWindow.theNeuronArray, true); } Update(); }
private static void Mi_Click(object sender, RoutedEventArgs e) { MenuItem mi = sender as MenuItem; //find out which neuron this context menu is from ContextMenu cm = mi.Parent as ContextMenu; if (cm == null) { MenuItem mi2 = mi.Parent as MenuItem; cm = mi2.Parent as ContextMenu; } int i = (int)cm.GetValue(NeuronIDProperty); Neuron n = MainWindow.theNeuronArray.GetNeuron(i); if ((string)mi.Header == "Always Fire") { if (n.FindSynapse(i) == null) { n.AddSynapse(i, 1, MainWindow.theNeuronArray, true); } else { n.DeleteSynapse(i); } } if ((string)mi.Header == "Paste Here") { theNeuronArrayView.targetNeuronIndex = i; theNeuronArrayView.PasteNeurons(); theNeuronArrayView.targetNeuronIndex = -1; cmCancelled = true; } if ((string)mi.Header == "Move Here") { theNeuronArrayView.targetNeuronIndex = i; theNeuronArrayView.MoveNeurons(); cmCancelled = true; } if ((string)mi.Header == "Connect to Here") { theNeuronArrayView.targetNeuronIndex = i; theNeuronArrayView.ConnectToHere(); } if ((string)mi.Header == "Connect from Here") { theNeuronArrayView.targetNeuronIndex = i; theNeuronArrayView.ConnectFromHere(); } if ((string)mi.Header == "Select as Target") { theNeuronArrayView.targetNeuronIndex = i; } }
public void ConnectToHere() { if (targetNeuronIndex == -1) { return; } List <int> neuronsInSelection = theSelection.EnumSelectedNeurons(); for (int i = 0; i < neuronsInSelection.Count; i++) { Neuron n = MainWindow.theNeuronArray.GetNeuron(neuronsInSelection[i]); n.AddSynapse(targetNeuronIndex, lastSynapseWeight, lastSynapseHebbian); } Update(); }
public void ConnectFromHere() { if (targetNeuronIndex == -1) { return; } Neuron targetNeuron = MainWindow.theNeuronArray.GetNeuron(targetNeuronIndex); List <int> neuronsInSelection = theSelection.EnumSelectedNeurons(); for (int i = 0; i < neuronsInSelection.Count; i++) { targetNeuron.AddSynapse(neuronsInSelection[i], lastSynapseWeight, MainWindow.theNeuronArray, true); } Update(); }
private static void Tb_TextChanged(object sender, TextChangedEventArgs e) { TextBox tb = sender as TextBox; //find out which neuron this context menu is from ContextMenu cm = tb.Parent as ContextMenu; float newWeight = -20; float.TryParse(tb.Text, out newWeight); if (newWeight == -20) { return; } theNeuronArrayView.lastSynapseWeight = newWeight; Neuron n = MainWindow.theNeuronArray.GetNeuron((int)cm.GetValue(SourceIDProperty)); n.AddSynapse((int)cm.GetValue(TargetIDProperty), newWeight, MainWindow.theNeuronArray, true); }
private static void cbHebbianChecked(object sender, RoutedEventArgs e) { CheckBox cb = sender as CheckBox; ContextMenu cm = cb.Parent as ContextMenu; cm.IsOpen = false; CheckBox cbHebbian = (CheckBox)Utils.FindByName(cm, "Hebbian"); Synapse s = MainWindow.theNeuronArray.GetNeuron((int)cm.GetValue(SourceIDProperty)). FindSynapse((int)cm.GetValue(TargetIDProperty)); if (s != null) { s.isHebbian = (bool)cb.IsChecked; } Neuron n = MainWindow.theNeuronArray.GetNeuron((int)cm.GetValue(SourceIDProperty)); n.AddSynapse((int)cm.GetValue(TargetIDProperty), s.weight, s.isHebbian); theNeuronArrayView.lastSynapseHebbian = s.isHebbian; }
private void CreateRandomSynapses(int i) { if (rand == null) { rand = new Random(); } Neuron n = MainWindow.theNeuronArray.GetNeuron(i); int nextNeuron = n.Id; for (int j = 0; j < 100; j++) { nextNeuron = rand.Next(MainWindow.theNeuronArray.arraySize - 1); float weight = (rand.Next(1000) / 1000f) * .2f - .09f; n.AddSynapse(nextNeuron, weight); } }
public void MoveOneNeuron(Neuron n, Neuron nNewLocation) { //copy the neuron attributes and delete them from the old neuron. n.Copy(nNewLocation); //for all the synapses going out this neuron, change to going from new location //don't use a foreach here because the body of the loop may delete a list entry if (n.synapses != null) { for (int k = 0; k < n.synapses.Count; k++) { Synapse s = n.Synapses[k]; Synapse s1 = nNewLocation.AddSynapse(s.TargetNeuron, s.Weight); if (s1 != null && s.IsHebbian) { s1.IsHebbian = true; } } } //for all the synapses coming into this neuron, change the synapse target to new location if (n.synapses != null) { for (int k = 0; k < n.synapsesFrom.Count; k++) { Synapse reverseSynapse = n.SynapsesFrom[k]; //(from synapses are sort-of backward if (reverseSynapse.TargetNeuron != -1) { Neuron sourceNeuron = MainWindow.theNeuronArray.GetNeuron(reverseSynapse.TargetNeuron); Synapse s = sourceNeuron.FindSynapse(n.Id); if (s != null) { s.TargetNeuron = nNewLocation.Id; nNewLocation.SynapsesFrom.Add(new Synapse(sourceNeuron.Id, s.Weight)); } } } } n.Clear(); }
public void MoveOneNeuron(Neuron n, Neuron nNewLocation, bool addUndo = true) { if (addUndo) { n.AddUndoInfo(); nNewLocation.AddUndoInfo(); } if (MainWindow.useServers) { n.synapses = NeuronClient.GetSynapses(n.id); n.synapsesFrom = NeuronClient.GetSynapsesFrom(n.id); } //copy the neuron attributes and delete them from the old neuron. n.Copy(nNewLocation); MainWindow.theNeuronArray.SetCompleteNeuron(nNewLocation); if (FiringHistory.NeuronIsInFiringHistory(n.id)) { FiringHistory.RemoveNeuronFromHistoryWindow(n.id); FiringHistory.AddNeuronToHistoryWindow(nNewLocation.id); } //for all the synapses going out this neuron, change to going from new location //don't use a foreach here because the body of the loop may delete a list entry for (int k = 0; k < n.Synapses.Count; k++) { Synapse s = n.Synapses[k]; if (addUndo) { if (s.targetNeuron != n.id) { nNewLocation.AddSynapseWithUndo(s.targetNeuron, s.weight, s.model); } else { nNewLocation.AddSynapseWithUndo(nNewLocation.id, s.weight, s.model); } n.DeleteSynapseWithUndo(n.synapses[k].targetNeuron); } else { if (s.targetNeuron != n.id) { nNewLocation.AddSynapse(s.targetNeuron, s.weight, s.model); } else { nNewLocation.AddSynapse(nNewLocation.id, s.weight, s.model); } n.DeleteSynapse(n.synapses[k].targetNeuron); } } //for all the synapses coming into this neuron, change the synapse target to new location for (int k = 0; k < n.SynapsesFrom.Count; k++) { Synapse reverseSynapse = n.SynapsesFrom[k]; //(from synapses are sort-of backward if (reverseSynapse.targetNeuron != -1) //? { Neuron sourceNeuron = MainWindow.theNeuronArray.GetNeuron(reverseSynapse.targetNeuron); sourceNeuron.DeleteSynapseWithUndo(n.id); if (sourceNeuron.id != n.id) { if (addUndo) { sourceNeuron.AddSynapseWithUndo(nNewLocation.id, reverseSynapse.weight, reverseSynapse.model); } else { sourceNeuron.AddSynapse(nNewLocation.id, reverseSynapse.weight, reverseSynapse.model); } } } } n.Clear(); }
//copy the selection to a clipboard public void CopyNeurons() { //get list of neurons to copy List <int> neuronsToCopy = theSelection.EnumSelectedNeurons(); theSelection.GetSelectedBoundingRectangle(out int X1o, out int Y1o, out int X2o, out int Y2o); MainWindow.myClipBoard = new NeuronArray(); NeuronArray myClipBoard; myClipBoard = MainWindow.myClipBoard; myClipBoard.Initialize((X2o - X1o + 1) * (Y2o - Y1o + 1), (Y2o - Y1o + 1), true); boundarySynapsesOut.Clear(); boundarySynapsesIn.Clear(); //copy the neurons foreach (int nID in neuronsToCopy) { int destId = GetClipboardId(X1o, Y1o, nID); //copy the source neuron to the clipboard Neuron sourceNeuron = MainWindow.theNeuronArray.GetNeuron(nID); Neuron destNeuron = sourceNeuron.Clone(); destNeuron.Owner = myClipBoard; destNeuron.Id = destId; destNeuron.Label = sourceNeuron.Label; myClipBoard.SetNeuron(destId, destNeuron); } //copy the synapses (this is two-pass so we make sure all neurons exist prior to copying foreach (int nID in neuronsToCopy) { Neuron sourceNeuron = MainWindow.theNeuronArray.GetNeuron(nID); if (MainWindow.useServers) { sourceNeuron.synapses = NeuronClient.GetSynapses(sourceNeuron.id); } int destId = GetClipboardId(X1o, Y1o, nID); Neuron destNeuron = myClipBoard.GetNeuron(destId); destNeuron.Owner = myClipBoard; if (sourceNeuron.Synapses != null) { foreach (Synapse s in sourceNeuron.Synapses) { //only copy synapses with both ends in the selection if (neuronsToCopy.Contains(s.TargetNeuron)) { destNeuron.AddSynapse(GetClipboardId(X1o, Y1o, s.TargetNeuron), s.Weight, s.model); } else { string targetName = MainWindow.theNeuronArray.GetNeuron(s.targetNeuron).label; if (targetName != "") { boundarySynapsesOut.Add(new BoundarySynapse { innerNeuronID = destNeuron.id, outerNeuronID = s.targetNeuron, weight = s.weight, model = s.model }); } } } } if (sourceNeuron.SynapsesFrom != null) { foreach (Synapse s in sourceNeuron.SynapsesFrom) { if (!neuronsToCopy.Contains(s.TargetNeuron)) { string sourceName = MainWindow.theNeuronArray.GetNeuron(s.targetNeuron).label; if (sourceName != "") { boundarySynapsesIn.Add(new BoundarySynapse { innerNeuronID = destNeuron.id, outerNeuronID = s.targetNeuron, weight = s.weight, model = s.model });; } } } } } //copy modules foreach (ModuleView mv in MainWindow.theNeuronArray.modules) { if (theSelection.NeuronInSelection(mv.FirstNeuron) > 0 && theSelection.NeuronInSelection(mv.LastNeuron) > 0) { ModuleView newMV = new ModuleView() { FirstNeuron = GetClipboardId(X1o, Y1o, mv.FirstNeuron), TheModule = mv.TheModule, Color = mv.Color, Height = mv.Height, Width = mv.Width, Label = mv.Label, CommandLine = mv.CommandLine, }; myClipBoard.modules.Add(newMV); } } }
//copy the selection to a clipboard public void CopyNeurons() { //get list of neurons to copy List <int> neuronsToCopy = theSelection.EnumSelectedNeurons(); theSelection.GetSelectedBoundingRectangle(out int X1o, out int Y1o, out int X2o, out int Y2o); MainWindow.myClipBoard = new NeuronArray(); NeuronArray myClipBoard; myClipBoard = MainWindow.myClipBoard; myClipBoard.Initialize((X2o - X1o + 1) * (Y2o - Y1o + 1), (Y2o - Y1o + 1)); //copy the neurons foreach (int nID in neuronsToCopy) { int destId = GetClipboardId(X1o, Y1o, nID); //copy the source neuron to the clipboard Neuron sourceNeuron = MainWindow.theNeuronArray.GetNeuron(nID); Neuron destNeuron = sourceNeuron.Clone(); destNeuron.Owner = myClipBoard; destNeuron.Id = destId; myClipBoard.SetNeuron(destId, destNeuron); } //copy the synapses (this is two-pass so we make sure all neurons exist prior to copying foreach (int nID in neuronsToCopy) { Neuron sourceNeuron = MainWindow.theNeuronArray.GetNeuron(nID); int destId = GetClipboardId(X1o, Y1o, nID); Neuron destNeuron = myClipBoard.GetNeuron(destId); destNeuron.Owner = myClipBoard; if (sourceNeuron.Synapses != null) { foreach (Synapse s in sourceNeuron.Synapses) { //only copy synapses with both ends in the selection if (neuronsToCopy.Contains(s.TargetNeuron)) { destNeuron.AddSynapse(GetClipboardId(X1o, Y1o, s.TargetNeuron), s.Weight, s.isHebbian); } } } } //copy modules foreach (ModuleView mv in MainWindow.theNeuronArray.modules) { if (theSelection.NeuronInSelection(mv.FirstNeuron) > 0 && theSelection.NeuronInSelection(mv.LastNeuron) > 0) { ModuleView newMV = new ModuleView() { FirstNeuron = GetClipboardId(X1o, Y1o, mv.FirstNeuron), TheModule = mv.TheModule, Color = mv.Color, Height = mv.Height, Width = mv.Width, Label = mv.Label, CommandLine = mv.CommandLine, }; myClipBoard.modules.Add(newMV); } } }
private static void Mi_Click(object sender, RoutedEventArgs e) { MenuItem mi = sender as MenuItem; //find out which neuron this context menu is from ContextMenu cm = mi.Parent as ContextMenu; if (cm == null) { MenuItem mi2 = mi.Parent as MenuItem; if (mi2.Header.ToString().IndexOf("Synapses") == 0) { int.TryParse(mi.Header.ToString().Substring(0, 8), out int newID); Neuron n1 = MainWindow.theNeuronArray.GetNeuron(newID); NeuronView.CreateContextMenu(n1.id, n1, new ContextMenu() { IsOpen = true, }); return; } cm = mi2.Parent as ContextMenu; } int i = (int)cm.GetValue(NeuronIDProperty); Neuron n = MainWindow.theNeuronArray.GetNeuron(i); if ((string)mi.Header == "Always Fire") { if (n.FindSynapse(i) == null) { n.AddSynapse(i, 1, MainWindow.theNeuronArray, true); } else { n.DeleteSynapse(i); } } if ((string)mi.Header == "Paste Here") { theNeuronArrayView.targetNeuronIndex = i; theNeuronArrayView.PasteNeurons(); theNeuronArrayView.targetNeuronIndex = -1; cmCancelled = true; } if ((string)mi.Header == "Move Here") { theNeuronArrayView.targetNeuronIndex = i; theNeuronArrayView.MoveNeurons(); cmCancelled = true; } if ((string)mi.Header == "Connect to Here") { theNeuronArrayView.targetNeuronIndex = i; theNeuronArrayView.ConnectToHere(); } if ((string)mi.Header == "Connect from Here") { theNeuronArrayView.targetNeuronIndex = i; theNeuronArrayView.ConnectFromHere(); } if ((string)mi.Header == "Select as Target") { theNeuronArrayView.targetNeuronIndex = i; } }
public static bool Load(ref NeuronArray theNeuronArray, string fileName) { FileStream file; try { file = File.Open(fileName, FileMode.Open, FileAccess.Read); } catch (Exception e) { MessageBox.Show("Could not open file because: " + e.Message); RemoveFileFromMRUList(fileName); return(false); } // first check if the required start tag is present in the file... byte[] buffer = new byte[60]; file.Read(buffer, 0, 60); string line = Encoding.UTF8.GetString(buffer, 0, buffer.Length); if (line.Contains("NeuronArray")) { file.Seek(0, SeekOrigin.Begin); } else { file.Close(); MessageBox.Show("File is no valid Brain Simulator II XML file."); return(false); } MainWindow.thisWindow.SetProgress(0, "Loading Network File"); theNeuronArray = new NeuronArray(); XmlSerializer reader1 = new XmlSerializer(typeof(NeuronArray), GetModuleTypes()); theNeuronArray = (NeuronArray)reader1.Deserialize(file); file.Close(); //the above automatically loads the content of the neuronArray object but can't load the neurons themselves //because of formatting changes XmlDocument xmldoc = new XmlDocument(); XmlNodeList neuronNodes; FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read); xmldoc.Load(fs); fs.Close(); int arraySize = theNeuronArray.arraySize; theNeuronArray.Initialize(arraySize, theNeuronArray.rows); neuronNodes = xmldoc.GetElementsByTagName("Neuron"); for (int i = 0; i < neuronNodes.Count; i++) { var progress = i / (float)neuronNodes.Count; progress *= 100; if (progress != 0 && MainWindow.thisWindow.SetProgress(progress, "")) { MainWindow.thisWindow.SetProgress(100, ""); return(false); } XmlElement neuronNode = (XmlElement)neuronNodes[i]; XmlNodeList idNodes = neuronNode.GetElementsByTagName("Id"); int id = i; //this is a hack to read files where all neurons were included but no Id's if (idNodes.Count > 0) { int.TryParse(idNodes[0].InnerText, out id); } if (id == -1) { continue; } Neuron n = theNeuronArray.GetNeuron(id); n.Owner = theNeuronArray; n.id = id; foreach (XmlElement node in neuronNode.ChildNodes) { string name = node.Name; switch (name) { case "Label": n.Label = node.InnerText; break; case "Model": Enum.TryParse(node.InnerText, out Neuron.modelType theModel); n.model = theModel; break; case "LeakRate": float.TryParse(node.InnerText, out float leakRate); n.leakRate = leakRate; break; case "AxonDelay": int.TryParse(node.InnerText, out int axonDelay); n.axonDelay = axonDelay; break; case "LastCharge": if (n.model != Neuron.modelType.Color) { float.TryParse(node.InnerText, out float lastCharge); n.LastCharge = lastCharge; n.currentCharge = lastCharge; } else //is color { int.TryParse(node.InnerText, out int lastChargeInt); n.LastChargeInt = lastChargeInt; n.currentCharge = lastChargeInt; //current charge is not used on color neurons } break; case "ShowSynapses": bool.TryParse(node.InnerText, out bool showSynapses); if (showSynapses) { MainWindow.arrayView.AddShowSynapses(n.id); } break; case "Synapses": theNeuronArray.SetCompleteNeuron(n); XmlNodeList synapseNodess = node.GetElementsByTagName("Synapse"); foreach (XmlNode synapseNode in synapseNodess) { Synapse s = new Synapse(); foreach (XmlNode synapseAttribNode in synapseNode.ChildNodes) { string name1 = synapseAttribNode.Name; switch (name1) { case "TargetNeuron": int.TryParse(synapseAttribNode.InnerText, out int target); s.targetNeuron = target; break; case "Weight": float.TryParse(synapseAttribNode.InnerText, out float weight); s.weight = weight; break; case "IsHebbian": //Obsolete: backwards compatibility bool.TryParse(synapseAttribNode.InnerText, out bool isheb); if (isheb) { s.model = Synapse.modelType.Hebbian1; } else { s.model = Synapse.modelType.Fixed; } break; case "Model": Enum.TryParse(synapseAttribNode.InnerText, out Synapse.modelType model); s.model = model; break; } } n.AddSynapse(s.targetNeuron, s.weight, s.model); } break; } } theNeuronArray.SetCompleteNeuron(n); } MainWindow.thisWindow.SetProgress(100, ""); return(true); }
private static void Mi_Click(object sender, RoutedEventArgs e) { //Handle delete & initialize commands if (sender is MenuItem mi) { if ((string)mi.Header == "Delete") { int i = (int)mi.Parent.GetValue(AreaNumberProperty); if (i < 0) { i = -i - 1; MainWindow.arrayView.theSelection.selectedRectangles.RemoveAt(i); deleted = true; } else { ModuleView mv = MainWindow.theNeuronArray.Modules[i]; foreach (Neuron n in mv.Neurons()) { n.Reset(); n.DeleteAllSynapes(); } MainWindow.theNeuronArray.Modules.RemoveAt(i); deleted = true; } } if ((string)mi.Header == "Initialize") { int i = (int)mi.Parent.GetValue(AreaNumberProperty); if (i < 0) { } else { MainWindow.theNeuronArray.Modules[i].TheModule.Initialize(); } } if ((string)mi.Header == "Show Dialog") { int i = (int)mi.Parent.GetValue(AreaNumberProperty); if (i < 0) { } else { MainWindow.theNeuronArray.Modules[i].TheModule.ShowDialog(); } } if ((string)mi.Header == "Info...") { int i = (int)mi.Parent.GetValue(AreaNumberProperty); if (i < 0) { } else { ModuleView m = MainWindow.theNeuronArray.Modules[i]; ModuleBase m1 = m.TheModule; ModuleDescription md = new ModuleDescription(m1.ShortDescription, m1.LongDescription); md.ShowDialog(); } } if ((string)mi.Header == "Reset Hebbian Weights") { foreach (NeuronSelectionRectangle sr in MainWindow.arrayView.theSelection.selectedRectangles) { foreach (int Id in sr.NeuronInRectangle()) { Neuron n = MainWindow.theNeuronArray.GetNeuron(Id); foreach (Synapse s in n.Synapses) { if (s.isHebbian) { s.Weight = 0; n.AddSynapse(s.targetNeuron, s.weight, s.isHebbian); } } } } MainWindow.Update(); } } }
public static bool Load(ref NeuronArray theNeuronArray, string fileName) { FileStream file = File.Open(fileName, FileMode.Open); XmlSerializer reader1 = new XmlSerializer(typeof(NeuronArray), GetModuleTypes()); theNeuronArray = (NeuronArray)reader1.Deserialize(file); file.Close(); //the above automatically loads the content of the neuronArray object but can't load the neurons themselves //because of formatting changes XmlDocument xmldoc = new XmlDocument(); XmlNodeList neuronNodes; FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read); xmldoc.Load(fs); fs.Close(); int arraySize = theNeuronArray.arraySize; theNeuronArray.Initialize(arraySize); neuronNodes = xmldoc.GetElementsByTagName("Neuron"); for (int i = 0; i < neuronNodes.Count; i++) { XmlElement neuronNode = (XmlElement)neuronNodes[i]; XmlNodeList idNodes = neuronNode.GetElementsByTagName("Id"); if (idNodes.Count < 1) { continue; } int id = -1; int.TryParse(idNodes[0].InnerText, out id); if (id == -1) { continue; } Neuron n = theNeuronArray.GetNeuron(id); n.Owner = theNeuronArray; n.id = id; foreach (XmlElement node in neuronNode.ChildNodes) { string name = node.Name; switch (name) { case "Label": n.label = node.InnerText; break; case "Model": Enum.TryParse(node.InnerText, out Neuron.modelType theModel); n.model = theModel; break; case "LeakRate": float.TryParse(node.InnerText, out float leakRate); n.leakRate = leakRate; break; case "LastCharge": float.TryParse(node.InnerText, out float lastCharge); n.LastCharge = lastCharge; n.currentCharge = lastCharge; break; case "Synapses": theNeuronArray.SetCompleteNeuron(n); XmlNodeList synapseNodess = node.GetElementsByTagName("Synapse"); foreach (XmlNode synapseNode in synapseNodess) { Synapse s = new Synapse(); foreach (XmlNode synapseAttribNode in synapseNode.ChildNodes) { string name1 = synapseAttribNode.Name; switch (name1) { case "TargetNeuron": int.TryParse(synapseAttribNode.InnerText, out int target); s.targetNeuron = target; break; case "Weight": float.TryParse(synapseAttribNode.InnerText, out float weight); s.weight = weight; break; case "IsHebbian": bool.TryParse(synapseAttribNode.InnerText, out bool isheb); s.isHebbian = isheb; break; } } n.AddSynapse(s.targetNeuron, s.weight, s.isHebbian); } break; } } theNeuronArray.SetCompleteNeuron(n); } return(true); }
public static bool Load(ref NeuronArray theNeuronArray, string fileName) { FileStream file = File.Open(fileName, FileMode.Open); XmlSerializer reader1 = new XmlSerializer(typeof(NeuronArray), GetModuleTypes()); theNeuronArray = (NeuronArray)reader1.Deserialize(file); file.Close(); //the above automatically loads the content of the neuronArray object but can't load the neurons themselves //because of formatting changes XmlDocument xmldoc = new XmlDocument(); XmlNodeList neuronNodes; FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read); xmldoc.Load(fs); fs.Close(); int arraySize = theNeuronArray.arraySize; theNeuronArray.Initialize(arraySize); neuronNodes = xmldoc.GetElementsByTagName("Neuron"); for (int i = 0; i < neuronNodes.Count; i++) { var progress = i / (float)neuronNodes.Count; if (i % 1000 == 0) { MainWindow.thisWindow.SetProgress(progress); } XmlElement neuronNode = (XmlElement)neuronNodes[i]; XmlNodeList idNodes = neuronNode.GetElementsByTagName("Id"); int id = i; //this is a hack to read files where all neurons were included but no Id's if (idNodes.Count > 0) { int.TryParse(idNodes[0].InnerText, out id); } if (id == -1) { continue; } Neuron n = theNeuronArray.GetNeuron(id); n.Owner = theNeuronArray; n.id = id; foreach (XmlElement node in neuronNode.ChildNodes) { string name = node.Name; switch (name) { case "Label": n.Label = node.InnerText; break; case "Model": Enum.TryParse(node.InnerText, out Neuron.modelType theModel); n.model = theModel; break; case "LeakRate": float.TryParse(node.InnerText, out float leakRate); n.leakRate = leakRate; break; case "AxonDelay": int.TryParse(node.InnerText, out int axonDelay); n.axonDelay = axonDelay; break; case "LastCharge": if (n.model != Neuron.modelType.Color) { float.TryParse(node.InnerText, out float lastCharge); n.LastCharge = lastCharge; n.currentCharge = lastCharge; } else //is color { int.TryParse(node.InnerText, out int lastChargeInt); n.LastChargeInt = lastChargeInt; n.currentCharge = lastChargeInt; //current charge is not used on color neurons } break; case "ShowSynapses": bool.TryParse(node.InnerText, out bool showSynapses); if (showSynapses) { MainWindow.arrayView.AddShowSynapses(n.id); } break; case "Synapses": theNeuronArray.SetCompleteNeuron(n); XmlNodeList synapseNodess = node.GetElementsByTagName("Synapse"); foreach (XmlNode synapseNode in synapseNodess) { Synapse s = new Synapse(); foreach (XmlNode synapseAttribNode in synapseNode.ChildNodes) { string name1 = synapseAttribNode.Name; switch (name1) { case "TargetNeuron": int.TryParse(synapseAttribNode.InnerText, out int target); s.targetNeuron = target; break; case "Weight": float.TryParse(synapseAttribNode.InnerText, out float weight); s.weight = weight; break; case "IsHebbian": //Obsolete: backwards compatibility bool.TryParse(synapseAttribNode.InnerText, out bool isheb); if (isheb) { s.model = Synapse.modelType.Hebbian1; } else { s.model = Synapse.modelType.Fixed; } break; case "Model": Enum.TryParse(synapseAttribNode.InnerText, out Synapse.modelType model); s.model = model; break; } } n.AddSynapse(s.targetNeuron, s.weight, s.model); } break; } } theNeuronArray.SetCompleteNeuron(n); } MainWindow.thisWindow.SetProgress(-1); return(true); }