Skip to content

Commit ade26a8

Browse files
committed
Made Hebbian3 synapse model work. Temporarily disabled Hebbian2 Hebbian1, and binary.
Added Graph display of weight on hebbian synapse
1 parent 7685894 commit ade26a8

File tree

6 files changed

+148
-233
lines changed

6 files changed

+148
-233
lines changed

BrainSimulator/NeuronArrayView.xaml.cs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ public NeuronArrayView()
8383

8484
private Canvas targetNeuronCanvas = null;
8585
Canvas animationCanvas = null; //used to animate synapses
86+
Canvas synapseGraphCanvas = null; //used to animate synapses
8687

8788
//refresh the display of the neuron network
8889
public void Update()
@@ -95,6 +96,11 @@ public void Update()
9596
theCanvas.Children.Remove(animationCanvas);
9697
animationCanvas = null;
9798
}
99+
if (synapseGraphCanvas != null)
100+
{
101+
theCanvas.Children.Remove(synapseGraphCanvas);
102+
synapseGraphCanvas = null;
103+
}
98104
Canvas labelCanvas = new Canvas();
99105
Canvas.SetLeft(labelCanvas, 0);
100106
Canvas.SetTop(labelCanvas, 0);
@@ -479,25 +485,43 @@ public void UpdateNeuronColors()
479485
theCanvas.Children.Add(animationCanvas);
480486
}
481487
animationCanvas.Children.Clear();
488+
if (synapseGraphCanvas == null)
489+
{
490+
synapseGraphCanvas = new Canvas();
491+
Canvas.SetLeft(synapseGraphCanvas, 0);
492+
Canvas.SetTop(synapseGraphCanvas, 0);
493+
theCanvas.Children.Add(synapseGraphCanvas);
494+
}
495+
synapseGraphCanvas.Children.Clear();
482496

483497
for (int i = 0; i < neuronsOnScreen.Count; i++)
484498
{
485499
NeuronOnScreen a = neuronsOnScreen[i];
486500
Neuron n = MainWindow.theNeuronArray.GetNeuronForDrawing(a.neuronIndex);
487501

488-
if (MainWindow.thisWindow.checkBoxAnimate.IsChecked == true && n.lastCharge >= 1)
502+
if (MainWindow.thisWindow.checkBoxAnimate.IsChecked == true )
489503
{
490504
//synapse animation trial
491505
float electronSize = dp.NeuronDisplaySize * .2f;
492506
n.synapses = MainWindow.theNeuronArray.GetSynapsesList(n.id);
493507
Point pStart = dp.pointFromNeuron(n.id);
494508
pStart.X += -electronSize / 2 + dp.NeuronDisplaySize / 2;
495509
pStart.Y += -electronSize / 2 + dp.NeuronDisplaySize / 2;
510+
Vector vOffset = new Vector(0, electronSize/2);
496511
foreach (Synapse synapse in n.synapses)
497512
{
498513
Point pTarget = dp.pointFromNeuron(synapse.targetNeuron);
499514
pTarget.X += -electronSize / 2 + dp.NeuronDisplaySize / 2;
500515
pTarget.Y += -electronSize / 2 + dp.NeuronDisplaySize / 2;
516+
//put the little bar graph in the center of hebbian3 synapses
517+
if (synapse.model == Synapse.modelType.Hebbian3 && dp.NeuronDisplaySize > 75)
518+
{
519+
var graph = SynapseView.GetWeightBargraph(pStart+vOffset, pTarget+vOffset, synapse.weight);
520+
synapseGraphCanvas.Children.Add(graph);
521+
}
522+
if (n.lastCharge != 1) continue;
523+
524+
//animate charges along the axons
501525
var fill = Brushes.Yellow;
502526
if (synapse.weight < 0)
503527
fill = Brushes.DeepPink;

BrainSimulator/Synapse.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace BrainSimulator
1010
[StructLayout(LayoutKind.Sequential, Pack = 1)]
1111
public class Synapse
1212
{
13-
public enum modelType { Fixed, Binary, Hebbian1, Hebbian2 };
13+
public enum modelType { Fixed, Binary, Hebbian1, Hebbian2, Hebbian3 };
1414

1515
public int targetNeuron;
1616
public float weight;

BrainSimulator/SynapseView.cs

Lines changed: 79 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.Windows.Controls;
1010
using System.Windows.Input;
1111
using System.Windows.Media;
12+
using System.Windows.Media.Effects;
1213
using System.Windows.Shapes;
1314

1415
namespace BrainSimulator
@@ -48,6 +49,12 @@ public static Shape GetSynapseView(int i, Point p1, Synapse s, NeuronArrayView t
4849

4950
Shape l = GetSynapseShape(p1, p2, s.model);
5051
l.Stroke = new SolidColorBrush(Utils.RainbowColorFromValue(s.weight));
52+
//if (s.weight >= 0)
53+
// l.StrokeThickness *= 4 * s.weight;
54+
//l.StrokeThickness = Math.Clamp(l.StrokeThickness, 2, dp.NeuronDisplaySize / 4);
55+
l.StrokeEndLineCap = PenLineCap.Round;
56+
l.StrokeStartLineCap = PenLineCap.Round;
57+
5158
if (l is Ellipse E)
5259
{ }
5360
else
@@ -87,15 +94,85 @@ public static Shape GetSynapseShape(Point p1, Point p2, Synapse.modelType model)
8794
Canvas.SetLeft(s, p1.X + dp.NeuronDisplaySize / 2);
8895
}
8996
s.Stroke = Brushes.Red;
90-
s.StrokeThickness = 1;
97+
s.StrokeThickness = 4;
9198
if (dp.ShowSynapseWideLines())
9299
{
93-
s.StrokeThickness = Math.Min(4, dp.NeuronDisplaySize / 15);
100+
s.StrokeThickness = Math.Min(4, dp.NeuronDisplaySize / 8);
94101
}
95102

96103
return s;
97104
}
98105

106+
public static UIElement GetWeightBargraph(Point p1, Point p2, double value)
107+
{
108+
Canvas canvas = new Canvas();
109+
// Compute center point of the line
110+
double centerX = (p1.X + p2.X) / 2;
111+
double centerY = (p1.Y + p2.Y) / 2;
112+
113+
// Parameters for the rectangle (bar)
114+
double barWidth = dp.NeuronDisplaySize * .25; // fixed width
115+
double barMaxHeight = dp.NeuronDisplaySize*.5; // max height for full value
116+
// Draw rectangle (centered on line midpoint)
117+
Rectangle border = new Rectangle
118+
{
119+
Width = barWidth,
120+
Height = barMaxHeight,
121+
Fill = Brushes.White,
122+
Stroke = Brushes.Black,
123+
StrokeThickness = 1
124+
};
125+
border.Effect = new DropShadowEffect
126+
{
127+
Color = Colors.Black,
128+
Direction = 315, // angle of the shadow in degrees
129+
ShadowDepth = dp.NeuronDisplaySize * 0.05, // distance of the shadow
130+
Opacity = 0.5, // transparency of the shadow
131+
BlurRadius = 10 // softness of the shadow edge
132+
};
133+
Canvas.SetLeft(border, centerX - barWidth / 2);
134+
Canvas.SetTop(border, centerY - barMaxHeight / 2);
135+
canvas.Children.Add(border);
136+
137+
// Height scaled to value
138+
double barHeight = barMaxHeight * value;
139+
140+
// Draw rectangle (centered on line midpoint)
141+
Rectangle bar = new Rectangle
142+
{
143+
Width = barWidth,
144+
Height = barHeight,
145+
Fill = Brushes.SteelBlue,
146+
Stroke = Brushes.Black,
147+
StrokeThickness = 1
148+
};
149+
150+
// Position rectangle (centered horizontally, aligned vertically from center)
151+
Canvas.SetLeft(bar, centerX - barWidth / 2);
152+
Canvas.SetTop(bar, centerY - barMaxHeight / 2 + (barMaxHeight - barHeight));
153+
canvas.Children.Add(bar);
154+
155+
// Add value text
156+
TextBlock text = new TextBlock
157+
{
158+
Text = $"{value:0.000}",
159+
Foreground = Brushes.Black,
160+
FontSize = 14,
161+
FontWeight = FontWeights.Bold
162+
};
163+
164+
// Measure text size
165+
text.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity));
166+
Size textSize = text.DesiredSize;
167+
168+
// Position text centered on rectangle
169+
Canvas.SetLeft(text, centerX - textSize.Width / 2);
170+
Canvas.SetTop(text, centerY - textSize.Height / 2);
171+
canvas.Children.Add(text);
172+
return canvas;
173+
}
174+
175+
99176
public static Shape DrawLinkArrow(Point p1, Point p2, bool canLearn) //helper to put an arrow in a synapse line
100177
{
101178
GeometryGroup lineGroup = new GeometryGroup();

NeuronEngine/NeuronArrayBase.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,12 @@ namespace NeuronEngine
228228
unsigned long long bitMask = 0x1;
229229
bitMask = bitMask << offset;
230230
fireList1[index] |= bitMask;
231+
//the following will fix a race condition but requires retyping fireList to
232+
// #include <atomic>
233+
// //std::vector<std::atomic<unsigned long long>> fireList1(size);
234+
235+
// std::atomic<unsigned long long> fireList1[size];
236+
//fireList1[index].fetch_or(bitMask, std::memory_order_relaxed);
231237
}
232238
void NeuronArrayBase::ClearFireLists()
233239
{
@@ -297,7 +303,7 @@ namespace NeuronEngine
297303
for (int i = 0; i < arraySize; i++)
298304
{
299305
NeuronBase* theNeuron = GetNeuron(i);
300-
theNeuron->Fire3();
306+
theNeuron->Fire3(cycle);
301307
}
302308
}
303309
}

0 commit comments

Comments
 (0)