public void MutualSuppression() { MainWindow.theNeuronArray.SetUndoPoint(); MainWindow.thisWindow.SetProgress(0, "Allocating Synapses"); List <int> neuronsInSelection = theSelection.EnumSelectedNeurons(); for (int i = 0; i < neuronsInSelection.Count; i++) { if (MainWindow.thisWindow.SetProgress(100 * i / (float)neuronsInSelection.Count, "")) { break; } Neuron n = MainWindow.theNeuronArray.GetNeuron(neuronsInSelection[i]); foreach (Neuron n1 in theSelection.Neurons) { if (n.id != n1.id) { n.AddSynapseWithUndo(n1.id, -1, Synapse.modelType.Fixed); } } } MainWindow.thisWindow.SetProgress(100, ""); Update(); }
public void CreateRandomSynapses(int synapsesPerNeuron) { MainWindow.theNeuronArray.SetUndoPoint(); MainWindow.thisWindow.SetProgress(0, "Allocating Random Synapses"); List <int> neuronsInSelection = theSelection.EnumSelectedNeurons(); for (int i = 0; i < neuronsInSelection.Count; i++) { if (MainWindow.thisWindow.SetProgress(100f * i / (float)neuronsInSelection.Count, "")) { break; } Neuron n = MainWindow.theNeuronArray.GetNeuron(neuronsInSelection[i]); if (rand == null) { rand = new Random(); } for (int j = 0; j < synapsesPerNeuron; j++) { int targetNeuron = neuronsInSelection[rand.Next(neuronsInSelection.Count)]; float weight = (rand.Next(521) / 1000f) - .2605f; n.AddSynapseWithUndo(targetNeuron, weight, Synapse.modelType.Fixed); } } MainWindow.thisWindow.SetProgress(100, ""); Update(); }
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(); }
public void StepAndRepeat(int source, int target, float weight, Synapse.modelType model) { int distance = target - source; theSelection.EnumSelectedNeurons(); for (Neuron n = theSelection.GetSelectedNeuron(); n != null; n = theSelection.GetSelectedNeuron()) { n.AddSynapseWithUndo(theSelection.selectedNeuronIndex + distance, weight, model); } Update(); }
public void ConnectToHere() { if (targetNeuronIndex == -1) { return; } MainWindow.theNeuronArray.SetUndoPoint(); List <int> neuronsInSelection = theSelection.EnumSelectedNeurons(); for (int i = 0; i < neuronsInSelection.Count; i++) { Neuron n = MainWindow.theNeuronArray.GetNeuron(neuronsInSelection[i]); n.AddSynapseWithUndo(targetNeuronIndex, lastSynapseWeight, lastSynapseModel); } Update(); }
public void MutualSuppression() { MainWindow.theNeuronArray.SetUndoPoint(); List <int> neuronsInSelection = theSelection.EnumSelectedNeurons(); for (int i = 0; i < neuronsInSelection.Count; i++) { Neuron n = MainWindow.theNeuronArray.GetNeuron(neuronsInSelection[i]); foreach (Neuron n1 in theSelection.Neurons) { if (n.id != n1.id) { n.AddSynapseWithUndo(n1.id, -1, Synapse.modelType.Fixed); } } } Update(); }
private static void Mi_Click(object sender, RoutedEventArgs e) { //Handle delete & initialize commands if (sender is MenuItem mi) { if (mi.Header is StackPanel sp && sp.Children[0] is Label l && l.Content.ToString().StartsWith("Random")) { if (sp.Children[1] is TextBox tb0) { if (int.TryParse(tb0.Text, out int count)) { MainWindow.arrayView.CreateRandomSynapses(count); MainWindow.theNeuronArray.ShowSynapses = true; MainWindow.thisWindow.SetShowSynapsesCheckBox(true); MainWindow.Update(); } } return; } if ((string)mi.Header == "Cut") { MainWindow.arrayView.CutNeurons(); MainWindow.Update(); } if ((string)mi.Header == "Copy") { MainWindow.arrayView.CopyNeurons(); } if ((string)mi.Header == "Clear Selection") { MainWindow.arrayView.ClearSelection(); MainWindow.Update(); } if ((string)mi.Header == "Mutual Suppression") { MainWindow.arrayView.MutualSuppression(); MainWindow.theNeuronArray.ShowSynapses = true; MainWindow.thisWindow.SetShowSynapsesCheckBox(true); MainWindow.Update(); } if ((string)mi.Header == "Delete") { int i = (int)mi.Parent.GetValue(SelectionNumberProperty); MainWindow.arrayView.DeleteSelection(); } if ((string)mi.Header == "Reset Hebbian Weights") { MainWindow.theNeuronArray.SetUndoPoint(); foreach (SelectionRectangle 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.model != Synapse.modelType.Fixed) { //TODO: Add some UI for this: //s.model = Synapse.modelType.Hebbian2; n.AddSynapseWithUndo(s.targetNeuron, 0, s.model); s.Weight = 0; } } } } MainWindow.Update(); } } }
public void PasteNeurons() { NeuronArray myClipBoard = MainWindow.myClipBoard; if (targetNeuronIndex == -1) { return; } if (myClipBoard == null) { return; } //We are pasting neurons from the clipboard. //The arrays have different sizes so we may by row-col. //first check to see if the destination is claar and warn List <int> targetNeurons = new List <int>(); for (int i = 0; i < myClipBoard.arraySize; i++) { if (myClipBoard.GetNeuron(i, true) != null) { targetNeurons.Add(GetNeuronArrayId(i)); } } MainWindow.theNeuronArray.GetNeuronLocation(targetNeuronIndex, out int col, out int row); if (col + myClipBoard.Cols > MainWindow.theNeuronArray.Cols || row + myClipBoard.rows > MainWindow.theNeuronArray.rows) { MessageBoxResult result = MessageBox.Show("Paste would exceed neuron array boundary!", "Error", MessageBoxButton.OK); return; } if (!IsDestinationClear(targetNeurons, 0, true)) { MessageBoxResult result = MessageBox.Show("Some desination neurons are in use and will be overwritten, continue?", "Continue", MessageBoxButton.YesNo); if (result == MessageBoxResult.No) { return; } } MainWindow.theNeuronArray.SetUndoPoint(); //now paste the neurons for (int i = 0; i < myClipBoard.arraySize; i++) { if (myClipBoard.GetNeuron(i) != null) { int destID = GetNeuronArrayId(i); MainWindow.theNeuronArray.GetNeuron(destID).AddUndoInfo(); Neuron n = myClipBoard.GetCompleteNeuron(i, true); n.Owner = myClipBoard; n.synapses = myClipBoard.GetSynapsesList(i); Neuron sourceNeuron = n.Clone(); sourceNeuron.id = destID; while (sourceNeuron.label != "" && MainWindow.theNeuronArray.GetNeuron(sourceNeuron.label) != null) { int num = 0; int digitCount = 0; while (sourceNeuron.label != "" && Char.IsDigit(sourceNeuron.label[sourceNeuron.label.Length - 1])) { int.TryParse(sourceNeuron.label[sourceNeuron.label.Length - 1].ToString(), out int digit); num = num + (int)Math.Pow(10, digitCount) * digit; digitCount++; sourceNeuron.label = sourceNeuron.label.Substring(0, sourceNeuron.label.Length - 1); } num++; sourceNeuron.label = sourceNeuron.label + num.ToString(); } sourceNeuron.Owner = MainWindow.theNeuronArray; sourceNeuron.Label = sourceNeuron.label; MainWindow.theNeuronArray.SetNeuron(destID, sourceNeuron); foreach (Synapse s in n.Synapses) { MainWindow.theNeuronArray.GetNeuron(destID). AddSynapseWithUndo(GetNeuronArrayId(s.TargetNeuron), s.Weight, s.model); } } } //handle boundary synapses foreach (BoundarySynapse b in boundarySynapsesOut) { int sourceID = GetNeuronArrayId(b.innerNeuronID); Neuron targetNeuron = MainWindow.theNeuronArray.GetNeuron(b.outerNeuronID); if (targetNeuron != null) { MainWindow.theNeuronArray.GetNeuron(sourceID).AddSynapseWithUndo(targetNeuron.id, b.weight, b.model); } } foreach (BoundarySynapse b in boundarySynapsesIn) { int targetID = GetNeuronArrayId(b.innerNeuronID); Neuron sourceNeuron = MainWindow.theNeuronArray.GetNeuron(b.outerNeuronID); if (sourceNeuron != null) { sourceNeuron.AddSynapseWithUndo(targetID, b.weight, b.model); } } //paste modules foreach (ModuleView mv in myClipBoard.modules) { ModuleView newMV = new ModuleView() { FirstNeuron = GetNeuronArrayId(mv.FirstNeuron), TheModule = mv.TheModule, Color = mv.Color, Height = mv.Height, Width = mv.Width, Label = mv.Label, CommandLine = mv.CommandLine, }; MainWindow.theNeuronArray.modules.Add(newMV); } Update(); }
private static void Cm_Closed(object sender, RoutedEventArgs e) { if (sender is ContextMenu cm) { if (cmCancelled || (Keyboard.GetKeyStates(Key.Escape) & KeyStates.Down) > 0) { cm.IsOpen = false; MainWindow.Update(); return; } int sourceID = (int)cm.GetValue(SourceIDProperty); int targetID = (int)cm.GetValue(TargetIDProperty); Neuron nSource = MainWindow.theNeuronArray.GetNeuron(sourceID); Neuron nTarget = MainWindow.theNeuronArray.GetNeuron(targetID); int newSourceID = sourceID; int newTargetID = targetID; Control cc = Utils.FindByName(cm, "Target"); if (cc is TextBox tb) { string targetLabel = tb.Text; if (nTarget.label != targetLabel) { if (!int.TryParse(tb.Text, out newTargetID)) { newTargetID = targetID; Neuron n = MainWindow.theNeuronArray.GetNeuron(targetLabel); if (n != null) { newTargetID = n.id; } } } } cc = Utils.FindByName(cm, "Source"); if (cc is TextBox tb1) { string sourceLabel = tb1.Text; if (nSource.label != sourceLabel) { if (!int.TryParse(tb1.Text, out newSourceID)) { newSourceID = sourceID; Neuron n = MainWindow.theNeuronArray.GetNeuron(sourceLabel); if (n != null) { newSourceID = n.id; } } } } if (newSourceID < 0 || newSourceID >= MainWindow.theNeuronArray.arraySize || newTargetID < 0 || newTargetID >= MainWindow.theNeuronArray.arraySize ) { MessageBox.Show("Neuron outside array boundary!"); return; } cc = Utils.FindByName(cm, "SynapseWeight"); if (cc is ComboBox tb2) { float.TryParse(tb2.Text, out float newWeight); if (weightChanged) { theNeuronArrayView.lastSynapseWeight = newWeight; Utils.AddToValues(newWeight, synapseWeightValues); } } cc = Utils.FindByName(cm, "Model"); if (cc is ComboBox cb0) { ListBoxItem lbi = (ListBoxItem)cb0.SelectedItem; Synapse.modelType sm = (Synapse.modelType)System.Enum.Parse(typeof(Synapse.modelType), lbi.Content.ToString()); theNeuronArrayView.lastSynapseModel = sm; } if (newSourceID != sourceID || newTargetID != targetID) { MainWindow.theNeuronArray.SetUndoPoint(); MainWindow.theNeuronArray.GetNeuron((int)cm.GetValue(SourceIDProperty)).DeleteSynapseWithUndo((int)cm.GetValue(TargetIDProperty)); } Neuron nNewSource = MainWindow.theNeuronArray.GetNeuron(newSourceID); nNewSource.AddSynapseWithUndo(newTargetID, theNeuronArrayView.lastSynapseWeight, theNeuronArrayView.lastSynapseModel); cm.IsOpen = false; MainWindow.Update(); } }
private static void Mi_Click(object sender, RoutedEventArgs e) { //Handle delete & initialize commands if (sender is MenuItem mi) { if (mi.Header is StackPanel sp && sp.Children[0] is Label l && l.Content.ToString().StartsWith("Random")) { if (sp.Children[1] is TextBox tb0) { if (int.TryParse(tb0.Text, out int count)) { MainWindow.arrayView.CreateRandomSynapses(count); MainWindow.theNeuronArray.ShowSynapses = true; MainWindow.thisWindow.SetShowSynapsesCheckBox(true); MainWindow.Update(); } } return; } if ((string)mi.Header == "Cut") { MainWindow.arrayView.CutNeurons(); MainWindow.Update(); } if ((string)mi.Header == "Copy") { MainWindow.arrayView.CopyNeurons(); } if ((string)mi.Header == "Clear Selection") { MainWindow.arrayView.ClearSelection(); MainWindow.Update(); } if ((string)mi.Header == "Mutual Suppression") { MainWindow.arrayView.MutualSuppression(); MainWindow.theNeuronArray.ShowSynapses = true; MainWindow.thisWindow.SetShowSynapsesCheckBox(true); MainWindow.Update(); } if ((string)mi.Header == "Delete") { int i = (int)mi.Parent.GetValue(AreaNumberProperty); if (i < 0) { MainWindow.arrayView.DeleteSelection(); } else { MainWindow.theNeuronArray.SetUndoPoint(); MainWindow.theNeuronArray.AddModuleUndo(-1, MainWindow.theNeuronArray.modules[i]); DeleteModule(i); deleted = true; } } if ((string)mi.Header == "Initialize") { int i = (int)mi.Parent.GetValue(AreaNumberProperty); if (i < 0) { } else { { try { MainWindow.theNeuronArray.Modules[i].TheModule.Initialize(); } catch (Exception e1) { MessageBox.Show("Initialize failed on module " + MainWindow.theNeuronArray.Modules[i].Label + ". Message: " + e1.Message); } } } } 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") { MainWindow.theNeuronArray.SetUndoPoint(); foreach (SelectionRectangle 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.model != Synapse.modelType.Fixed) { //TODO: Add some UI for this: //s.model = Synapse.modelType.Hebbian2; n.AddSynapseWithUndo(s.targetNeuron, 0, s.model); s.Weight = 0; } } } } MainWindow.Update(); } } }
private static void Mi_Click(object sender, RoutedEventArgs e) { //Handle delete & initialize commands if (sender is MenuItem mi) { if ((string)mi.Header == "Cut") { MainWindow.arrayView.CutNeurons(); MainWindow.Update(); } if ((string)mi.Header == "Copy") { MainWindow.arrayView.CopyNeurons(); } if ((string)mi.Header == "Clear Selection") { MainWindow.arrayView.ClearSelection(); MainWindow.Update(); } if ((string)mi.Header == "Mutual Suppression") { MainWindow.arrayView.MutualSuppression(); MainWindow.Update(); } if ((string)mi.Header == "Delete") { int i = (int)mi.Parent.GetValue(AreaNumberProperty); if (i < 0) { MainWindow.arrayView.DeleteSelection(); } 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") { MainWindow.theNeuronArray.SetUndoPoint(); 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.model != Synapse.modelType.Fixed) { //TODO: Add some UI for this: //s.model = Synapse.modelType.Hebbian2; n.AddSynapseWithUndo(s.targetNeuron, 0, s.model); s.Weight = 0; } } } } MainWindow.Update(); } } }