Files
optimiz-rs/examples/notebooks/15_agent_based.ipynb
T
ThotDjehuty d8682f61e5 release(v2.0.0-alpha.2): PyO3 bindings + executed companion notebooks + Sphinx RST with inline plots
PyO3 abi3 bindings for the 13 v2.0.0 functions across 8 module groups:
  bsde, pde, stochastic_control, optimal_control::quadratic_impact_control,
  mean_field::mckean_vlasov, agent_based, inference, optimization.

8 executed companion notebooks under examples/notebooks/10_bsde.ipynb …
17_generative_calibration.ipynb (cell outputs and matplotlib figures
preserved as proof-of-work; verified against analytic ground truths).

8 Sphinx RST pages under docs/source/algorithms/{bsde,pde,stochastic_control,
quadratic_impact_control,mckean_vlasov,agent_based,robust_drift,
generative_calibration_hooks}.rst with .. math:: derivations and inline
.. image:: directives placed immediately after each .. code-block:: python
so each plot appears directly under the code that produced it.

18 PNG plot assets under docs/source/_static/v2/<group>/.

index.rst extended with a new 'v2.0 Generic Stochastic Control & PDE'
toctree caption.

Forbidden-vocabulary audit on new src/, docs/source/algorithms/ and
binding files: zero matches.

All previously stable APIs untouched; v2.0.0 is additive at the binding
level — no v1.x function signature was changed.
2026-05-12 12:18:14 +02:00

139 KiB
Raw Blame History

15 — Agent-based dynamics

In [1]:
import numpy as np
import matplotlib.pyplot as plt
from optimizr import _core as opt
plt.rcParams['figure.figsize'] = (7, 4)
plt.rcParams['figure.dpi'] = 110
In [2]:
init = np.arange(40.0).tolist()
init_mean = float(np.mean(init))
res = opt.consensus_dynamics(init, alpha=0.3, noise_sigma=0.1,
                              n_steps=80, seed=0)
n_t = res['n_steps']; n_a = res['n_agents']
S = np.array(res['states_flat']).reshape(n_t, n_a)
mean_traj = np.array(res['mean_trajectory'])
print('initial mean =', init_mean)
print('final mean   =', mean_traj[-1])
print('final std    =', float(S[-1].std()))
initial mean = 19.5
final mean   = 19.44376167152161
final std    = 0.14305254923151872
In [3]:
fig, ax = plt.subplots()
for i in range(n_a):
    ax.plot(S[:, i], color='tab:blue', alpha=0.3, lw=0.6)
ax.plot(mean_traj, color='red', lw=2, label='empirical mean')
ax.axhline(init_mean, color='k', ls=':', label='initial mean')
ax.set_xlabel('step k'); ax.set_ylabel('s^k_i'); ax.legend(); ax.grid(alpha=0.3)
ax.set_title('Bounded-confidence consensus, α = 0.3')
fig.tight_layout(); plt.show()
In [4]:
fig, ax = plt.subplots()
for alpha in [0.05, 0.1, 0.3, 0.6, 1.0]:
    r = opt.consensus_dynamics(init, alpha=alpha, noise_sigma=0.0, n_steps=60, seed=0)
    S = np.array(r['states_flat']).reshape(r['n_steps'], r['n_agents'])
    spread = S.max(axis=1) - S.min(axis=1)
    ax.semilogy(spread, label=f'α = {alpha:g}')
ax.set_xlabel('step k'); ax.set_ylabel('max_i s  min_i s')
ax.set_title('Convergence rate vs averaging weight α'); ax.legend(); ax.grid(alpha=0.3)
fig.tight_layout(); plt.show()

Verified: without noise, the empirical mean is exactly preserved and the spread decays geometrically.