{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## February 19\n", "More planning. \n", "\n", "- Air Cargo\n", "- Spare Tire\n", "- Three Block Tower\n", "- Shopping\n", "- Socks and Shoes\n", "- Cake " ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from planning import *\n", "from notebook import psource" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Air Cargo Problem\n", "\n", "In the Air Cargo problem, we start with cargo at two airports, SFO and JFK. Our goal is to send each cargo to the other airport. We have two airplanes to help us accomplish the task. The problem can be defined with three actions: Load, Unload and Fly. Let us look how the air_cargo problem has been defined in the module.\n" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "
\n", "def air_cargo():\n",
" """\n",
" [Figure 10.1] AIR-CARGO-PROBLEM\n",
"\n",
" An air-cargo shipment problem for delivering cargo to different locations,\n",
" given the starting location and airplanes.\n",
"\n",
" Example:\n",
" >>> from planning import *\n",
" >>> ac = air_cargo()\n",
" >>> ac.goal_test()\n",
" False\n",
" >>> ac.act(expr('Load(C2, P2, JFK)'))\n",
" >>> ac.act(expr('Load(C1, P1, SFO)'))\n",
" >>> ac.act(expr('Fly(P1, SFO, JFK)'))\n",
" >>> ac.act(expr('Fly(P2, JFK, SFO)'))\n",
" >>> ac.act(expr('Unload(C2, P2, SFO)'))\n",
" >>> ac.goal_test()\n",
" False\n",
" >>> ac.act(expr('Unload(C1, P1, JFK)'))\n",
" >>> ac.goal_test()\n",
" True\n",
" >>>\n",
" """\n",
"\n",
" return PlanningProblem(init='At(C1, SFO) & At(C2, JFK) & At(P1, SFO) & At(P2, JFK) & Cargo(C1) & Cargo(C2) & Plane(P1) & Plane(P2) & Airport(SFO) & Airport(JFK)', \n",
" goals='At(C1, JFK) & At(C2, SFO)',\n",
" actions=[Action('Load(c, p, a)', \n",
" precond='At(c, a) & At(p, a) & Cargo(c) & Plane(p) & Airport(a)',\n",
" effect='In(c, p) & ~At(c, a)'),\n",
" Action('Unload(c, p, a)',\n",
" precond='In(c, p) & At(p, a) & Cargo(c) & Plane(p) & Airport(a)',\n",
" effect='At(c, a) & ~In(c, p)'),\n",
" Action('Fly(p, f, to)',\n",
" precond='At(p, f) & Plane(p) & Airport(f) & Airport(to)',\n",
" effect='At(p, to) & ~At(p, f)')])\n",
"
def spare_tire():\n",
" """[Figure 10.2] SPARE-TIRE-PROBLEM\n",
"\n",
" A problem involving changing the flat tire of a car\n",
" with a spare tire from the trunk.\n",
"\n",
" Example:\n",
" >>> from planning import *\n",
" >>> st = spare_tire()\n",
" >>> st.goal_test()\n",
" False\n",
" >>> st.act(expr('Remove(Spare, Trunk)'))\n",
" >>> st.act(expr('Remove(Flat, Axle)'))\n",
" >>> st.goal_test()\n",
" False\n",
" >>> st.act(expr('PutOn(Spare, Axle)'))\n",
" >>> st.goal_test()\n",
" True\n",
" >>>\n",
" """\n",
"\n",
" return PlanningProblem(init='Tire(Flat) & Tire(Spare) & At(Flat, Axle) & At(Spare, Trunk)',\n",
" goals='At(Spare, Axle) & At(Flat, Ground)',\n",
" actions=[Action('Remove(obj, loc)',\n",
" precond='At(obj, loc)',\n",
" effect='At(obj, Ground) & ~At(obj, loc)'),\n",
" Action('PutOn(t, Axle)',\n",
" precond='Tire(t) & At(t, Ground) & ~At(Flat, Axle)',\n",
" effect='At(t, Axle) & ~At(t, Ground)'),\n",
" Action('LeaveOvernight',\n",
" precond='',\n",
" effect='~At(Spare, Ground) & ~At(Spare, Axle) & ~At(Spare, Trunk) & \\\n",
" ~At(Flat, Ground) & ~At(Flat, Axle) & ~At(Flat, Trunk)')])\n",
"
def three_block_tower():\n",
" """\n",
" [Figure 10.3] THREE-BLOCK-TOWER\n",
"\n",
" A blocks-world problem of stacking three blocks in a certain configuration,\n",
" also known as the Sussman Anomaly.\n",
"\n",
" Example:\n",
" >>> from planning import *\n",
" >>> tbt = three_block_tower()\n",
" >>> tbt.goal_test()\n",
" False\n",
" >>> tbt.act(expr('MoveToTable(C, A)'))\n",
" >>> tbt.act(expr('Move(B, Table, C)'))\n",
" >>> tbt.goal_test()\n",
" False\n",
" >>> tbt.act(expr('Move(A, Table, B)'))\n",
" >>> tbt.goal_test()\n",
" True\n",
" >>>\n",
" """\n",
"\n",
" return PlanningProblem(init='On(A, Table) & On(B, Table) & On(C, A) & Block(A) & Block(B) & Block(C) & Clear(B) & Clear(C)',\n",
" goals='On(A, B) & On(B, C)',\n",
" actions=[Action('Move(b, x, y)',\n",
" precond='On(b, x) & Clear(b) & Clear(y) & Block(b) & Block(y)',\n",
" effect='On(b, y) & Clear(x) & ~On(b, x) & ~Clear(y)'),\n",
" Action('MoveToTable(b, x)',\n",
" precond='On(b, x) & Clear(b) & Block(b)',\n",
" effect='On(b, Table) & Clear(x) & ~On(b, x)')])\n",
"
def shopping_problem():\n",
" """\n",
" SHOPPING-PROBLEM\n",
"\n",
" A problem of acquiring some items given their availability at certain stores.\n",
"\n",
" Example:\n",
" >>> from planning import *\n",
" >>> sp = shopping_problem()\n",
" >>> sp.goal_test()\n",
" False\n",
" >>> sp.act(expr('Go(Home, HW)'))\n",
" >>> sp.act(expr('Buy(Drill, HW)'))\n",
" >>> sp.act(expr('Go(HW, SM)'))\n",
" >>> sp.act(expr('Buy(Banana, SM)'))\n",
" >>> sp.goal_test()\n",
" False\n",
" >>> sp.act(expr('Buy(Milk, SM)'))\n",
" >>> sp.goal_test()\n",
" True\n",
" >>>\n",
" """\n",
"\n",
" return PlanningProblem(init='At(Home) & Sells(SM, Milk) & Sells(SM, Banana) & Sells(HW, Drill)',\n",
" goals='Have(Milk) & Have(Banana) & Have(Drill)', \n",
" actions=[Action('Buy(x, store)',\n",
" precond='At(store) & Sells(store, x)',\n",
" effect='Have(x)'),\n",
" Action('Go(x, y)',\n",
" precond='At(x)',\n",
" effect='At(y) & ~At(x)')])\n",
"
def socks_and_shoes():\n",
" """\n",
" SOCKS-AND-SHOES-PROBLEM\n",
"\n",
" A task of wearing socks and shoes on both feet\n",
"\n",
" Example:\n",
" >>> from planning import *\n",
" >>> ss = socks_and_shoes()\n",
" >>> ss.goal_test()\n",
" False\n",
" >>> ss.act(expr('RightSock'))\n",
" >>> ss.act(expr('RightShoe'))\n",
" >>> ss.act(expr('LeftSock'))\n",
" >>> ss.goal_test()\n",
" False\n",
" >>> ss.act(expr('LeftShoe'))\n",
" >>> ss.goal_test()\n",
" True\n",
" >>>\n",
" """\n",
"\n",
" return PlanningProblem(init='',\n",
" goals='RightShoeOn & LeftShoeOn',\n",
" actions=[Action('RightShoe',\n",
" precond='RightSockOn',\n",
" effect='RightShoeOn'),\n",
" Action('RightSock',\n",
" precond='',\n",
" effect='RightSockOn'),\n",
" Action('LeftShoe',\n",
" precond='LeftSockOn',\n",
" effect='LeftShoeOn'),\n",
" Action('LeftSock',\n",
" precond='',\n",
" effect='LeftSockOn')])\n",
"
def have_cake_and_eat_cake_too():\n",
" """\n",
" [Figure 10.7] CAKE-PROBLEM\n",
"\n",
" A problem where we begin with a cake and want to \n",
" reach the state of having a cake and having eaten a cake.\n",
" The possible actions include baking a cake and eating a cake.\n",
"\n",
" Example:\n",
" >>> from planning import *\n",
" >>> cp = have_cake_and_eat_cake_too()\n",
" >>> cp.goal_test()\n",
" False\n",
" >>> cp.act(expr('Eat(Cake)'))\n",
" >>> cp.goal_test()\n",
" False\n",
" >>> cp.act(expr('Bake(Cake)'))\n",
" >>> cp.goal_test()\n",
" True\n",
" >>>\n",
" """\n",
"\n",
" return PlanningProblem(init='Have(Cake)',\n",
" goals='Have(Cake) & Eaten(Cake)',\n",
" actions=[Action('Eat(Cake)',\n",
" precond='Have(Cake)',\n",
" effect='Eaten(Cake) & ~Have(Cake)'),\n",
" Action('Bake(Cake)',\n",
" precond='~Have(Cake)',\n",
" effect='Have(Cake)')])\n",
"