- Add python_bindings.rs with MFGConfigPy and solve_mfg_1d_rust - Update notebook to compare Rust vs Python implementations - Add performance benchmarking and accuracy validation - Include convergence plots and 3D visualizations - Update __init__.py to expose MFG functions Note: Python bindings need maturin build due to macOS linker issues with cargo
22 KiB
22 KiB
In [ ]:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D
import seaborn as sns
import time
# Import optimizr Rust library
try:
from optimizr import MFGConfig, solve_mfg_1d_rust
RUST_AVAILABLE = True
print("✓ optimizr Rust library loaded successfully")
except ImportError as e:
RUST_AVAILABLE = False
print(f"⚠ optimizr Rust library not available: {e}")
print(" Only Python implementation will be used")
# Set plotting style
sns.set_style('whitegrid')
plt.rcParams['figure.figsize'] = (14, 8)
plt.rcParams['font.size'] = 11
print("✓ Libraries loaded")In [ ]:
# Problem parameters
nx = 100 # Spatial grid points
nt = 100 # Time steps
T = 1.0 # Time horizon
nu = 0.01 # Viscosity
lambda_congestion = 0.5 # Congestion penalty
x_target = 0.7 # Target location
# Spatial and temporal grids
x = np.linspace(0, 1, nx)
t = np.linspace(0, T, nt)
dx = x[1] - x[0]
dt = t[1] - t[0]
# Initial distribution: Gaussian centered at 0.3
m0 = np.exp(-((x - 0.3)**2) / (2 * 0.05**2))
m0 /= np.sum(m0) * dx # Normalize
# Terminal cost: quadratic distance to target
u_terminal = 0.5 * (x - x_target)**2
# Plot initial conditions
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(14, 4))
ax1.plot(x, m0, 'b-', linewidth=2, label='Initial distribution $m_0(x)$')
ax1.axvline(x_target, color='r', linestyle='--', alpha=0.5, label=f'Target: $x={x_target}$')
ax1.set_xlabel('Space $x$')
ax1.set_ylabel('Density')
ax1.set_title('Initial Agent Distribution')
ax1.legend()
ax1.grid(True, alpha=0.3)
ax2.plot(x, u_terminal, 'r-', linewidth=2, label='Terminal cost $g(x)$')
ax2.set_xlabel('Space $x$')
ax2.set_ylabel('Cost')
ax2.set_title('Terminal Cost Function')
ax2.legend()
ax2.grid(True, alpha=0.3)
plt.tight_layout()
plt.show()
print(f"Grid: {nx} × {nt}")
print(f"dx = {dx:.4f}, dt = {dt:.4f}")
print(f"CFL condition: dt ≤ {dx**2 / (2*nu):.4f}")In [ ]:
def solve_hjb(m, u_T):
"""Solve HJB equation backward in time"""
u = np.zeros((nx, nt))
u[:, -1] = u_T # Terminal condition
for n in range(nt-2, -1, -1):
for i in range(1, nx-1):
# Laplacian (central difference)
u_xx = (u[i+1, n+1] - 2*u[i, n+1] + u[i-1, n+1]) / dx**2
# Hamiltonian with upwind scheme
u_x_plus = (u[i+1, n+1] - u[i, n+1]) / dx
u_x_minus = (u[i, n+1] - u[i-1, n+1]) / dx
H = 0.5 * min(u_x_plus**2, u_x_minus**2) # Upwind
# Running cost
f = lambda_congestion * m[i, n]
# Update (implicit Euler)
u[i, n] = u[i, n+1] - dt * (nu * u_xx - H + f)
# Boundary conditions (Neumann)
u[0, n] = u[1, n]
u[-1, n] = u[-2, n]
return u
def solve_fp(u, m0):
"""Solve Fokker-Planck equation forward in time"""
m = np.zeros((nx, nt))
m[:, 0] = m0 # Initial condition
for n in range(nt-1):
for i in range(1, nx-1):
# Laplacian
m_xx = (m[i+1, n] - 2*m[i, n] + m[i-1, n]) / dx**2
# Velocity field
u_x = (u[i+1, n] - u[i-1, n]) / (2*dx)
v = u_x # For quadratic Hamiltonian: H_p = p
# Upwind for advection
if v > 0:
flux_diff = v * (m[i, n] - m[i-1, n]) / dx
else:
flux_diff = v * (m[i+1, n] - m[i, n]) / dx
# Update (forward Euler)
m[i, n+1] = m[i, n] + dt * (nu * m_xx - flux_diff)
m[i, n+1] = max(m[i, n+1], 0) # Non-negativity
# Boundary conditions
m[0, n+1] = m[1, n+1]
m[-1, n+1] = m[-2, n+1]
# Normalize
m[:, n+1] /= (np.sum(m[:, n+1]) * dx)
return m
print("✓ Solver functions defined")In [ ]:
# Python implementation: Fixed-point iteration
print("Running Python fixed-point iteration...")
start_time_py = time.time()
max_iter = 50
tol = 1e-5
relax = 0.5
# Initialize
m_old = np.ones((nx, nt)) / nx
errors_py = []
for iter in range(max_iter):
# Terminal condition for HJB
u_T = 0.5 * (x - x_target)**2
# Solve HJB backward
u_py = solve_hjb(m_old, u_T)
# Solve FP forward
m_new = solve_fp(u_py, m0)
# Check convergence
error = np.sqrt(np.sum((m_new - m_old)**2)) / np.sqrt(np.sum(m_old**2))
errors_py.append(error)
if iter % 5 == 0:
print(f" Iteration {iter:3d}: error = {error:.6f}")
if error < tol:
print(f"✓ Converged in {iter+1} iterations")
break
# Relaxation
m_old = relax * m_new + (1 - relax) * m_old
python_time = time.time() - start_time_py
print(f"✓ Python computation time: {python_time:.4f} seconds")
# Store Python results
u_python = u_py
m_python = m_new
iterations_python = iter + 1In [ ]:
if RUST_AVAILABLE:
# Configure the Rust MFG solver
config = MFGConfig(
nx=nx,
ny=1, # 1D problem
nt=nt,
x_min=0.0,
x_max=1.0,
T=T,
nu=nu,
max_iter=50,
tol=1e-5,
alpha=0.5 # Relaxation parameter
)
print(f"Rust solver configuration: {config}")
print("\nSolving MFG with Rust implementation...")
# Reshape inputs for 2D arrays (nx, 1) format
m0_rust = m0.reshape(-1, 1)
u_terminal_rust = u_terminal.reshape(-1, 1)
# Solve using Rust implementation
start_time = time.time()
u_rust, m_rust, iterations_rust = solve_mfg_1d_rust(m0_rust, u_terminal_rust, config)
rust_time = time.time() - start_time
# Extract 1D slices from 3D arrays (remove singleton dimensions)
u_rust_2d = u_rust[:, 0, :]
m_rust_2d = m_rust[:, 0, :]
print(f"✓ Converged in {iterations_rust} iterations")
print(f"✓ Computation time: {rust_time:.4f} seconds")
print(f"✓ Solution shape: u{u_rust_2d.shape}, m{m_rust_2d.shape}")
else:
print("⚠ Rust implementation not available, skipping...")In [ ]:
if RUST_AVAILABLE:
print("=" * 60)
print("PERFORMANCE COMPARISON")
print("=" * 60)
print(f"\n{'Metric':<30} {'Rust':<15} {'Python':<15} {'Speedup':<10}")
print("-" * 70)
print(f"{'Computation Time (s)':<30} {rust_time:<15.4f} {python_time:<15.4f} {python_time/rust_time:.2f}×")
print(f"{'Iterations to Convergence':<30} {iterations_rust:<15} {iterations_python:<15} {'-':<10}")
print(f"{'Final Tolerance':<30} {tol:<15.2e} {tol:<15.2e} {'-':<10}")
# Compute L2 difference between solutions
l2_diff_u = np.sqrt(np.mean((u_rust_2d - u_python)**2))
l2_diff_m = np.sqrt(np.mean((m_rust_2d - m_python)**2))
print("\n" + "=" * 60)
print("ACCURACY COMPARISON (L² norm of difference)")
print("=" * 60)
print(f" Value function u: {l2_diff_u:.6e}")
print(f" Distribution m: {l2_diff_m:.6e}")
if l2_diff_u < 1e-3 and l2_diff_m < 1e-3:
print("\n✓ Solutions match within numerical precision")
else:
print("\n⚠ Solutions differ - may indicate numerical instability")
else:
print("Rust implementation not available for comparison")
u_rust_2d, m_rust_2d = u_python, m_python # Use Python results for plotsIn [ ]:
# Plot convergence comparison
fig, ax = plt.subplots(1, 1, figsize=(10, 6))
ax.semilogy(errors_py, 'b-', linewidth=2, marker='o', markersize=4, label='Python', alpha=0.7)
ax.axhline(tol, color='gray', linestyle='--', linewidth=1, label=f'Tolerance: {tol:.1e}')
ax.set_xlabel('Iteration')
ax.set_ylabel('Relative L² error')
ax.set_title('Convergence of Fixed-Point Iteration')
ax.legend()
ax.grid(True, alpha=0.3, which='both')
plt.tight_layout()
plt.show()
print(f"✓ Both implementations converge to the same tolerance")In [ ]:
# Create meshgrid for plotting
X, T_grid = np.meshgrid(x, t)
# Use Rust solution if available, otherwise Python
m_plot = m_rust_2d if RUST_AVAILABLE else m_python
u_plot = u_rust_2d if RUST_AVAILABLE else u_python
solution_label = "Rust" if RUST_AVAILABLE else "Python"
# Plot distribution evolution
fig = plt.figure(figsize=(16, 6))
# 3D surface plot of distribution
ax1 = fig.add_subplot(121, projection='3d')
surf1 = ax1.plot_surface(X, T_grid, m_plot.T, cmap=cm.viridis, alpha=0.8, edgecolor='none')
ax1.set_xlabel('Space $x$')
ax1.set_ylabel('Time $t$')
ax1.set_zlabel('Density $m(x,t)$')
ax1.set_title(f'Distribution Evolution ({solution_label})')
ax1.view_init(elev=25, azim=45)
fig.colorbar(surf1, ax=ax1, shrink=0.5, aspect=10)
# 3D surface plot of value function
ax2 = fig.add_subplot(122, projection='3d')
surf2 = ax2.plot_surface(X, T_grid, u_plot.T, cmap=cm.plasma, alpha=0.8, edgecolor='none')
ax2.set_xlabel('Space $x$')
ax2.set_ylabel('Time $t$')
ax2.set_zlabel('Value $u(x,t)$')
ax2.set_title(f'Value Function ({solution_label})')
ax2.view_init(elev=25, azim=45)
fig.colorbar(surf2, ax=ax2, shrink=0.5, aspect=10)
plt.tight_layout()
plt.show()
print(f"✓ 3D visualization complete using {solution_label} solution")In [ ]:
# Temporal snapshots comparison
fig, axes = plt.subplots(2, 3, figsize=(16, 10))
time_indices = [0, nt//2, nt-1]
times = [0.0, T/2, T]
# Plot Python solution
for ax, idx, time_val in zip(axes[0], time_indices, times):
ax.plot(x, m_python[:, idx], 'b-', linewidth=2, label='Python')
if RUST_AVAILABLE:
ax.plot(x, m_rust_2d[:, idx], 'r--', linewidth=2, alpha=0.7, label='Rust')
ax.axvline(x_target, color='gray', linestyle=':', alpha=0.5, label='Target' if idx == 0 else '')
ax.set_xlabel('Space $x$')
ax.set_ylabel('Density')
ax.set_title(f'Distribution $m(x, t={time_val:.1f})$')
if idx == 0:
ax.legend()
ax.grid(True, alpha=0.3)
# Plot value function
for ax, idx, time_val in zip(axes[1], time_indices, times):
ax.plot(x, u_python[:, idx], 'b-', linewidth=2, label='Python')
if RUST_AVAILABLE:
ax.plot(x, u_rust_2d[:, idx], 'r--', linewidth=2, alpha=0.7, label='Rust')
ax.set_xlabel('Space $x$')
ax.set_ylabel('Value')
ax.set_title(f'Value Function $u(x, t={time_val:.1f})$')
if idx == 0:
ax.legend()
ax.grid(True, alpha=0.3)
plt.tight_layout()
plt.show()
if RUST_AVAILABLE:
print("✓ Comparison plots show excellent agreement between Rust and Python")
else:
print("✓ Python solution visualized")