Skip to content

Commit 163eb87

Browse files
Implemented FieldActivationFunction
1 parent 31f6aa3 commit 163eb87

File tree

7 files changed

+108
-0
lines changed

7 files changed

+108
-0
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ set(SOURCES
2222
src/fields/division.cpp
2323
src/fields/exponential_function.cpp
2424
src/fields/summation.cpp
25+
src/fields/field_activation_function.cpp
2526
src/fields/field_link_definition.cpp
2627
src/fields/obj.cpp
2728
src/fields/type.cpp
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#ifndef ACTIVATION_FUNCTION_H
2+
#define ACTIVATION_FUNCTION_H
3+
4+
class ActivationFunction {
5+
public:
6+
virtual ~ActivationFunction() = default;
7+
8+
virtual double f(double x) = 0;
9+
virtual double outerGrad(double x) = 0;
10+
};
11+
12+
#endif // ACTIVATION_FUNCTION_H
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#ifndef FIELD_ACTIVATION_FUNCTION_H
2+
#define FIELD_ACTIVATION_FUNCTION_H
3+
4+
#include "fields/abstract_function_definition.h"
5+
#include "fields/type.h"
6+
#include "fields/obj.h"
7+
#include "fields/field_link_definition.h"
8+
#include "fields/activation_function.h"
9+
10+
class FieldActivationFunction : public AbstractFunctionDefinition {
11+
public:
12+
FieldActivationFunction(Type* ref, const std::string& name, ActivationFunction* actFunction, double tolerance);
13+
14+
double computeUpdate(Obj* obj, FieldLinkDefinition* fl, double u) override;
15+
16+
private:
17+
ActivationFunction* actFunction;
18+
double tolerance;
19+
};
20+
21+
#endif // FIELD_ACTIVATION_FUNCTION_H

install_manifest.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@
1616
/Users/lukasmolzberger/CLionProjects/aika-cpp/.venv/lib/python3.12/site-packages/aika/include/fields/field_link_definition.h
1717
/Users/lukasmolzberger/CLionProjects/aika-cpp/.venv/lib/python3.12/site-packages/aika/include/fields/obj.h
1818
/Users/lukasmolzberger/CLionProjects/aika-cpp/.venv/lib/python3.12/site-packages/aika/include/fields/direction.h
19+
/Users/lukasmolzberger/CLionProjects/aika-cpp/.venv/lib/python3.12/site-packages/aika/include/fields/field_activation_function.h
1920
/Users/lukasmolzberger/CLionProjects/aika-cpp/.venv/lib/python3.12/site-packages/aika/include/fields/multiplication.h
2021
/Users/lukasmolzberger/CLionProjects/aika-cpp/.venv/lib/python3.12/site-packages/aika/include/fields/subtraction.h
2122
/Users/lukasmolzberger/CLionProjects/aika-cpp/.venv/lib/python3.12/site-packages/aika/include/fields/queue_provider.h
2223
/Users/lukasmolzberger/CLionProjects/aika-cpp/.venv/lib/python3.12/site-packages/aika/include/fields/abstract_function_definition.h
2324
/Users/lukasmolzberger/CLionProjects/aika-cpp/.venv/lib/python3.12/site-packages/aika/include/fields/type_registry.h
25+
/Users/lukasmolzberger/CLionProjects/aika-cpp/.venv/lib/python3.12/site-packages/aika/include/fields/activation_function.h
2426
/Users/lukasmolzberger/CLionProjects/aika-cpp/.venv/lib/python3.12/site-packages/aika/include/fields/queue_interceptor.h
2527
/Users/lukasmolzberger/CLionProjects/aika-cpp/.venv/lib/python3.12/site-packages/aika/include/fields/addition.h
2628
/Users/lukasmolzberger/CLionProjects/aika-cpp/.venv/lib/python3.12/site-packages/aika/include/fields/field_update.h
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import unittest
2+
import sys
3+
import os
4+
5+
# Add the project root to Python's module search path
6+
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
7+
8+
import aika
9+
10+
class FieldActivationFunctionTestCase(unittest.TestCase):
11+
12+
def testFieldActivationFunction(self):
13+
print("Module 'aika' was loaded from:", aika.__file__)
14+
15+
TEST_RELATION_FROM = aika.RelationOne(1, "TEST_FROM")
16+
TEST_RELATION_TO = aika.RelationOne(2, "TEST_TO")
17+
TEST_RELATION_TO.setReversed(TEST_RELATION_FROM)
18+
TEST_RELATION_FROM.setReversed(TEST_RELATION_TO)
19+
20+
registry = aika.TypeRegistry()
21+
22+
typeA = aika.TestType(registry, "A")
23+
typeB = aika.TestType(registry, "B")
24+
25+
a = typeA.inputField("a")
26+
b = typeA.inputField("b")
27+
28+
c = typeB.add("c")
29+
30+
c.input(TEST_RELATION_FROM, a, 0)
31+
c.input(TEST_RELATION_FROM, b, 1)
32+
33+
registry.flattenTypeHierarchy()
34+
35+
oa = typeA.instantiate()
36+
ob = typeB.instantiate()
37+
38+
aika.TestObj.linkObjects(oa, ob)
39+
ob.initFields()
40+
41+
oa.setFieldValue(a, 50.0)
42+
oa.setFieldValue(b, 20.0)
43+
44+
print("ob.getFieldValue(c):", ob.getFieldValue(c))
45+
46+
self.assertEqual(70.0, ob.getFieldValue(c))
47+
48+
if __name__ == '__main__':
49+
unittest.main()
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include "fields/field_activation_function.h"
2+
3+
FieldActivationFunction::FieldActivationFunction(Type* ref, const std::string& name, ActivationFunction* actFunction, double tolerance)
4+
: AbstractFunctionDefinition(ref, name, 1, tolerance), actFunction(actFunction) {}
5+
6+
7+
double FieldActivationFunction::computeUpdate(Obj* obj, FieldLinkDefinition* fl, double u) {
8+
double value = obj->getOrCreateFieldInput(this)->getValue();
9+
return actFunction->f(fl->getUpdatedInputValue(obj)) - value;
10+
}

src/fields/python_bindings.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "fields/division.h"
1717
#include "fields/exponential_function.h"
1818
#include "fields/summation.h"
19+
#include "fields/field_activation_function.h"
1920

2021

2122
// ----------------
@@ -68,6 +69,10 @@ PYBIND11_MODULE(aika, m)
6869
// Bind Summation (inherits from AbstractFunctionDefinition)
6970
py::class_<Summation, AbstractFunctionDefinition>(m, "Summation");
7071

72+
// Bind FieldActivationFunction (inherits from AbstractFunctionDefinition)
73+
py::class_<FieldActivationFunction, AbstractFunctionDefinition>(m, "FieldActivationFunction")
74+
.def(py::init<Type*, const std::string&, ActivationFunction*, double>());
75+
7176
py::class_<InputField, FieldDefinition>(m, "InputField")
7277
.def(py::init<Type*, const std::string &>())
7378
.def("__str__", [](const InputField &f) {
@@ -120,6 +125,14 @@ PYBIND11_MODULE(aika, m)
120125
const_cast<Type*>(&ref),
121126
name
122127
);
128+
}, py::return_value_policy::reference_internal)
129+
.def("fieldActivationFunc", [](const Type &ref, const std::string &name, ActivationFunction* actFunction, double tolerance) {
130+
return new FieldActivationFunction(
131+
const_cast<Type*>(&ref),
132+
name,
133+
actFunction,
134+
tolerance
135+
);
123136
}, py::return_value_policy::reference_internal);
124137

125138
py::class_<Obj>(m, "Obj")

0 commit comments

Comments
 (0)