{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## February 17, 2020\n", "Planning Introduction\n", "
Create plans to travel in Romania" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from planning import *\n", "from notebook import psource" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "from utils import *\n", "# this imports the required expr so we can create our knowledge base\n", "\n", "knowledge_base = [\n", " expr(\"Connected(Bucharest,Pitesti)\"),\n", " expr(\"Connected(Pitesti,Rimnicu)\"),\n", " expr(\"Connected(Rimnicu,Sibiu)\"),\n", " expr(\"Connected(Sibiu,Fagaras)\"),\n", " expr(\"Connected(Fagaras,Bucharest)\"),\n", " expr(\"Connected(Pitesti,Craiova)\"),\n", " expr(\"Connected(Craiova,Rimnicu)\")\n", " ]" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "knowledge_base.extend([\n", " expr(\"Connected(x,y) ==> Connected(y,x)\"),\n", " expr(\"Connected(x,y) & Connected(y,z) ==> Connected(x,z)\"),\n", " expr(\"At(Sibiu)\")\n", " ])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We have a First Order Logic knowledge base containing expressions showing connections among cities in Romanian, and also general rules for reasoning about locations.\n", "

\n", " We are currently at Sibiu." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[Connected(Bucharest, Pitesti),\n", " Connected(Pitesti, Rimnicu),\n", " Connected(Rimnicu, Sibiu),\n", " Connected(Sibiu, Fagaras),\n", " Connected(Fagaras, Bucharest),\n", " Connected(Pitesti, Craiova),\n", " Connected(Craiova, Rimnicu),\n", " (Connected(x, y) ==> Connected(y, x)),\n", " ((Connected(x, y) & Connected(y, z)) ==> Connected(x, z)),\n", " At(Sibiu)]" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "knowledge_base" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We now define possible actions to our problem. We know that we can drive between any connected places. But, we can also fly directly between Sibiu, Bucharest, and Craiova.\n", "

\n", "We can define these flight actions like this:\n" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "#Sibiu to Bucharest\n", "precond = 'At(Sibiu)'\n", "effect = 'At(Bucharest) & ~At(Sibiu)'\n", "fly_s_b = Action('Fly(Sibiu, Bucharest)', precond, effect)\n", "\n", "#Bucharest to Sibiu\n", "precond = 'At(Bucharest)'\n", "effect = 'At(Sibiu) & ~At(Bucharest)'\n", "fly_b_s = Action('Fly(Bucharest, Sibiu)', precond, effect)\n", "\n", "#Sibiu to Craiova\n", "precond = 'At(Sibiu)'\n", "effect = 'At(Craiova) & ~At(Sibiu)'\n", "fly_s_c = Action('Fly(Sibiu, Craiova)', precond, effect)\n", "\n", "#Craiova to Sibiu\n", "precond = 'At(Craiova)'\n", "effect = 'At(Sibiu) & ~At(Craiova)'\n", "fly_c_s = Action('Fly(Craiova, Sibiu)', precond, effect)\n", "\n", "#Bucharest to Craiova\n", "precond = 'At(Bucharest)'\n", "effect = 'At(Craiova) & ~At(Bucharest)'\n", "fly_b_c = Action('Fly(Bucharest, Craiova)', precond, effect)\n", "\n", "#Craiova to Bucharest\n", "precond = 'At(Craiova)'\n", "effect = 'At(Bucharest) & ~At(Craiova)'\n", "fly_c_b = Action('Fly(Craiova, Bucharest)', precond, effect)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And the drive actions like this." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "#Drive\n", "precond = 'At(x)'\n", "effect = 'At(y) & ~At(x)'\n", "drive = Action('Drive(x, y)', precond, effect)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Our goal is defined as" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "goals = 'At(Bucharest)'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Thus, with all the components in place, we can define the planning problem." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "prob = PlanningProblem(knowledge_base, goals, [fly_s_b, fly_b_s, fly_s_c, fly_c_s, fly_b_c, fly_c_b, drive])" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['__class__',\n", " '__delattr__',\n", " '__dict__',\n", " '__dir__',\n", " '__doc__',\n", " '__eq__',\n", " '__format__',\n", " '__ge__',\n", " '__getattribute__',\n", " '__gt__',\n", " '__hash__',\n", " '__init__',\n", " '__init_subclass__',\n", " '__le__',\n", " '__lt__',\n", " '__module__',\n", " '__ne__',\n", " '__new__',\n", " '__reduce__',\n", " '__reduce_ex__',\n", " '__repr__',\n", " '__setattr__',\n", " '__sizeof__',\n", " '__str__',\n", " '__subclasshook__',\n", " '__weakref__',\n", " 'act',\n", " 'actions',\n", " 'convert',\n", " 'goal_test',\n", " 'goals',\n", " 'init']" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dir(prob)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[Connected(Bucharest, Pitesti),\n", " Connected(Pitesti, Rimnicu),\n", " Connected(Rimnicu, Sibiu),\n", " Connected(Sibiu, Fagaras),\n", " Connected(Fagaras, Bucharest),\n", " Connected(Pitesti, Craiova),\n", " Connected(Craiova, Rimnicu),\n", " (Connected(x, y) ==> Connected(y, x)),\n", " ((Connected(x, y) & Connected(y, z)) ==> Connected(x, z)),\n", " At(Sibiu)]" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "prob.init" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[At(Bucharest)]" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "prob.goals" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "prob.goal_test()" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[Action(Fly(Sibiu, Bucharest)),\n", " Action(Fly(Bucharest, Sibiu)),\n", " Action(Fly(Sibiu, Craiova)),\n", " Action(Fly(Craiova, Sibiu)),\n", " Action(Fly(Bucharest, Craiova)),\n", " Action(Fly(Craiova, Bucharest)),\n", " Action(Drive(x, y))]" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "prob.actions" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [], "source": [ "solution = [expr(\"Fly(Sibiu, Bucharest)\")]" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [], "source": [ "for action in solution:\n", " prob.act(action)" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "prob.goal_test()" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[Connected(Bucharest, Pitesti),\n", " Connected(Pitesti, Rimnicu),\n", " Connected(Rimnicu, Sibiu),\n", " Connected(Sibiu, Fagaras),\n", " Connected(Fagaras, Bucharest),\n", " Connected(Pitesti, Craiova),\n", " Connected(Craiova, Rimnicu),\n", " (Connected(x, y) ==> Connected(y, x)),\n", " ((Connected(x, y) & Connected(y, z)) ==> Connected(x, z)),\n", " At(Bucharest),\n", " NotAt(Sibiu)]" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "prob.init" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [], "source": [ "solution = [expr(\"Drive(Bucharest,Sibiu)\")]" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [], "source": [ "prob.goals = [expr(\"At(Sibiu)\")]" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "prob.goal_test()" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [], "source": [ "for act in solution:\n", " prob.act(act)" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "prob.goal_test()" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[Connected(Bucharest, Pitesti),\n", " Connected(Pitesti, Rimnicu),\n", " Connected(Rimnicu, Sibiu),\n", " Connected(Sibiu, Fagaras),\n", " Connected(Fagaras, Bucharest),\n", " Connected(Pitesti, Craiova),\n", " Connected(Craiova, Rimnicu),\n", " (Connected(x, y) ==> Connected(y, x)),\n", " ((Connected(x, y) & Connected(y, z)) ==> Connected(x, z)),\n", " At(Sibiu),\n", " NotAt(Bucharest)]" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "prob.init" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "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.7.6" } }, "nbformat": 4, "nbformat_minor": 2 }