Ejemplo n.º 1
0
 def __call__(self, *args):
     args_ = []
     for arg in args:
         if type(arg) is types.GeneratorType:
             args_.extend(val for val in arg)
         else:
             args_.append(arg)
     #
     # Loop and do two thing:
     #   1. Wrap non-numeric arguments
     #   2. See if we have a potentially variable argument
     #
     pv = False
     for i, arg in enumerate(args_):
         try:
             # Q: Is there a better way to test if a value is an object
             #    not in native_types and not a standard expression type?
             if arg.__class__ in native_types or arg.is_expression_type():
                 pass
             if not arg.__class__ in native_types and arg.is_potentially_variable(
             ):
                 pv = True
         except AttributeError:
             args_[i] = NonNumericValue(arg)
     #
     if pv:
         return EXPR.ExternalFunctionExpression(args_, self)
     return EXPR.NPV_ExternalFunctionExpression(args_, self)
Ejemplo n.º 2
0
    def __call__(self, *args):
        """Return a Pyomo expression that evaluates this function

        Note that this does not evaluate the external function
        represented by this :class:`ExternalFunction` component, but
        rather generates a Pyomo expression object that represents
        symbolic evaluation of the external function at the specified
        arguments (which themselves may be Pyomo expression objects).

        Parameters
        ----------
        *args:
            Arguments (constants, variables, expressions) to pass to
            this External function.

        Returns
        -------
        ExternalFunctionExpression or NPV_ExternalFunctionExpression:
            The new Pyomo expression node.

        """

        args_ = []
        for arg in args:
            if type(arg) is types.GeneratorType:
                args_.extend(arg)
            else:
                args_.append(arg)
        #
        # Loop and do two thing:
        #   1. Wrap non-numeric arguments
        #   2. See if we have a potentially variable argument
        #
        pv = False
        for i, arg in enumerate(args_):
            try:
                # Q: Is there a better way to test if a value is an object
                #    not in native_types and not a standard expression type?
                if arg.__class__ in native_types:
                    continue
                if arg.is_potentially_variable():
                    pv = True
            except AttributeError:
                args_[i] = NonNumericValue(arg)
        #
        if pv:
            return EXPR.ExternalFunctionExpression(args_, self)
        return EXPR.NPV_ExternalFunctionExpression(args_, self)