diff --git a/images/blocks_world.png b/images/blocks_world.png new file mode 100644 index 000000000..cc772ae36 Binary files /dev/null and b/images/blocks_world.png differ diff --git a/logic.ipynb b/logic.ipynb index 079f1170b..f017a15ac 100644 --- a/logic.ipynb +++ b/logic.ipynb @@ -306,11 +306,11 @@ "|--------------------------|----------------------|-------------------------|---|---|\n", "| Negation | ¬ P | `~P` | `~P` | `Expr('~', P)`\n", "| And | P ∧ Q | `P & Q` | `P & Q` | `Expr('&', P, Q)`\n", - "| Or | P ∨ Q | `P` | `Q`| `P` | `Q` | `Expr('`|`', P, Q)`\n", + "| Or | P ∨ Q | `P` | `Q`| `P` | `Q` | `Expr('`|`', P, Q)\n", "| Inequality (Xor) | P ≠ Q | `P ^ Q` | `P ^ Q` | `Expr('^', P, Q)`\n", "| Implication | P → Q | `P` |`'==>'`| `Q` | `P ==> Q` | `Expr('==>', P, Q)`\n", "| Reverse Implication | Q ← P | `Q` |`'<=='`| `P` |`Q <== P` | `Expr('<==', Q, P)`\n", - "| Equivalence | P ↔ Q | `P` |`'<=>'`| `Q` |`P <=> Q` | `Expr('<=>', P, Q)`\n", + "| Equivalence | P ↔ Q | `P` |`'<=>'`| `Q` |`P ==> Q` | `Expr('==>', P, Q)`\n", "\n", "Here's an example of defining a sentence with an implication arrow:" ] @@ -321,18 +321,7 @@ "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "(~(P & Q) ==> (~P | ~Q))" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "~(P & Q) |'==>'| (~P | ~Q)" ] @@ -412,6 +401,7 @@ "\n", "The class `PropKB` can be used to represent a knowledge base of propositional logic sentences.\n", "\n", + "\n", "We see that the class `KB` has four methods, apart from `__init__`. A point to note here: the `ask` method simply calls the `ask_generator` method. Thus, this one has already been implemented and what you'll have to actually implement when you create your own knowledge base class (if you want to, though I doubt you'll ever need to; just use the ones we've created for you), will be the `ask_generator` function and not the `ask` function itself.\n", "\n", "The class `PropKB` now.\n", @@ -421,6 +411,329 @@ "* `retract(self, sentence)` : This function removes all the clauses of the sentence given, from the knowledge base. Like the `tell` function, you don't have to pass clauses to remove them from the knowledge base; any sentence will do fine. The function will take care of converting that sentence to clauses and then remove those." ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The following gives an idea how the python code works in logic.py:" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [], + "source": [ + "KB = PropKB()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "creates an empty knowledge base" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "KB.tell(A & C)\n", + "KB.ask(A) == KB.ask(C) == {}" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "adds sentence and checks if KB entails the query." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "KB.ask(E)" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{}" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "KB.tell(E)\n", + "KB.ask(E)" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "KB.retract(C)\n", + "KB.ask(C)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "removes all clauses of 'C' from the sentence." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n", + "\n", + "\n", + "# The truth table enumeration algorithm from Figure 7.10 codes as follows:" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [], + "source": [ + "def tt_entails(kb, alpha):\n", + " \n", + " assert not variables(alpha)\n", + " return tt_check_all(kb, alpha, prop_symbols(kb & alpha), {})" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Example:" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "tt_entails(expr('P & Q'), expr('Q'))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The tt_entail algorithm makes use of the tt_check_all() function, which gives results in the following manner." + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "tt_check_all(expr('P & Q'), expr('Q'), prop_symbols(expr('Q')), {})" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "prop_symbols() is used to give the function argument in the form of symbols." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Example of the DPLL algorithm from Figure 7.17." + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{A: True, B: True}\n{Q: True}\n{Q: False}\n{Q: True}\n" + ] + } + ], + "source": [ + "print (dpll_satisfiable(expr('A & B')))\n", + "\n", + "print (dpll_satisfiable(expr('P | Q')))\n", + "\n", + "print (dpll_satisfiable(expr('P | ~Q')))\n", + "\n", + "print (dpll_satisfiable(expr('~P | Q')))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Example of the WalkSAT algorithm from Figure 7.18." + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{C: True, A: True, B: True}" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "WalkSAT([A & B, A & C], 0.5, 100 )" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Example of the SATplan algorithm from Figure 7.22." + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Left', 'Left']" + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "transition = {'A': {'Left': 'A', 'Right': 'B'},\n", + " 'B': {'Left': 'A', 'Right': 'C'},\n", + " 'C': {'Left': 'B', 'Right': 'C'}}\n", + "\n", + "SAT_plan('C', transition, 'A', 3)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# The unify() algorithm from Figure 9.1 \n", + "\n","Returns a substitution to make the first 2 arguments identical, and the third argument is the substitution built up so far." + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{x: 2}\n" + ] + } + ], + "source": [ + "print(unify(x, 2, {}))" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -441,7 +754,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": null, "metadata": { "collapsed": false }, @@ -452,7 +765,7 @@ "(P ==> ~Q)" ] }, - "execution_count": 15, + "execution_count": 29, "metadata": {}, "output_type": "execute_result" } @@ -470,7 +783,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": null, "metadata": { "collapsed": false }, @@ -481,7 +794,7 @@ "(P ==> ~Q)" ] }, - "execution_count": 16, + "execution_count": 30, "metadata": {}, "output_type": "execute_result" } @@ -499,7 +812,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": null, "metadata": { "collapsed": false }, @@ -510,7 +823,7 @@ "PartialExpr('==>', P)" ] }, - "execution_count": 17, + "execution_count": 31, "metadata": {}, "output_type": "execute_result" } @@ -530,7 +843,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": null, "metadata": { "collapsed": false }, @@ -541,7 +854,7 @@ "(P ==> ~Q)" ] }, - "execution_count": 18, + "execution_count": 32, "metadata": {}, "output_type": "execute_result" } @@ -571,22 +884,11 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "(~(P & Q) ==> (~P | ~Q))" - ] - }, - "execution_count": 19, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "expr('~(P & Q) ==> (~P | ~Q)')" ] @@ -600,7 +902,7 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": null, "metadata": { "collapsed": false }, @@ -630,7 +932,7 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": null, "metadata": { "collapsed": false }, @@ -659,7 +961,7 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": null, "metadata": { "collapsed": false }, @@ -679,6 +981,33 @@ "(P & Q) |'==>'| (P | Q)" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "" + ] + }, { "cell_type": "markdown", "metadata": { @@ -701,14 +1030,14 @@ "language_info": { "codemirror_mode": { "name": "ipython", - "version": 3 + "version": 3.0 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.4.3" + "version": "3.5.1" } }, "nbformat": 4, diff --git a/planning.ipynb b/planning.ipynb deleted file mode 100644 index d5a5eb25d..000000000 --- a/planning.ipynb +++ /dev/null @@ -1,45 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "import planning" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [] - } - ], - "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.5.1" - } - }, - "nbformat": 4, - "nbformat_minor": 0 -} diff --git a/planning.py b/planning.py index b92cb6eaa..866911afd 100644 --- a/planning.py +++ b/planning.py @@ -551,22 +551,22 @@ def double_tennis_problem(): expr('Partner(B, A)')] def goal_test(kb): - required = [expr('Goal(Returned(Ball))'), expr('At(a, RightNet)'), expr('At(a, LeftNet)')] + required = [expr('Goal(Returned(Ball))'), expr('At(a, RightNet)')] or [expr('Goal(Returned(Ball))'), expr('At(a, LeftNet)')] for q in required: if kb.ask(q) is False: return False return True - # Actions - # Hit - precond_pos = [expr("Approaching(Ball,loc)"), expr("At(actor,loc)")] - precond_neg = [] - effect_add = [expr("Returned(Ball)")] + ##actions + #hit + precond_pos=[expr("Approaching(Ball, loc)"), expr("At(actor, loc)")] + precond_neg=[] + effect_add=[expr("Returned(Ball)")] effect_rem = [] hit = Action(expr("Hit(actor, Ball)"), [precond_pos, precond_neg], [effect_add, effect_rem]) - # Go + #go precond_pos = [expr("At(actor, loc)")] precond_neg = [] effect_add = [expr("At(actor, to)")]
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: