Пример #1
0
class PiFunction(Function):
    r"""
    Realize a Pi-shaped fuzzy set::
        
                _
               /|\
              / | \
            _/  |  \_
             |  a  |
             |     |
              delta

    See also U{http://pyfuzzy.sourceforge.net/demo/set/PiFunction.png}

    
    @ivar a: center of set.
    @type a: float
    @ivar delta: absolute distance between x-values for minimum and maximum.
    @type delta: float
    """

    def __init__(self, a=0.0, delta=1.0):
        """Initialize a Pi-shaped fuzzy set.

        @param a: center of set
        @type a: float
        @param delta: absolute distance between x-values for minimum and maximum
        @type delta: float
        """
        super(PiFunction, self).__init__()
        self.a = a
        self.delta = delta
        self._sfunction = SFunction(a - delta/2., delta/2)
        self._zfunction = ZFunction(a + delta/2., delta/2)

    def __call__(self, x):
        """Return membership of x in this fuzzy set.
           This method makes the set work like a function.
           
           @param x: value for which the membership is to calculate
           @type x: float
           @return: membership
           @rtype: float
           """
        if x < self.a:
            return self._sfunction(x)
        else:
            return self._zfunction(x)

    def getCOG(self):
        """Return center of gravity."""
        return self.a

    def getValuesX(self):
        """Return sequence of x-values so we get a smooth function."""
        for x in self._sfunction.getValuesX():
            yield x
        # first value is equal the last of the previous sequence    
        skippedFirst = False 
        for x in self._zfunction.getValuesX():
            if not skippedFirst:
                skippedFirst = True
            else:
                yield x

    def __repr__(self):
        """Return representation of instance.
                   
           @return: representation of instance
           @rtype: string
           """
        return "%s.%s(a=%s, delta=%s)" % (self.__class__.__module__, self.__class__.__name__, self.a, self.delta)