107template <
typename O,
typename Lattice>
108concept ObserverLike =
requires(O &o, std::size_t s,
const Lattice &f) {
109 o.on_step_begin(s, f);
127template <
typename... Observers>
156 template <std::
size_t I>
157 [[nodiscard]]
const auto &
get() const noexcept
159 return std::get<I>(
obs_);
167 template <std::
size_t I>
168 [[nodiscard]]
auto &
get() noexcept
170 return std::get<I>(
obs_);
177 [[nodiscard]]
static constexpr std::size_t
size() noexcept
179 return sizeof...(Observers);
191 template <
typename Lattice>
194 std::apply([&](
auto &...o)
196 (o.on_step_begin(step, frame), ...);
209 template <
typename Lattice>
212 std::apply([&](
auto &...o)
214 (o.on_step_end(step, frame), ...);
233template <
typename... Observers>
261template <
typename Engine,
typename Observer>
264 using Lat =
typename Engine::lattice_type;
265 engine.on_pre_step([&observer](std::size_t step,
const Lat &frame)
267 observer.on_step_begin(step, frame);
269 engine.on_post_step([&observer](std::size_t step,
const Lat &frame)
291template <
typename Engine,
typename Observer>
294 engine.on_pre_step([&observer](std::size_t step,
const auto &frame)
296 observer.on_step_begin(step, frame);
298 engine.on_post_step([&observer](std::size_t step,
const auto &frame)
327template <
typename State>
381 std::is_nothrow_swappable_v<State>)
426 <<
"Density_Observer: index " << i <<
" out of range";
434 [[nodiscard]] std::size_t
size() const noexcept
458 template <
typename Lattice>
475 template <
typename Lattice>
510template <
typename Lattice>
535 [[nodiscard]] std::size_t
size() const noexcept
560 prev_.emplace(frame);
571 if (
prev_.has_value())
600template <std::
size_t MaxState>
627 :
entropy_(std::move(other.entropy_)),
seeded_(std::exchange(other.seeded_,
false))
654 std::swap(
seeded_, other.seeded_);
670 [[nodiscard]] std::size_t
size() const noexcept
693 template <
typename Lattice>
709 template <
typename Lattice>
765 void push(
const std::uint64_t
h,
const std::size_t step)
831 [[nodiscard]] std::optional<std::size_t>
cycle_start() const noexcept
865 template <
typename Lattice>
887 template <
typename Lattice>
932template <
typename Lattice>
973 [[nodiscard]] std::size_t
size() const noexcept
Exception handling system with formatted messages for Aleph-w.
#define ah_out_of_range_error_if(C)
Throws std::out_of_range if condition holds.
Free metric helpers for cellular automata frames.
Common typedefs and tag types for the Cellular Automata module.
Simple dynamic array with automatic resizing and functional operations.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
void clear() noexcept
Empties the container.
constexpr bool is_empty() const noexcept
Checks if the container is empty.
void swap(Array &s) noexcept
Swap this with s
T & append(const T &data)
Append a copy of data
void reserve(size_t cap)
Reserves cap cells into the array.
Records the number of cells that changed across each step.
void on_step_begin(std::size_t step, const Lattice &frame)
Capture the frame before a step.
std::optional< Lattice > prev_
Activity_Observer()=default
Construct an empty activity observer.
const Array< ca_size_t > & activity() const noexcept
Return the activity time series.
void on_step_end(const std::size_t step, const Lattice &frame)
Record changed cells after a step.
Array< ca_size_t > activity_
void reset() noexcept
Clear the previous frame and activity samples.
std::size_t size() const noexcept
Return the number of recorded steps.
Variadic adaptor that fan-outs notifications to many observers.
std::tuple< Observers... > obs_
static constexpr std::size_t size() noexcept
Return the number of child observers.
void on_step_end(std::size_t step, const Lattice &frame)
Forward a step-end event to every child observer.
Composite_Observer(Observers... observers)
Build the composite from child observers.
auto & get() noexcept
Return mutable access to a child observer.
const auto & get() const noexcept
Return read-only access to a child observer.
void on_step_begin(std::size_t step, const Lattice &frame)
Forward a step-begin event to every child observer.
Records the count of cells in tracked_state per step.
Density_Observer & operator=(Density_Observer &&other) noexcept(std::is_nothrow_swappable_v< State >)
Exchange this observer with another, which is left with the previous samples of this one.
Density_Observer(State s)
Construct an observer tracking a specific state.
Density_Observer(Density_Observer &&other) noexcept(std::is_nothrow_copy_constructible_v< State >)
Take the samples of another observer, which is left with none, tracking the same state.
Density_Observer(const Density_Observer &other)=default
Copy another observer: the state that it counts, the counts of each step, their totals,...
Array< ca_size_t > counts_
double density_at(const std::size_t i) const
Return density at a recorded sample.
void swap(Density_Observer &other) noexcept(std::is_nothrow_swappable_v< State >)
Exchange in O(1) the tracked state and the samples of this observer and another.
const Array< ca_size_t > & counts() const noexcept
Return the count time series.
const Array< ca_size_t > & totals() const noexcept
Return the total-cell time series.
Density_Observer()=default
Construct an observer that tracks State{}.
Density_Observer & operator=(const Density_Observer &other)=default
Copy another observer into this one: the state that it counts, the counts of each step,...
Array< ca_size_t > totals_
void reset() noexcept
Clear all recorded samples.
std::size_t size() const noexcept
Return the number of recorded samples.
void on_step_begin(const std::size_t step, const Lattice &frame)
Record the initial frame before step zero.
void on_step_end(const std::size_t step, const Lattice &frame)
Record the frame after a completed step.
Records the Shannon entropy of the state distribution.
Entropy_Observer(Entropy_Observer &&other) noexcept
Take the samples of another observer, which is left with none.
std::size_t size() const noexcept
Return the number of entropy samples.
void on_step_end(const std::size_t step, const Lattice &frame)
Record entropy after a completed step.
void swap(Entropy_Observer &other) noexcept
Exchange in O(1) the samples of this observer and another.
void on_step_begin(const std::size_t step, const Lattice &frame)
Record entropy of the initial frame.
Entropy_Observer()=default
Construct an empty entropy observer.
Entropy_Observer & operator=(const Entropy_Observer &other)=default
Copy another observer into this one: the entropy of each step, and whether it has seen a frame.
Entropy_Observer(const Entropy_Observer &other)=default
Copy another observer: the entropy of each step, and whether it has seen a frame.
Entropy_Observer & operator=(Entropy_Observer &&other) noexcept
Exchange this observer with another, which is left with the previous samples of this one.
void reset() noexcept
Clear all entropy samples.
const Array< double > & entropy() const noexcept
Return the entropy time series.
Lattice that adds boundary-aware access on top of a storage.
Snapshots the lattice every period steps.
Sampling_Observer()=default
Construct an observer that snapshots every step.
std::size_t size() const noexcept
Return the number of stored snapshots.
const Array< std::size_t > & step_indices() const noexcept
Return step indices for collected snapshots.
Array< Lattice > snapshots_
Array< std::size_t > step_indices_
const Array< Lattice > & snapshots() const noexcept
Return collected snapshots.
void reset() noexcept
Clear all snapshots and step indices.
void on_step_begin(const std::size_t step, const Lattice &frame)
Capture the initial frame when sampling is enabled.
void on_step_end(const std::size_t step, const Lattice &frame)
Capture the completed frame at period boundaries.
Sampling_Observer(const std::size_t period)
Construct an observer with an explicit sampling period.
Detects fixed points and short cycles via frame hashing.
std::optional< std::size_t > cycle_start_
std::size_t buffered_hashes() const noexcept
Return the number of buffered hashes.
Stationary_Detector()=default
Construct a detector with the default 16-frame window.
void reset() noexcept
Clear buffered hashes and detected-cycle state.
std::optional< std::size_t > cycle_start() const noexcept
Return the cycle start step.
Array< std::size_t > steps_
Array< std::uint64_t > hashes_
bool cycle_detected() const noexcept
Return whether a cycle has been detected.
bool spans_a_full_window_at_most(const std::size_t length) const noexcept
std::optional< std::size_t > cycle_length() const noexcept
Return the detected cycle length.
std::size_t max_cycle_length_
std::optional< std::size_t > cycle_length_
void on_step_end(std::size_t step, const Lattice &frame)
Check the completed frame against the recent hash window.
void on_step_begin(const std::size_t step, const Lattice &frame)
Seed the detector with the initial frame hash.
void push(const std::uint64_t h, const std::size_t step)
Stationary_Detector(std::size_t max_cycle_length)
Construct a detector with an explicit window size.
Concept satisfied by any cellular-automaton observer.
void attach_observer(Engine &engine, Observer &observer)
Plug an observer into an engine's pre/post-step hooks.
double shannon_entropy(const Lattice &lat, std::size_t max_state)
Shannon entropy in nats of the [0, max_state) distribution.
void attach_observer_graph(Engine &engine, Observer &observer)
Attach an observer to engines without a lattice_type alias.
ca_size_t total_cells(const Lattice &lat) noexcept
std::uint64_t frame_hash(const Lattice &lat)
Deterministic 64-bit FNV-1a hash of the lattice cells.
ca_size_t count_state(const Lattice &lat, const typename Lattice::state_type &s)
auto make_composite_observer(Observers &&...observers)
Build a Composite_Observer with deduced ownership semantics.
ca_size_t cell_diff_count(const Lattice &a, const Lattice &b)
Main namespace for Aleph-w library functions.
and
Check uniqueness with explicit hash + equality functors.
void close_gap(T *ptr, size_t n, size_t pos, size_t num_entries=1)
Close a gap in an array by shifting elements left.
void on_step_end(std::size_t, const Lattice &)
Dynamic array container with automatic resizing.
C++20 concepts for the Cellular Automata module.