Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
ca_observability_example.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
44#include <cstdint>
45#include <cstdio>
46#include <random>
47
48#include <ca-engine-utils.H>
49#include <ca-metrics.H>
50#include <ca-observer.H>
51#include <ca-traits.H>
52#include <tpl_ca_engine.H>
53#include <tpl_ca_lattice.H>
54#include <tpl_ca_neighborhood.H>
55#include <tpl_ca_rule.H>
56#include <tpl_ca_storage.H>
57
58using namespace Aleph;
59using namespace Aleph::CA;
60
61namespace {
62
65
66Lat make_random_seed(std::size_t n, std::uint32_t seed, double density)
67{
68 Lat lat({n, n}, 0);
69 std::mt19937 rng(seed);
70 std::bernoulli_distribution flip(density);
71 for (std::size_t i = 0; i < n; ++i)
72 for (std::size_t j = 0; j < n; ++j)
73 lat.set({static_cast<ca_index_t>(i), static_cast<ca_index_t>(j)},
74 flip(rng) ? 1 : 0);
75 return lat;
76}
77
78} // namespace
79
80int main()
81{
82 std::printf("Aleph::CA Phase-7 example: full observability pipeline\n");
83 std::printf("------------------------------------------------------\n");
84
85 // ---- Random GoL run ---------------------------------------------------
86 Lat random_seed = make_random_seed(64, /*seed=*/0xCAFEu, /*density=*/0.4);
87
93
97 comp(dens, act, ent, det, samp);
98
101 eng.run(200);
102
103 std::printf("\nRandom GoL 64x64, 200 steps\n");
104 std::printf(" density samples : %zu\n", dens.size());
105 std::printf(" activity samples : %zu\n", act.size());
106 std::printf(" entropy samples : %zu\n", ent.size());
107 std::printf(" snapshot count : %zu\n", samp.size());
108 std::printf(" density (initial) : %.4f\n", dens.density_at(0));
109 std::printf(" density (final) : %.4f\n",
110 dens.density_at(dens.size() - 1));
111
112 double avg_density = 0.0;
113 for (std::size_t i = dens.size() - 50; i < dens.size(); ++i)
114 avg_density += dens.density_at(i);
115 avg_density /= 50.0;
116 std::printf(" density (last-50 avg) : %.4f\n", avg_density);
117
118 if (act.size() > 0)
119 {
120 double avg_act = 0.0;
121 for (auto a : act.activity())
122 avg_act += static_cast<double>(a);
123 avg_act /= static_cast<double>(act.size());
124 std::printf(" activity (avg) : %.2f cells/step\n", avg_act);
125 }
126 if (ent.size() > 0)
127 std::printf(" entropy (final) : %.4f nats\n",
128 ent.entropy().get_last());
129 if (det.cycle_detected())
130 std::printf(" short cycle detected : length=%zu starting at step=%zu\n",
131 *det.cycle_length(), *det.cycle_start());
132 else
133 std::printf(" short cycle detected : none within window=%zu\n",
134 det.buffered_hashes());
135
136 // ---- Blinker reference run -------------------------------------------
137 Lat blinker({5, 5}, 0);
138 blinker.set({2, 1}, 1);
139 blinker.set({2, 2}, 1);
140 blinker.set({2, 3}, 1);
141
145 b_act, b_det);
146
149 b_eng.run(8);
150
151 std::printf("\nBlinker reference run (8 steps)\n");
152 std::printf(" activity per step :");
153 for (auto a : b_act.activity())
154 std::printf(" %zu", a);
155 std::putchar('\n');
156 std::printf(" cycle length : %zu\n",
157 b_det.cycle_detected() ? *b_det.cycle_length() : 0);
158
159 return 0;
160}
Convenience builders for the Phase 3 synchronous engine.
Free metric helpers for cellular automata frames.
Phase-7 observability layer for cellular automata engines.
Common typedefs and tag types for the Cellular Automata module.
Records the number of cells that changed across each step.
Variadic adaptor that fan-outs notifications to many observers.
Records the count of cells in tracked_state per step.
Records the Shannon entropy of the state distribution.
Lattice that adds boundary-aware access on top of a storage.
Moore (Chebyshev) neighborhood of radius R in N dimensions.
Snapshots the lattice every period steps.
Detects fixed points and short cycles via frame hashing.
Synchronous double-buffered engine.
static mt19937 rng
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
void attach_observer(Engine &engine, Observer &observer)
Plug an observer into an engine's pre/post-step hooks.
constexpr Game_Of_Life_Rule make_game_of_life_rule() noexcept
Build the canonical Game of Life rule.
double density(const Lattice &lat, const typename Lattice::state_type &s)
Definition ca-metrics.H:212
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
The lattice wraps around on every axis.
Definition ca-traits.H:124
ValueArg< size_t > seed
Definition testHash.C:53
Synchronous double-buffered engine for cellular automata.
Cellular automata lattice with pluggable boundary policies.
Neighborhoods catalogue for Aleph::CA.
Rule mechanisms for Aleph::CA.
Dense, contiguous storage for cellular automata cells (1D/2D/3D).