diff --git a/utils.ipynb b/utils.ipynb new file mode 100644 index 000000000..2dd7fcc0e --- /dev/null +++ b/utils.ipynb @@ -0,0 +1,175 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "# Utilities\n", + "\n", + "This notebook provides some utilities widely used by other modules for the book *Artificial Intelligence: A Modern Approach.* This notebook uses implementations from [utils.py](https://github.com/aimacode/aima-python/blob/master/utils.py) module. Let's start by importing everything from utilities module." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": true, + "scrolled": true + }, + "outputs": [], + "source": [ + "from utils import *\n", + "from notebook import psource\n", + "\n", + "# Needed to hide warnings in the matplotlib sections\n", + "import warnings\n", + "warnings.filterwarnings(\"ignore\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## CONTENTS\n", + "\n", + "* Queue\n", + "* FIFOQueue\n", + "* PriorityQueue" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## QUEUE\n", + "\n", + "Let's see how we define a Queue. Run the next cell to see how abstract class `Queue` is defined in the search module." + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "%psource Queue" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The `Queue` class has two methods.\n", + "\n", + "* `__init__(self)` : This is what is called a `constructor` and is the first method called when you create an instance of the class.\n", + "\n", + "* `extend(self, items)` : This simply adds all the items in `items` to the queue." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## FIFOQUEUE\n", + "\n", + "Let's see how we define a FIFOQueue (First-In-First-Out Queue). Run the next cell to see how abstract class `FIFOQueue` is defined in the search module." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "%psource FIFOQueue" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The `FIFOQueue` class has two methods.\n", + "\n", + "* `__init__(self, maxlen, items)` : This method creates a `FIFOQueue` where `maxlen` is the maximum capacity of the queue and `items` is referred to the all items in the queue.\n", + "\n", + "* `append(self, item)` : This method adds the `item` to the queue until the maximum capacity of the `queue` is reached. An error will be raised if you try to add an `item` after the maximum capacity is reached of queue is reached.\n", + "\n", + "* `extend(self, items)` : This method adds all the items in `items` to the queue until the maximum capacity is reached. An error will be raised if you try to add an `item` after the maximum capacity is reached of queue is reached.\n", + "\n", + "* `pop(self)` : This returns the item at the top of the queue and if the `FIFOQueue` is empty an error is raised.\n", + "\n", + "* `__len__(self)` : This returns the length of the `FIFOQueue`, i.e. number of items in queue.\n", + "\n", + "* `__contains__(self, item)` : Checks if `item` exists in the queue, if it does exist, the `item` is returned." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## PRIORITYQUEUE\n", + "\n", + "Let's see how we define a PriorityQueue. Run the next cell to see how abstract class `PriorityQueue` is defined in the search module." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "%psource PriorityQueue" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The `PriorityQueue` class has two methods.\n", + "\n", + "* `__init__(self, order, f)` : This method creates a `PriorityQueue`. `order` determines whether the items in the queue will be sorted in increasing priority or decreasing priorirty. If `order` is min, then `f` is the item with the least priority; if `order` is max, then `f` is the item with the maximum priority.\n", + "\n", + "* `append(self, item)` : This method adds the `item` to the queue in the order predefined during the initialization.\n", + "\n", + "* `__len__(self)` : This returns the length of the `PriorityQueue`, i.e. number of items in queue.\n", + "\n", + "* `pop(self)` : This returns the item at the top of the queue.\n", + "\n", + "* `__contains__(self, item)` : Checks if `item` exists in the queue or not. Returns `True` if it exists.\n", + "\n", + "* `__getitem__(self, key)` : Checks if `key` matches with any `item` in the queue or not. If it does, the `item` is returned.\n", + "\n", + "* `__delitem__(self, key)` : Checks if `key` matches with any `item` in the queue or not. If it does, the `item` is deleted.\n" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.6.3" + } + }, + "nbformat": 4, + "nbformat_minor": 1 +}