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
field_coupling.h
Go to the documentation of this file.
1#pragma once
2
3#include <set>
4
5#include "tools/math.h"
6#include "element.h"
7#include "neural_field.h"
8#include "tools/utils.h"
9
10
11namespace dnf_composer
12{
15 enum class LearningRule : int
16 {
17 HEBB,
18 OJA,
19 DELTA
20 };
21
23 inline const std::map<LearningRule, std::string> LearningRuleToString = {
24 {LearningRule::HEBB, "Hebb"},
25 {LearningRule::OJA, "Oja"},
26 {LearningRule::DELTA, "Delta"}
27 };
28
29 namespace element
30 {
34 {
37 double scalar;
38 double learningRate;
40
53
54 bool operator==(const FieldCouplingParameters& other) const
55 {
56 constexpr double epsilon = 1e-6;
57
58 return std::abs(inputFieldDimensions.x_max - other.inputFieldDimensions.x_max) < epsilon &&
59 std::abs(inputFieldDimensions.d_x - other.inputFieldDimensions.d_x) < epsilon &&
60 learningRule == other.learningRule &&
61 std::abs(scalar - other.scalar) < epsilon &&
62 std::abs(learningRate - other.learningRate) < epsilon;
63 }
64
65 [[nodiscard]] std::string toString() const override
66 {
67 std::ostringstream result;
68 result << std::fixed << std::setprecision(2);
69 result << "Parameters: ["
70 << "Input field dimensions: " << inputFieldDimensions.toString() << ", "
71 << "Learning rule: " << LearningRuleToString.at(learningRule) << ", "
72 << "Learning rate: " << learningRate << ", "
73 << "Scalar: " << scalar
74 << "]";
75 return result.str();
76 }
77 };
78
90 class FieldCoupling final : public Element
91 {
92 protected:
94 std::shared_ptr<Element> input;
95 std::shared_ptr<Element> output;
96 std::string weightsDirectory;
97 public:
101 FieldCoupling(const ElementCommonParameters& elementCommonParameters,
102 const FieldCouplingParameters& fc_parameters);
103
104 void init() override;
105 void step(double t, double deltaT) override;
106 void addInput(const std::shared_ptr<Element>& inputElement,
107 const std::string& inputComponent = "output") override;
108 std::string toString() const override;
109 std::shared_ptr<Element> clone() const override;
110
114 void changeDimensions(const ElementDimensions& newDimensions) override;
115
119 void changeInputDimensions(const ElementDimensions& newInputDimensions);
120
121 void setLearningRate(double learningRate);
122
125 void setLearning(bool learning);
126
127 void setParameters(const FieldCouplingParameters& fcp);
128
130 void setWeightsDirectory(const std::string& dir);
131
133 std::string getWeightsDirectory() const;
134
136 void readWeights();
137
141 void tryReadWeights();
142
144 void writeWeights() const;
145
147 void clearWeights();
148 private:
149 void updateOutput();
150 void updateInputField();
151 void updateOutputField();
152 void updateWeights();
153 bool checkValidConnections();
154 };
155 }
156}
Abstract base class for all simulation elements.
Definition element.h:28
Full-matrix learned coupling between two neural fields.
Definition field_coupling.h:91
std::shared_ptr< Element > output
Definition field_coupling.h:95
void tryReadWeights()
Load weights if the file exists; log INFO in either case. Unlike readWeights(), this never logs an er...
Definition field_coupling.cpp:237
std::string toString() const override
Definition field_coupling.cpp:44
void changeDimensions(const ElementDimensions &newDimensions) override
Resize the output field dimensions and rebuild the weight matrix. Preserves input field dimensions an...
Definition field_coupling.cpp:58
std::shared_ptr< Element > input
Definition field_coupling.h:94
void init() override
Initialize the element (called once before the simulation loop).
Definition field_coupling.cpp:23
void clearWeights()
Reset the weight matrix to all zeros.
Definition field_coupling.cpp:283
FieldCouplingParameters parameters
Definition field_coupling.h:93
void setParameters(const FieldCouplingParameters &fcp)
Definition field_coupling.cpp:76
std::string weightsDirectory
Directory used for weight serialization.
Definition field_coupling.h:96
void setLearning(bool learning)
Enable or disable online weight updates.
Definition field_coupling.cpp:91
void changeInputDimensions(const ElementDimensions &newInputDimensions)
Resize the input field dimensions and rebuild the weight matrix. Preserves output field dimensions an...
Definition field_coupling.cpp:67
void addInput(const std::shared_ptr< Element > &inputElement, const std::string &inputComponent="output") override
Register inputElement as an upstream source for this element.
Definition field_coupling.cpp:120
void setLearningRate(double learningRate)
Definition field_coupling.cpp:86
void writeWeights() const
Save the current weight matrix to a binary file in weightsDirectory.
Definition field_coupling.cpp:252
FieldCouplingParameters getParameters() const
Definition field_coupling.cpp:96
std::string getWeightsDirectory() const
Definition field_coupling.cpp:101
void step(double t, double deltaT) override
Advance the element by one time step.
Definition field_coupling.cpp:35
std::shared_ptr< Element > clone() const override
Definition field_coupling.cpp:52
void readWeights()
Load the weight matrix from a binary file in weightsDirectory.
Definition field_coupling.cpp:193
void setWeightsDirectory(const std::string &dir)
Set the directory used for readWeights() / writeWeights().
Definition field_coupling.cpp:81
LearningRule
Selects the synaptic weight update rule used by FieldCoupling.
Definition field_coupling.h:16
@ OJA
Oja's rule: Hebbian with weight-decay for stability.
@ DELTA
Delta rule: error-driven weight updates.
@ HEBB
Classic Hebbian: Δw ∝ pre × post.
Definition application.h:20
const std::map< LearningRule, std::string > LearningRuleToString
Maps LearningRule values to human-readable strings.
Definition field_coupling.h:23
Definition element_parameters.h:188
Definition element_parameters.h:159
std::string toString() const
Definition element_parameters.cpp:55
int x_max
Definition element_parameters.h:161
double d_x
Definition element_parameters.h:162
Definition element_parameters.h:206
Parameters for a learned full-matrix field coupling.
Definition field_coupling.h:34
double learningRate
Learning rate η (step size for weight updates).
Definition field_coupling.h:38
bool isLearningActive
If true, weights are updated each step.
Definition field_coupling.h:39
ElementDimensions inputFieldDimensions
Spatial dimensions of the source (input) field.
Definition field_coupling.h:35
FieldCouplingParameters(const ElementDimensions &inputFieldDimensions=ElementDimensions{}, const LearningRule learningRule=LearningRule::HEBB, const double scalar=1.0, const double learningRate=0.01)
Construct FieldCoupling parameters.
Definition field_coupling.h:46
LearningRule learningRule
Which weight update rule to use.
Definition field_coupling.h:36
bool operator==(const FieldCouplingParameters &other) const
Definition field_coupling.h:54
std::string toString() const override
Definition field_coupling.h:65
double scalar
Scaling factor applied to the coupling output.
Definition field_coupling.h:37