def create_random_problem(): state0 = pyhop2.State() state0.loc = {} goal = pyhop2.State() goal.loc = {} for o in objects(): state0.loc[o] = random.sample(locations(), 1)[0] goal.loc[o] = random.sample(locations(), 1)[0] state0.loc['briefcase'] = random.sample(locations(), 1)[0] return state0, goal
def small_state(): s0 = pyhop2.State() s0.pos = {'0': '1', '1': '2', '2': 'table'} s0.stacks = [['2', '1', '0'], [], [], [], [], []] s0.clear = {'0': True, '1': False, '2': False} s0.max_stacks = 6 return s0
def simple_state(): s0 = pyhop2.State('s0') s0.pos = { 'a': 'table', 'b': 'table', 'c': 'a', 'd': 'table', 'e': 'c', 'f': 'e', 'g': 'f', 'h': 'table' } s0.clear = { 'a': False, 'b': True, 'c': False, 'd': True, 'e': False, 'f': False, 'g': True, 'h': True } s0.stacks = [['a', 'c', 'e', 'f', 'g'], ['b'], ['d'], ['h']] s0.max_stacks = 4 return s0
def gen_blocks_state(num_blocks: int, stacking_likelihood: float, max_stacks, start_state=False): """ creating a list of procedurally named blocks """ block_counter = 0 blocks = [] while(block_counter < num_blocks): blocks += [block_counter.__str__()] block_counter += 1 """ initializing the random state to be developed and returned """ state = pyhop2.State() state.pos = {} state.clear = {} state.max_stacks = max_stacks if num_blocks == 0 : return state state.pos[blocks[0]] = 'table' state.clear[blocks[0]] = True clear_blocks = [blocks[0]] #the set of blocks where state.clear[block] == True """ Iterating through the blocks and putting the block on the table with probability (1 - stacking_probability) unless the maximum number of stacks is reached, in which case it automatically choses to stack. If the block is to be stacked, it is equally likely to be placed on any clear block. """ table_counter = 1 for block in blocks[1:] : observed = np.random.uniform(0,1) if observed > stacking_likelihood and table_counter < max_stacks : state.pos[block] = 'table' state.clear[block] = True clear_blocks += [block] table_counter += 1 else : clear_index = random.randint(0, len(clear_blocks)-1) #choosing a random stack random_clear_block = clear_blocks[clear_index] state.pos[block] = random_clear_block state.clear[random_clear_block] = False state.clear[block] = True #current block replaces random_clear_block in clear status clear_blocks[clear_index] = block permute_blocks(state) if start_state: state.stacks = gen_list_representation(state, max_stacks) return state
def small_state(): s0 = pyhop2.State() s0.pos = { 'a': 'table', 'b': 'table', 'c': 'a', 'd': 'table', 'e': 'table', 'f': 'table' } s0.clear = { 'a': False, 'b': True, 'c': True, 'd': True, 'e': True, 'f': True } s0.stacks = [['a', 'c'], ['b'], ['d'], ['e'], ['f']] s0.max_stacks = _current_domain.max_stacks return s0
renderer = Renderer() ######################################################### ## Grocery Plan ######################################### # loc = {'robot': (7.4, 12.85), 'brocoli': (1.54, 5.4), 'cucumber': (1.54, 7.91), 'salt': (8.39, 4.79), 'watermelon': (5.1, 8.44), 'strawberry': (2.31, 6.36), 'potato': (3.58, 7.44), 'hamburger': (7.4, 12.85)} domain_name = 'groceryplan' # Create a new domain to contain the methods and operators pyhop2.Domain(domain_name) # states and rigid relations rigid = pyhop2.State('rigid relations') # These types are used by the 'is_a' helper function, later in this file rigid.types = { 'person': ['robot'], 'location': item_list} # prototypical initial state state0 = pyhop2.State('state0') state0.loc = {'robot':(1,1)} state0.cost = {'robot': 0} # adding items in the list for item in item_list: state0.loc[item] = prod.product_list[item]
return [[('walk', a, x, y)]] return False def travel_by_taxi(state, a, x, y): if state.cash[a] >= taxi_rate(state.dist[x][y]): return [[('call_taxi', a, x), ('ride_taxi', a, x, y), ('pay_driver', a)]] return False pyhop2.declare_methods('travel', travel_by_foot, travel_by_taxi) print('') pyhop2.print_methods() state1 = pyhop2.State('state1') state1.loc = {'me': 'home'} state1.cash = {'me': 20} state1.owe = {'me': 0} state1.dist = {'home': {'park': 8}, 'park': {'home': 8}} print(""" ******************************************************************************** Call pyhop.pyhop(state1,[('travel','me','home','park')]) with different verbosity levels ******************************************************************************** """) print( "- If verbose=0 (the default), Pyhop returns the solution but prints nothing.\n" ) pyhop2.pyhop(state1, [('travel', 'me', 'home', 'park')])
def main(): # If we've changed to some other domain, this will change us back. print(f"Changing current domain to {__name__}, if it isn't that already.") pyhop2.set_current_domain(__name__) pyhop2.print_domain() print("\nLet's call find_plan on some simple things that should fail.\n") state1 = pyhop2.State('state1') state1.pos = {'a': 'b', 'b': 'table', 'c': 'table'} state1.clear = {'c': True, 'b': False, 'a': True} state1.holding = {'hand': False} state1.display('Initial state is') plan = pyhop2.find_plan(state1, [('pickup', 'a')], verbose=1) check_result(plan, False) plan = pyhop2.find_plan(state1, [('pickup', 'b')], verbose=1) check_result(plan, False) plan = pyhop2.find_plan(state1, [('pos', 'b', 'hand')], verbose=1) check_result(plan, False) pause() print(""" Next, some simple things that should succeed. As a reminder, in state1, block a is on block b, block b is on the table, and block c is on the table. """) plan = pyhop2.find_plan(state1, [('pickup', 'c')], verbose=1) check_result(plan, [('pickup', 'c')]) plan = pyhop2.find_plan(state1, [('unstack', 'a', 'b')], verbose=1) check_result(plan, [('unstack', 'a', 'b')]) plan = pyhop2.find_plan(state1, [('pos', 'a', 'hand')], verbose=1) check_result(plan, [('unstack', 'a', 'b')]) plan = pyhop2.find_plan(state1, [('pos', 'c', 'hand')], verbose=1) check_result(plan, [('pickup', 'c')]) plan = pyhop2.find_plan(state1, [('pos', 'a', 'table')], verbose=1) check_result(plan, [('unstack', 'a', 'b'), ('putdown', 'a')]) pause() print(""" A Multigoal is a data structure that specifies desired values for some of the state variables. In blocks_tasks.py there are examples of tasks whose arguments are multigoals, but here we give the multigoals directly to find_plan. Below, goal1a says we want the blocks in the configuration "c on b on a", and goal1a says we want "c on b on a on the table". goal1a and goal1b have the same set of solutions, because the "a on the table" will be achieved anyway. """) state1.display("Initial state is") goal1a = pyhop2.Multigoal('goal1a') goal1a.pos = {'c': 'b', 'b': 'a', 'a': 'table'} goal1a.display() goal1b = pyhop2.Multigoal('goal1b') goal1b.pos = {'c': 'b', 'b': 'a'} goal1b.display() expected = [('unstack', 'a', 'b'), ('putdown', 'a'), ('pickup', 'b'), ('stack', 'b', 'a'), ('pickup', 'c'), ('stack', 'c', 'b')] plan1 = pyhop2.find_plan(state1, [goal1a], verbose=1) check_result(plan1, expected) plan2 = pyhop2.find_plan(state1, [goal1b], verbose=1) check_result(plan2, expected) pause() print(""" Run find_plan on two more planning problems. Like before, goal2a omits some of the conditions in goal2a, but both goals should produce the same plan. """) state2 = pyhop2.State('state2') state2.pos = {'a': 'c', 'b': 'd', 'c': 'table', 'd': 'table'} state2.clear = {'a': True, 'c': False, 'b': True, 'd': False} state2.holding = {'hand': False} state2.display('Initial state is') goal2a = pyhop2.Multigoal('goal2a') goal2a.pos = {'b': 'c', 'a': 'd', 'c': 'table', 'd': 'table'} goal2a.clear = {'a': True, 'c': False, 'b': True, 'd': False} goal2a.holding = {'hand': False} goal2a.display() goal2b = pyhop2.Multigoal('goal2b') goal2b.pos = {'b': 'c', 'a': 'd'} goal2b.display() ### goal2b omits some of the conditions of goal2a, ### but those conditions will need to be achieved anyway. expected = [('unstack', 'a', 'c'), ('putdown', 'a'), ('unstack', 'b', 'd'), ('stack', 'b', 'c'), ('pickup', 'a'), ('stack', 'a', 'd')] plan1 = pyhop2.find_plan(state2, [goal2a], verbose=1) check_result(plan1, expected) plan2 = pyhop2.find_plan(state2, [goal2b], verbose=1) check_result(plan2, expected) pause() print( "\nRun find_plan on problem bw_large_d from the SHOP distribution:\n") state3 = pyhop2.State('state3') state3.pos = { 1: 12, 12: 13, 13: 'table', 11: 10, 10: 5, 5: 4, 4: 14, 14: 15, 15: 'table', 9: 8, 8: 7, 7: 6, 6: 'table', 19: 18, 18: 17, 17: 16, 16: 3, 3: 2, 2: 'table' } state3.clear = {x: False for x in range(1, 20)} state3.clear.update({1: True, 11: True, 9: True, 19: True}) state3.holding = {'hand': False} state3.display('Initial state is') goal3 = pyhop2.Multigoal('goal3') goal3.pos = { 15: 13, 13: 8, 8: 9, 9: 4, 4: 'table', 12: 2, 2: 3, 3: 16, 16: 11, 11: 7, 7: 6, 6: 'table' } goal3.clear = {17: True, 15: True, 12: True} goal3.display() expected = [('unstack', 1, 12), ('putdown', 1), ('unstack', 19, 18), ('putdown', 19), ('unstack', 18, 17), ('putdown', 18), ('unstack', 17, 16), ('putdown', 17), ('unstack', 9, 8), ('putdown', 9), ('unstack', 8, 7), ('putdown', 8), ('unstack', 11, 10), ('stack', 11, 7), ('unstack', 10, 5), ('putdown', 10), ('unstack', 5, 4), ('putdown', 5), ('unstack', 4, 14), ('putdown', 4), ('pickup', 9), ('stack', 9, 4), ('pickup', 8), ('stack', 8, 9), ('unstack', 14, 15), ('putdown', 14), ('unstack', 16, 3), ('stack', 16, 11), ('unstack', 3, 2), ('stack', 3, 16), ('pickup', 2), ('stack', 2, 3), ('unstack', 12, 13), ('stack', 12, 2), ('pickup', 13), ('stack', 13, 8), ('pickup', 15), ('stack', 15, 13)] plan = pyhop2.find_plan(state3, [goal3], verbose=1) check_result(plan, expected) pause() print(""" Call run_lazy_lookahead on the following problem, with verbose=1: """) state2.display(heading='Initial state is') goal2b.display(heading='Goal is') new_state = pyhop2.run_lazy_lookahead(state2, [goal2b], verbose=1) pause() print( "The goal should now be satisfied, so the planner should return an empty plan:\n" ) plan = pyhop2.find_plan(new_state, [goal2b], verbose=1) check_result(plan, []) print("No more examples")
""" Some examples that show Pyhop 2 backtracking over several goals and methods. Author: Dana Nau <*****@*****.**> Feb 22, 2021 """ import pyhop2 # Create a new domain that has the same name as this file. This avoids having # to explicitly change the domain name every time I change the filename. domain_name = __name__ pyhop2.Domain(domain_name) ############################################################################### # States: state0 = pyhop2.State() state0.flag = -1 ############################################################################### # Methods: def m_err(state): return [('putv', 0), ('getv', 1)] def m0(state): return [('putv', 0), ('getv', 0)] def m1(state):
def small_goal(): s = pyhop2.State() s.pos = {'2': '1', '1': '0', '0': 'table'} s.clear = {'2': True, '1': False, '0': False} s.stacks = [['0', '1', '2'], [], [], [], [], []] return s
The methods are not set up to solve state.clear[b1] == False the 'clear' dict can be inferred from the 'pos' dict. """ # g.clear = g_rand.clear """ NOTE 2: The methods are only intended to solve complete plans, so I'm not sure if trying to plan for a goal in which not all blocks from the initial state have a declared position will work. """ """Finding a solution, the displaying the initial state and goal""" """ You can either pass a [multigoal] as the todo or you can pass [('solve_goal', s0, goal_state)] as the todo """ print(pyhop2.find_plan(s_rand, [('solve_goal', g_rand)])) # print(gen.gen_list_representation(s_rand, max_stacks)) # print(gen.gen_list_representation(g, max_stacks)) """Some simple tests I used for debugging""" s0 = simple_state() s0 = pyhop2.State() s0.pos = {'a': 'table', 'b': 'table', 'c': 'a', 'd': 'table', 'e': 'table'} s0.clear = {'a': False, 'b': True, 'c': True, 'd': True, 'e': True} s0.stacks = [['a', 'c'], ['b'], ['d'], ['e']] s0.max_stacks = 4 g_dummy = s0.copy() g_dummy.clear = {} g_dummy.pos['c'] = 'table' g_dummy.pos['b'] = 'a' print(pyhop2.find_plan(s0, [('solve_goal', g_dummy)]))
import random from itertools import combinations from timeit import default_timer as timer # For a more clever way to specify the domain name, # see blocks_tasks.py or blocks_goals.py domain_name = 'briefcaseworld' # Create a new domain to contain the methods and operators _current_domain = pyhop2.Domain(domain_name) ################################################################################ # states and rigid relations rigid = pyhop2.State('rigid relations') # These types are used by the 'is_a' helper function, later in this file rigid.types = { 'location': [], 'object': []} rigid.dist = {} ############################################################################### # Helper functions: def is_a(variable,type): return variable in rigid.types[type] def locations(): return rigid.types['location']
""" import pyhop2 import random # For a more clever way to specify the domain name, # see blocks_tasks.py or blocks_goals.py domain_name = 'simple_tasks2' # Create a new domain to contain the methods and operators. pyhop2.Domain(domain_name) ################################################################################ # states and rigid relations rigid = pyhop2.State('rigid relations') # These types are used by the 'is_a' helper function, later in this file rigid.types = { 'person': ['alice', 'bob'], 'location': ['home_a', 'home_b', 'park', 'station'], 'taxi': ['taxi1', 'taxi2'] } rigid.dist = { ('home_a', 'park'): 8, ('home_b', 'park'): 2, ('station', 'home_a'): 1, ('station', 'home_b'): 7, ('home_a', 'home_b'): 7, ('station', 'park'): 9 }