Skip to content

About

Python implementations of the algorithms of Russell and Norvig's "Artificial Intelligence: A Modern Approach"

Topics

Resources

Contributing

Stars

8.9k stars

Watchers

315 watching

Forks

Repository files navigation

aima-python tests license docs Binder lite-badge

Python code for the book Artificial Intelligence: A Modern Approach.

Installation

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.

Notebooks

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).

Index of algorithms

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 Name (in 4th edition) Code Tests
2.7 TABLE-DRIVEN-AGENT TableDrivenAgentProgram test_agents.py
2.8 REFLEX-VACUUM-AGENT ReflexVacuumAgent test_agents.py
2.10 SIMPLE-REFLEX-AGENT SimpleReflexAgentProgram test_agents.py
2.12 MODEL-BASED-REFLEX-AGENT ModelBasedReflexAgentProgram test_agents.py
3.7 BEST-FIRST-SEARCH best_first_graph_search test_search.py
3.9 BREADTH-FIRST-SEARCH breadth_first_graph_search test_search.py
3.9 UNIFORM-COST-SEARCH uniform_cost_search test_search.py
3.12 DEPTH-LIMITED-SEARCH depth_limited_search test_search.py
3.12 ITERATIVE-DEEPENING-SEARCH iterative_deepening_search test_search.py
3.14 BIBF-SEARCH bidirectional_search test_search.py
3.22 RECURSIVE-BEST-FIRST-SEARCH recursive_best_first_search test_search.py
§3.5.5 SMA* sma_star_search test_search.py
4.2 HILL-CLIMBING hill_climbing test_search.py
4.4 SIMULATED-ANNEALING simulated_annealing test_search.py
4.7 GENETIC-ALGORITHM genetic_algorithm test_search.py
4.10 AND-OR-GRAPH-SEARCH and_or_graph_search test_search.py
4.20 ONLINE-DFS-AGENT OnlineDFSAgent test_search.py
4.23 LRTA*-AGENT LRTAStarAgent test_search.py
5.3 MINIMAX-SEARCH minmax_decision test_games.py
5.7 ALPHA-BETA-SEARCH alpha_beta_search test_games.py
5.11 MONTE-CARLO-TREE-SEARCH monte_carlo_tree_search test_games.py
6.3 AC-3 AC3 test_csp.py
6.5 BACKTRACKING-SEARCH backtracking_search test_csp.py
6.9 MIN-CONFLICTS min_conflicts test_csp.py
6.11 TREE-CSP-SOLVER tree_csp_solver test_csp.py
7.1 KB-AGENT KBAgentProgram —
7.10 TT-ENTAILS tt_entails test_logic.py
7.13 PL-RESOLUTION pl_resolution test_logic.py
7.15 PL-FC-ENTAILS? pl_fc_entails test_logic.py
7.17 DPLL-SATISFIABLE? dpll_satisfiable test_logic.py
7.18 WALKSAT WalkSAT test_logic.py
7.20 HYBRID-WUMPUS-AGENT HybridWumpusAgent test_logic.py
7.22 SATPLAN SAT_plan test_logic.py
9.1 UNIFY unify test_logic.py
9.3 FOL-FC-ASK fol_fc_ask test_logic.py
9.6 FOL-BC-ASK fol_bc_ask test_logic.py
9.8 APPEND append test_knowledge.py
11.1 AIR-CARGO-TRANSPORT-PROBLEM air_cargo test_planning.py
11.2 SPARE-TIRE-PROBLEM spare_tire test_planning.py
11.4 BLOCKS-WORLD blocks_world test_graphplan.py
11.7 REFINEMENT-HIGH-LEVEL-ACTIONS refinements test_planning.py
11.8 HIERARCHICAL-SEARCH hierarchical_search test_planning.py
11.11 ANGELIC-SEARCH angelic_search test_planning.py
11.13 JOB-SHOP-SCHEDULING-PROBLEM job_shop_problem test_planning.py
12.1 DT-AGENT DTAgentProgram —
13.11 ENUMERATION-ASK enumeration_ask test_probability.py
13.13 ELIMINATION-ASK elimination_ask test_probability.py
13.16 PRIOR-SAMPLE prior_sample test_probability.py
13.17 REJECTION-SAMPLING rejection_sampling test_probability.py
13.18 LIKELIHOOD-WEIGHTING likelihood_weighting test_probability.py
13.20 GIBBS-ASK gibbs_ask test_probability.py
14.4 FORWARD-BACKWARD forward_backward test_probability.py
14.6 FIXED-LAG-SMOOTHING fixed_lag_smoothing test_probability.py
14.17 PARTICLE-FILTERING particle_filtering test_probability.py
15.5 OUPM generate_citation_world test_probabilistic_programming.py
15.6 NET-VISA generate_seismic_world test_probabilistic_programming.py
15.9 RADAR generate_radar_world test_probabilistic_programming.py
15.11 GENERATE-IMAGE generate_image test_probabilistic_programming.py
15.15 GENERATE-MARKOV-LETTERS generate_markov_letters test_probabilistic_programming.py
16.9 INFORMATION-GATHERING-AGENT InformationGatheringAgent —
17.6 VALUE-ITERATION value_iteration test_mdp.py
17.9 POLICY-ITERATION policy_iteration test_mdp.py
17.16 POMDP-VALUE-ITERATION pomdp_value_iteration test_mdp.py
18.1 DOUBLES-TENNIS-PROBLEM double_tennis_problem test_planning.py
19.5 LEARN-DECISION-TREE DecisionTreeLearner test_learning.py
19.8 MODEL-SELECTION cross_validation_wrapper —
19.11 DECISION-LIST-LEARNING DecisionListLearner test_learning.py
19.25 ADABOOST ada_boost test_learning.py
22.2 PASSIVE-ADP-AGENT PassiveADPAgent test_reinforcement_learning.py
22.4 PASSIVE-TD-AGENT PassiveTDAgent test_reinforcement_learning.py
22.8 Q-LEARNING-AGENT QLearningAgent test_reinforcement_learning.py
23.5 CYK-PARSE CYK_parse test_nlp.py
23.8 SENTENCE-TREE sentence_tree test_nlp.py
26.6 MONTE-CARLO-LOCALIZATION monte_carlo_localization test_probability.py
— SIMPLE-PROBLEM-SOLVING-AGENT SimpleProblemSolvingAgentProgram test_search.py
— HAVE-CAKE-AND-EAT-CAKE-TOO-PROBLEM have_cake_and_eat_cake_too test_planning.py
— GRAPHPLAN GraphPlan test_graphplan.py
— CURRENT-BEST-LEARNING current_best_learning test_knowledge.py
— VERSION-SPACE-LEARNING version_space_learning test_knowledge.py
— MINIMAL-CONSISTENT-DET minimal_consistent_det test_knowledge.py
— FOIL FOILContainer test_knowledge.py
— BACK-PROP-LEARNING BackPropagationLearner —
— HITS HITS test_nlp.py
— POWERS-OF-2 powers_of_2 test_utils.py
— Random-Vacuum-Agent RandomVacuumAgent test_agents.py
— Model-Based-Vacuum-Agent ModelBasedVacuumAgent test_agents.py
— Table-Driven-Vacuum-Agent TableDrivenVacuumAgent test_agents.py
— Environment Environment test_agents.py
— Agent Agent test_agents.py
— Problem Problem test_search.py
— Node Node test_search.py
— Tree-Search depth_first_tree_search test_search.py
— Tree-Search breadth_first_tree_search test_search.py
— Graph-Search depth_first_graph_search test_search.py
— A*-Search astar_search test_search.py
— Expectiminimax expect_minmax —
— Iterative-Deepening-Alpha-Beta iterative_deepening_alpha_beta_search test_games.py
— CSP CSP test_csp.py
— KB KB test_logic.py
— Propositional-Logic-Sentence Expr test_utils.py
— Convert-to-CNF to_cnf test_logic.py
— Subst subst test_logic.py
— FOL-Resolution fol_resolution test_logic.py
— Demodulation demodulate test_logic.py
— Paramodulation paramodulate test_logic.py
— Three-Block-Tower three_block_tower test_planning.py
— Partial-Order-Planner PartialOrderPlanner test_planning.py
— Discrete-Probability-Distribution ProbDist test_probability.py
— Kalman-Filter KalmanFilter test_probability.py
— Dynamic-Bayesian-Network DynamicBayesNet test_probability.py
— Baum-Welch baum_welch test_probability.py
— Dynamic-Decision-Network pomdp_lookahead test_mdp.py
— Iterated-Dominance iterated_dominance test_game_theory.py
— Pure-Nash-Equilibria pure_nash_equilibria test_game_theory.py
— Zero-Sum-Game (LP) solve_zero_sum_game test_game_theory.py
— Shapley-Value shapley_value test_game_theory.py
— Core (cooperative game) is_in_core test_game_theory.py
— Voting (plurality) plurality_winner test_game_theory.py
— Vickrey-Auction vickrey_auction test_game_theory.py
— Contract-Net-Protocol contract_net test_game_theory.py
— Alternating-Offers-Bargaining alternating_offers_bargaining test_game_theory.py
— EM (mixture of Gaussians) gaussian_mixture_em test_learning.py
— EM (naive Bayes, hidden variable) naive_bayes_em test_learning.py
— SARSA-Agent SARSALearningAgent test_reinforcement_learning.py
— Chart-Parse Chart test_nlp.py

Data structures

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

Contributing

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.

Other aimacode projects

aima-java · aima-csharp · aima-scala · aima-julia · aima-lisp · aima-javascript · aima-pseudocode · aima-exercises · aima-data · aima-glossary

About

Python implementations of the algorithms of Russell and Norvig's "Artificial Intelligence: A Modern Approach"

Topics

Resources

Contributing

Stars

8.9k stars

Watchers

315 watching

Forks

Releases

Packages

Used by

Contributors

Languages