Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
ca_metrics_test.cc
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
45#include <cmath>
46#include <cstdint>
47#include <vector>
48
49#include <gtest/gtest.h>
50
51#include <ca-metrics.H>
52#include <ca-traits.H>
53#include <tpl_ca_concepts.H>
55#include <tpl_ca_lattice.H>
56#include <tpl_ca_storage.H>
57
58using namespace Aleph;
59using namespace Aleph::CA;
60
61namespace {
62
66
67} // namespace
68
70{
71 L2 lat({4, 4}, 0);
72 EXPECT_EQ(count_state(lat, 0), 16u);
73 EXPECT_EQ(count_state(lat, 1), 0u);
75
76 lat.set({1, 1}, 2);
77 lat.set({2, 2}, 1);
78 lat.set({3, 0}, 1);
79 EXPECT_EQ(count_state(lat, 0), 13u);
80 EXPECT_EQ(count_state(lat, 1), 2u);
81 EXPECT_EQ(count_state(lat, 2), 1u);
83}
84
86{
87 L2 empty({0, 0}, 0);
88 EXPECT_DOUBLE_EQ(density(empty, 0), 0.0);
90}
91
93{
94 L2 lat({3, 3}, 0);
95 lat.set({0, 0}, 1);
96 lat.set({1, 1}, 1);
97 lat.set({2, 2}, 1);
98 EXPECT_DOUBLE_EQ(density(lat, 1), 3.0 / 9.0);
100}
101
103{
104 L2 lat({4, 4}, 0);
105 for (ca_index_t i = 0; i < 4; ++i)
106 lat.set({i, i}, static_cast<int>(i % 3));
107 const auto h = state_histogram(lat, 4);
108 ASSERT_EQ(h.size(), 4u);
109 ca_size_t total = 0;
110 for (auto c : h)
111 total += c;
112 EXPECT_EQ(total, 16u);
113}
114
120
122{
123 // Half 0, half 1. Entropy should be ln(2) nats.
124 L2 lat({4, 4}, 0);
125 for (ca_index_t i = 0; i < 4; ++i)
126 for (ca_index_t j = 0; j < 4; ++j)
127 lat.set({i, j}, ((i + j) & 1) != 0 ? 1 : 0);
128 EXPECT_NEAR(shannon_entropy(lat, 2), std::log(2.0), 1e-12);
129}
130
132{
133 L2 a({3, 3}, 0);
134 L2 b({3, 3}, 0);
136
137 a.set({1, 1}, 1);
138 b.set({1, 1}, 1);
141}
142
144{
145 L2 a({3, 3}, 0);
146 L2 b({3, 3}, 0);
147 a.set({1, 1}, 1);
148 b.set({2, 2}, 1);
151}
152
154{
155 // Build the same frame twice from independent storage objects;
156 // the hash must coincide.
157 auto build = []() {
158 L2 lat({4, 4}, 0);
159 for (ca_index_t i = 0; i < 4; ++i)
160 for (ca_index_t j = 0; j < 4; ++j)
161 lat.set({i, j}, ((i * 4 + j) % 3 == 0) ? 1 : 0);
162 return lat;
163 };
164 EXPECT_EQ(frame_hash(build()), frame_hash(build()));
165}
166
168{
169 L2 a({4, 4}, 0);
170 L2 b({4, 4}, 0);
171 EXPECT_EQ(cell_diff_count(a, b), 0u);
172
173 a.set({0, 0}, 1);
174 a.set({1, 1}, 1);
175 a.set({2, 2}, 1);
176 EXPECT_EQ(cell_diff_count(a, b), 3u);
177
178 b.set({0, 0}, 1);
179 b.set({3, 3}, 1);
180 // a has alive at (0,0),(1,1),(2,2); b has alive at (0,0),(3,3).
181 // Differences: (1,1), (2,2), (3,3) → 3 diffs.
182 EXPECT_EQ(cell_diff_count(a, b), 3u);
183}
184
186{
187 L1 a({5}, 0);
188 for (ca_index_t i = 0; i < 5; ++i)
189 a.set({i}, static_cast<int>(i));
190 std::vector<int> seen;
191 for_each_cell(a, [&](int v) { seen.push_back(v); });
192 EXPECT_EQ(seen, (std::vector<int>{0, 1, 2, 3, 4}));
193
194 L3 b({2, 2, 2}, 0);
195 ca_size_t cnt = 0;
196 for_each_cell(b, [&](int) { ++cnt; });
197 EXPECT_EQ(cnt, 8u);
198}
199
201{
203 g.set_node(0, 1);
204 g.set_node(2, 1);
205 EXPECT_EQ(count_state(g, 1), 2u);
206 EXPECT_EQ(count_alive(g), 2u);
207 EXPECT_DOUBLE_EQ(density(g, 1), 0.4);
208
209 // Hash + equality.
211 g2.set_node(0, 1);
212 g2.set_node(2, 1);
215
216 g2.set_node(4, 1);
219}
long double h
Definition btreepic.C:154
Free metric helpers for cellular automata frames.
Common typedefs and tag types for the Cellular Automata module.
Graph lattice: one cell per node + precomputed adjacency.
void set_node(std::size_t n, const T &v)
Direct write at node id n.
Lattice that adds boundary-aware access on top of a storage.
#define TEST(name)
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
double shannon_entropy(const Lattice &lat, std::size_t max_state)
Shannon entropy in nats of the [0, max_state) distribution.
Definition ca-metrics.H:276
Array< ca_size_t > state_histogram(const Lattice &lat, std::size_t max_state)
Count occurrences of every integer state in [0, max_state).
Definition ca-metrics.H:241
std::ptrdiff_t ca_index_t
Signed coordinate component used by lattices and neighborhoods.
Definition ca-traits.H:60
std::uint64_t frame_hash(const Lattice &lat)
Deterministic 64-bit FNV-1a hash of the lattice cells.
Definition ca-metrics.H:311
void for_each_cell(const Lattice &lat, F &&f)
Visit every cell of a lattice in canonical row-major order.
Definition ca-metrics.H:133
double density(const Lattice &lat, const typename Lattice::state_type &s)
Definition ca-metrics.H:212
bool frames_equal(const Lattice &a, const Lattice &b)
Definition ca-metrics.H:323
Array< Array< std::size_t > > make_path_graph_adjacency(std::size_t n, bool cycle=false)
Build the adjacency of a path graph with n nodes.
ca_size_t count_state(const Lattice &lat, const typename Lattice::state_type &s)
Definition ca-metrics.H:175
ca_size_t count_alive(const Lattice &lat)
Definition ca-metrics.H:188
double alive_density(const Lattice &lat)
Definition ca-metrics.H:223
std::size_t ca_size_t
Unsigned size component used for extents and counts.
Definition ca-traits.H:63
ca_size_t cell_diff_count(const Lattice &a, const Lattice &b)
Definition ca-metrics.H:386
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
Out-of-range neighbours behave as if the lattice ended.
Definition ca-traits.H:119
C++20 concepts for the Cellular Automata module.
CA whose underlying topology is an arbitrary undirected graph.
Cellular automata lattice with pluggable boundary policies.
Dense, contiguous storage for cellular automata cells (1D/2D/3D).