59using Schelling_Lattice
62void seed_population(Schelling_Lattice &lat, std::uint32_t rng_seed)
64 std::mt19937
rng(rng_seed);
65 std::discrete_distribution<int> pick({0.10, 0.45, 0.45});
66 for (
ca_size_t i = 0; i < lat.size(0); ++i)
67 for (
ca_size_t j = 0; j < lat.size(1); ++j)
68 lat.set({static_cast<ca_index_t>(i), static_cast<ca_index_t>(j)},
76 case Schelling_Cell::EMPTY:
return '.';
77 case Schelling_Cell::TYPE_A:
return 'A';
78 case Schelling_Cell::TYPE_B:
return 'B';
83void render(
const Schelling_Lattice &lat, std::ostream &os)
85 for (
ca_size_t i = 0; i < lat.size(0); ++i)
87 for (
ca_size_t j = 0; j < lat.size(1); ++j)
88 os << glyph_for(lat.at({static_cast<ca_index_t>(i),
89 static_cast<ca_index_t>(j)}));
97double same_type_edge_fraction(
const Schelling_Lattice &lat)
100 std::size_t total = 0;
106 const int c = lat.at({
static_cast<ca_index_t>(i),
108 if (c ==
static_cast<int>(Schelling_Cell::EMPTY))
115 = lat.at({
static_cast<ca_index_t>((i + 1) % n0),
118 if (right !=
static_cast<int>(Schelling_Cell::EMPTY))
121 if (right == c) ++same;
123 if (down !=
static_cast<int>(Schelling_Cell::EMPTY))
126 if (down == c) ++same;
129 return total == 0 ? 0.0
130 :
static_cast<double>(same) /
static_cast<double>(total);
133std::string
bar(
double v, std::size_t width = 40)
136 = std::min(width,
static_cast<std::size_t
>(v *
static_cast<double>(width)));
137 return std::string(
k,
'|') + std::string(width -
k,
' ');
140void run_scenario(
const Schelling_Lattice &
seed,
142 std::uint64_t master_seed,
143 std::size_t total_steps)
145 std::cout <<
"\n========================================\n";
146 std::cout <<
" Schelling threshold = "
147 << std::fixed << std::setprecision(2) << threshold
149 <<
" steps = " << total_steps
150 <<
" seed = 0x" << std::hex << master_seed << std::dec <<
'\n';
151 std::cout <<
"========================================\n";
158 const double q0 = same_type_edge_fraction(
engine.frame());
159 std::cout <<
" same-type edge fraction (initial) = "
160 << std::fixed << std::setprecision(3) << q0 << std::defaultfloat
161 <<
" [" <<
bar(q0) <<
"]\n";
165 const double q1 = same_type_edge_fraction(
engine.frame());
166 std::cout <<
" same-type edge fraction (final) = "
167 << std::fixed << std::setprecision(3) << q1 << std::defaultfloat
168 <<
" [" <<
bar(q1) <<
"]\n";
170 std::cout <<
"\nFinal configuration:\n";
171 render(
engine.frame(), std::cout);
180 constexpr std::uint64_t master_seed = 0x5C4E11Aull;
181 constexpr std::size_t total_steps = 80;
183 std::cout <<
"Schelling-style local segregation on a 48x48 toroidal lattice\n";
184 std::cout <<
" Moore radius 1, two types of agents + 10% vacancy\n";
185 std::cout <<
" same-type edge fraction is a cluster-quality proxy:\n";
186 std::cout <<
" ~0.50 = mixed ~1.00 = perfectly segregated\n";
189 seed_population(
seed, 0xACE0u);
191 std::cout <<
"\nInitial configuration (shared by every scenario):\n";
192 render(
seed, std::cout);
193 const double q_seed = same_type_edge_fraction(
seed);
194 std::cout <<
"same-type edge fraction = "
195 << std::fixed << std::setprecision(3) << q_seed << std::defaultfloat
196 <<
" [" <<
bar(q_seed) <<
"]\n";
198 for (
const double threshold : {0.30, 0.50, 0.70})
199 run_scenario(
seed, threshold, master_seed, total_steps);
201 std::cout <<
"\nNote: master_seed and the initial frame are shared across\n"
202 " threshold scenarios, so the trace is reproducible\n"
203 " bit-for-bit on any thread count.\n";
Reproducible random-number support for stochastic CA rules (Phase 8).
Common typedefs and tag types for the Cellular Automata module.
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.
Synchronous double-buffered engine.
static void bar(int val, int scale=1)
std::ptrdiff_t ca_index_t
Signed coordinate component used by lattices and neighborhoods.
Schelling_Cell
Discrete states of the Schelling automaton.
std::size_t ca_size_t
Unsigned size component used for extents and counts.
Main namespace for Aleph-w library functions.
The lattice wraps around on every axis.
Synchronous double-buffered engine for cellular automata.
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).