def __init__(self): self.failure_reason = "" self.verbose = 0 # Helper methods def is_within_bounds(location1, min_bounds, max_bounds): for i in range(len(location1)): if location1[i] < min_bounds[i] or location1[i] > max_bounds[i]: return False return True # Operators (primitive tasks) def move_arm(state, to_): arm_min_bounds = state.min_bounds["xyz"] arm_max_bounds = state.max_bounds["xyz"] if to_ == "target_object" or to_ == "container": xyz = state.xyz[to_] if not is_within_bounds(xyz, arm_min_bounds, arm_max_bounds): self.failure_reason = "can't move arm to {}: {} outside of bounds: {} {}"\ .format(to_, xyz, arm_min_bounds, arm_max_bounds) return False return state return state def move_arm_above(state, to_): return move_arm(state, to_) def close_hand(state): gripper_min_bounds = state.min_bounds["object_side_length"] gripper_max_bounds = state.max_bounds["object_side_length"] distance = state.size['object_side_length'] if distance < gripper_min_bounds or distance > gripper_max_bounds: self.failure_reason = "can't close hand to distance {}".format( distance) print(self.failure_reason) return False return state def open_hand(state): gripper_min_bounds = state.min_bounds["object_side_length"] gripper_max_bounds = state.max_bounds["object_side_length"] distance = state.size['object_side_length'] if distance < gripper_min_bounds or distance > gripper_max_bounds: self.failure_reason = "can't open hand to distance {}".format( distance) return False return state def initialize(state, actor): if actor == "arm": state.initialized["arm"] = True return state self.failure_reason = "{} can't initialize".format(actor) return False pyhop.declare_operators(initialize, move_arm, move_arm_above, close_hand, open_hand) if self.verbose > 0: pyhop.print_operators() # Methods (compound tasks) def put_grabbed(state, actor, actee, from_, to_): if actor == "arm": if state.grabbed["target_object"]: return [('move_arm_above', to_), ('open_hand', )] return False def full_transfer(state, actor, actee, from_, to_): if actor == "arm": return [('initialize', actor), ('open_hand', ), ('move_arm_above', 'target_object'), ('move_arm', 'target_object'), ('close_hand', ), ('move_arm_above', 'target_object'), ('move_arm_above', to_), ('open_hand', )] return False def transfer(state, actor, actee, from_, to_): if actor == "arm": if state.initialized["arm"]: return [('move_arm_above', 'target_object'), ('move_arm', 'target_object'), ('close_hand', ), ('move_arm_above', 'target_object'), ('move_arm_above', to_), ('open_hand', )] return False pyhop.declare_methods('transfer_target_object_to_container', full_transfer, put_grabbed, transfer)
'''cleancups = 0 while cleancups <= state.NUMBER_OF_DIRTY_TEACUPS: cup = 'teacup'+str(random.randint(1, state.TOTAL_NUMBER_OF_TEACUPS)) if(state2.itemstate[cup]['cleanstate'] == Itemstate.unknown): state2.itemstate[cup]['cleanstate'] = Itemstate.clean cleancups = cleancups + 1''' state.currentcup = '' return state print( '''Running: pyhop.pyhop(teaathome.setupRobotArm(test1()),[('taskmaketea','robot','teabag', 1)],verbose=2)''' ) print('') teaathome.setupTeaAtHome() pyhop.print_operators() print('') pyhop.print_methods() print('') pyhop.pyhop(teaathome.setupRobotArm(test1()), [('taskmaketea', 'robot', 'teabag', 1)], verbose=2) sys.stdout.close() sys.stdout = sys.__stdout__ print('Result log file: logs/test1.log')
""" The "travel from home to the park" example from my lectures. Author: Dana Nau <*****@*****.**>, November 15, 2012 This file should work correctly in both Python 2.7 and Python 3.2. """ from __future__ import print_function import pyhop import simple_travel_operators print('') pyhop.print_operators() import simple_travel_methods print('') pyhop.print_methods() state1 = pyhop.State('state1') state1.loc = {'me':'home'} state1.cash = {'me':20} # To get multidimensional tables in Python, # you have to use nested lists # (or in this case, nested dictionaries) state1.dist = {'home':{'park':8}} print(""" **************************************** Call pyhop.pyhop(state1,[('travel','me','home','park')]) with different levels of verbosity ****************************************