Beispiel #1
0
    def _convert_action_instance(
        self, msg: proto.ActionInstance, problem: Problem
    ) -> Union[Tuple[model.timing.Timing, unified_planning.plan.ActionInstance,
                     model.timing.Duration],
               unified_planning.plan.ActionInstance]:
        # action instance paramaters are atoms but in UP they are FNodes
        # converting to up.model.FNode
        parameters = tuple(
            [self.convert(param, problem) for param in msg.parameters])

        action_instance = unified_planning.plan.ActionInstance(
            problem.action(msg.action_name),
            parameters,
        )

        start_time = (self.convert(msg.start_time)
                      if msg.HasField("start_time") else None)
        end_time = self.convert(
            msg.end_time) if msg.HasField("end_time") else None
        if start_time is not None:
            return (
                start_time,  # Absolute Start Time
                action_instance,
                end_time - start_time if end_time else None,  # Duration
            )
        else:
            return action_instance
Beispiel #2
0
    def _convert_metric(
        self, msg: proto.Metric, problem: Problem
    ) -> Union[metrics.MinimizeActionCosts,
               metrics.MinimizeSequentialPlanLength, metrics.MinimizeMakespan,
               metrics.MinimizeExpressionOnFinalState,
               metrics.MaximizeExpressionOnFinalState]:
        if msg.kind == proto.Metric.MINIMIZE_ACTION_COSTS:
            costs = {}
            for a, cost in msg.action_costs.items():
                costs[problem.action(a)] = self.convert(cost, problem)
            return metrics.MinimizeActionCosts(
                costs=costs,
                default=self.convert(msg.default_action_cost, problem)
                if msg.HasField("default_action_cost") else None,
            )

        elif msg.kind == proto.Metric.MINIMIZE_SEQUENTIAL_PLAN_LENGTH:
            return metrics.MinimizeSequentialPlanLength()

        elif msg.kind == proto.Metric.MINIMIZE_MAKESPAN:
            return metrics.MinimizeMakespan()

        elif msg.kind == proto.Metric.MINIMIZE_EXPRESSION_ON_FINAL_STATE:
            return metrics.MinimizeExpressionOnFinalState(
                expression=self.convert(msg.expression, problem))

        elif msg.kind == proto.Metric.MAXIMIZE_EXPRESSION_ON_FINAL_STATE:
            return metrics.MaximizeExpressionOnFinalState(
                expression=self.convert(msg.expression, problem))
        else:
            raise UPException(f"Unknown metric kind `{msg.kind}`")