Planning Introduction
Create plans to travel in Romania
from planning import *
from notebook import psource
from utils import *
# this imports the required expr so we can create our knowledge base
knowledge_base = [
expr("Connected(Bucharest,Pitesti)"),
expr("Connected(Pitesti,Rimnicu)"),
expr("Connected(Rimnicu,Sibiu)"),
expr("Connected(Sibiu,Fagaras)"),
expr("Connected(Fagaras,Bucharest)"),
expr("Connected(Pitesti,Craiova)"),
expr("Connected(Craiova,Rimnicu)")
]
knowledge_base.extend([
expr("Connected(x,y) ==> Connected(y,x)"),
expr("Connected(x,y) & Connected(y,z) ==> Connected(x,z)"),
expr("At(Sibiu)")
])
We have a First Order Logic knowledge base containing expressions showing connections among cities in Romanian, and also general rules for reasoning about locations.
We are currently at Sibiu.
knowledge_base
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.
We can define these flight actions like this:
#Sibiu to Bucharest
precond = 'At(Sibiu)'
effect = 'At(Bucharest) & ~At(Sibiu)'
fly_s_b = Action('Fly(Sibiu, Bucharest)', precond, effect)
#Bucharest to Sibiu
precond = 'At(Bucharest)'
effect = 'At(Sibiu) & ~At(Bucharest)'
fly_b_s = Action('Fly(Bucharest, Sibiu)', precond, effect)
#Sibiu to Craiova
precond = 'At(Sibiu)'
effect = 'At(Craiova) & ~At(Sibiu)'
fly_s_c = Action('Fly(Sibiu, Craiova)', precond, effect)
#Craiova to Sibiu
precond = 'At(Craiova)'
effect = 'At(Sibiu) & ~At(Craiova)'
fly_c_s = Action('Fly(Craiova, Sibiu)', precond, effect)
#Bucharest to Craiova
precond = 'At(Bucharest)'
effect = 'At(Craiova) & ~At(Bucharest)'
fly_b_c = Action('Fly(Bucharest, Craiova)', precond, effect)
#Craiova to Bucharest
precond = 'At(Craiova)'
effect = 'At(Bucharest) & ~At(Craiova)'
fly_c_b = Action('Fly(Craiova, Bucharest)', precond, effect)
And the drive actions like this.
#Drive
precond = 'At(x)'
effect = 'At(y) & ~At(x)'
drive = Action('Drive(x, y)', precond, effect)
Our goal is defined as
goals = 'At(Bucharest)'
Thus, with all the components in place, we can define the planning problem.
prob = PlanningProblem(knowledge_base, goals, [fly_s_b, fly_b_s, fly_s_c, fly_c_s, fly_b_c, fly_c_b, drive])
dir(prob)
prob.init
prob.goals
prob.goal_test()
prob.actions
solution = [expr("Fly(Sibiu, Bucharest)")]
for action in solution:
prob.act(action)
prob.goal_test()
prob.init
solution = [expr("Drive(Bucharest,Sibiu)")]
prob.goals = [expr("At(Sibiu)")]
prob.goal_test()
for act in solution:
prob.act(act)
prob.goal_test()
prob.init