Python code for the book Artificial Intelligence: A Modern Approach.
The code targets the 4th edition of the book and runs on Python 3.11 and up; the CI runs the tests on 3.11 and 3.12, since some optional dependencies (tensorflow, keras, opencv-python) do not yet ship wheels for 3.13+. Clone the repository together with the aima-data submodule (the datasets), install the package aima and its dependencies, and run the tests:
git clone --recursive https://github.com/aimacode/aima-python.git
cd aima-python
pip install -e .
pip install pytest
pytest
In an existing clone, git submodule update --init fetches the datasets. The algorithms are then imported from the package, e.g., from aima.search import astar_search, and python -i -m aima.search opens an interactive session on one module. A couple of notebooks also need the Graphviz binary dot (e.g., apt install graphviz or brew install graphviz), since the graphviz package on PyPI is only a wrapper.
The folder notebooks/ holds the Jupyter notebooks that explain and demonstrate the code, one per topic plus the per-chapter demos in notebooks/chapterNN/; each starts with %run bootstrap.ipynb, which puts the repository root on sys.path. The workflow notebooks-to-py.yml keeps a jupytext .py mirror beside every notebook: the .ipynb is the source, so edit the notebook and not the mirror. The notebooks also run in Binder, and those of most chapters run in the browser with no install at all through JupyterLite (see lite/README.md).
One row per algorithm, with the figure and the name of the 4th edition as in aima-pseudocode; algorithms of the 3rd edition only and code that is not a figure of the book have Figure —.
| Figure (3rd edition) | Name | Code |
|---|---|---|
| 3.2 | Romania map | romania_map |
| 4.9 | Vacuum world | vacuum_world |
| 4.23 | One-dimensional state space | one_dim_state_space |
| 6.1 | Australia map | australia_map |
| 7.13 | Wumpus world KB | wumpus_world_inference |
| 7.16 | Horn clauses KB | horn_clauses_KB |
| 17.1 | Sequential decision environment | sequential_decision_environment |
| 18.2 | Restaurant waiting decision tree | waiting_decision_tree |
See CONTRIBUTING.md: the code follows the pseudocode of the 4th edition, every change comes with its tests and pytest must pass, and each pull request makes one change. Many thanks to all the contributors, in particular to Darius Bacon, Phil Ruggera, Peng Shao, Amit Patil, Ted Nienstedt, Jim Martin, Ben Catanzariti, @antmarakis, @dmeoli, @SnShine, @reachtarunhere, @Chipe1, @ad71 and @MariannaSpyrakou.
aima-java · aima-csharp · aima-scala · aima-julia · aima-lisp · aima-javascript · aima-pseudocode · aima-exercises · aima-data · aima-glossary