56void seed_grid(Grid &g, std::uint32_t seed_value)
58 std::mt19937
rng(seed_value);
59 std::discrete_distribution<int> pick({0.20, 0.40, 0.40});
62 g.set({static_cast<ca_index_t>(i), static_cast<ca_index_t>(j)},
67double segregation_index(
const Grid &g)
70 std::size_t agents = 0;
74 const auto self = g.at({
static_cast<ca_index_t>(i),
79 std::size_t other = 0;
83 if (di == 0
and dj == 0)
85 const auto v = g.at_safe({
static_cast<ca_index_t>(i) + di,
94 const std::size_t total = same + other;
97 accum +=
static_cast<double>(same) /
static_cast<double>(total);
101 return agents == 0 ? 0.0 : accum /
static_cast<double>(agents);
105void render(
const Grid &g, std::ostream &os)
107 for (
ca_size_t i = 0; i < g.size(0); ++i)
109 for (
ca_size_t j = 0; j < g.size(1); ++j)
111 const auto v = g.at({
static_cast<ca_index_t>(i),
113 os << (v == 0 ?
" " : v == 1 ?
"AA" :
"BB");
123 std::size_t
steps = 30;
125 std::uint32_t seed_value = 0xBADC0DEu;
126 if (argc >= 2)
steps =
static_cast<std::size_t
>(std::stoul(argv[1]));
127 if (argc >= 3) side =
static_cast<ca_size_t>(std::stoul(argv[2]));
128 if (argc >= 4) seed_value =
static_cast<std::uint32_t
>(std::stoul(argv[3]));
130 Grid initial({side, side}, 0);
131 seed_grid(initial, seed_value);
133 std::cout <<
"Schelling segregation: same rule, three schemes\n";
134 std::cout <<
" side = " << side <<
", steps = " <<
steps
135 <<
", seed = 0x" << std::hex << seed_value << std::dec <<
"\n\n";
136 std::cout <<
"Initial frame (segregation index "
137 << segregation_index(initial) <<
"):\n";
138 render(initial, std::cout);
143 sync(initial, Schelling{0.5, 1.0, 1.0, 1u},
Moore<2, 1>{});
145 std::cout <<
"Synchronous_Update (double-buffer) — segregation "
146 << segregation_index(sync.frame()) <<
":\n";
147 render(sync.frame(), std::cout);
152 seq(initial, Schelling{0.5, 1.0, 1.0, 2u},
Moore<2, 1>{});
154 std::cout <<
"Sequential_Update (in-place) — segregation "
155 << segregation_index(seq.frame()) <<
":\n";
156 render(seq.frame(), std::cout);
162 async(std::move(initial), Schelling{0.5, 1.0, 1.0, 3u},
Moore<2, 1>{},
164 static_cast<std::size_t
>(side * side)});
166 std::cout <<
"Random_Asynchronous_Update — segregation "
167 << segregation_index(async.frame()) <<
":\n";
168 render(async.frame(), std::cout);
172 <<
"Compare segregation indices: typically the asynchronous schemes\n"
173 <<
"converge more smoothly to higher segregation than synchronous\n"
174 <<
"ones, because synchronous updates can swap occupants in lockstep\n"
175 <<
"and oscillate.\n";
Reproducible random-number support for stochastic CA rules (Phase 8).
Common typedefs and tag types for the Cellular Automata module.
Update-scheme aware engine.
Lattice that adds boundary-aware access on top of a storage.
Moore (Chebyshev) neighborhood of radius R in N dimensions.
Local approximation of the Schelling segregation model.
std::ptrdiff_t ca_index_t
Signed coordinate component used by lattices and neighborhoods.
std::size_t ca_size_t
Unsigned size component used for extents and counts.
Main namespace for Aleph-w library functions.
and
Check uniqueness with explicit hash + equality functors.
Pick one cell at random per sub-step, update it in place.
In-place sequential update (no double buffer).
Classical double-buffer synchronous update.
The lattice wraps around on every axis.
Phase 13 update-scheme aware engine.
Cellular automata lattice with pluggable boundary policies.
Neighborhoods catalogue for Aleph::CA.
Reproducible stochastic CA rules (Phase 8).
Dense, contiguous storage for cellular automata cells (1D/2D/3D).
Phase 13 update-scheme strategies for Aleph::CA.