Exemplo n.º 1
0
def plan_cost(universe, plan):
    if plan is None:
        return INF
    cost = universe.initial_cost
    for action, args in plan:
        instance = action.instantiate(args)
        increases = get_increases(instance.effect)
        # TODO - ensure that the last effect is used
        new_cost = 0  # 0 | INF
        if action.cost is not None:
            new_cost += action.cost
        for increase in increases:
            if increase.function == TotalCost():
                if isinstance(increase.value, Atom):
                    atom = increase.value
                    if atom in universe.perm_functions:
                        value = universe.evaluate_func(atom)
                    elif atom in universe.temp_functions:
                        value = universe.temp_functions[atom]
                    else:
                        raise ValueError(increase)
                else:
                    value = increase.value
                new_cost += value
                #new_cost = min(new_cost, value)
                #new_cost = max(new_cost, value)
                #new_cost = value # TODO: several ways to handle repeated costs
        cost += new_cost
    return cost
Exemplo n.º 2
0
    def initial_pddl(self, costs):
        atoms = list(self.get_initial_atoms())

        if costs:
            atoms.append(Initialize(TotalCost(), self.initial_cost))
        return '(' + '\n\t'.join([':init'] +
                                 sorted([atom.pddl() for atom in atoms])) + ')'
Exemplo n.º 3
0
def plan_cost(universe, plan):
    if plan is None:
        return INF
    cost = universe.initial_cost
    for action, args in plan:
        instance = action.instantiate(args)
        increases = get_increases(instance.effect)

        new_cost = 0
        if action.cost is not None:
            new_cost += action.cost
        for increase in increases:
            if increase.function == TotalCost():
                if isinstance(increase.value, Atom):
                    if increase.value in universe.perm_functions:
                        value = universe.perm_functions[increase.value]
                    elif increase.value in universe.temp_functions:
                        value = universe.temp_functions[increase.value]
                    else:
                        raise ValueError(increase)
                else:
                    value = increase.value
                new_cost += value

        cost += new_cost
    return cost
def Cost(cost):  # This is a shortcut
    """
  Increase cost atom (increase total-cost F) which extends :class:`.Function` by
  setting ``function`` to be :class:`.TotalCost()`.

  :param cost: the numeric or :class:`.Function` amount that :class:`.TotalCost()` increases

  """
    return Increase(TotalCost(), cost)
Exemplo n.º 5
0
 def problem_pddl(self, costs):
     s = '(define (problem %s)\n' % self.problem_name
     s += '(:domain %s)\n' % self.domain_name
     #s += '(:objects' + self.constants_pddl() + ')\n' # NOTE - don't need this
     s += self.initial_pddl(costs) + '\n'
     s += self.goal_pddl() + '\n'
     if costs:
         s += '(:metric minimize %s)' % TotalCost().pddl()
     return s + ')\n'
Exemplo n.º 6
0
    def problem_pddl(self, costs):
        s = '(define (problem %s)\n' % self.problem_name
        s += '(:domain %s)\n' % self.domain_name

        s += self.initial_pddl(costs) + '\n'
        s += self.goal_pddl() + '\n'
        if costs:
            s += '(:metric minimize %s)' % TotalCost().pddl()
        return s + ')\n'
Exemplo n.º 7
0
 def __init__(self, cost):
     """
     :param cost: the numeric or :class:`.Function` amount that :class:`.TotalCost()` increases
     """
     super(Cost, self).__init__(TotalCost(), cost)
Exemplo n.º 8
0
 def initial_pddl(self, costs):
     atoms = list(self.get_initial_atoms())
     #atoms = list(apply_axioms(set(atoms), self.type_to_objects, instantiate_axioms(self)))
     if costs: atoms.append(Initialize(TotalCost(), self.initial_cost))
     return '(' + '\n\t'.join([':init'] +
                              sorted([atom.pddl() for atom in atoms])) + ')'