예제 #1
0
    def __str__(self):

        init = indent(
            '\n'.join(['{}'.format(x) for x in self.start_propositions]), 2)
        goal = indent(self._propositions_to_str(self.goal_propositions), 2)
        description = ' (define (problem {})\n' \
                      '\t(:domain {})\n\n' \
                      '\t(:init\n{}\n' \
                      '\t)\n\n' \
                      '\t(:goal\n{}\n' \
                      '\t)\n)'.format(self.name, self.domain, init, goal)
        return description
 def _extract_error(self, error: str, domain: str, problem: str) -> str:
     temp = error.split(':')
     line = int(temp[2])
     error = ':'.join(temp[3:])
     if isinstance(domain, str):
         num_lines = sum(1 for line in open(domain))
     else:
         num_lines = len(str(domain).split('\n'))
     if line <= num_lines:
         # error is in domain file
         return "Error on line {} in domain PDDL:\n{}".format(line, indent(error))
     line = line - num_lines - 1  # because we added a blank line in
     return "Error on line {} in problem PDDL:\n{}".format(line, indent(error))
예제 #3
0
    def __str__(self):

        if self._probabilistic:
            effects = self._operator.effects
        else:
            effects = [max(self._operator.effects,
                           key=lambda x: x[0])]  # get most probable

        precondition = Clause(self._operator.preconditions)

        effect = Probabilistic()
        for prob, eff, reward in effects:
            if self._use_rewards and reward is not None:
                clause = Clause(eff +
                                [RewardPredicate(reward)])  # add the reward
            else:
                clause = Clause(eff)
            effect.add(clause, prob)

        if len(effects) > 1:
            effect = '\n' + indent(str(effect), 2)
        else:
            effect = str(effect)

        if self._option_descriptor is None:
            name = self._operator.name
        else:
            name = '{}-partition-{}'.format(
                self._option_descriptor(self._operator.option),
                self._operator.partition)
        if self._index is not None:
            name += '-{}'.format(self._index)

        return '(:action {}\n\t:parameters ()\n\t:precondition {}\n\t:effect {}\n)'.format(
            name, precondition, effect)
예제 #4
0
    def _make_conditional_operator(self, preconditions: List[Proposition],
                                   effects: List[Tuple[float,
                                                       List[Proposition],
                                                       float]],
                                   link_effects: Dict[int,
                                                      List[Tuple[int,
                                                                 float]]]):

        if not self._conditional_effects:
            raise ValueError("Should be using _make_operator function")

        precondition = Clause(preconditions)
        effect = Probabilistic()
        for prob, eff, reward in effects:
            if self._use_rewards and reward is not None:
                clause = Clause(eff +
                                [RewardPredicate(reward)])  # add the reward
            else:
                clause = Clause(eff)
            effect.add(clause, prob)

        conditional_effects = list()

        precondition_links = Clause(conjunctive=False)
        for start, next_links in link_effects.items():
            link_pre = FluentPredicate('=', 'linking', start)
            precondition_links += link_pre
            link_effect = Probabilistic()
            for end, link_prob in next_links:
                if end != -1:
                    link_effect.add(FluentPredicate('assign', 'linking', end),
                                    link_prob)
            if len(link_effect) > 0:
                conditional_effects.append(
                    ConditionalEffect(link_pre, link_effect))

        mixed_effect = MixedEffect(effect, conditional_effects)
        if len(mixed_effect) > 1:
            effect = '\n' + indent(str(mixed_effect), 2)
        else:
            effect = str(mixed_effect)

        if self._option_descriptor is None:
            name = self._operator.name
        else:
            name = '{}-partition-{}'.format(
                self._option_descriptor(self._operator.option),
                self._operator.partition)
        if self._index is not None:
            name += '-{}'.format(self._index)

        return '(:action {}\n\t:parameters ()\n\t:precondition {}\n\t:effect {}\n)'.format(
            name,
            precondition,
            # precondition_links,
            effect)
예제 #5
0
    def __str__(self):
        comment = ';Automatically generated {} domain PPDDL file.'.format(
            self._env.name)
        definition = 'define (domain {})'.format(self._env.name)
        requirements = '(:requirements :strips{}{}{})'.format(
            ' :probabilistic-effects' if self.probabilistic else '',
            ' :rewards' if self.specify_rewards else '',
            ' :conditional-effects :fluents :equality :disjunctive-preconditions'
            if self.conditional_effects else '')

        symbols = '{}\n'.format(Proposition.not_failed()) + '\n'.join(
            ['{}'.format(x) for x in self._vocabulary])

        if self.conditional_effects:
            requirements += '\n\n(:functions (linking))'
        else:
            symbols += '\n\n' + '\n'.join(
                ['(psymbol_{})'.format(name) for name in self.problem_symbols])

        predicates = '(:predicates\n{}\n)'.format(indent(symbols))

        format_spec = ':'
        if self._probabilistic:
            format_spec += 'p'
        if self._rewards:
            format_spec += 'r'

        operators = '\n\n'.join([
            x.pretty_print(i,
                           self._env.describe_option,
                           probabilistic=self.probabilistic,
                           use_rewards=self.specify_rewards,
                           conditional_effects=self.conditional_effects)
            for i, x in enumerate(self._operators)
        ])

        description = '{}\n({}\n{}\n\n{}\n\n{}\n)'.format(
            comment, definition, indent(requirements), indent(predicates),
            indent(operators))
        return description
예제 #6
0
    def __str__(self):

        if len(self._values) == 1:
            return str(self._values[0][1])

        effect = '(probabilistic'
        for prob, value in self._values:
            if value.has_effect():
                prob = round(prob / self.prob_sum,
                             3)  # TODO probably a better way!
                effect += indent('\n{} {}'.format(prob, value), 2)
        effect += ')'
        return effect
예제 #7
0
    def __str__(self):
        precondition = self._propositions_to_str(self._operator.preconditions)

        if self._probabilistic:
            effects = self._operator.effects
        else:
            effects = [max(self._operator.effects,
                           key=lambda x: x[0])]  # get most probable

        if len(effects) == 1:
            end = None
            if self._use_rewards and effects[0][2] is not None:
                end = '{} (reward) {:.2f}'.format(
                    'increase' if effects[0][2] >= 0 else 'decrease',
                    abs(effects[0][2]))
            effect = self._propositions_to_str(effects[0][1], end=end)
        else:
            effect = 'probabilistic '

            total_prob = sum(
                prob for prob, _, _ in effects
            )  # sometimes total prob is just over 1 because rounding :(

            for prob, eff, reward in effects:
                prob = round(prob / total_prob,
                             3)  # TODO probably a better way!
                end = None
                if self._use_rewards and reward is not None:
                    end = '{} (reward) {:.2f}'.format(
                        'increase' if reward >= 0 else 'decrease', abs(reward))
                effect += indent(
                    '\n\t{} ({})'.format(prob,
                                         self._propositions_to_str(eff, end)),
                    3)
            effect += '\n\t\t\t\n\t'

        if self._option_descriptor is None:
            name = self._operator.name
        else:
            name = '{}-partition-{}'.format(
                self._option_descriptor(self._operator.option),
                self._operator.partition)
        if self._index is not None:
            name += '-{}'.format(self._index)

        return '(:action {}\n\t:parameters ()\n\t:precondition ({})\n\t:effect ({})\n)'.format(
            name, precondition, effect)
예제 #8
0
    def _make_operator(self, link_index: int, preconditions: List[Proposition],
                       effects: List[Tuple[float, List[Proposition], float]],
                       start_link: int, link_effects: List[Tuple[int, float]]):

        if self._conditional_effects:
            raise ValueError(
                "Should be using _make_conditional_operator function")

        start_prop = Proposition('psymbol_{}'.format(start_link), None)
        precondition = Clause(preconditions) + start_prop

        for end_link, link_prob in link_effects:

            effect = Probabilistic()
            for prob, eff, reward in effects:
                if self._use_rewards and reward is not None:
                    clause = Clause(
                        eff + [RewardPredicate(reward)])  # add the reward
                else:
                    clause = Clause(eff)
                prob *= link_prob
                if end_link != -1:  # end partition changed!
                    clause += Proposition('psymbol_{}'.format(end_link), None)

                effect.add(clause, prob)

            if len(effects) > 1:
                effect = '\n' + indent(str(effect), 2)
            else:
                effect = str(effect)

            if self._option_descriptor is None:
                name = self._operator.name
            else:
                name = '{}-partition-{}'.format(
                    self._option_descriptor(self._operator.option),
                    self._operator.partition)
            if self._index is not None:
                name += '-{}'.format(self._index)

            name += '-{}'.format(link_index)  # to avoid duplicate names

            return '(:action {}\n\t:parameters ()\n\t:precondition {}\n\t:effect {}\n)'.format(
                name, precondition, effect)
예제 #9
0
 def __str__(self):
     return '(and {}\n{})'.format(
         self._regular_effect, '\n'.join([
             indent('{}'.format(x), 3) for x in self._conditional_effects
         ]))