Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
ca_forest_fire_example.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
26#include <cstddef>
27#include <cstdint>
28#include <iostream>
29#include <random>
30
31#include <ca-rng.H>
32#include <ca-traits.H>
33#include <tpl_ca_engine.H>
34#include <tpl_ca_lattice.H>
35#include <tpl_ca_neighborhood.H>
36#include <tpl_ca_storage.H>
38
39using namespace Aleph;
40using namespace Aleph::CA;
41
42namespace
43{
44
46
48{
49 std::mt19937 rng(rng_seed);
50 std::discrete_distribution<int> pick({0.40, 0.55, 0.05});
51 for (ca_size_t i = 0; i < lat.size(0); ++i)
52 for (ca_size_t j = 0; j < lat.size(1); ++j)
53 lat.set({static_cast<ca_index_t>(i), static_cast<ca_index_t>(j)},
54 pick(rng));
55}
56
57char glyph_for(int v)
58{
59 switch (static_cast<Forest_Cell>(v))
60 {
61 case Forest_Cell::EMPTY: return '.';
62 case Forest_Cell::TREE: return 'T';
63 case Forest_Cell::BURNING: return '#';
64 }
65 return '?';
66}
67
68void render(const Forest_Lattice &lat, std::ostream &os)
69{
70 for (ca_size_t i = 0; i < lat.size(0); ++i)
71 {
72 for (ca_size_t j = 0; j < lat.size(1); ++j)
73 os << glyph_for(lat.at({static_cast<ca_index_t>(i),
74 static_cast<ca_index_t>(j)}));
75 os << '\n';
76 }
77}
78
79struct Tally
80{
81 std::size_t empty = 0;
82 std::size_t tree = 0;
83 std::size_t burning = 0;
84};
85
86Tally count(const Forest_Lattice &lat)
87{
88 Tally t;
89 for (ca_size_t i = 0; i < lat.size(0); ++i)
90 for (ca_size_t j = 0; j < lat.size(1); ++j)
91 {
92 const int v = lat.at({static_cast<ca_index_t>(i),
93 static_cast<ca_index_t>(j)});
94 if (v == static_cast<int>(Forest_Cell::EMPTY)) ++t.empty;
95 else if (v == static_cast<int>(Forest_Cell::TREE)) ++t.tree;
96 else ++t.burning;
97 }
98 return t;
99}
100
101} // namespace
102
103int main()
104{
105 constexpr ca_size_t rows = 32;
106 constexpr ca_size_t cols = 32;
107 constexpr std::uint64_t master_seed = 0xC0FFEE0042Aull;
108 constexpr std::size_t total_steps = 60;
109 constexpr std::size_t snapshot_every = 10;
110
111 std::cout << "Forest-fire reference example\n";
112 std::cout << " size=" << rows << "x" << cols
113 << " master_seed=0x" << std::hex << master_seed << std::dec
114 << " p_growth=0.02 p_lightning=0.001\n";
115
117 seed_initial_frame(lat, /*rng_seed=*/0xACE0u);
118 // Sprinkle two extra burning cells so the dynamics are immediately
119 // interesting.
120 lat.set({rows / 2, cols / 2}, static_cast<int>(Forest_Cell::BURNING));
121 lat.set({1, 1}, static_cast<int>(Forest_Cell::BURNING));
122
123 Forest_Fire_Rule<> rule(/*p_growth=*/0.02, /*p_lightning=*/0.001,
124 master_seed);
126 engine(lat, rule);
127
128 std::cout << "\n=== step 0 (initial) ===\n";
129 render(engine.frame(), std::cout);
130 Tally t = count(engine.frame());
131 std::cout << "tally: empty=" << t.empty << " trees=" << t.tree
132 << " burning=" << t.burning << '\n';
133
134 for (std::size_t step = 1; step <= total_steps; ++step)
135 {
136 engine.step();
137 if (step % snapshot_every == 0 or step == total_steps)
138 {
139 std::cout << "\n=== step " << step << " ===\n";
140 render(engine.frame(), std::cout);
141 t = count(engine.frame());
142 std::cout << "tally: empty=" << t.empty << " trees=" << t.tree
143 << " burning=" << t.burning << '\n';
144 }
145 }
146
147 std::cout << "\nDone after " << total_steps << " steps.\n";
148 return 0;
149}
size_t * rows
Definition ca-c-api.h:112
size_t cols
Definition ca-c-api.h:105
Reproducible random-number support for stochastic CA rules (Phase 8).
Common typedefs and tag types for the Cellular Automata module.
Forest-fire rule (Drossel & Schwabl, 1992).
Lattice that adds boundary-aware access on top of a storage.
Moore (Chebyshev) neighborhood of radius R in N dimensions.
Synchronous double-buffered engine.
static mt19937 rng
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
Forest_Cell
Discrete states of the forest-fire automaton.
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
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Definition ahAlgo.H:127
The lattice wraps around on every axis.
Definition ca-traits.H:124
static mt19937 engine
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).