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]
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 _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]
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)