bench-mfg-suite 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (109) hide show
  1. bench_mfg_suite-0.1.0/.gitignore +48 -0
  2. bench_mfg_suite-0.1.0/Dockerfile +34 -0
  3. bench_mfg_suite-0.1.0/EXPERIMENTS.md +13 -0
  4. bench_mfg_suite-0.1.0/Makefile +39 -0
  5. bench_mfg_suite-0.1.0/PKG-INFO +203 -0
  6. bench_mfg_suite-0.1.0/README.md +155 -0
  7. bench_mfg_suite-0.1.0/README_GARNET.md +11 -0
  8. bench_mfg_suite-0.1.0/conftest.py +6 -0
  9. bench_mfg_suite-0.1.0/docs/EXTENDING.md +157 -0
  10. bench_mfg_suite-0.1.0/docs/MFG_GARNET.md +111 -0
  11. bench_mfg_suite-0.1.0/pyproject.toml +179 -0
  12. bench_mfg_suite-0.1.0/src/benchmfg/__init__.py +25 -0
  13. bench_mfg_suite-0.1.0/src/benchmfg/api.py +66 -0
  14. bench_mfg_suite-0.1.0/src/benchmfg/cli.py +143 -0
  15. bench_mfg_suite-0.1.0/src/benchmfg/conf/__init__.py +0 -0
  16. bench_mfg_suite-0.1.0/src/benchmfg/conf/algorithm/__init__.py +0 -0
  17. bench_mfg_suite-0.1.0/src/benchmfg/conf/algorithm/algorithm_schema.py +86 -0
  18. bench_mfg_suite-0.1.0/src/benchmfg/conf/config_schema.py +120 -0
  19. bench_mfg_suite-0.1.0/src/benchmfg/conf/config_utils.py +189 -0
  20. bench_mfg_suite-0.1.0/src/benchmfg/conf/environment/__init__.py +0 -0
  21. bench_mfg_suite-0.1.0/src/benchmfg/conf/environment/environemnt_schema.py +3 -0
  22. bench_mfg_suite-0.1.0/src/benchmfg/conf/environment/environment_schema.py +146 -0
  23. bench_mfg_suite-0.1.0/src/benchmfg/conf/experiment/__init__.py +0 -0
  24. bench_mfg_suite-0.1.0/src/benchmfg/conf/experiment/experiment_scheme.py +18 -0
  25. bench_mfg_suite-0.1.0/src/benchmfg/conf/visualization/__init__.py +0 -0
  26. bench_mfg_suite-0.1.0/src/benchmfg/conf/visualization/visualization_schema.py +39 -0
  27. bench_mfg_suite-0.1.0/src/benchmfg/config/__init__.py +1 -0
  28. bench_mfg_suite-0.1.0/src/benchmfg/config/algorithm/__init__.py +1 -0
  29. bench_mfg_suite-0.1.0/src/benchmfg/config/algorithm/damped_fixed_point.yaml +17 -0
  30. bench_mfg_suite-0.1.0/src/benchmfg/config/algorithm/omd.yaml +15 -0
  31. bench_mfg_suite-0.1.0/src/benchmfg/config/algorithm/pi.yaml +15 -0
  32. bench_mfg_suite-0.1.0/src/benchmfg/config/algorithm/pso.yaml +16 -0
  33. bench_mfg_suite-0.1.0/src/benchmfg/config/defaults.yaml +25 -0
  34. bench_mfg_suite-0.1.0/src/benchmfg/config/environment/__init__.py +1 -0
  35. bench_mfg_suite-0.1.0/src/benchmfg/config/environment/contraction_game.yaml +39 -0
  36. bench_mfg_suite-0.1.0/src/benchmfg/config/environment/four_rooms_obstacles.yaml +43 -0
  37. bench_mfg_suite-0.1.0/src/benchmfg/config/environment/kinetic_congestion.yaml +46 -0
  38. bench_mfg_suite-0.1.0/src/benchmfg/config/environment/lasry_lions_chain.yaml +32 -0
  39. bench_mfg_suite-0.1.0/src/benchmfg/config/environment/mf_garnet.yaml +37 -0
  40. bench_mfg_suite-0.1.0/src/benchmfg/config/environment/multiple_equilibria.yaml +39 -0
  41. bench_mfg_suite-0.1.0/src/benchmfg/config/environment/no_interaction_game.yaml +30 -0
  42. bench_mfg_suite-0.1.0/src/benchmfg/config/environment/potential_game2d.yaml +32 -0
  43. bench_mfg_suite-0.1.0/src/benchmfg/config/environment/rock_paper_scissors.yaml +37 -0
  44. bench_mfg_suite-0.1.0/src/benchmfg/config/environment/sis_epidemic.yaml +41 -0
  45. bench_mfg_suite-0.1.0/src/benchmfg/config/experiment/__init__.py +1 -0
  46. bench_mfg_suite-0.1.0/src/benchmfg/config/experiment/experiment.yaml +13 -0
  47. bench_mfg_suite-0.1.0/src/benchmfg/config/logging/__init__.py +1 -0
  48. bench_mfg_suite-0.1.0/src/benchmfg/config/logging/logging.yaml +9 -0
  49. bench_mfg_suite-0.1.0/src/benchmfg/config/visualization/__init__.py +1 -0
  50. bench_mfg_suite-0.1.0/src/benchmfg/config/visualization/visualization.yaml +18 -0
  51. bench_mfg_suite-0.1.0/src/benchmfg/envs/__init__.py +29 -0
  52. bench_mfg_suite-0.1.0/src/benchmfg/envs/contraction_game/contraction_game.py +146 -0
  53. bench_mfg_suite-0.1.0/src/benchmfg/envs/contraction_game/contraction_game_jit.py +54 -0
  54. bench_mfg_suite-0.1.0/src/benchmfg/envs/four_rooms_obstacles/four_rooms_obstacles.py +171 -0
  55. bench_mfg_suite-0.1.0/src/benchmfg/envs/four_rooms_obstacles/four_rooms_obstacles_jit.py +69 -0
  56. bench_mfg_suite-0.1.0/src/benchmfg/envs/kinetic_congestion/__init__.py +5 -0
  57. bench_mfg_suite-0.1.0/src/benchmfg/envs/kinetic_congestion/kinetic_congestion.py +199 -0
  58. bench_mfg_suite-0.1.0/src/benchmfg/envs/kinetic_congestion/kinetic_congestion_jit.py +91 -0
  59. bench_mfg_suite-0.1.0/src/benchmfg/envs/lasry_lions_chain/lasry_lions_chain.py +135 -0
  60. bench_mfg_suite-0.1.0/src/benchmfg/envs/lasry_lions_chain/lasry_lions_chain_jit.py +39 -0
  61. bench_mfg_suite-0.1.0/src/benchmfg/envs/mf_garnet/mf_garnet.py +227 -0
  62. bench_mfg_suite-0.1.0/src/benchmfg/envs/mf_garnet/mf_garnet_jit.py +129 -0
  63. bench_mfg_suite-0.1.0/src/benchmfg/envs/mfg_model_class.py +350 -0
  64. bench_mfg_suite-0.1.0/src/benchmfg/envs/mfg_model_class_jit.py +348 -0
  65. bench_mfg_suite-0.1.0/src/benchmfg/envs/multiple_equilibria/multiple_equilibria.py +110 -0
  66. bench_mfg_suite-0.1.0/src/benchmfg/envs/multiple_equilibria/multiple_equilibria_jit.py +66 -0
  67. bench_mfg_suite-0.1.0/src/benchmfg/envs/no_interaction/no_interaction.py +101 -0
  68. bench_mfg_suite-0.1.0/src/benchmfg/envs/no_interaction/no_interaction_jit.py +39 -0
  69. bench_mfg_suite-0.1.0/src/benchmfg/envs/rock_paper_scissors/__init__.py +5 -0
  70. bench_mfg_suite-0.1.0/src/benchmfg/envs/rock_paper_scissors/rock_paper_scissors.py +141 -0
  71. bench_mfg_suite-0.1.0/src/benchmfg/envs/rock_paper_scissors/rock_paper_scissors_jit.py +42 -0
  72. bench_mfg_suite-0.1.0/src/benchmfg/envs/sis_epidemic/__init__.py +5 -0
  73. bench_mfg_suite-0.1.0/src/benchmfg/envs/sis_epidemic/sis_epidemic.py +132 -0
  74. bench_mfg_suite-0.1.0/src/benchmfg/envs/sis_epidemic/sis_epidemic_jit.py +57 -0
  75. bench_mfg_suite-0.1.0/src/benchmfg/learner/__init__.py +0 -0
  76. bench_mfg_suite-0.1.0/src/benchmfg/learner/jax/fp_jax.py +185 -0
  77. bench_mfg_suite-0.1.0/src/benchmfg/learner/jax/omd_jax.py +160 -0
  78. bench_mfg_suite-0.1.0/src/benchmfg/learner/jax/pi_jax.py +230 -0
  79. bench_mfg_suite-0.1.0/src/benchmfg/learner/jax/pso_cuda_jax.py +359 -0
  80. bench_mfg_suite-0.1.0/src/benchmfg/learner/jax/pso_jax.py +508 -0
  81. bench_mfg_suite-0.1.0/src/benchmfg/learner/python/fp_py.py +139 -0
  82. bench_mfg_suite-0.1.0/src/benchmfg/learner/python/omd_py.py +118 -0
  83. bench_mfg_suite-0.1.0/src/benchmfg/learner/python/pi_py.py +187 -0
  84. bench_mfg_suite-0.1.0/src/benchmfg/train.py +46 -0
  85. bench_mfg_suite-0.1.0/src/benchmfg/utility/MFGPlots.py +703 -0
  86. bench_mfg_suite-0.1.0/src/benchmfg/utility/__init__.py +0 -0
  87. bench_mfg_suite-0.1.0/src/benchmfg/utility/collect_best_artifacts.py +157 -0
  88. bench_mfg_suite-0.1.0/src/benchmfg/utility/config_utils.py +547 -0
  89. bench_mfg_suite-0.1.0/src/benchmfg/utility/create_environment.py +64 -0
  90. bench_mfg_suite-0.1.0/src/benchmfg/utility/create_solver.py +330 -0
  91. bench_mfg_suite-0.1.0/src/benchmfg/utility/garnet_results_table.py +224 -0
  92. bench_mfg_suite-0.1.0/src/benchmfg/utility/hyperparameter_table.py +182 -0
  93. bench_mfg_suite-0.1.0/src/benchmfg/utility/main_utils.py +140 -0
  94. bench_mfg_suite-0.1.0/src/benchmfg/utility/path_utils.py +148 -0
  95. bench_mfg_suite-0.1.0/src/benchmfg/utility/plot_comparison.py +238 -0
  96. bench_mfg_suite-0.1.0/src/benchmfg/utility/plot_discovery.py +486 -0
  97. bench_mfg_suite-0.1.0/src/benchmfg/utility/plot_loaders.py +124 -0
  98. bench_mfg_suite-0.1.0/src/benchmfg/utility/plot_primitives.py +1065 -0
  99. bench_mfg_suite-0.1.0/src/benchmfg/utility/plot_results.py +77 -0
  100. bench_mfg_suite-0.1.0/src/benchmfg/utility/plot_single_run.py +249 -0
  101. bench_mfg_suite-0.1.0/src/benchmfg/utility/plot_sweep.py +295 -0
  102. bench_mfg_suite-0.1.0/src/benchmfg/utility/policy_average.py +47 -0
  103. bench_mfg_suite-0.1.0/src/benchmfg/utility/run_training.py +28 -0
  104. bench_mfg_suite-0.1.0/src/benchmfg/utility/save_results.py +81 -0
  105. bench_mfg_suite-0.1.0/src/benchmfg/utility/wandb_logger.py +328 -0
  106. bench_mfg_suite-0.1.0/tests/__init__.py +0 -0
  107. bench_mfg_suite-0.1.0/tests/test_jax_python_compatibility.py +177 -0
  108. bench_mfg_suite-0.1.0/tests/test_python_parallel_fallback.py +71 -0
  109. bench_mfg_suite-0.1.0/tests/test_smoke_jax_kernels.py +311 -0
@@ -0,0 +1,48 @@
1
+ # Python bytecode
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+ *.pyo
6
+
7
+ # Virtual environments
8
+ .venv/
9
+ venv/
10
+ env/
11
+
12
+ # Distribution / packaging
13
+ dist/
14
+ build/
15
+ *.egg-info/
16
+
17
+ # Testing
18
+ .pytest_cache/
19
+ .coverage
20
+ htmlcov/
21
+
22
+ # Ruff / mypy
23
+ .ruff_cache/
24
+ .mypy_cache/
25
+
26
+ # Editor
27
+ .vscode/
28
+ .idea/
29
+ *.swp
30
+
31
+ # Local assistant configuration
32
+ .claude/
33
+
34
+ # Local/anonymous publishing excludes
35
+ .github/
36
+
37
+ # OS
38
+ .DS_Store
39
+
40
+ # Hydra / experiment outputs
41
+ outputs/
42
+ multirun/
43
+ .hydra_logs/
44
+ profiling_output/
45
+ profiling_ouput/
46
+
47
+ # uv lock (keep uv.lock but not cache)
48
+ .uv/
@@ -0,0 +1,34 @@
1
+ # ── Stage 1: builder ──────────────────────────────────────────────────────────
2
+ FROM python:3.11.9-slim AS builder
3
+
4
+ WORKDIR /app
5
+
6
+ # Install build deps
7
+ RUN apt-get update && apt-get install -y --no-install-recommends \
8
+ build-essential \
9
+ && rm -rf /var/lib/apt/lists/*
10
+
11
+ COPY pyproject.toml .
12
+ COPY README.md .
13
+
14
+ # Install CPU-only JAX and all project dependencies
15
+ RUN pip install --upgrade pip \
16
+ && pip install --no-cache-dir "jax[cpu]" \
17
+ && pip install --no-cache-dir -e ".[dev]"
18
+
19
+ # ── Stage 2: runtime ──────────────────────────────────────────────────────────
20
+ FROM python:3.11.9-slim AS runtime
21
+
22
+ WORKDIR /app
23
+
24
+ # Copy installed packages from builder
25
+ COPY --from=builder /usr/local/lib/python3.11 /usr/local/lib/python3.11
26
+ COPY --from=builder /usr/local/bin /usr/local/bin
27
+
28
+ # Copy source code and configuration
29
+ COPY config/ config/
30
+ COPY src/ src/
31
+ COPY main.py .
32
+
33
+ # Default: run the main training entry point
34
+ ENTRYPOINT ["python", "main.py"]
@@ -0,0 +1,13 @@
1
+ # Experiments
2
+
3
+ For each environment: LasryLionsChain, NoInteractionChain, FourRoomsAversion2D, RockPaperScissors, SISEpidemic, KineticCongestion, MultipleEquilibriaGame, ContractionGame; update the `environment=...` Hydra group and `ENVIRONMENT=...` display variable in each script before running a full sweep.
4
+
5
+ ```bash
6
+ ./scripts/run_dampedfp.sh
7
+
8
+ ./scripts/run_omd.sh
9
+
10
+ ./scripts/run_pi.sh
11
+
12
+ ./scripts/run_pso.sh
13
+ ```
@@ -0,0 +1,39 @@
1
+ .PHONY: install run profile test lint format clean help
2
+
3
+ PROFILE_OUT ?= profiling_output/profile_$(shell date +%Y%m%d_%H%M%S).html
4
+
5
+ help:
6
+ @echo "Available commands:"
7
+ @echo " make install - Install dependencies with uv"
8
+ @echo " make run - Run main.py"
9
+ @echo " make profile - Profile main.py and open flamegraph HTML"
10
+ @echo " make test - Run tests"
11
+ @echo " make lint - Run ruff linter"
12
+ @echo " make format - Run black formatter"
13
+ @echo " make clean - Clean cache files"
14
+
15
+ install:
16
+ uv pip install pyinstrument
17
+ uv sync
18
+
19
+ run:
20
+ uv run python main.py
21
+
22
+ profile:
23
+ @mkdir -p profiling_output
24
+ uv run python -m pyinstrument --renderer html --outfile $(PROFILE_OUT) main.py
25
+ uv run python scripts/patch_flamegraph_colors.py $(PROFILE_OUT)
26
+ @echo "Profile saved → $(PROFILE_OUT)"
27
+ open $(PROFILE_OUT)
28
+
29
+ test:
30
+ uv run pytest tests/
31
+
32
+ lint:
33
+ uv run ruff check .
34
+
35
+ format:
36
+ uv run black .
37
+
38
+ clean:
39
+ find . -type d -name "__pycache__" -exec rm -r {} + 2>/dev/null || true
@@ -0,0 +1,203 @@
1
+ Metadata-Version: 2.4
2
+ Name: bench-mfg-suite
3
+ Version: 0.1.0
4
+ Summary: A benchmark suite for Mean Field Game algorithms
5
+ License: MIT
6
+ Keywords: machine-learning,mean-field-games,optimization,reinforcement-learning
7
+ Classifier: Development Status :: 3 - Alpha
8
+ Classifier: Intended Audience :: Science/Research
9
+ Classifier: License :: OSI Approved :: MIT License
10
+ Classifier: Programming Language :: Python :: 3.10
11
+ Classifier: Programming Language :: Python :: 3.11
12
+ Classifier: Programming Language :: Python :: 3.12
13
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
14
+ Classifier: Topic :: Scientific/Engineering :: Mathematics
15
+ Requires-Python: >=3.10
16
+ Requires-Dist: cma>=3.2.0
17
+ Requires-Dist: colorlog>=6.6.0
18
+ Requires-Dist: hydra-core>=1.3.0
19
+ Requires-Dist: jax>=0.4.13
20
+ Requires-Dist: matplotlib>=3.5.0
21
+ Requires-Dist: numpy>=1.21.0
22
+ Requires-Dist: pyyaml>=6.0.0
23
+ Requires-Dist: rich>=13.0.0
24
+ Requires-Dist: scipy>=1.7.0
25
+ Requires-Dist: tqdm>=4.62.0
26
+ Provides-Extra: cuda
27
+ Requires-Dist: jax[cuda12]>=0.4.13; (sys_platform == 'linux') and extra == 'cuda'
28
+ Provides-Extra: cuda12
29
+ Requires-Dist: jax[cuda12]>=0.4.13; (sys_platform == 'linux') and extra == 'cuda12'
30
+ Provides-Extra: cuda12-local
31
+ Requires-Dist: jax[cuda12-local]>=0.4.13; (sys_platform == 'linux') and extra == 'cuda12-local'
32
+ Provides-Extra: dev
33
+ Requires-Dist: black>=22.0.0; extra == 'dev'
34
+ Requires-Dist: pre-commit>=2.20.0; extra == 'dev'
35
+ Requires-Dist: pytest-cov>=4.0.0; extra == 'dev'
36
+ Requires-Dist: pytest>=7.0.0; extra == 'dev'
37
+ Requires-Dist: ruff>=0.1.0; extra == 'dev'
38
+ Provides-Extra: docs
39
+ Requires-Dist: sphinx-rtd-theme>=1.0.0; extra == 'docs'
40
+ Requires-Dist: sphinx>=5.0.0; extra == 'docs'
41
+ Provides-Extra: jupyter
42
+ Requires-Dist: ipykernel>=6.0.0; extra == 'jupyter'
43
+ Requires-Dist: ipywidgets>=8.0.0; extra == 'jupyter'
44
+ Requires-Dist: jupyter>=1.0.0; extra == 'jupyter'
45
+ Provides-Extra: wandb
46
+ Requires-Dist: wandb>=0.23.0; extra == 'wandb'
47
+ Description-Content-Type: text/markdown
48
+
49
+ <div align="center">
50
+
51
+ <img src="favicon_v3.svg" width="90" alt="BenchMFG icon"/>
52
+
53
+ # BenchMFG
54
+
55
+ Benchmark suite for Mean Field Game algorithms.
56
+
57
+ [![Python](https://img.shields.io/badge/python-3.10+-blue.svg)](https://www.python.org/downloads/)
58
+ [![JAX](https://img.shields.io/badge/JAX-accelerated-salmon.svg)](https://github.com/google/jax)
59
+ [![Hydra](https://img.shields.io/badge/Hydra-config-89b8cd.svg)](https://hydra.cc)
60
+ [![uv](https://img.shields.io/badge/uv-package%20manager-purple.svg)](https://github.com/astral-sh/uv)
61
+
62
+ [![License: MIT](https://img.shields.io/badge/License-MIT-red.svg)](https://opensource.org/licenses/MIT)
63
+ [![Unit tests](https://img.shields.io/badge/unit%20tests-passed-brightgreen.svg)](https://docs.pytest.org/)
64
+ [![ruff](https://img.shields.io/badge/ruff-%E2%9A%A1-gold.svg)](https://github.com/astral-sh/ruff)
65
+ [![pre-commit](https://img.shields.io/badge/pre--commit-enabled-brightgreen.svg)](https://pre-commit.com/)
66
+
67
+ </div>
68
+
69
+ ## Contents
70
+
71
+ - [Install](#install)
72
+ - [Quick Start](#quick-start)
73
+ - [Registered Configs: envs and algos](#registered-configs)
74
+ - [Sweep](#sweep)
75
+ - [Python API](#python-api)
76
+ - [Outputs And Plots](#outputs-and-plots)
77
+ - [Extending BenchMFG: create new envs and algos](docs/EXTENDING.md)
78
+ - [MF-Garnet: scaling laws and robustness](docs/MFG_GARNET.md)
79
+
80
+ ## Install
81
+
82
+ ```bash
83
+ uv add bench-mfg-suite
84
+ # or
85
+ pip install bench-mfg-suite
86
+ ```
87
+
88
+ For local development:
89
+
90
+ ```bash
91
+ uv sync --extra dev
92
+ ```
93
+
94
+ CUDA is optional. The default install uses CPU-compatible JAX.
95
+
96
+ ```bash
97
+ # Linux/NVIDIA, pip-managed CUDA runtime components:
98
+ uv add "bench-mfg-suite[cuda12]"
99
+ pip install "bench-mfg-suite[cuda12]"
100
+
101
+ # Linux/NVIDIA, local CUDA installation:
102
+ pip install "bench-mfg-suite[cuda12-local]"
103
+ ```
104
+
105
+ If GPU initialization fails, check `nvidia-smi` and the official JAX install matrix:
106
+ https://docs.jax.dev/en/latest/installation.html
107
+
108
+ ## Quick Start
109
+
110
+ List registered configs:
111
+
112
+ ```bash
113
+ benchmfg env list
114
+ benchmfg algo list
115
+ ```
116
+
117
+ Run one experiment:
118
+
119
+ ```bash
120
+ benchmfg train algorithm=pso environment=kinetic_congestion device=cpu
121
+ ```
122
+
123
+ ## Registered Configs
124
+
125
+ Environments:
126
+ `contraction_game`, `four_rooms_obstacles`, `kinetic_congestion`, `lasry_lions_chain`, `mf_garnet`, `multiple_equilibria`, `no_interaction_game`, `potential_game2d`, `rock_paper_scissors`, `sis_epidemic`.
127
+
128
+ Algorithms:
129
+ `damped_fixed_point`, `omd`, `pi`, `pso`.
130
+
131
+ Use `benchmfg env list` and `benchmfg algo list` for the installed package’s authoritative list.
132
+
133
+ ## Sweep
134
+
135
+ Run a sweep:
136
+
137
+ ```bash
138
+ benchmfg sweep \
139
+ algorithm=omd \
140
+ environment=lasry_lions_chain \
141
+ experiment.name=omd_sweep \
142
+ experiment.random_seed=42,10,111,1032 \
143
+ algorithm.omd.learning_rate=0.5,0.05,0.005 \
144
+ algorithm.omd.temperature=0.2,0.5,0.8
145
+ ```
146
+
147
+ ## Python API
148
+
149
+ ```python
150
+ import benchmfg
151
+
152
+ cfg = benchmfg.load_config(["algorithm=omd", "environment=lasry_lions_chain"])
153
+ environment, initial_policy = benchmfg.make_environment(cfg)
154
+ solver = benchmfg.make_solver(
155
+ cfg,
156
+ environment=environment,
157
+ initial_policy=initial_policy,
158
+ )
159
+ ```
160
+
161
+ ## Outputs And Plots
162
+
163
+ Runs write artifacts under:
164
+
165
+ ```text
166
+ outputs/<Env>/<Algorithm>/seed_<seed>/<Experiment>/<run_id>/
167
+ ```
168
+
169
+ Important files: `exploitabilities.npz`, `final_mean_field.npz`, `final_policy.npz`,
170
+ `metrics.npz`, `config.yaml`.
171
+
172
+ Plot commands:
173
+
174
+ ```bash
175
+ benchmfg plot single-run <run_dir>
176
+ benchmfg plot sweep <environment> <algorithm>
177
+ benchmfg plot compare <environment>
178
+ ```
179
+
180
+ Plot discovery defaults:
181
+
182
+ - `single-run <run_dir>` plots exactly that timestamped run.
183
+ - `sweep <environment> <algorithm>` scans `outputs/` by default. For each seed and
184
+ hyperparameter version, it selects the latest timestamped run containing
185
+ `exploitabilities.npz`.
186
+ - `compare <environment>` reads the `results/<environment>/<algorithm>/best_model.yaml`
187
+ files written by `plot sweep`; rerun `plot sweep` first if new runs were added.
188
+ - Use `--outputs-dir <path>` on sweep/compare commands when artifacts are not under
189
+ `outputs/`.
190
+
191
+ ## Repository Layout
192
+
193
+ ```text
194
+ src/benchmfg/
195
+ ├── config/ # packaged Hydra configs
196
+ ├── envs/ # MFG environments
197
+ ├── learner/ # solvers
198
+ ├── utility/ # training, saving, plotting helpers
199
+ ├── cli.py # benchmfg command
200
+ └── train.py # Hydra train entrypoint
201
+ ```
202
+
203
+ See [EXPERIMENTS.md](EXPERIMENTS.md) for batch-run scripts.
@@ -0,0 +1,155 @@
1
+ <div align="center">
2
+
3
+ <img src="favicon_v3.svg" width="90" alt="BenchMFG icon"/>
4
+
5
+ # BenchMFG
6
+
7
+ Benchmark suite for Mean Field Game algorithms.
8
+
9
+ [![Python](https://img.shields.io/badge/python-3.10+-blue.svg)](https://www.python.org/downloads/)
10
+ [![JAX](https://img.shields.io/badge/JAX-accelerated-salmon.svg)](https://github.com/google/jax)
11
+ [![Hydra](https://img.shields.io/badge/Hydra-config-89b8cd.svg)](https://hydra.cc)
12
+ [![uv](https://img.shields.io/badge/uv-package%20manager-purple.svg)](https://github.com/astral-sh/uv)
13
+
14
+ [![License: MIT](https://img.shields.io/badge/License-MIT-red.svg)](https://opensource.org/licenses/MIT)
15
+ [![Unit tests](https://img.shields.io/badge/unit%20tests-passed-brightgreen.svg)](https://docs.pytest.org/)
16
+ [![ruff](https://img.shields.io/badge/ruff-%E2%9A%A1-gold.svg)](https://github.com/astral-sh/ruff)
17
+ [![pre-commit](https://img.shields.io/badge/pre--commit-enabled-brightgreen.svg)](https://pre-commit.com/)
18
+
19
+ </div>
20
+
21
+ ## Contents
22
+
23
+ - [Install](#install)
24
+ - [Quick Start](#quick-start)
25
+ - [Registered Configs: envs and algos](#registered-configs)
26
+ - [Sweep](#sweep)
27
+ - [Python API](#python-api)
28
+ - [Outputs And Plots](#outputs-and-plots)
29
+ - [Extending BenchMFG: create new envs and algos](docs/EXTENDING.md)
30
+ - [MF-Garnet: scaling laws and robustness](docs/MFG_GARNET.md)
31
+
32
+ ## Install
33
+
34
+ ```bash
35
+ uv add bench-mfg-suite
36
+ # or
37
+ pip install bench-mfg-suite
38
+ ```
39
+
40
+ For local development:
41
+
42
+ ```bash
43
+ uv sync --extra dev
44
+ ```
45
+
46
+ CUDA is optional. The default install uses CPU-compatible JAX.
47
+
48
+ ```bash
49
+ # Linux/NVIDIA, pip-managed CUDA runtime components:
50
+ uv add "bench-mfg-suite[cuda12]"
51
+ pip install "bench-mfg-suite[cuda12]"
52
+
53
+ # Linux/NVIDIA, local CUDA installation:
54
+ pip install "bench-mfg-suite[cuda12-local]"
55
+ ```
56
+
57
+ If GPU initialization fails, check `nvidia-smi` and the official JAX install matrix:
58
+ https://docs.jax.dev/en/latest/installation.html
59
+
60
+ ## Quick Start
61
+
62
+ List registered configs:
63
+
64
+ ```bash
65
+ benchmfg env list
66
+ benchmfg algo list
67
+ ```
68
+
69
+ Run one experiment:
70
+
71
+ ```bash
72
+ benchmfg train algorithm=pso environment=kinetic_congestion device=cpu
73
+ ```
74
+
75
+ ## Registered Configs
76
+
77
+ Environments:
78
+ `contraction_game`, `four_rooms_obstacles`, `kinetic_congestion`, `lasry_lions_chain`, `mf_garnet`, `multiple_equilibria`, `no_interaction_game`, `potential_game2d`, `rock_paper_scissors`, `sis_epidemic`.
79
+
80
+ Algorithms:
81
+ `damped_fixed_point`, `omd`, `pi`, `pso`.
82
+
83
+ Use `benchmfg env list` and `benchmfg algo list` for the installed package’s authoritative list.
84
+
85
+ ## Sweep
86
+
87
+ Run a sweep:
88
+
89
+ ```bash
90
+ benchmfg sweep \
91
+ algorithm=omd \
92
+ environment=lasry_lions_chain \
93
+ experiment.name=omd_sweep \
94
+ experiment.random_seed=42,10,111,1032 \
95
+ algorithm.omd.learning_rate=0.5,0.05,0.005 \
96
+ algorithm.omd.temperature=0.2,0.5,0.8
97
+ ```
98
+
99
+ ## Python API
100
+
101
+ ```python
102
+ import benchmfg
103
+
104
+ cfg = benchmfg.load_config(["algorithm=omd", "environment=lasry_lions_chain"])
105
+ environment, initial_policy = benchmfg.make_environment(cfg)
106
+ solver = benchmfg.make_solver(
107
+ cfg,
108
+ environment=environment,
109
+ initial_policy=initial_policy,
110
+ )
111
+ ```
112
+
113
+ ## Outputs And Plots
114
+
115
+ Runs write artifacts under:
116
+
117
+ ```text
118
+ outputs/<Env>/<Algorithm>/seed_<seed>/<Experiment>/<run_id>/
119
+ ```
120
+
121
+ Important files: `exploitabilities.npz`, `final_mean_field.npz`, `final_policy.npz`,
122
+ `metrics.npz`, `config.yaml`.
123
+
124
+ Plot commands:
125
+
126
+ ```bash
127
+ benchmfg plot single-run <run_dir>
128
+ benchmfg plot sweep <environment> <algorithm>
129
+ benchmfg plot compare <environment>
130
+ ```
131
+
132
+ Plot discovery defaults:
133
+
134
+ - `single-run <run_dir>` plots exactly that timestamped run.
135
+ - `sweep <environment> <algorithm>` scans `outputs/` by default. For each seed and
136
+ hyperparameter version, it selects the latest timestamped run containing
137
+ `exploitabilities.npz`.
138
+ - `compare <environment>` reads the `results/<environment>/<algorithm>/best_model.yaml`
139
+ files written by `plot sweep`; rerun `plot sweep` first if new runs were added.
140
+ - Use `--outputs-dir <path>` on sweep/compare commands when artifacts are not under
141
+ `outputs/`.
142
+
143
+ ## Repository Layout
144
+
145
+ ```text
146
+ src/benchmfg/
147
+ ├── config/ # packaged Hydra configs
148
+ ├── envs/ # MFG environments
149
+ ├── learner/ # solvers
150
+ ├── utility/ # training, saving, plotting helpers
151
+ ├── cli.py # benchmfg command
152
+ └── train.py # Hydra train entrypoint
153
+ ```
154
+
155
+ See [EXPERIMENTS.md](EXPERIMENTS.md) for batch-run scripts.
@@ -0,0 +1,11 @@
1
+ # MF-Garnet
2
+
3
+ The canonical MF-Garnet guide is now in [docs/MFG_GARNET.md](docs/MFG_GARNET.md).
4
+
5
+ It covers:
6
+
7
+ - what MF-Garnet generates;
8
+ - the main transition and reward structures;
9
+ - quick CLI runs;
10
+ - the paired-seed benchmark protocol;
11
+ - key config fields and output paths.
@@ -0,0 +1,6 @@
1
+ """Root conftest: add src/ to sys.path for the src-layout package structure."""
2
+
3
+ from pathlib import Path
4
+ import sys
5
+
6
+ sys.path.insert(0, str(Path(__file__).parent / "src"))
@@ -0,0 +1,157 @@
1
+ # Extending BenchMFG
2
+
3
+ This guide shows the minimal path for adding a new environment or a new JAX
4
+ algorithm. BenchMFG uses Hydra config groups, so code and YAML registration both
5
+ matter.
6
+
7
+ ## Add A New Environment
8
+
9
+ Create a Python environment class under `src/benchmfg/envs/<name>/`.
10
+
11
+ ```python
12
+ # src/benchmfg/envs/my_game/my_game.py
13
+ import numpy as np
14
+
15
+ from benchmfg.envs.mfg_model_class import MFGStationary
16
+
17
+
18
+ class MyGame(MFGStationary):
19
+ def __init__(self, N_states, N_actions, N_noises, horizon, mean_field, noise_prob, gamma=0.99):
20
+ super().__init__(
21
+ N_states=N_states,
22
+ N_actions=N_actions,
23
+ N_noises=N_noises,
24
+ horizon=horizon,
25
+ mean_field=mean_field,
26
+ noise_prob=noise_prob,
27
+ gamma=gamma,
28
+ )
29
+
30
+ def transition(self, mean_field: np.ndarray, state: int, action: int, noise: int) -> int:
31
+ next_state = state + action - 1
32
+ return int(np.clip(next_state, 0, self.N_states - 1))
33
+
34
+ def reward(self, mean_field: np.ndarray, state: int, action: int) -> float:
35
+ return float(state - 0.1 * abs(action - 1))
36
+ ```
37
+
38
+ Add JAX transition/reward functions. These are used by the JAX solvers through
39
+ `EnvSpec`.
40
+
41
+ ```python
42
+ # src/benchmfg/envs/my_game/my_game_jit.py
43
+ import jax.numpy as jnp
44
+
45
+ from benchmfg.envs.mfg_model_class import MFGStationary
46
+
47
+
48
+ def transition_my_game(mean_field, state: int, action: int, noise: int, environment: MFGStationary):
49
+ next_state = state + action - 1
50
+ return jnp.clip(next_state, 0, environment.N_states - 1).astype(jnp.int32)
51
+
52
+
53
+ def reward_my_game(mean_field, state: int, action: int, environment: MFGStationary):
54
+ return state - 0.1 * jnp.abs(action - 1)
55
+ ```
56
+
57
+ Register the environment in four places:
58
+
59
+ 1. Add a YAML file under `src/benchmfg/config/environment/my_game.yaml`.
60
+ 2. Add any config dataclass fields in `src/benchmfg/conf/environment/environment_schema.py`.
61
+ 3. Add a constructor helper and branch in `src/benchmfg/utility/config_utils.py` and `create_environment.py`.
62
+ 4. Add the JAX functions to `ENV_JIT_FUNCTIONS` in `src/benchmfg/utility/create_solver.py`.
63
+
64
+ Minimal YAML:
65
+
66
+ ```yaml
67
+ # @package _global_
68
+ environment:
69
+ name: MyGame
70
+ num_states: 10
71
+ num_actions: 3
72
+ num_noises: 1
73
+ horizon: 50
74
+ gamma: 0.99
75
+ grid:
76
+ is_grid: false
77
+ dynamics:
78
+ is_noisy: false
79
+ noise_probabilities: [1.0]
80
+ initial_distribution:
81
+ type: uniform
82
+ ```
83
+
84
+ Smoke test:
85
+
86
+ ```bash
87
+ benchmfg train environment=my_game algorithm=omd device=cpu algorithm.omd.num_iterations=2
88
+ ```
89
+
90
+ ## Add A New JAX Algorithm
91
+
92
+ Create a solver under `src/benchmfg/learner/jax/`. A JAX solver should accept an
93
+ `EnvSpec`, return NumPy arrays, and expose an `eval(logger=None)` method.
94
+
95
+ ```python
96
+ # src/benchmfg/learner/jax/my_algo_jax.py
97
+ import numpy as np
98
+
99
+ from benchmfg.envs.mfg_model_class_jit import (
100
+ EnvSpec,
101
+ exploitability_jax,
102
+ mean_field_by_transition_kernel_multi_jax,
103
+ )
104
+
105
+
106
+ class MyAlgo_jax:
107
+ def __init__(self, env_spec: EnvSpec, initial_policy: np.ndarray, num_iterations: int, jax_device=None):
108
+ self.env_spec = env_spec
109
+ self.initial_policy = initial_policy
110
+ self.num_iterations = num_iterations
111
+ self.jax_device = jax_device
112
+
113
+ def eval(self, logger=None):
114
+ policy = self.initial_policy
115
+ mean_field = mean_field_by_transition_kernel_multi_jax(
116
+ policy,
117
+ self.env_spec,
118
+ num_iterations=20,
119
+ initial_mean_field=self.env_spec.environment.stationary_mean_field,
120
+ )
121
+ exploitabilities = [
122
+ float(exploitability_jax(policy, self.env_spec, initial_mean_field=mean_field))
123
+ ]
124
+ return np.asarray(policy), np.asarray(mean_field), exploitabilities
125
+ ```
126
+
127
+ Register the algorithm:
128
+
129
+ 1. Add a dataclass in `src/benchmfg/conf/algorithm/algorithm_schema.py`.
130
+ 2. Add a config field in `AlgorithmConfig`.
131
+ 3. Add `src/benchmfg/config/algorithm/my_algo.yaml`.
132
+ 4. Add a creation branch in `src/benchmfg/utility/create_solver.py`.
133
+ 5. Add the algorithm name to plotting metadata only if it produces comparable output directories.
134
+
135
+ Minimal YAML:
136
+
137
+ ```yaml
138
+ # @package _global_
139
+ algorithm:
140
+ _target_: MyAlgo
141
+ my_algo:
142
+ num_iterations: 100
143
+ ```
144
+
145
+ Smoke test:
146
+
147
+ ```bash
148
+ benchmfg train algorithm=my_algo environment=lasry_lions_chain device=cpu
149
+ ```
150
+
151
+ ## Checklist
152
+
153
+ - Run `benchmfg env list` or `benchmfg algo list` and confirm the new YAML appears.
154
+ - Run a tiny CPU experiment before testing CUDA.
155
+ - Add focused tests for transition/reward validity or solver output shape.
156
+ - Run `uv run ruff check src/benchmfg tests main.py`.
157
+ - Run `uv run pytest tests/`.