File: utest-syncpr.cpp

package info (click to toggle)
python-pyclustering 0.10.1.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 11,128 kB
  • sloc: cpp: 38,888; python: 24,311; sh: 384; makefile: 105
file content (207 lines) | stat: -rwxr-xr-x 5,889 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*!

@authors Andrei Novikov (pyclustering@yandex.ru)
@date 2014-2020
@copyright BSD-3-Clause

*/


#include <gtest/gtest.h>

#include <pyclustering/nnet/syncpr.hpp>

#include <algorithm>


using namespace pyclustering::nnet;


static void template_syncpr_create_delete(const unsigned int size) {
    syncpr * network = new syncpr(size, 0.5, 0.5);
    ASSERT_EQ(size, network->size());

    delete network;
}

TEST(utest_syncpr, create_delete_10_oscillators) {
    template_syncpr_create_delete(10);
}

TEST(utest_syncpr, create_delete_50_oscillators) {
    template_syncpr_create_delete(50);
}

TEST(utest_syncpr, create_delete_100_oscillators) {
    template_syncpr_create_delete(100);
}

static void template_simulation_static(const unsigned int steps,
                                           const solve_type solver,
                                           const bool collect_flag) {
    syncpr network(5, 0.3, 0.3);
    syncpr_dynamic output_dynamic;
    syncpr_pattern pattern = { 1, 1, 1, -1, -1 };

    network.simulate_static(steps, 10, pattern, solver, collect_flag, output_dynamic);

    if (collect_flag) {
        ASSERT_TRUE(output_dynamic.size() > steps);
    }
    else {
        ASSERT_EQ(1U, output_dynamic.size());
    }
}

TEST(utest_syncpr, static_simulation_10_FAST) {
    template_simulation_static(10, solve_type::FORWARD_EULER, true);
}

TEST(utest_syncpr, static_simulation_100_FAST) {
    template_simulation_static(100, solve_type::FORWARD_EULER, true);
}

TEST(utest_syncpr, static_simulation_5_RK4) {
    template_simulation_static(5, solve_type::RUNGE_KUTTA_4, true);
}

TEST(utest_syncpr, static_simulation_6_RKF45) {
    template_simulation_static(6, solve_type::RUNGE_KUTTA_FEHLBERG_45, true);
}

TEST(utest_syncpr, static_simulation_FAST_no_collecting) {
    template_simulation_static(5, solve_type::FORWARD_EULER, false);
}

TEST(utest_syncpr, static_simulation_RK4_no_collecting) {
    template_simulation_static(5, solve_type::RUNGE_KUTTA_4, false);
}

TEST(utest_syncpr, static_simulation_RKF45_no_collecting) {
    template_simulation_static(5, solve_type::RUNGE_KUTTA_FEHLBERG_45, false);
}


static void template_simulation_dynamic(const solve_type solver,
                                        const bool collect_flag) {
    syncpr network(5, 0.3, 0.3);
    syncpr_dynamic output_dynamic;
    syncpr_pattern pattern = { 1, 1, 1, -1, -1 };

    network.simulate_dynamic(pattern, 0.95, 1, solver, collect_flag, output_dynamic);

    if (collect_flag) {
        ASSERT_TRUE(output_dynamic.size() > 1);
    }
    else {
        ASSERT_EQ(1U, output_dynamic.size());
    }
}

TEST(utest_syncpr, dynamic_simulation_FAST) {
    template_simulation_dynamic(solve_type::FORWARD_EULER, true);
}

TEST(utest_syncpr, dynamic_simulation_RK4) {
    template_simulation_dynamic(solve_type::RUNGE_KUTTA_4, true);
}

#ifndef VALGRIND_ANALYSIS_SHOCK

TEST(utest_syncpr, dynamic_simulation_RKF45) {
    template_simulation_dynamic(solve_type::RUNGE_KUTTA_FEHLBERG_45, true);
}

#endif

TEST(utest_syncpr, dynamic_simulation_FAST_no_collecting) {
    template_simulation_dynamic(solve_type::FORWARD_EULER, false);
}

TEST(utest_syncpr, dynamic_simulation_RK4_no_collecting) {
    template_simulation_dynamic(solve_type::RUNGE_KUTTA_4, false);
}

#ifndef VALGRIND_ANALYSIS_SHOCK

TEST(utest_syncpr, dynamic_simulation_RKF45_no_collecting) {
    template_simulation_dynamic(solve_type::RUNGE_KUTTA_FEHLBERG_45, false);
}

#endif

TEST(utest_syncpr, train_and_recognize_pattern) {
    syncpr network(10, 0.1, 0.1);
    syncpr_dynamic output_dynamic;

    std::vector<syncpr_pattern> patterns = {
        {  1,  1,  1,  1,  1, -1, -1, -1, -1, -1 },
        { -1, -1, -1, -1, -1,  1,  1,  1,  1,  1 }
    };

    double memory_order1 = network.memory_order(patterns[0]);
    double memory_order2 = network.memory_order(patterns[1]);

    ASSERT_TRUE(memory_order1 < 0.9);
    ASSERT_TRUE(memory_order2 < 0.9);

    network.train(patterns);

    /* recognize it */
    for (size_t i = 0; i < patterns.size(); i++) {
        syncpr_dynamic output_dynamic;
        network.simulate_static(20, 10, patterns[i], solve_type::RUNGE_KUTTA_4, true, output_dynamic);

        double memory_order = network.memory_order(patterns[i]);
        ASSERT_TRUE(memory_order > 0.995);

        ensemble_data<sync_ensemble> sync_ensembles;
        output_dynamic.allocate_sync_ensembles(0.1, sync_ensembles);

        ASSERT_EQ(2U, sync_ensembles.size());

        for (sync_ensemble & ensemble : sync_ensembles) {
            std::sort(ensemble.begin(), ensemble.end());
        }

        sync_ensemble expected_ensemble1 = { 0, 1, 2, 3, 4 };
        sync_ensemble expected_ensemble2 = { 5, 6, 7, 8, 9 };

        ASSERT_TRUE( (expected_ensemble1 == sync_ensembles[0]) || (expected_ensemble2 == sync_ensembles[0]) );
        ASSERT_TRUE( (expected_ensemble1 == sync_ensembles[1]) || (expected_ensemble2 == sync_ensembles[1]));
    }
}

TEST(utest_syncpr, sync_local_order) {
    syncpr network(10, 0.1, 0.1);

    double local_order = network.sync_local_order();
    ASSERT_TRUE((local_order > 0.0) && (local_order < 1.0));

    std::vector<syncpr_pattern> patterns = {
        { 1, 1, 1, 1, 1, -1, -1, -1, -1, -1 },
        { -1, -1, -1, -1, -1, 1, 1, 1, 1, 1 }
    };

    network.train(patterns);

    local_order = network.sync_local_order();
    ASSERT_TRUE((local_order > 0.0) && (local_order < 1.0));
}

TEST(utest_syncpr, sync_global_order) {
    syncpr network(10, 0.1, 0.1);

    double global_order = network.sync_order();
    ASSERT_TRUE((global_order > 0.0) && (global_order < 1.0));

    std::vector<syncpr_pattern> patterns = {
        { 1, 1, 1, 1, 1, -1, -1, -1, -1, -1 },
        { -1, -1, -1, -1, -1, 1, 1, 1, 1, 1 }
    };

    network.train(patterns);

    global_order = network.sync_order();
    ASSERT_TRUE((global_order > 0.0) && (global_order < 1.0));
}