Dynamic Neural Field Composer 0.0.0
A C++20 library and interactive application for building and simulating Dynamic Neural Field (DNF) architectures.
Loading...
Searching...
No Matches
neural_field.h
Go to the documentation of this file.
1#pragma once
2
3#include "element.h"
6#include "elements/kernel.h"
7
9{
13 {
14 double tau;
16 std::unique_ptr<ActivationFunction> activationFunction;
17
19 {
20 if (this != &other)
21 {
22 tau = other.tau;
24 if (other.activationFunction)
26 else
27 activationFunction.reset();
28 }
29 return *this;
30 }
31
32 bool operator==(const NeuralFieldParameters& other) const
33 {
34 constexpr double epsilon = 1e-6;
35 return std::abs(tau - other.tau) < epsilon &&
36 std::abs(startingRestingLevel - other.startingRestingLevel) < epsilon &&
38 }
39
44
49 NeuralFieldParameters(double tau, double restingLevel,
51 : tau(tau), startingRestingLevel(restingLevel),
53 { }
54
56 {
57 tau = other.tau;
59 if (other.activationFunction == nullptr)
60 activationFunction = std::make_unique<SigmoidFunction>(0.0, 10.0);
61 else
63 }
64
65 [[nodiscard]] std::string toString() const override
66 {
67 std::ostringstream result;
68 result << "Parameters: ["
69 << "Tau: " << std::fixed << std::setprecision(2) << tau << ", "
70 << "Resting level: " << std::fixed << std::setprecision(2) << startingRestingLevel << ", "
71 << "Activation Function: " << (activationFunction ? activationFunction->toString() : "None")
72 << "]";
73 return result.str();
74 }
75
76 };
77
81 {
82 double centroid;
84 double endPosition;
85 double amplitude;
86 double width;
87 double previousCentroid = 0.0;
88 double velocity;
89 double acceleration;
90
91 explicit NeuralFieldBump(const double centroid = 0.0,
92 const double startPosition = 0.0,
93 const double endPosition = 0.0,
94 const double amplitude = 0.0,
95 const double width = 0.0,
96 const double previousCentroid = 0.0,
97 const double velocity = 0.0,
98 const double acceleration = 0.0)
103 width(width),
107 {}
108
109 [[nodiscard]] std::string toString() const
110 {
111 std::string str = "Bump: [";
112 str += "Centroid: " + std::format("{:.2f}", centroid) + ", ";
113 str += "Amplitude: " + std::format("{:.2f}", amplitude) + ", ";
114 str += "Width: " + std::format("{:.2f}", width) + ", ";
115 str += "Start pos.: " + std::format("{:.2f}", startPosition) + ", ";
116 str += "End pos.: " + std::format("{:.2f}", endPosition) + ", ";
117 str += "Velocity: " + std::format("{:.2f}", velocity) + ", ";
118 str += "Acceleration: " + std::format("{:.2f}", acceleration) + "]";
119 return str;
120 }
121
122 void print() const;
123
124 };
125
129 {
130 std::vector<NeuralFieldBump> bumps;
131 bool stable;
138
140 :bumps({}), stable(false), lowestActivation(0.0),
142 {}
143
144 [[nodiscard]] std::string toString() const
145 {
146 std::string str = "Neural field state [";
147 str += "Stable: " + std::string(stable ? "true" : "false") + ", ";
148 str += "Lowest act.: " + std::format("{:.2f}", lowestActivation) + ", ";
149 str += "Highest act.: " + std::format("{:.2f}", highestActivation) + ", ";
150 str += "Threshold: " + std::format("{:.2f}", thresholdForStability) + "]\n";
151 str += "Bumps: {";
152 for (const auto& bump : bumps)
153 str += bump.toString();
154 str += "}";
155
156 return str;
157 }
158
159 void print() const;
160
161 };
162
174 class NeuralField final : public Element
175 {
176 protected:
179 private:
180 // Cached raw pointers into component vectors — valid between init() calls, never resized during step().
181 double* act_ = nullptr;
182 double* inp_ = nullptr;
183 double* rest_ = nullptr;
184
185 bool computeStateMetrics_ = true;
186 std::vector<NeuralFieldBump> prevBumps_;
187 public:
191 NeuralField(const ElementCommonParameters& elementCommonParameters,
193
194 void init() override;
195 void step(double t, double deltaT) override;
196 std::string toString() const override;
197 std::shared_ptr<Element> clone() const override;
198
201 void setThresholdForStability(const double threshold) { state.thresholdForStability = threshold; }
202
205 bool isStable() const;
206
207 double getLowestActivation() const { return state.lowestActivation; }
209
211 std::vector<NeuralFieldBump> getBumps() const { return state.bumps; }
212
214 std::shared_ptr<Kernel> getSelfExcitationKernel() const;
215
217
223 void setComputeStateMetrics(bool enable) { computeStateMetrics_ = enable; }
224 bool getComputeStateMetrics() const { return computeStateMetrics_; }
225 protected:
226 void calculateActivation(double t, double deltaT);
227 void calculateOutput();
228 void updateState(double deltaT);
229 void updateBumps(double deltaT);
230 };
231}
Abstract base class for all simulation elements.
Definition element.h:28
Continuous attractor neural field — the core DFT building block.
Definition neural_field.h:175
void init() override
Initialize the element (called once before the simulation loop).
Definition neural_field.cpp:30
NeuralFieldState state
Runtime state (bumps, stability, min/max).
Definition neural_field.h:178
void step(double t, double deltaT) override
Advance the element by one time step.
Definition neural_field.cpp:44
std::shared_ptr< Kernel > getSelfExcitationKernel() const
Return the registered self-excitation kernel, if any.
Definition neural_field.cpp:71
NeuralFieldParameters parameters
Dynamics parameters (tau, h, activation function).
Definition neural_field.h:177
double getStabilityThreshold() const
Definition neural_field.h:216
double getHighestActivation() const
Definition neural_field.h:208
void updateState(double deltaT)
Definition neural_field.cpp:117
void setParameters(const NeuralFieldParameters &parameters)
Definition neural_field.cpp:53
double getLowestActivation() const
Definition neural_field.h:207
std::vector< NeuralFieldBump > getBumps() const
Return all currently detected above-threshold bumps.
Definition neural_field.h:211
void setThresholdForStability(const double threshold)
Set the stability convergence threshold.
Definition neural_field.h:201
bool getComputeStateMetrics() const
Definition neural_field.h:224
std::shared_ptr< Element > clone() const override
Definition neural_field.cpp:98
void updateBumps(double deltaT)
Definition neural_field.cpp:151
std::string toString() const override
Definition neural_field.cpp:89
void setComputeStateMetrics(bool enable)
Enable or disable per-step state-metric computation (stability, bumps, min/max). The default (enabled...
Definition neural_field.h:223
void calculateActivation(double t, double deltaT)
Definition neural_field.cpp:104
void calculateOutput()
Definition neural_field.cpp:112
bool isStable() const
Definition neural_field.cpp:64
NeuralFieldParameters getParameters() const
Definition neural_field.cpp:59
Definition element_parameters.h:10
Abstract base for activation functions applied to a neural field.
Definition activation_function.h:36
Definition element_parameters.h:188
Definition element_parameters.h:206
Describes a single activation bump (peak) in a neural field.
Definition neural_field.h:81
double amplitude
Peak activation value.
Definition neural_field.h:85
double startPosition
Left edge of the above-threshold region.
Definition neural_field.h:83
double endPosition
Right edge of the above-threshold region.
Definition neural_field.h:84
double velocity
Rate of change of the centroid (positions/step).
Definition neural_field.h:88
std::string toString() const
Definition neural_field.h:109
double centroid
Spatial position of the bump's centre of mass.
Definition neural_field.h:82
double width
Width of the above-threshold region.
Definition neural_field.h:86
NeuralFieldBump(const double centroid=0.0, const double startPosition=0.0, const double endPosition=0.0, const double amplitude=0.0, const double width=0.0, const double previousCentroid=0.0, const double velocity=0.0, const double acceleration=0.0)
Definition neural_field.h:91
void print() const
Definition neural_field_parameters.cpp:87
double acceleration
Rate of change of velocity (positions/step²).
Definition neural_field.h:89
double previousCentroid
Centroid at the previous time step.
Definition neural_field.h:87
Parameters that govern a NeuralField's dynamics.
Definition neural_field.h:13
std::unique_ptr< ActivationFunction > activationFunction
Nonlinearity applied to activation to produce output.
Definition neural_field.h:16
NeuralFieldParameters & operator=(const NeuralFieldParameters &other)
Definition neural_field.h:18
NeuralFieldParameters()
Default constructor: tau=25, restingLevel=-5, sigmoid(0, 10).
Definition neural_field.h:41
bool operator==(const NeuralFieldParameters &other) const
Definition neural_field.h:32
NeuralFieldParameters(const NeuralFieldParameters &other)
Definition neural_field.h:55
double tau
Time constant of the field's relaxation dynamics.
Definition neural_field.h:14
double startingRestingLevel
Homogeneous resting level (h); sub-threshold when negative.
Definition neural_field.h:15
std::string toString() const override
Definition neural_field.h:65
NeuralFieldParameters(double tau, double restingLevel, const ActivationFunction &activationFunction)
Construct with explicit tau, resting level, and activation function.
Definition neural_field.h:49
Snapshot of a neural field's observable state.
Definition neural_field.h:129
double highestActivation
Maximum activation across the field.
Definition neural_field.h:133
double previousActivationAvg
Activation average at the previous step — used to detect convergence.
Definition neural_field.h:136
double lowestActivation
Minimum activation across the field.
Definition neural_field.h:132
std::string toString() const
Definition neural_field.h:144
double previousActivationNorm
L2 norm of activation at the previous step — used to detect convergence.
Definition neural_field.h:137
double thresholdForStability
Convergence criterion (default 0.895).
Definition neural_field.h:134
void print() const
Definition neural_field_parameters.cpp:115
std::vector< NeuralFieldBump > bumps
Currently active above-threshold peaks.
Definition neural_field.h:130
bool stable
True when the activation change falls below the stability threshold.
Definition neural_field.h:131
NeuralFieldState()
Definition neural_field.h:139
double previousActivationSum
Activation sum at the previous step — used to detect convergence.
Definition neural_field.h:135