Пример #1
0
    def eval(cls, *arg):
        """ Called from sympy to evaluate the function """
        if (not isinstance(arg[0], Variable)
                and not isinstance(arg[0], Parameter)):
            raise EquationError('not-a-variable', str(arg[0]),
                                'Must be a var or param')

        if arg[0].model is None:
            raise EquationError('no-model', arg[0].name,
                                'Variable must belong to a model')
        return arg[0].model.get_at(arg[0], arg[1])
Пример #2
0
 def _validate_equations(self):
     """ Does some validation """
     # Make sure that each variable has an equation
     for variable in self.variables.values():
         if variable.equation is None:
             raise EquationError('under-specified', variable.name,
                                 'variable does not have equation')
Пример #3
0
 def eval(cls, *arg):
     """ Called from sympy to evaluate the function """
     if (not isinstance(arg[0], Variable)
             and not isinstance(arg[0], Parameter)):
         raise EquationError('d-arg-not-a-variable', str(arg[0]),
                             'The arg to d() must be a var or param')
     return arg[0] - arg[0].model.get_at(arg[0], -1)
Пример #4
0
 def get_at(self, name, iteration):
     """ Implements the model get_at() """
     # pylint: disable=no-self-use
     if not iteration.is_number or not iteration.is_Number:
         raise EquationError('test-not-a-number', '', '')
     if iteration < 0:
         return Symbol("_{0}__{1}".format(name, -iteration))
     else:
         return Symbol("_{0}_{1}".format(name, iteration))
Пример #5
0
    def get_at(self, variable, iteration):
        """ Returns the variable for a previous iteration.
            The value for iter may be positive or negative:
                If >= 0, absolute position
                    0 is the very first iteration.  Note that
                    iterations are available only AFTER the
                    solver has returned success.  Otherwise the
                    variable's default value is returned.
                If < 0, relative position
                    -1 is the iteration previous to the current
                    iteration.

            Arguments:
                variable:
                iteration:

            Returns:
                The value of the variable for that iteration.
        """
        # Need to replace the iteration needed with a variable
        # that represents the accessed variable
        #   for example, if are processing iteration 10
        #     then x[-1] -> _x__1
        #   _x__1 then gets added as a parameter
        #
        #   For positive iterations, that's a static iteration
        #   x[5] -> _x_5
        #   _x_5 will get added as a parameter.
        #   If iteration 5 has not happened yet, the default value
        #   value will be returned.
        #
        # Before solving, the appropriate values will be set in the
        # parameters.
        #
        iter_value = sympify(iteration)
        if not iter_value.is_number or not iter_value.is_Number:
            raise EquationError('iteration-not-a-number', str(iter_value),
                                'iteration value must be a number')
        iter_value = int(iter_value)
        if iter_value < 0:
            iter_name = "_{0}__{1}".format(str(variable), -iter_value)
        else:
            iter_name = "_{0}_{1}".format(str(variable), iter_value)

        if iter_name not in self._private_parameters:
            param = SeriesParameter(iter_name,
                                    variable=variable,
                                    iteration=iter_value,
                                    default=variable.default)
            self._private_parameters[iter_name] = param
            _add_param_to_context(self._local_context, param)
            self._need_function_update = True
        return self._private_parameters[iter_name]