Esempio n. 1
0
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)
Esempio n. 2
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]