An Introduction To aima-python

The aima-python repository implements, in Python code, the algorithms in the textbook Artificial Intelligence: A Modern Approach. A typical module in the repository has the code for a single chapter in the book, but some modules combine several chapters. See the index if you can't find the algorithm you want. The code in this repository attempts to mirror the pseudocode in the textbook as closely as possible and to stress readability foremost; if you are looking for high-performance code with advanced features, there are other repositories for you. For each module, there are three files, for example:

  • nlp.py: Source code with data types and algorithms for natural language processing; functions have docstrings explaining their use.
  • nlp.ipynb: A notebook like this one; gives more detailed examples and explanations of use.
  • nlp_apps.ipynb: A Jupyter notebook that gives example applications of the code.
  • tests/test_nlp.py: Test cases, used to verify the code is correct, and also useful to see examples of use.

There is also an aima-java repository, if you prefer Java.

What version of Python?

The code is tested in Python 3.4 and 3.5. If you try a different version of Python 3 and find a problem, please report it as an Issue. There is an incomplete legacy branch for those who must run in Python 2.

We recommend the Anaconda distribution of Python 3.5. It comes with additional tools like the powerful IPython interpreter, the Jupyter Notebook and many helpful packages for scientific computing. After installing Anaconda, you will be good to go to run all the code and all the IPython notebooks.

IPython notebooks

The IPython notebooks in this repository explain how to use the modules, and give examples of usage. You can use them in three ways:

  1. View static HTML pages. (Just browse to the repository and click on a .ipynb file link.)
  2. Run, modify, and re-run code, live. (Download the repository (by zip file or by git commands), start a Jupyter notebook server with the shell command "jupyter notebook" (issued from the directory where the files are), and click on the notebook you want to interact with.)
  3. Binder - Click on the binder badge on the repository main page to open the notebooks in an executable environment, online. This method does not require any extra installation. The code can be executed and modified from the browser itself. Note that this is an unstable option; there is a chance the notebooks will never load.

You can read about notebooks and then get started.

Helpful Tips

Most of these notebooks start by importing all the symbols in a module:


In [1]:
from logic import *

From there, the notebook alternates explanations with examples of use. You can run the examples as they are, and you can modify the code cells (or add new cells) and run your own examples. If you have some really good examples to add, you can make a github pull request.

If you want to see the source code of a function, you can open a browser or editor and see it in another window, or from within the notebook you can use the IPython magic function %psource (for "print source") or the function psource from notebook.py. Also, if the algorithm has pseudocode, you can read it by calling the pseudocode function with input the name of the algorithm.


In [2]:
%psource WalkSAT

In [ ]:
from notebook import psource, pseudocode

psource(WalkSAT)
pseudocode("WalkSAT")

Or see an abbreviated description of an object with a trailing question mark:


In [3]:
WalkSAT?

Authors

This notebook is written by Chirag Vertak and Peter Norvig.