Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
ca_reproductions_smoke_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
18#include <cstddef>
19#include <cstdint>
20#include <sstream>
21#include <vector>
22
23#include <gtest/gtest.h>
24
26#include <tpl_ca_hashlife.H>
28
29using namespace Aleph::CA;
30using namespace Aleph::CA::Reproductions;
31
33{
34 std::vector<std::size_t> samples;
35 for (std::size_t size = 2; size <= 64; ++size)
36 for (std::size_t copy = 0; copy < 20000 / (size * size); ++copy)
37 samples.push_back(size);
38
39 const Linear_Fit fit = fit_log_log_histogram(samples, 2, 64, 10);
40 const Linear_Fit weighted = fit_log_log_histogram(samples, 2, 64, 10, 1.0);
41 EXPECT_NEAR(fit.slope, -2.0, 0.25);
42 EXPECT_NEAR(weighted.slope, fit.slope - 1.0, 0.10);
43 EXPECT_GT(fit.r_squared, 0.95);
44}
45
47{
49 BTW_Sandpile sandpile(16, 0x425457u);
50 std::size_t nonzero = 0;
51 for (std::size_t i = 0; i < 2000; ++i)
52 if (sandpile.drop_random().size != 0)
53 ++nonzero;
54
55 EXPECT_TRUE(sandpile.stable());
56 EXPECT_GT(nonzero, 0u);
57}
58
60{
62 constexpr int a = static_cast<int>(Schelling_Cell::TYPE_A);
63 constexpr int b = static_cast<int>(Schelling_Cell::TYPE_B);
64 constexpr int empty = static_cast<int>(Schelling_Cell::EMPTY);
65 Grid frame({20, 20}, a);
66 for (ca_size_t row = 0; row < frame.size(0); ++row)
67 for (ca_size_t column = frame.size(1) / 2; column < frame.size(1); ++column)
68 frame.set({static_cast<ca_index_t>(row), static_cast<ca_index_t>(column)}, b);
69
70 EXPECT_GT(morans_i_binary(frame, empty, a, b), 0.7);
71}
72
74{
76 constexpr int tree = static_cast<int>(Forest_Cell::TREE);
77 constexpr int burning = static_cast<int>(Forest_Cell::BURNING);
78 constexpr int empty = static_cast<int>(Forest_Cell::EMPTY);
79 Grid frame({9, 9}, tree);
80 frame.set({4, 4}, burning);
82 engine(std::move(frame), Forest_Fire_Rule<>(0.0, 0.0, 0xD2055u));
83
84 engine.step();
85 std::size_t burning_count = 0;
86 for (ca_size_t row = 0; row < engine.frame().size(0); ++row)
87 for (ca_size_t column = 0; column < engine.frame().size(1); ++column)
88 if (engine.frame().at({static_cast<ca_index_t>(row),
89 static_cast<ca_index_t>(column)}) == burning)
91 EXPECT_EQ(engine.frame().at({4, 4}), empty);
93}
94
96{
97 const Gray_Scott_Lattice frame
98 = run_gray_scott(gray_scott_presets[2], 24, 40, 0x5eedu);
99 std::ostringstream lhs(std::ios::binary);
100 std::ostringstream rhs(std::ios::binary);
101 write_png(lhs, frame, gray_scott_rgb);
102 write_png(rhs, frame, gray_scott_rgb);
103 std::istringstream lhs_in(lhs.str(), std::ios::binary);
104 std::istringstream rhs_in(rhs.str(), std::ios::binary);
105
108 0.0);
109}
110
112{
114 for_each_gosper_gun_cell([&](const std::int64_t x, const std::int64_t y)
115 {
116 engine.set_alive(x, y);
117 });
118 const std::uint64_t initial = engine.population();
119 engine.run(120);
120 EXPECT_GT(engine.population(), initial);
121}
size_t row
Definition ca-c-api.h:115
Internal helpers shared by the cellular-automata reproductions.
Forest-fire rule (Drossel & Schwabl, 1992).
Hashlife engine for outer-totalistic binary cellular automata.
std::uint64_t population() const noexcept
Number of alive cells.
Lattice that adds boundary-aware access on top of a storage.
Moore (Chebyshev) neighborhood of radius R in N dimensions.
Deterministic open-boundary Bak-Tang-Wiesenfeld sandpile.
Synchronous double-buffered engine.
#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
static mpfr_t y
Definition mpfr_mul_d.c:3
RGB8 gray_scott_rgb(const Gray_Scott_Cell &cell)
Convert one Gray-Scott state to an RGB colour.
Linear_Fit fit_log_log_histogram(const std::vector< std::size_t > &samples, const std::size_t min_value, const std::size_t max_value, const std::size_t bins, const double sample_weight_power=0.0)
Fit a power-law exponent from logarithmically binned samples.
void for_each_gosper_gun_cell(F &&visitor, const std::int64_t offset_x=0, const std::int64_t offset_y=0)
Visit every live coordinate in the canonical Gosper gun.
constexpr std::array< Gray_Scott_Preset, 3 > gray_scott_presets
Canonical visual presets used by the weekly Gray-Scott reproduction.
Native_Png decode_native_png(std::istream &in)
Decode a PNG emitted by Aleph::CA::write_png.
Gray_Scott_Lattice run_gray_scott(const Gray_Scott_Preset &preset, const ca_size_t side, const std::size_t steps, const std::uint64_t master_seed)
Run one Gray-Scott preset from the shared deterministic seed.
double morans_i_binary(const Lattice &frame, const typename Lattice::state_type empty_state, const typename Lattice::state_type type_a, const typename Lattice::state_type type_b)
Compute Moran's I for two occupied Schelling cell types.
double mean_channel_difference(const Native_Png &lhs, const Native_Png &rhs)
Compute normalized mean absolute per-channel PNG difference.
std::ptrdiff_t ca_index_t
Signed coordinate component used by lattices and neighborhoods.
Definition ca-traits.H:60
std::size_t ca_size_t
Unsigned size component used for extents and counts.
Definition ca-traits.H:63
void write_png(std::ostream &out, const Lattice &frame, Mapper &&mapper)
Write a rank-2 frame as an 8-bit RGB PNG image.
Definition ca-png.H:171
size_t size(Node *root) noexcept
Itor2 copy(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg)
Copy elements from one range to another.
Definition ahAlgo.H:584
Least-squares result for a log-log histogram.
double r_squared
coefficient of determination.
static constexpr bool requires_double_buffer
Operates in place, no second buffer needed.
The lattice wraps around on every axis.
Definition ca-traits.H:124
static mt19937 engine
Hashlife engine for outer-totalistic binary cellular automata.
Reproducible stochastic CA rules (Phase 8).