Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
schelling_segregation.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 <filesystem>
21#include <fstream>
22#include <iomanip>
23#include <iostream>
24#include <random>
25
28
29using namespace Aleph::CA;
30using namespace Aleph::CA::Reproductions;
31
32#ifndef ALEPH_REPRODUCTIONS_SOURCE_DIR
33# define ALEPH_REPRODUCTIONS_SOURCE_DIR "reproductions"
34#endif
35
36namespace
37{
38
40
45void seed_population(Grid &grid, const std::uint32_t seed)
46{
47 std::mt19937 rng(seed);
48 std::discrete_distribution<int> pick({0.10, 0.45, 0.45});
49 for (ca_size_t r = 0; r < grid.size(0); ++r)
50 for (ca_size_t c = 0; c < grid.size(1); ++c)
51 grid.set({static_cast<ca_index_t>(r), static_cast<ca_index_t>(c)}, pick(rng));
52}
53
54} // namespace
55
56int main()
57{
58 constexpr ca_size_t side = 100;
59 constexpr std::size_t steps = 200;
60 constexpr double threshold = 0.5;
61 constexpr std::uint64_t master_seed = 0x5C4E111971ull;
62
63 Grid frame({side, side});
64 seed_population(frame, 0xACE0u);
65 const double initial = morans_i_binary(
66 frame,
67 static_cast<int>(Schelling_Cell::EMPTY),
68 static_cast<int>(Schelling_Cell::TYPE_A),
69 static_cast<int>(Schelling_Cell::TYPE_B));
70
71 Schelling_Rule<> rule(threshold, /*p_move=*/0.7, /*p_fill=*/0.7, master_seed);
73 engine.run(steps);
74
75 const double final = morans_i_binary(
76 engine.frame(),
77 static_cast<int>(Schelling_Cell::EMPTY),
78 static_cast<int>(Schelling_Cell::TYPE_A),
79 static_cast<int>(Schelling_Cell::TYPE_B));
80 const std::filesystem::path root = ALEPH_REPRODUCTIONS_SOURCE_DIR;
81 std::filesystem::create_directories(root / "results");
82 std::ofstream summary(root / "results" / "schelling_summary.csv");
83 if (not summary)
84 {
85 std::cerr << "Cannot write Schelling summary\n";
86 return 1;
87 }
88 summary << "side,steps,threshold,initial_morans_i,final_morans_i\n"
89 << side << ',' << steps << ',' << threshold << ','
90 << std::setprecision(12) << initial << ',' << final << '\n';
91
92 std::cout << "Schelling segregation: initial Moran's I=" << std::fixed
93 << std::setprecision(4) << initial << " final=" << final << '\n';
94 if (final <= 0.7)
95 {
96 std::cerr << "Schelling final Moran's I must be greater than 0.7\n";
97 return 2;
98 }
99 return 0;
100}
size_t steps
Definition ca-c-api.h:126
Internal helpers shared by the cellular-automata reproductions.
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 mt19937 rng
__gmp_expr< T, __gmp_binary_expr< __gmp_expr< T, U >, unsigned long int, __gmp_root_function > > root(const __gmp_expr< T, U > &expr, unsigned long int l)
Definition gmpfrxx.h:4071
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
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.
std::size_t ca_size_t
Unsigned size component used for extents and counts.
Definition ca-traits.H:63
#define ALEPH_REPRODUCTIONS_SOURCE_DIR
int main()
The lattice wraps around on every axis.
Definition ca-traits.H:124
ValueArg< size_t > seed
Definition testHash.C:53
static mt19937 engine
gsl_rng * r
Reproducible stochastic CA rules (Phase 8).