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 __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 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 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, 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 load_pddl(domain_file, problem_dir): """ Parse domain and problem PDDL files. Parameters ---------- domain_file : str Path to a PDDL domain file. problem_dir : str Path to a directory of PDDL problem files. Returns ------- domain : PDDLDomainParser problems : [ PDDLProblemParser ] """ domain = PDDLDomainParser(domain_file) problems = [] problem_files = [ f for f in glob.glob(os.path.join(problem_dir, "*.pddl")) ] for problem_file in sorted(problem_files): problem = PDDLProblemParser(problem_file, domain.domain_name, domain.types, domain.predicates) problems.append(problem) return domain, problems
def test_hierarchical_spaces(): dir_path = os.path.dirname(os.path.realpath(__file__)) domain_file = os.path.join(dir_path, 'pddl', 'hierarchical_type_test_domain.pddl') problem_file = os.path.join(dir_path, 'pddl', 'hierarchical_type_test_domain', 'hierarchical_type_test_problem.pddl') domain = PDDLDomainParser(domain_file) problem = PDDLProblemParser(problem_file, domain.domain_name, domain.types, domain.predicates, domain.actions) actions = list(domain.actions) action_predicates = [domain.predicates[a] for a in actions] space = LiteralSpace(set(domain.predicates.values()) - set(action_predicates), type_to_parent_types=domain.type_to_parent_types) all_ground_literals = space.all_ground_literals( State(problem.initial_state, problem.objects, problem.goal)) ispresent = Predicate("ispresent", 1, [Type("entity")]) islight = Predicate("islight", 1, [Type("object")]) isfurry = Predicate("isfurry", 1, [Type("animal")]) ishappy = Predicate("ishappy", 1, [Type("animal")]) attending = Predicate("attending", 2, [Type("animal"), Type("object")]) nomsy = Type("jindo")("nomsy") rover = Type("corgi")("rover") rene = Type("cat")("rene") block1 = Type("block")("block1") block2 = Type("block")("block2") cylinder1 = Type("cylinder")("cylinder1") assert all_ground_literals == { ispresent(nomsy), ispresent(rover), ispresent(rene), ispresent(block1), ispresent(block2), ispresent(cylinder1), islight(block1), islight(block2), islight(cylinder1), isfurry(nomsy), isfurry(rover), isfurry(rene), ishappy(nomsy), ishappy(rover), ishappy(rene), attending(nomsy, block1), attending(nomsy, block2), attending(nomsy, cylinder1), attending(rover, block1), attending(rover, block2), attending(rover, cylinder1), attending(rene, block1), attending(rene, block2), attending(rene, cylinder1), } print("Test passed.")
def load_pddl(domain_file, problem_dir, operators_as_actions=False): """ Parse domain and problem PDDL files. Parameters ---------- domain_file : str Path to a PDDL domain file. problem_dir : str Path to a directory of PDDL problem files. operators_as_actions : bool See class docstirng. Returns ------- domain : PDDLDomainParser problems : [ PDDLProblemParser ] """ domain = PDDLDomainParser( domain_file, expect_action_preds=(not operators_as_actions), operators_as_actions=operators_as_actions) problems = [] problem_files = [ f for f in glob.glob(os.path.join(problem_dir, "*.pddl")) ] for problem_file in sorted(problem_files): problem = PDDLProblemParser(problem_file, domain.domain_name, domain.types, domain.predicates, domain.actions, domain.constants) problems.append(problem) return domain, problems
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 test_hierarchical_types(): dir_path = os.path.dirname(os.path.realpath(__file__)) domain_file = os.path.join(dir_path, 'pddl', 'hierarchical_type_test_domain.pddl') problem_file = os.path.join(dir_path, 'pddl', 'hierarchical_type_test_domain', 'hierarchical_type_test_problem.pddl') domain = PDDLDomainParser(domain_file) problem = PDDLProblemParser(problem_file, domain.domain_name, domain.types, domain.predicates) assert set(domain.types.keys()) == { Type("dog"), Type("cat"), Type("animal"), Type("block"), Type("cylinder"), Type("jindo"), Type("corgi"), Type("object"), Type("entity") } assert domain.type_hierarchy == { Type("animal"): {Type("dog"), Type("cat")}, Type("dog"): {Type("jindo"), Type("corgi")}, Type("object"): {Type("block"), Type("cylinder")}, Type("entity"): {Type("object"), Type("animal")}, } assert domain.type_to_parent_types == { Type("entity"): {Type("entity")}, Type("object"): {Type("object"), Type("entity")}, Type("animal"): {Type("animal"), Type("entity")}, Type("dog"): {Type("dog"), Type("animal"), Type("entity")}, Type("cat"): {Type("cat"), Type("animal"), Type("entity")}, Type("corgi"): {Type("corgi"), Type("dog"), Type("animal"), Type("entity")}, Type("jindo"): {Type("jindo"), Type("dog"), Type("animal"), Type("entity")}, Type("block"): {Type("block"), Type("object"), Type("entity")}, Type("cylinder"): {Type("cylinder"), Type("object"), Type("entity")}, } print("Test passed.")
def _create_problem_file(self, raw_problem_fname, use_cache=True): if (not use_cache) or (raw_problem_fname not in self._problem_files): problem_fname = os.path.split(raw_problem_fname)[-1] problem_fname = problem_fname.split('.pddl')[0] problem_fname += '_{}_with_diffs_{}.pddl'.format( self.domain_name, random.randint(0, 9999999)) problem_fname = os.path.join('/tmp', problem_fname) # Parse raw problem problem_parser = PDDLProblemParser(raw_problem_fname, self.domain_name.lower(), self._types, self._predicates, self._actions) new_initial_state = set(problem_parser.initial_state) # Add actions action_lits = set( self._action_space.all_ground_literals(State( new_initial_state, problem_parser.objects, problem_parser.goal), valid_only=False)) new_initial_state |= action_lits # Add 'Different' pairs for each pair of objects Different = Predicate('Different', 2) for obj1 in problem_parser.objects: for obj2 in problem_parser.objects: if obj1 == obj2: continue # if obj1.var_type != obj2.var_type: # continue diff_lit = Different(obj1, obj2) new_initial_state.add(diff_lit) # Write out new temporary problem file problem_parser.initial_state = frozenset(new_initial_state) problem_parser.write(problem_fname) # Add to cache self._problem_files[raw_problem_fname] = problem_fname return self._problem_files[raw_problem_fname]
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 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 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 integration_test(): dir_path = os.path.dirname(os.path.realpath(__file__)) domain_file = os.path.join(dir_path, 'pddl', 'test_domain.pddl') problem_file = os.path.join(dir_path, 'pddl', 'test_domain', 'test_problem.pddl') domain = PDDLDomainParser(domain_file) problem = PDDLProblemParser(problem_file, domain.domain_name, domain.types, domain.predicates) ## Check domain type1 = Type('type1') type2 = Type('type2') # Action predicates action_pred = Predicate('actionpred', 1, [type1]) # Predicates pred1 = Predicate('pred1', 1, [type1]) pred2 = Predicate('pred2', 1, [type2]) pred3 = Predicate('pred3', 1, [type1, type2, type2]) assert set(domain.predicates.values()) == { pred1, pred2, pred3, action_pred } assert domain.actions == { action_pred.name } # Operators assert len(domain.operators) == 1 operator1 = Predicate('action1', 4, [type1, type1, type2, type2]) assert operator1 in domain.operators operator = domain.operators[operator1] # Operator parameters assert len(operator.params) == 4 assert operator.params[0] == type1('?a') assert operator.params[1] == type1('?b') assert operator.params[2] == type2('?c') assert operator.params[3] == type2('?d') # Operator preconditions (set of Literals) assert len(operator.preconds.literals) == 4 assert set(operator.preconds.literals) == { action_pred('?b'), pred1('?b'), pred3('?a', '?c', '?d'), Not(pred2('?c')) } # Operator effects (set of Literals) assert len(operator.effects.literals) == 3 assert set(operator.effects.literals) == { Anti(pred2('?d')), pred2('?c'), pred3('?b', '?d', '?c')} ## Check problem # Objects assert set(problem.objects) == {type1('a1'), type1('a2'), type1('b1'), type1('b2'), type1('b3'), type2('c1'), type2('c2'), type2('d1'), type2('d2'), type2('d3')} # Goal assert isinstance(problem.goal, LiteralConjunction) assert set(problem.goal.literals) == {pred2('c2'), pred3('b1', 'c1', 'd1')} # Init assert problem.initial_state == { pred1('b2'), pred2('c1'), pred3('a1', 'c1', 'd1'), pred3('a2', 'c2', 'd2'), action_pred('a1'), action_pred('a2'), action_pred('b1'), action_pred('b2')} print("Test passed.")
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.")
from transform.parse_func import parse_objects, parse_initial_state, parse_goal import os TAR_DIR = "transform/puzzles/hard" 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)
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))
from pddlgym.parser import PDDLDomainParser, PDDLProblemParser from pddlgym.structs import Equation if __name__ == '__main__': # Test parser domain_file = "/home/dongbox/work/nips-flatland/pddlgym/pddlgym/pddl/flatland.pddl" problem_file = "/home/dongbox/work/nips-flatland/pddlgym/pddlgym/pddl/flatland/problem.pddl" domain = PDDLDomainParser(domain_file) problem = PDDLProblemParser(problem_file, domain.domain_name, domain.types, domain.predicates, domain.functions, domain.actions) # Initial # example 1: Add a position:c31 cause the update of the map maybe.(Usage of initial function) f_c31 = domain.functions['position']('c31') f_c31.function.form = Equation() f_c31.function.value = 23 # example 2: Add an agent:agent2 to the position:c12 in the map.(Usage of initial predicate) p_a = domain.predicates['at'] # Add initial definition to problem initial_state = set(problem.initial_state) initial_state.add(f_c31) initial_state.add(p_a('agent2', 'c12')) problem.initial_state = frozenset(initial_state) # Goal # example 3: Set the goal of position for agent:agent2 to position:c21. problem.goal.literals.append(p_a('agent2', 'c21')) print(problem)
def _create_problem_file(self, raw_problem_fname, use_cache=True): if (not use_cache) or (raw_problem_fname not in self._problem_files): problem_fname = os.path.split(raw_problem_fname)[-1] problem_fname = problem_fname.split('.pddl')[0] problem_fname += '_{}_with_diffs_{}.pddl'.format( self.domain_name, random.randint(0, 9999999)) problem_fname = os.path.join('/tmp', problem_fname) # Parse raw problem action_names = [ ] # purposely empty b/c we WANT action literals in there problem_parser = PDDLProblemParser(raw_problem_fname, self.domain_name.lower(), self._types, self._predicates, action_names) # Add action literals (in case they're not already present in the initial state) # which will be true when the original domain uses operators_as_actions init_state = State(problem_parser.initial_state, problem_parser.objects, None) act_lits = self._action_space.all_ground_literals(init_state, valid_only=False) problem_parser.initial_state = frozenset( act_lits | problem_parser.initial_state) # Add 'Different' pairs for each pair of objects Different = Predicate('Different', 2) init_state = set(problem_parser.initial_state) for obj1 in problem_parser.objects: for obj2 in problem_parser.objects: if obj1 == obj2: continue # if obj1.var_type != obj2.var_type: # continue diff_lit = Different(obj1, obj2) init_state.add(diff_lit) problem_parser.initial_state = frozenset(init_state) # Also add 'different' pairs for goal if it's existential if isinstance(problem_parser.goal, Exists): diffs = [] for var1 in problem_parser.goal.variables: for var2 in problem_parser.goal.variables: if var1 == var2: continue diffs.append(Different(var1, var2)) problem_parser.goal = Exists( problem_parser.goal.variables, type(problem_parser.goal.body)( problem_parser.goal.body.literals + diffs)) # If no objects, write a dummy one to make FF not crash. if not problem_parser.objects: problem_parser.objects.append("DUMMY") # Write out new temporary problem file problem_parser.write(problem_fname) # Add to cache self._problem_files[raw_problem_fname] = (problem_fname, problem_parser.objects) return self._problem_files[raw_problem_fname]