private static void AddSynapseEntryToMenu(MenuItem mi, Synapse s) { string targetLabel = MainWindow.theNeuronArray.GetNeuron(s.targetNeuron).Label; StackPanel sp0 = new StackPanel { Orientation = Orientation.Horizontal }; TextBlock tbWeight = new TextBlock { Text = s.Weight.ToString("F3").PadLeft(9) }; tbWeight.MouseEnter += SynapseEntry_MouseEnter; tbWeight.MouseLeave += SynapseEntry_MouseLeave; tbWeight.ToolTip = "Click to edit synapse"; tbWeight.MouseDown += SynapseEntry_MouseDown; tbWeight.Name = "weight"; TextBlock tbTarget = new TextBlock { Text = s.targetNeuron.ToString().PadLeft(8) + " " + targetLabel }; sp0.Children.Add(tbWeight); sp0.Children.Add(tbTarget); tbTarget.MouseEnter += SynapseEntry_MouseEnter; tbTarget.MouseLeave += SynapseEntry_MouseLeave; tbTarget.ToolTip = "Click to go to neuron"; tbTarget.MouseDown += SynapseEntry_MouseDown; tbTarget.Name = "neuron"; mi.Items.Add(sp0); }
public static Shape GetSynapseView(int i, Point p1, Synapse s, NeuronArrayView theNeuronArrayView1) { theNeuronArrayView = theNeuronArrayView1; Point p2 = dp.pointFromNeuron(s.TargetNeuron); if (!PtOnScreen(p1) && !PtOnScreen(p2)) { return(null); } Shape l = GetSynapseShape(p1, p2, theNeuronArrayView); l.Stroke = Brushes.Red; if (s.Weight < 0.9) { l.Stroke = Brushes.Yellow; } if (s.Weight < 0.1) { l.Stroke = Brushes.Green; } if (s.Weight < -0.1) { l.Stroke = Brushes.Blue; } if (s.Weight < -0.9) { l.Stroke = Brushes.Black; } l.SetValue(SourceIDProperty, i); l.SetValue(TargetIDProperty, s.TargetNeuron); l.SetValue(WeightValProperty, s.Weight); return(l); }
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 static Shape GetSynapseView(int i, Point p1, Synapse s, NeuronArrayView theNeuronArrayView1) { theNeuronArrayView = theNeuronArrayView1; Point p2 = dp.pointFromNeuron(s.TargetNeuron); if (!PtOnScreen(p1) && !PtOnScreen(p2)) { return(null); } Shape l = GetSynapseShape(p1, p2, theNeuronArrayView, s.isHebbian); l.Stroke = new SolidColorBrush(Utils.RainbowColorFromValue(s.weight)); if (l is Ellipse E) { } else { l.Fill = l.Stroke; } l.SetValue(SourceIDProperty, i); l.SetValue(TargetIDProperty, s.TargetNeuron); l.SetValue(WeightValProperty, s.Weight); return(l); }
//SYNAPSE private static void OpenSynapseContextMenu(FrameworkElement theShape) { int source = (int)theShape.GetValue(SynapseView.SourceIDProperty); int target = (int)theShape.GetValue(SynapseView.TargetIDProperty); float weight = (float)theShape.GetValue(SynapseView.WeightValProperty); Neuron n1 = MainWindow.theNeuronArray.GetCompleteNeuron(source); n1 = MainWindow.theNeuronArray.AddSynapses(n1); Synapse s1 = n1.FindSynapse(target); theShape.ContextMenu = new ContextMenu(); SynapseView.CreateContextMenu(source, s1, theShape.ContextMenu); }
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; } }
public void MoveOneNeuron(Neuron n, Neuron nNewLocation) { n.AddUndoInfo(); nNewLocation.AddUndoInfo(); //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 (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); } //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) { sourceNeuron.AddSynapseWithUndo(nNewLocation.id, reverseSynapse.weight, reverseSynapse.model); } } } n.Clear(); }
private static void SynapseMenuItem_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e) { MenuItem mi = sender as MenuItem; MenuItem mi2 = mi.Parent as MenuItem; ContextMenu cm = mi2.Parent as ContextMenu; int sourceID = (int)cm.GetValue(NeuronIDProperty); ContextMenu newCm = new ContextMenu(); int.TryParse(mi.Header.ToString().Substring(0, 8), out int targetID); Neuron n = MainWindow.theNeuronArray.GetNeuron(sourceID); Synapse s = n.FindSynapse(targetID); if (s != null) { SynapseView.CreateContextMenu(sourceID, s, newCm); newCm.IsOpen = true; e.Handled = true; } }
List<Synapse> ConvertToSynapseList(byte[] input) { List<Synapse> retVal = new List<Synapse>(); Synapse s = new Synapse(); int sizeOfSynapse = Marshal.SizeOf(s); int numberOfSynapses = input.Length / sizeOfSynapse; byte[] oneSynapse = new byte[sizeOfSynapse]; for (int i = 0; i < numberOfSynapses; i++) { int offset = i * sizeOfSynapse; for (int k = 0; k < sizeOfSynapse; k++) oneSynapse[k] = input[k + offset]; GCHandle handle = GCHandle.Alloc(oneSynapse, GCHandleType.Pinned); s = (Synapse)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(Synapse)); retVal.Add(s); handle.Free(); } return retVal; }
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 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); }
//these aren't added to synapses for performance but are built on the fly if the user right-clicks public static void CreateContextMenu(int i, Synapse s, ContextMenu cm) { cm.SetValue(SourceIDProperty, i); cm.SetValue(TargetIDProperty, s.TargetNeuron); cm.SetValue(WeightValProperty, s.Weight); MenuItem mi = new MenuItem(); mi.Header = "Delete"; mi.Click += DeleteSynapse_Click; cm.Items.Add(mi); mi = new MenuItem(); mi.Header = "Step & Repeat"; mi.Click += StepAndRepeatSynapse_Click; cm.Items.Add(mi); mi = new MenuItem(); mi.Header = "1"; mi.Click += ANDSynapse_Click; cm.Items.Add(mi); mi = new MenuItem(); mi.Header = ".9"; mi.Click += ANDSynapse_Click; cm.Items.Add(mi); mi = new MenuItem(); mi.Header = ".5"; mi.Click += ANDSynapse_Click; cm.Items.Add(mi); mi = new MenuItem(); mi.Header = ".34"; mi.Click += ANDSynapse_Click; cm.Items.Add(mi); mi = new MenuItem(); mi.Header = ".25"; mi.Click += ANDSynapse_Click; cm.Items.Add(mi); mi = new MenuItem(); mi.Header = "-1"; mi.Click += ANDSynapse_Click; cm.Items.Add(mi); mi = new MenuItem(); mi.Header = "Weight:"; mi.IsEnabled = false; mi.Click += ANDSynapse_Click; cm.Items.Add(mi); TextBox tb = new TextBox(); tb.Text = s.Weight.ToString("F4"); tb.Width = 200; tb.TextChanged += Tb_TextChanged; cm.Items.Add(tb); CheckBox cbHebbian = new CheckBox { IsChecked = s.IsHebbian, Content = "Hebbian Learning", Name = "Hebbian", }; cbHebbian.Checked += cbHebbianChecked; cbHebbian.Unchecked += cbHebbianChecked; cm.Items.Add(cbHebbian); }
//these aren't added to synapses (for performance) but are built on the fly if the user right-clicks public static void CreateContextMenu(int i, Synapse s, ContextMenu cm) { cmCancelled = false; weightChanged = false; //set defaults for next synapse add theNeuronArrayView.lastSynapseModel = s.model; theNeuronArrayView.lastSynapseWeight = s.weight; cm.SetValue(SourceIDProperty, i); cm.SetValue(TargetIDProperty, s.TargetNeuron); cm.SetValue(WeightValProperty, s.Weight); cm.SetValue(ModelProperty, s.model); cm.Closed += Cm_Closed; cm.PreviewKeyDown += Cm_PreviewKeyDown; cm.Opened += Cm_Opened; Neuron nSource = MainWindow.theNeuronArray.GetNeuron(i); Neuron nTarget = MainWindow.theNeuronArray.GetNeuron(s.targetNeuron); StackPanel sp = new StackPanel { Orientation = Orientation.Horizontal, Margin = new Thickness(0, 3, 3, 3) }; sp.Children.Add(new Label { Content = "Source: ", Padding = new Thickness(0) }); string source = nSource.id.ToString(); if (nSource.label != "") { source = nSource.Label; } TextBox t0 = Utils.ContextMenuTextBox(source, "Source", 150); t0.TextChanged += TextChanged; sp.Children.Add(t0); cm.Items.Add(new MenuItem { Header = sp, StaysOpenOnClick = true }); sp = new StackPanel { Orientation = Orientation.Horizontal, Margin = new Thickness(0, 3, 3, 3) }; sp.Children.Add(new Label { Content = "Target: ", Padding = new Thickness(0) }); string target = nTarget.id.ToString(); if (nTarget.label != "") { target = nTarget.Label; } TextBox t1 = Utils.ContextMenuTextBox(target, "Target", 150); t1.TextChanged += TextChanged; sp.Children.Add(t1); cm.Items.Add(new MenuItem { Header = sp, StaysOpenOnClick = true }); //The Synapse model sp = new StackPanel { Orientation = Orientation.Horizontal, Margin = new Thickness(0, 3, 3, 3) }; sp.Children.Add(new Label { Content = "Model: ", Padding = new Thickness(0) }); ComboBox cb = new ComboBox() { Width = 100, Name = "Model" }; for (int index = 0; index < Enum.GetValues(typeof(Synapse.modelType)).Length; index++) { Synapse.modelType model = (Synapse.modelType)index; cb.Items.Add(new ListBoxItem() { Content = model.ToString(), ToolTip = Synapse.modelToolTip[index], Width = 100, }); } cb.SelectedIndex = (int)s.model; sp.Children.Add(cb); cm.Items.Add(new MenuItem { Header = sp, StaysOpenOnClick = true }); cm.Items.Add(Utils.CreateComboBoxMenuItem("SynapseWeight", s.weight, synapseWeightValues, "F3", "Weight: ", 100, ComboBox_ContentChanged)); MenuItem mi = new MenuItem(); mi.Header = "Delete"; mi.Click += DeleteSynapse_Click; cm.Items.Add(mi); sp = new StackPanel { Orientation = Orientation.Horizontal }; Button b0 = new Button { Content = "OK", Width = 100, Height = 25, Margin = new Thickness(10), IsDefault = true }; b0.Click += B0_Click; sp.Children.Add(b0); b0 = new Button { Content = "Cancel", Width = 100, Height = 25, Margin = new Thickness(10), IsCancel = true }; b0.Click += B0_Click; sp.Children.Add(b0); cm.Items.Add(new MenuItem { Header = sp, StaysOpenOnClick = true }); }
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 SynapseEntry_MouseDown(object sender, MouseButtonEventArgs e) { if (sender is TextBlock tb0) { if (tb0.Parent is StackPanel sp) { if (sp.Parent is MenuItem mi) { if (mi.Parent is ContextMenu cm) { if (tb0.Name == "weight") { int sourceID = (int)cm.GetValue(NeuronIDProperty); if (sp.Children.Count > 1 && sp.Children[1] is TextBlock tb1) { int.TryParse(tb1.Text.Substring(0, 8), out int targetID); if (mi.Header.ToString().Contains("In")) { int temp = targetID; targetID = sourceID; sourceID = temp; } ContextMenu newCm = new ContextMenu(); Neuron n = MainWindow.theNeuronArray.GetNeuron(sourceID); Synapse s = n.FindSynapse(targetID); if (s != null) { SynapseView.CreateContextMenu(sourceID, s, newCm); newCm.IsOpen = true; e.Handled = true; } } } if (tb0.Name == "neuron") { int.TryParse(tb0.Text.Substring(0, 8), out int targetID); Neuron n1 = MainWindow.theNeuronArray.GetNeuron(targetID); ContextMenu cm1 = NeuronView.CreateContextMenu(n1.id, n1, new ContextMenu() { IsOpen = true, }); MainWindow.arrayView.targetNeuronIndex = targetID; Point loc = dp.pointFromNeuron(targetID); if (loc.X < 0 || loc.X > theCanvas.ActualWidth - cm.ActualWidth || loc.Y < 0 || loc.Y > theCanvas.ActualHeight - cm.ActualHeight) { MainWindow.arrayView.PanToNeuron(targetID); loc = dp.pointFromNeuron(targetID); } loc.X += dp.NeuronDisplaySize / 2; loc.Y += dp.NeuronDisplaySize / 2; loc = MainWindow.arrayView.theCanvas.PointToScreen(loc); cm1.PlacementRectangle = new Rect(loc.X, loc.Y, 0, 0); cm1.Placement = System.Windows.Controls.Primitives.PlacementMode.AbsolutePoint; } } } } e.Handled = true; } }
static void ProcessIncomingMessages(string message) { string[] commands = message.Trim().Split(' '); string command = commands[0]; switch (command) { case "PingBack": GetSystemTimePreciseAsFileTime(out returnTime); pingCount++; break; case "ServerInfo": int index = serverList.FindIndex(x => x.name == commands[2]); if (index == -1) { Server s = new Server(); IPAddress.TryParse(commands[1], out s.ipAddress); s.name = commands[2]; int.TryParse(commands[3], out s.firstNeuron); int.TryParse(commands[4], out s.lastNeuron); if (commands.Length > 5) { int.TryParse(commands[5], out s.neuronsInUse); } if (commands.Length > 6) { long.TryParse(commands[6], out s.totalSynapses); } serverList.Add(s); index = serverList.Count - 1; } break; case "Done": index = serverList.FindIndex(x => x.name == commands[1]); if (index != -1) { serverList[index].busy = false; long.TryParse(commands[2], out serverList[index].generation); int.TryParse(commands[3], out serverList[index].firedCount); } break; case "Neuron": Neuron n = new Neuron(); int.TryParse(commands[1], out n.id); float.TryParse(commands[2], out n.lastCharge); n.label = commands[3].Replace("_", " "); bool.TryParse(commands[4], out n.inUse); tempNeuron = n; break; case "Neurons": int.TryParse(commands[1], out int count); for (int i = 2; i < commands.Length; i += 4) { n = new Neuron(); int.TryParse(commands[i], out n.id); float.TryParse(commands[i + 1], out n.lastCharge); n.label = commands[i + 2].Replace("_", " "); bool.TryParse(commands[i + 3], out n.inUse); tempNeurons.Add(n); } break; case "Synapses": int.TryParse(commands[1], out int neuronID); List <Synapse> synapses = new List <Synapse>(); for (int i = 2; i < commands.Length; i += 3) { Synapse s = new Synapse(); int.TryParse(commands[i], out s.targetNeuron); float.TryParse(commands[i + 1], out s.weight); int.TryParse(commands[i + 2], out int modelInt); s.model = (Synapse.modelType)modelInt; synapses.Add(s); } tempSynapses = synapses; break; case "SynapsesFrom": int.TryParse(commands[1], out neuronID); synapses = new List <Synapse>(); for (int i = 2; i < commands.Length; i += 3) { Synapse s = new Synapse(); int.TryParse(commands[i], out s.targetNeuron); float.TryParse(commands[i + 1], out s.weight); int.TryParse(commands[i + 2], out int modelInt); s.model = (Synapse.modelType)modelInt; synapses.Add(s); } tempSynapses = synapses; break; } }
public void theCanvas_MouseDown(object sender, MouseButtonEventArgs e) { if (MainWindow.theNeuronArray == null) { return; } Debug.WriteLine("theCanvas_MouseDown" + MainWindow.theNeuronArray.Generation); Point currentPosition = e.GetPosition(theCanvas); LimitMousePostion(ref currentPosition); mouseDownNeuronIndex = dp.NeuronFromPoint(currentPosition); if (e.RightButton == MouseButtonState.Pressed) { if (sender is Image img) { int i = (int)img.GetValue(ModuleView.AreaNumberProperty); int i1 = -i - 1; ModuleView nr = new ModuleView { Label = "new", Width = theSelection.selectedRectangles[i1].Width, Height = theSelection.selectedRectangles[i1].Height, Color = Utils.ColorToInt(Colors.Aquamarine), CommandLine = "" }; img.ContextMenu = new ContextMenu(); ModuleView.CreateContextMenu(i, nr, img, img.ContextMenu); img.ContextMenu.IsOpen = true; e.Handled = true; } else if (sender is Rectangle r) { int i = (int)r.GetValue(ModuleView.AreaNumberProperty); if (i >= 0) { ModuleView nr = MainWindow.theNeuronArray.Modules[i]; r.ContextMenu = new ContextMenu(); ModuleView.CreateContextMenu(i, nr, r, r.ContextMenu); r.ContextMenu.IsOpen = true; e.Handled = true; } if (i < 0) { int i1 = -i - 1; ModuleView nr = new ModuleView { Label = "new", Width = theSelection.selectedRectangles[i1].Width, Height = theSelection.selectedRectangles[i1].Height, Color = Utils.ColorToInt(Colors.Aquamarine), CommandLine = "" }; r.ContextMenu = new ContextMenu(); ModuleView.CreateContextMenu(i, nr, r, r.ContextMenu); r.ContextMenu.IsOpen = true; e.Handled = true; } } else if (sender is Label l) { l.ContextMenu = new ContextMenu(); Neuron n1 = MainWindow.theNeuronArray.GetNeuron(mouseDownNeuronIndex); NeuronView.CreateContextMenu(mouseDownNeuronIndex, n1, l.ContextMenu); l.ContextMenu.IsOpen = true; e.Handled = true; } else if (sender is Shape s) { if ((s is Path || s is Line || (s is Ellipse && (int)s.GetValue(SynapseView.SourceIDProperty) != 0))) // a synapse { int source = (int)s.GetValue(SynapseView.SourceIDProperty); int target = (int)s.GetValue(SynapseView.TargetIDProperty); float weight = (float)s.GetValue(SynapseView.WeightValProperty); Neuron n1 = MainWindow.theNeuronArray.GetCompleteNeuron(source); n1 = MainWindow.theNeuronArray.AddSynapses(n1); Synapse s1 = n1.FindSynapse(target); s.ContextMenu = new ContextMenu(); SynapseView.CreateContextMenu(source, s1, s.ContextMenu); } else if (s is Ellipse) // a neuron { s.ContextMenu = new ContextMenu(); Neuron n1 = MainWindow.theNeuronArray.GetNeuron(mouseDownNeuronIndex); NeuronView.CreateContextMenu(mouseDownNeuronIndex, n1, s.ContextMenu); targetNeuronIndex = mouseDownNeuronIndex; } if (s.ContextMenu != null) { s.ContextMenu.IsOpen = true; } e.Handled = true; } else { } return; } Neuron n = null; if (mouseDownNeuronIndex >= 0 && mouseDownNeuronIndex < MainWindow.theNeuronArray.arraySize) { n = MainWindow.theNeuronArray.GetNeuron(mouseDownNeuronIndex) as Neuron; } if (theCanvas.Cursor == Cursors.Cross) { // Debug.WriteLine("dragStart" + MainWindow.theNeuronArray.Generation); if (dragRectangle != null) { theCanvas.Children.Remove(dragRectangle); } MainWindow.theNeuronArray.SetUndoPoint(); MainWindow.theNeuronArray.AddSelectionUndo(); if (!MainWindow.ctrlPressed) { theSelection.selectedRectangles.Clear(); } else { Update(); } //snap to neuron point currentPosition = dp.pointFromNeuron(mouseDownNeuronIndex); //build the draggable selection rectangle dragRectangle = new Rectangle(); dragRectangle.Width = dragRectangle.Height = dp.NeuronDisplaySize; dragRectangle.Stroke = new SolidColorBrush(Colors.Red); dragRectangle.Fill = new SolidColorBrush(Colors.Red); dragRectangle.Fill.Opacity = 0.5; Canvas.SetLeft(dragRectangle, currentPosition.X); Canvas.SetTop(dragRectangle, currentPosition.Y); theCanvas.Children.Add(dragRectangle); firstSelectedNeuron = mouseDownNeuronIndex; lastSelectedNeuron = mouseDownNeuronIndex; Mouse.Capture(theCanvas); } if (theCanvas.Cursor == Cursors.ScrollAll) { dragging = true; MainWindow.theNeuronArray.SetUndoPoint(); } if (theCanvas.Cursor == Cursors.UpArrow) { if (e.ClickCount == 2 && sender is Canvas) { //double-click detected n = MainWindow.theNeuronArray.GetNeuron(mouseDownNeuronIndex); n.leakRate = -n.leakRate; n.Update(); } Mouse.Capture(theCanvas); if (mouseRepeatTimer == null) { mouseRepeatTimer = new DispatcherTimer(); } if (mouseRepeatTimer.IsEnabled) { mouseRepeatTimer.Stop(); } mouseRepeatTimer.Interval = new TimeSpan(0, 0, 0, 0, 250); mouseRepeatTimer.Tick += MouseRepeatTimer_Tick; mouseRepeatTimer.Start(); dragging = true; targetNeuronIndex = mouseDownNeuronIndex; } if (theCanvas.Cursor == Cursors.Hand) { StartPan(e.GetPosition((UIElement)theCanvas.Parent)); Mouse.Capture(theCanvas); } }
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); }