68Tally
count(
const SIR_Lattice &lat)
71 for (
ca_size_t i = 0; i < lat.size(0); ++i)
72 for (
ca_size_t j = 0; j < lat.size(1); ++j)
74 const int v = lat.at({
static_cast<ca_index_t>(i),
76 if (v ==
static_cast<int>(SIR_Cell::S)) ++t.s;
77 else if (v ==
static_cast<int>(SIR_Cell::I)) ++t.i;
87 case SIR_Cell::S:
return '.';
88 case SIR_Cell::I:
return '*';
89 case SIR_Cell::R:
return '#';
94void render(
const SIR_Lattice &lat, std::ostream &os)
96 for (
ca_size_t i = 0; i < lat.size(0); ++i)
98 for (
ca_size_t j = 0; j < lat.size(1); ++j)
99 os << glyph_for(lat.at({static_cast<ca_index_t>(i),
100 static_cast<ca_index_t>(j)}));
105std::string
bar(std::size_t v, std::size_t total, std::size_t width = 40)
107 if (total == 0)
return std::string(width,
' ');
109 = std::min(width,
static_cast<std::size_t
>(
110 (
static_cast<double>(v) /
static_cast<double>(total))
111 *
static_cast<double>(width)));
112 return std::string(
k,
'|') + std::string(width -
k,
' ');
115void run_scenario(
const char *name,
double beta,
double gamma,
116 std::uint64_t master_seed,
117 std::size_t total_steps,
118 std::size_t snapshot_every)
124 const double R0 = beta * 8.0 /
gamma;
126 std::cout <<
"\n========================================\n";
127 std::cout <<
" Scenario: " << name <<
"\n";
128 std::cout <<
" size=" <<
rows <<
"x" <<
cols
129 <<
" beta=" << beta <<
" gamma=" <<
gamma
130 <<
" R0~" << std::fixed << std::setprecision(2) << R0
132 <<
" seed=0x" << std::hex << master_seed << std::dec <<
'\n';
133 std::cout <<
"========================================\n";
135 SIR_Lattice lat({
rows,
cols},
static_cast<int>(SIR_Cell::S));
136 lat.set({
rows / 2,
cols / 2},
static_cast<int>(SIR_Cell::I));
141 const std::size_t total =
rows *
cols;
142 std::size_t peak_I = 1;
143 std::size_t peak_step = 0;
145 std::cout <<
"\n--- step 0 (initial) ---\n";
146 render(
engine.frame(), std::cout);
148 std::cout <<
"S=" << t.s <<
" I=" << t.i <<
" R=" << t.r
149 <<
" [" <<
bar(t.i, total) <<
"]\n";
151 for (std::size_t step = 1; step <= total_steps; ++step)
155 if (t.i > peak_I) { peak_I = t.i; peak_step = step; }
156 if (step % snapshot_every == 0 or step == total_steps or t.i == 0)
158 std::cout <<
"\n--- step " << step <<
" ---\n";
159 render(
engine.frame(), std::cout);
160 std::cout <<
"S=" << t.s <<
" I=" << t.i <<
" R=" << t.r
161 <<
" [" <<
bar(t.i, total) <<
"]\n";
165 std::cout <<
"\n[epidemic extinct at step " << step <<
"]\n";
170 std::cout <<
"\nSummary: peak prevalence I=" << peak_I
171 <<
" at step " << peak_step
172 <<
" (final attack rate R="
173 << std::fixed << std::setprecision(3)
174 << (
static_cast<double>(t.r) /
static_cast<double>(total))
175 << std::defaultfloat <<
")\n";
182 std::cout <<
"SIR epidemic on a 2-D toroidal lattice (Moore radius 1)\n";
183 std::cout <<
"Glyphs: '.' susceptible '*' infected '#' recovered\n";
186 run_scenario(
"Subcritical (R0 < 1)",
188 0x5EEDA1Cull, 120, 30);
191 run_scenario(
"Supercritical (R0 > 1)",
193 0x5EEDA1Cull, 40, 8);
195 std::cout <<
"\nNote: master_seed is identical across runs, so the\n"
196 " trace is bit-for-bit reproducible regardless of\n"
197 " the build mode and the number of worker threads.\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.
SIR epidemic transition rule.
Synchronous double-buffered engine.
static void bar(int val, int scale=1)
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_gamma_function > > gamma(const __gmp_expr< T, U > &expr)
SIR_Cell
Discrete states of the SIR automaton.
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.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
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).