def policy(obs): nonlocal expected_next_state nonlocal plan state = obs.literals goal = obs.goal objects = obs.objects if False: #len(plan) > 0: expected_next_state = get_next_expected_state(state, plan[0]) return plan.pop(0) # Add possible actions to state full_state = set(state) full_state.update(action_space.all_ground_literals(obs)) # Create problem file fname = '/tmp/problem.pddl' PDDLProblemParser.create_pddl_file(fname, objects, full_state, "myproblem", domain_name, goal) # Get plan print("goal:", goal) try: plan = planner.get_plan(fname, use_cache=False) print("plan:", plan) except NoPlanFoundException: # Default to random print("no plan found") return action_space.sample(obs) # Updated expected next state expected_next_state = get_next_expected_state(state, plan[0]) return plan.pop(0)
def sample_problem(domain, problem_dir, problem_outfile): blocks, block_state = sample_blocks(domain, num_blocks=np.random.randint(30, 61)) goal = create_goal(domain, blocks, pile_heights=np.random.randint(3, 7, size=np.random.randint( 1, 3))) objects = blocks initial_state = block_state filepath = os.path.join(PDDLDIR, problem_dir, problem_outfile) PDDLProblemParser.create_pddl_file( filepath, objects=objects, initial_state=initial_state, problem_name="manyblocksnopiles", domain_name=domain.domain_name, goal=goal, fast_downward_order=True, ) print("Wrote out to {}.".format(filepath))
def __call__(self, domain, state, horizon=np.inf, timeout=10, return_files=False): act_predicates = [domain.predicates[a] for a in list(domain.actions)] act_space = LiteralSpace( act_predicates, type_to_parent_types=domain.type_to_parent_types) dom_file = tempfile.NamedTemporaryFile(delete=False).name prob_file = tempfile.NamedTemporaryFile(delete=False).name domain.write(dom_file) lits = set(state.literals) if not domain.operators_as_actions: lits |= set(act_space.all_ground_literals(state, valid_only=False)) PDDLProblemParser.create_pddl_file(prob_file, state.objects, lits, "myproblem", domain.domain_name, state.goal, fast_downward_order=True) pddl_plan = self.plan_from_pddl(dom_file, prob_file, horizon=horizon, timeout=timeout, remove_files=(not return_files)) plan = [ self._plan_step_to_action(domain, state, act_predicates, plan_step) for plan_step in pddl_plan ] if return_files: return plan, dom_file, prob_file return plan
def __call__(self, domain, state, timeout): act_preds = [domain.predicates[a] for a in list(domain.actions)] act_space = LiteralSpace( act_preds, type_to_parent_types=domain.type_to_parent_types) dom_file = tempfile.NamedTemporaryFile(delete=False).name prob_file = tempfile.NamedTemporaryFile(delete=False).name domain.write(dom_file) lits = set(state.literals) if not domain.operators_as_actions: lits |= set(act_space.all_ground_literals(state, valid_only=False)) PDDLProblemParser.create_pddl_file( prob_file, state.objects, lits, "myproblem", domain.domain_name, state.goal, fast_downward_order=True) cmd_str = self._get_cmd_str(dom_file, prob_file, timeout) start_time = time.time() output = subprocess.getoutput(cmd_str) self._cleanup() os.remove(dom_file) os.remove(prob_file) if time.time()-start_time > timeout: raise PlanningTimeout("Planning timed out!") pddl_plan = self._output_to_plan(output) plan = [self._plan_step_to_action(domain, state, act_preds, plan_step) for plan_step in pddl_plan] return plan
def sample_problem(domain, problem_dir, problem_outfile, num_locs, min_extra_papers=1, max_extra_papers=10): loc_type = domain.types['loc'] paper_type = domain.types['paper'] at = domain.predicates['at'] isHomeBase = domain.predicates['ishomebase'] wantsPaper = domain.predicates['wantspaper'] unpacked = domain.predicates['unpacked'] satisfied = domain.predicates['satisfied'] # Create objects and state state = set() objects = set() home_loc = None target_locs = [] # Add locations for loc_idx in range(num_locs): loc = loc_type(f"loc-{loc_idx}") objects.add(loc) # home if loc_idx == 0: state.add(isHomeBase(loc)) state.add(at(loc)) home_loc = loc # wants paper else: state.add(wantsPaper(loc)) target_locs.append(loc) # Add papers num_papers = num_locs + np.random.randint(min_extra_papers, max_extra_papers + 1) for paper_idx in range(num_papers): paper = paper_type(f"paper-{paper_idx}") objects.add(paper) state.add(unpacked(paper)) # Create goal goal_lits = [satisfied(loc) for loc in target_locs] goal = LiteralConjunction(goal_lits) filepath = os.path.join(PDDLDIR, problem_dir, problem_outfile) PDDLProblemParser.create_pddl_file( filepath, objects=objects, initial_state=state, problem_name="newspaper", domain_name=domain.domain_name, goal=goal, fast_downward_order=True, ) print("Wrote out to {}.".format(filepath))
def _create_problem_pddl(self, state, goal, prefix): fname = "/tmp/{}_problem_{}.pddl".format(prefix, random.randint(0, 9999999)) objects = state.objects all_action_lits = self._action_space.all_ground_literals(state) initial_state = state.literals | all_action_lits problem_name = "{}_problem".format(prefix) domain_name = self._planning_module.domain_name PDDLProblemParser.create_pddl_file(fname, objects, initial_state, problem_name, domain_name, goal) return fname
def sample_problem(domain, problem_dir, problem_outfile, num_rows=6, num_cols=6, num_people=1, num_selected_people=1, randomize_person_loc=False, randomize_robot_start=True, wall_probability=0.2, randomize_walls=False, randomize_hospital_loc=False, num_chickens=0): all_objects, initial_state, people, hospital_loc = sample_state( domain, num_rows=num_rows, num_cols=num_cols, num_people=num_people, randomize_person_loc=randomize_person_loc, randomize_robot_start=randomize_robot_start, wall_probability=wall_probability, randomize_walls=randomize_walls, randomize_hospital_loc=randomize_hospital_loc, num_chickens=num_chickens, ) if isinstance(num_selected_people, tuple): lo, hi = num_selected_people num_selected_people = np.random.randint(lo, hi + 1) goal = create_goal(domain, people, hospital_loc, num_selected_people=num_selected_people) filedir = os.path.join(PDDLDIR, problem_dir) os.makedirs(filedir, exist_ok=True) filepath = os.path.join(filedir, problem_outfile) PDDLProblemParser.create_pddl_file( filepath, objects=all_objects, initial_state=initial_state, problem_name=DOMAIN_NAME, domain_name=domain.domain_name, goal=goal, fast_downward_order=True, ) print("Wrote out to {}.".format(filepath)) problem_id = (frozenset(initial_state), goal) return problem_id, filepath
def sample_problem(domain, problem_dir, problem_outfile, min_num_piles=30, max_num_piles=50, min_num_piles_goal=1, max_num_piles_goal=2, max_num_blocks=1000): while True: blocks, block_state = sample_blocks( domain, pile_heights=np.random.randint(1, 3, size=np.random.randint( min_num_piles, max_num_piles + 1)), ) if len(blocks) <= max_num_blocks: break while True: goal = create_goal(domain, blocks, pile_heights=np.random.randint( 2, 4, size=np.random.randint(min_num_piles_goal, max_num_piles_goal + 1))) if not goal.holds(block_state): break objects = blocks initial_state = block_state filepath = os.path.join(PDDLDIR, problem_dir, problem_outfile) PDDLProblemParser.create_pddl_file( filepath, objects=objects, initial_state=initial_state, problem_name="manyquantifiedblocks", domain_name=domain.domain_name, goal=goal, fast_downward_order=True, ) print("Wrote out to {}.".format(filepath))
def sample_problem(domain, problem_dir, problem_outfile, seen_problems, min_num_piles=30, max_num_piles=50, min_num_piles_goal=1, max_num_piles_goal=2): while True: blocks, block_state = sample_blocks( domain, pile_heights=np.random.randint(1, 3, size=np.random.randint( min_num_piles, max_num_piles + 1)), ) goal = create_goal(domain, blocks, pile_heights=np.random.randint( 2, 4, size=np.random.randint(min_num_piles_goal, max_num_piles_goal + 1))) objects = frozenset(blocks) initial_state = frozenset(block_state) if (initial_state, objects, goal) in seen_problems: continue seen_problems.add((initial_state, objects, goal)) break filepath = os.path.join(PDDLDIR, problem_dir, problem_outfile) PDDLProblemParser.create_pddl_file( filepath, objects=objects, initial_state=initial_state, problem_name="generatedblocks", domain_name=domain.domain_name, goal=goal, fast_downward_order=True, ) print("Wrote out to {}.".format(filepath))
def _compute_all_ground_literals(self, state): """Call FastDownward's instantiator. """ # Generate temporary files to hand over to instantiator. assert state.objects == self._initial_state.objects dom_file = tempfile.NamedTemporaryFile(delete=False).name prob_file = tempfile.NamedTemporaryFile(delete=False).name # d_desc, domain_fname = tempfile.mkstemp(dir=TMP_PDDL_DIR, text=True) self.domain.write(dom_file) # p_desc, problem_fname = tempfile.mkstemp(dir=TMP_PDDL_DIR, text=True) # with os.fdopen(p_desc, "w") as f: PDDLProblemParser.create_pddl_file( file_or_filepath=prob_file, objects=state.objects - set(self.domain.constants), initial_state=self._initial_state.literals, problem_name="myproblem", domain_name=self.domain.domain_name, goal=state.goal, fast_downward_order=True) # PDDLProblemParser.create_pddl_file( # prob_file, objects, lits, "myproblem", # domain.domain_name, state.goal, fast_downward_order=True) # Call instantiator. task = downward_open(domain_filename=dom_file, task_filename=prob_file) with nostdout(): _, _, actions, _, _ = downward_explore(task) # Post-process to our representation. obj_name_to_obj = {obj.name: obj for obj in state.objects} all_ground_literals = set() for action in actions: name = action.name.strip().strip("()").split() pred_name, obj_names = name[0], name[1:] if len(set(obj_names)) != len(obj_names): continue pred = None for p in self.predicates: if p.name == pred_name: assert pred is None pred = p break assert pred is not None objs = [obj_name_to_obj[obj_name] for obj_name in obj_names] all_ground_literals.add(pred(*objs)) # os.close(d_desc) os.remove(dom_file) os.remove(prob_file) return all_ground_literals
def sample_problem(domain, problem_dir, problem_outfile, min_num_piles=30, max_num_piles=50, min_num_piles_goal=1, max_num_piles_goal=2): blocks, block_state = sample_blocks( domain, pile_heights=np.random.randint(1, 3, size=np.random.randint( min_num_piles, max_num_piles + 1)), ) goal = create_goal(domain, blocks, pile_heights=np.random.randint( 2, 4, size=np.random.randint(min_num_piles_goal, max_num_piles_goal + 1))) objects = blocks initial_state = block_state filepath = os.path.join(PDDLDIR, problem_dir, problem_outfile) PDDLProblemParser.create_pddl_file( filepath, objects=objects, initial_state=initial_state, problem_name="manyblockssmallpiles", domain_name=domain.domain_name, goal=goal, fast_downward_order=True, ) print("Wrote out to {}.".format(filepath))
def sample_problem(domain, problem_outfile): feet, feet_state = sample_feet(domain) socks, socks_state = sample_socks(domain, num_pairs=np.random.randint(10, 21)) shoes, shoes_state = sample_shoes(domain, num_pairs=np.random.randint(10, 21)) places, places_state = sample_places(domain, num_places_per_type=np.random.randint(5, 11)) objects = feet | socks | shoes | places initial_state = feet_state | socks_state | shoes_state | places_state goal = create_goal(domain, objects) filepath = os.path.join(PDDLDIR, "footwear", problem_outfile) PDDLProblemParser.create_pddl_file( filepath, objects=objects, initial_state=initial_state, problem_name="footwear", domain_name=domain.domain_name, goal=goal, fast_downward_order=True, ) print("Wrote out to {}.".format(filepath))
def policy(obs): nonlocal expected_next_state nonlocal plan state = obs.literals goal = obs.goal objects = obs.objects # Add possible actions to state full_state = set(state) full_state.update(self._action_space.all_ground_literals(obs)) # Create problem file file = tempfile.NamedTemporaryFile(delete=False) fname = file.name PDDLProblemParser.create_pddl_file(fname, objects, full_state, "myproblem", self.domain_name, goal) # Get plan plan = self._ff_planner.get_plan(fname, use_cache=False) # Updated expected next state expected_next_state = self._get_predicted_next_state_ops( obs, plan[0]) return plan.pop(0)
def sample_problem(domain, problem_dir, problem_outfile, min_num_balls=201, max_num_balls=300, min_num_rooms=101, max_num_rooms=200, min_num_balls_goal=5, max_num_balls_goal=20): all_objects, balls, rooms, initial_state = sample_state(domain, num_balls=np.random.randint(min_num_balls, max_num_balls+1), num_rooms=np.random.randint(min_num_rooms, max_num_rooms+1), ) goal = create_goal(domain, balls, rooms, num_balls=np.random.randint(min_num_balls_goal, max_num_balls_goal+1)) filepath = os.path.join(PDDLDIR, problem_dir, problem_outfile) PDDLProblemParser.create_pddl_file( filepath, objects=all_objects, initial_state=initial_state, problem_name="manygripper", domain_name=domain.domain_name, goal=goal, fast_downward_order=True, ) print("Wrote out to {}.".format(filepath))
def create_problem(grid, domain, problem_dir, problem_outfile): # Create location objects loc_type = domain.types['loc'] objects = set() grid_locs = np.empty(grid.shape, dtype=object) for r in range(grid.shape[0]): for c in range(grid.shape[1]): obj = loc_type(f'r{r}_c{c}') objects.add(obj) grid_locs[r, c] = obj initial_state = set() # Add at, isWater, isHill, isGoal at = domain.predicates['at'] isWater = domain.predicates['iswater'] isHill = domain.predicates['ishill'] isGoal = domain.predicates['isgoal'] for r in range(grid.shape[0]): for c in range(grid.shape[1]): obj = grid_locs[r, c] if grid[r, c] == I: initial_state.add(at(obj)) elif grid[r, c] == W: initial_state.add(isWater(obj)) elif grid[r, c] == H: initial_state.add(isHill(obj)) elif grid[r, c] == G: initial_state.add(isGoal(obj)) # Add adjacent adjacent = domain.predicates['adjacent'] def get_neighbors(r, c): for dr, dc in [(-1, 0), (1, 0), (0, -1), (0, 1)]: nr = r + dr nc = c + dc if 0 <= nr < grid.shape[0] and 0 <= nc < grid.shape[1]: yield (nr, nc) for r in range(grid.shape[0]): for c in range(grid.shape[1]): obj = grid_locs[r, c] for (nr, nc) in get_neighbors(r, c): nobj = grid_locs[nr, nc] initial_state.add(adjacent(obj, nobj)) # Add onTrail onTrail = domain.predicates['ontrail'] # Get the path path = [] r, c = np.argwhere(grid == I)[0] while True: path.append((r, c)) if grid[r, c] == G: break for (nr, nc) in get_neighbors(r, c): if (nr, nc) in path: continue if grid[nr, nc] in [P, G, H]: r, c = nr, nc break else: raise Exception("Should not happen") for (r, c), (nr, nc) in zip(path[:-1], path[1:]): obj = grid_locs[r, c] nobj = grid_locs[nr, nc] initial_state.add(onTrail(obj, nobj)) # Goal goal_rcs = np.argwhere(grid == G) assert len(goal_rcs) == 1 goal_r, goal_c = goal_rcs[0] goal_obj = grid_locs[goal_r, goal_c] goal = LiteralConjunction([at(goal_obj)]) filepath = os.path.join(PDDLDIR, problem_dir, problem_outfile) PDDLProblemParser.create_pddl_file( filepath, objects=objects, initial_state=initial_state, problem_name="hiking", domain_name=domain.domain_name, goal=goal, fast_downward_order=True, ) print("Wrote out to {}.".format(filepath))
def __call__(self, domain, state, timeout): act_preds = [domain.predicates[a] for a in list(domain.actions)] act_space = LiteralSpace( act_preds, type_to_parent_types=domain.type_to_parent_types) dom_file = tempfile.NamedTemporaryFile(delete=False).name prob_file = tempfile.NamedTemporaryFile(delete=False).name domain.write(dom_file) lits = set(state.literals) if not domain.operators_as_actions: lits |= set(act_space.all_ground_literals(state, valid_only=False)) PDDLProblemParser.create_pddl_file(prob_file, state.objects, lits, "myproblem", domain.domain_name, state.goal, fast_downward_order=True) cur_objects = set() start_time = time.time() if self._force_include_goal_objects: # Always start off considering objects in the goal. for lit in state.goal.literals: cur_objects |= set(lit.variables) # Get scores once. object_to_score = { obj: self._guidance.score_object(obj, state) for obj in state.objects if obj not in cur_objects } # Initialize threshold. threshold = self._gamma for _ in range(self._max_iterations): # Find new objects by incrementally lowering threshold. unused_objs = sorted(list(state.objects - cur_objects)) new_objs = set() while unused_objs: # Geometrically lower threshold. threshold *= self._gamma # See if there are any new objects. new_objs = { o for o in unused_objs if object_to_score[o] > threshold } # If there are, try planning with them. if new_objs: break cur_objects |= new_objs # Keep only literals referencing currently considered objects. cur_lits = set() for lit in state.literals: if all(var in cur_objects for var in lit.variables): cur_lits.add(lit) dummy_state = State(cur_lits, cur_objects, state.goal) # Try planning with only this object set. print("[Trying to plan with {} objects of {} total, " "threshold is {}...]".format(len(cur_objects), len(state.objects), threshold), flush=True) try: time_elapsed = time.time() - start_time # Get a plan from base planner & validate it. plan = self._planner(domain, dummy_state, timeout - time_elapsed) if not validate_strips_plan(domain_file=dom_file, problem_file=prob_file, plan=plan): raise PlanningFailure("Invalid plan") except PlanningFailure: # Try again with more objects. if len(cur_objects) == len(state.objects): # We already tried with all objects, give up. break continue return plan raise PlanningFailure("Plan not found! Reached max_iterations.")
if __name__ == "__main__": cnt = 0 for file in sorted(os.listdir(TAR_DIR)): sok = SokobanGame() boards = sok.new_board(os.path.join(TAR_DIR, file)) for b in boards: domian_file = "/Users/yangchen/PycharmProjects/pddlgym/pddlgym/pddl/sokoban.pddl" problem_file = "/Users/yangchen/PycharmProjects/pddlgym/pddlgym/pddl/sokoban_test/task02.pddl" domain = PDDLDomainParser(domian_file) problem = PDDLProblemParser(problem_file, domain.domain_name, domain.types, domain.predicates, domain.actions, domain.constants) problem.objects = parse_objects(b, domain.types) problem.goal = parse_goal(b, domain.types, domain.predicates) problem.initial_state = parse_initial_state( b, domain.types, domain.predicates) with open( '/Users/yangchen/PycharmProjects/pddlgym/pddlgym/pddl/sokoban/{}.pddl' .format(cnt), 'w') as f: PDDLProblemParser.create_pddl_file(f, problem.objects, problem.initial_state, problem.problem_name, problem.domain_name, problem.goal) cnt += 1 if cnt == 5: exit(0)
def sample_problem(domain, problem_dir, problem_outfile, maze_array): loc_type = domain.types['location'] player_type = domain.types['player'] at = domain.predicates['at'] clear = domain.predicates['clear'] move_dir_down = domain.predicates['move-dir-down'] move_dir_left = domain.predicates['move-dir-left'] move_dir_right = domain.predicates['move-dir-right'] move_dir_up = domain.predicates['move-dir-up'] oriented_down = domain.predicates['oriented-down'] oriented_left = domain.predicates['oriented-left'] oriented_right = domain.predicates['oriented-right'] oriented_up = domain.predicates['oriented-up'] is_goal = domain.predicates['is-goal'] # Create objects and state state = set() objects = set() goal = None player = player_type("player-1") objects.add(player) state.add( np.random.choice( [oriented_down, oriented_left, oriented_right, oriented_up])(player)) for r in range(len(maze_array)): for c in range(len(maze_array[r])): loc = loc_type(f"loc-{r+1}-{c+1}") objects.add(loc) if maze_array[r][c] == 0: #empty: clear state.add(clear(loc)) elif maze_array[r][c] == 1: #blocked: pass elif maze_array[r][c] == 2: #player init: at state.add(at(player, loc)) else: #goal: clear (should be 3) goal = at(player, loc) state.add(clear(loc)) state.add(is_goal(loc)) for r in range(1, len(maze_array) - 1): for c in range(1, len(maze_array[r]) - 1): if maze_array[r][c] == 1: continue if maze_array[r - 1][c] != 1: state.add( move_dir_up(loc_type(f"loc-{r+1}-{c+1}"), loc_type(f"loc-{r}-{c+1}"))) if maze_array[r + 1][c] != 1: state.add( move_dir_down(loc_type(f"loc-{r+1}-{c+1}"), loc_type(f"loc-{r+2}-{c+1}"))) if maze_array[r][c - 1] != 1: state.add( move_dir_left(loc_type(f"loc-{r+1}-{c+1}"), loc_type(f"loc-{r+1}-{c}"))) if maze_array[r][c + 1] != 1: state.add( move_dir_right(loc_type(f"loc-{r+1}-{c+1}"), loc_type(f"loc-{r+1}-{c+2}"))) filepath = os.path.join(PDDLDIR, problem_dir, problem_outfile) PDDLProblemParser.create_pddl_file( filepath, objects=objects, initial_state=state, problem_name="maze", domain_name=domain.domain_name, goal=goal, fast_downward_order=True, ) print("Wrote out to {}.".format(filepath))