Skip to content

Commit

Permalink
updated set_table example for new API
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Binney committed Jun 3, 2012
1 parent 3043a5e commit 435831d
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 97 deletions.
3 changes: 2 additions & 1 deletion .gitignore
@@ -1 +1,2 @@
*.pyc
*.pyc
build
9 changes: 9 additions & 0 deletions python_task_planning/examples/set_table/README
@@ -0,0 +1,9 @@
# set_table.py runs HPN on a simple domain described only by conjunction of fluents.
# the optional argument to set_table.py is the filename where it should output a .dot graph
# description which shows the hierarchy created by the planner
roscd python_task_planning
python examples/set_table/set_table.py graph.dot

# to view the resulting graph, first convert it to a png file
dot -Tpng -ograph.png graph.dot
display graph.png
179 changes: 95 additions & 84 deletions python_task_planning/examples/set_table/set_table.py
@@ -1,7 +1,7 @@
import roslib; roslib.load_manifest('hierarchical_interactive_planning')
import roslib; roslib.load_manifest('python_task_planning')
import sys

import hierarchical_interactive_planning as hip
import python_task_planning as ptp

######################################################################################################################
# World
Expand All @@ -19,7 +19,7 @@ def execute(self, op):
for f in [op.target] + list(op.side_effects.fluents):
if not self.current_state.entails(f):
fluents.append(f)
self.current_state = hip.ConjunctionOfFluents(fluents)
self.current_state = ptp.ConjunctionOfFluents(fluents)

return self.current_state

Expand All @@ -30,112 +30,123 @@ def entails(self, cof):
# Suggesters
######################################################################################################################

def table_to_set_suggester(current_state, goal):
def table_to_set_suggester(world, current_state, goal):
for f in goal.fluents:
if f.name == 'TableIsSet':
yield f._args[0]

def cup_suggester(current_state, goal):
yield 'cup1'
def cup_suggester(world, current_state, goal):
yield ptp.Symbol('cup1')

def bowl_suggester(current_state, goal):
yield bowl
def bowl_suggester(world, current_state, goal):
yield ptp.Symbol('bowl1')

def setting_location_suggester(current_state, goal):
yield 'loc1'
def setting_location_suggester(world, current_state, goal):
yield ptp.Symbol('loc1')

######################################################################################################################
# Predicates
######################################################################################################################

TableIsSet = hip.Predicate('TableIsSet', ['table'])
CupIsSet = hip.Predicate('CupIsSet', ['cup'])
BowlIsSet = hip.Predicate('BowlIsSet', ['bowl'])
LocKnown = hip.Predicate('LocKnown', ['object'])
ObjectAtLoc = hip.Predicate('ObjectAtLoc', ['object', 'loc'])
ObjectInGripper = hip.Predicate('ObjectInGripper', ['object'])
TableIsSet = ptp.Predicate('TableIsSet', ['table'])
CupIsSet = ptp.Predicate('CupIsSet', ['cup'])
BowlIsSet = ptp.Predicate('BowlIsSet', ['bowl'])
LocKnown = ptp.Predicate('LocKnown', ['object'])
ObjectAtLoc = ptp.Predicate('ObjectAtLoc', ['object', 'loc'])
ObjectInGripper = ptp.Predicate('ObjectInGripper', ['object'])

######################################################################################################################
# Operators
######################################################################################################################

operators = [
hip.Operator(
'SetTable',
target = TableIsSet(('Table',)),
exists = {'Cup':cup_suggester, 'Bowl':bowl_suggester, 'SettingLocation':setting_location_suggester},
preconditions = [
(1, CupIsSet(('Cup', 'SettingLocation'))),
(1, BowlIsSet(('Bowl', 'SettingLocation')))],
side_effects = hip.ConjunctionOfFluents([]),
primitive = False
),

hip.Operator(
'SetCup',
target = CupIsSet(('Cup', 'SettingLocation')),
exists = {},
preconditions = [
(1, LocKnown(('Cup',))),
(2, ObjectAtLoc(('Cup', 'SettingLocation')))],
side_effects = hip.ConjunctionOfFluents([]),
primitive = False
),

hip.Operator(
'SetBowl',
target = BowlIsSet(('Bowl', 'SettingLocation')),
exists = {},
preconditions = [
(1, LocKnown(('Bowl',))),
(2, ObjectAtLoc(('Bowl', 'SettingLocation')))],
side_effects = hip.ConjunctionOfFluents([]),
primitive = False
),

hip.Operator(
'Put',
target = ObjectAtLoc(('Object', 'Location')),
exists = {},
preconditions = [
(1, ObjectInGripper(('Object',)))],
side_effects = hip.ConjunctionOfFluents([]),
primitive=True
),

hip.Operator(
'Pick',
target = ObjectInGripper(('Object',)),
exists = {},
preconditions = [],
side_effects = hip.ConjunctionOfFluents([]),
primitive=True
),

hip.Operator(
'DoDetection',
target = LocKnown(('Object',)),
exists = {},
preconditions = [],
side_effects = hip.ConjunctionOfFluents([]),
primitive=True
)
]

table = ptp.Variable('table')
cup = ptp.Variable('cup')
bowl = ptp.Variable('bowl')
setting_location = ptp.Variable('setting_location')
SetTable = ptp.Operator(
'SetTable',
target = TableIsSet((table,)),
suggesters = {cup:cup_suggester, bowl:bowl_suggester, setting_location:setting_location_suggester},
preconditions = [
(1, CupIsSet((cup, setting_location))),
(1, BowlIsSet((bowl, setting_location)))],
side_effects = ptp.ConjunctionOfFluents([]),
primitive = False
)

cup = ptp.Variable('cup')
setting_location = ptp.Variable('setting_location')
SetCup = ptp.Operator(
'SetCup',
target = CupIsSet((cup, setting_location)),
suggesters = {},
preconditions = [
(1, LocKnown((cup,))),
(2, ObjectAtLoc((cup, setting_location)))],
side_effects = ptp.ConjunctionOfFluents([]),
primitive = False
)

bowl = ptp.Variable('bowl')
setting_location = ptp.Variable('setting_location')
SetBowl = ptp.Operator(
'SetBowl',
target = BowlIsSet((bowl, setting_location)),
suggesters = {},
preconditions = [
(1, LocKnown((bowl,))),
(2, ObjectAtLoc((bowl, setting_location)))],
side_effects = ptp.ConjunctionOfFluents([]),
primitive = False
)

obj = ptp.Variable('object')
location = ptp.Variable('location')
Put = ptp.Operator(
'Put',
target = ObjectAtLoc((obj, location)),
suggesters = {},
preconditions = [
(1, ObjectInGripper((obj,)))],
side_effects = ptp.ConjunctionOfFluents([]),
primitive=True
)

obj = ptp.Variable('object')
Pick = ptp.Operator(
'Pick',
target = ObjectInGripper((obj,)),
suggesters = {},
preconditions = [],
side_effects = ptp.ConjunctionOfFluents([]),
primitive=True
)

obj = ptp.Variable('object')
DoDetection = ptp.Operator(
'DoDetection',
target = LocKnown((obj,)),
suggesters = {},
preconditions = [],
side_effects = ptp.ConjunctionOfFluents([]),
primitive=True
)

operators = [SetTable, SetCup, SetBowl, Put, Pick, DoDetection]

if __name__ == '__main__':
start_state = hip.ConjunctionOfFluents([])
start_state = ptp.ConjunctionOfFluents([])
world = SushiWorld(start_state)
goal = hip.ConjunctionOfFluents([TableIsSet(('table',))])
goal = ptp.ConjunctionOfFluents([TableIsSet((ptp.Symbol('table'),))])

# run HPN to generate plan
tree = hip.HPlanTree()
hip.hpn(operators, start_state, goal, world, tree=tree)
tree = ptp.HPlanTree()
ptp.hpn(operators, start_state, goal, world, tree=tree)

# write out a dot graph of the plan
if len(sys.argv) > 1:
outfile = sys.argv[1]
f = open(outfile, 'w+')
f.write(hip.dot_from_plan_tree(tree).string())
f.write(ptp.dot_from_plan_tree(tree).string())
f.close()

4 changes: 2 additions & 2 deletions python_task_planning/manifest.xml
@@ -1,5 +1,5 @@
<package>
<description brief="hip">
<description brief="python_task_planning">

hip

Expand All @@ -9,7 +9,7 @@
<review status="unreviewed" notes=""/>
<url>http://ros.org/wiki/hip</url>

<rosdep name="pygraphviz"/>
<rosdep name="python-pygraphviz"/>

</package>

Expand Down
2 changes: 0 additions & 2 deletions python_task_planning/rosdep.yaml

This file was deleted.

8 changes: 4 additions & 4 deletions python_task_planning/src/python_task_planning/__init__.py
@@ -1,5 +1,5 @@
from hierarchical_interactive_planning.common import Symbol, Variable, Fluent, ConjunctionOfFluents, AbstractionInfo, \
from python_task_planning.common import Symbol, Variable, Fluent, ConjunctionOfFluents, AbstractionInfo, \
Operator, OperatorInstance, HPlanTree, Predicate
from hierarchical_interactive_planning.hpn import hpn
from hierarchical_interactive_planning.dot_graph import dot_from_plan_tree
from hierarchical_interactive_planning.exceptions import PlanningFailedError
from python_task_planning.hpn import hpn
from python_task_planning.dot_graph import dot_from_plan_tree
from python_task_planning.exceptions import PlanningFailedError
2 changes: 1 addition & 1 deletion python_task_planning/src/python_task_planning/dot_graph.py
@@ -1,4 +1,4 @@
from hierarchical_interactive_planning import ConjunctionOfFluents, OperatorInstance
from python_task_planning import ConjunctionOfFluents, OperatorInstance
import pygraphviz as pgv

lpk_style = dict(
Expand Down
6 changes: 3 additions & 3 deletions python_task_planning/src/python_task_planning/hpn.py
@@ -1,7 +1,7 @@
import numpy as np
from hierarchical_interactive_planning import AbstractionInfo, ConjunctionOfFluents, HPlanTree
from hierarchical_interactive_planning.exceptions import PlanningFailedError
from hierarchical_interactive_planning.a_star import a_star
from python_task_planning import AbstractionInfo, ConjunctionOfFluents, HPlanTree
from python_task_planning.exceptions import PlanningFailedError
from python_task_planning.a_star import a_star

def hpn(operators, current_state, goal, world, abs_info=None, maxdepth=np.inf, depth=0, tree=None):
'''Implements HPN (Hierarchical Planning in the Now) algorithm of Kaelbing and Lozano-Perez.
Expand Down

0 comments on commit 435831d

Please sign in to comment.