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 test_set_get_pyfuzzy_for_zfunction_type(self): " shoud return the correct corresponding pyfuzzy object for the ZFunction type " new_set = self._mock_setModel('fuzzy.set.ZFunction.ZFunction') a = 1.2 delta = 2.3 self.parameters_mock = [ self._parameters_mock(name="a", value=a), self._parameters_mock(name="delta", value=delta), ] # mocking parameters (queryset) parameters_queryset = mock.Mock() parameters_queryset.all = lambda : self.parameters_mock self.set_pre_mock(SetModel,'parameters') SetModel.parameters = parameters_queryset new_pyfuzzy_set = new_set.get_pyfuzzy() # the expected pyfuzzy system pyfuzzy_set_expected = ZFunction(a = a, delta = delta) # are from the same class self.assertEquals(type(pyfuzzy_set_expected), type(new_pyfuzzy_set)) # have the same args self.assertEquals(pyfuzzy_set_expected.a, new_pyfuzzy_set.a) self.assertEquals(pyfuzzy_set_expected.delta, new_pyfuzzy_set.delta)
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 """ a = self.a d = self.delta / 2.0 if x < a: return SFunction(a - d, d)(x) else: return ZFunction(a + d, d)(x)
def test_set_from_pyfuzzy_for_zfunction_type(self): " shoud return the correct corresponding SetModel for the ZFunction pyfuzzy object " a = 1.2 delta = 2.3 pyfuzzy_set = ZFunction(a = a, delta = delta) new_set = SetModel.from_pyfuzzy(pyfuzzy_set) pyfuzzy_set_full_namespace = pyfuzzy_set.__module__ + "." + pyfuzzy_set.__class__.__name__ # are from the same class self.assertEquals(pyfuzzy_set_full_namespace, new_set.set) # have the same args self.assertEquals(2,new_set.parameters.all().count()) a_param = new_set.parameters.get(name="a") delta_param = new_set.parameters.get(name="delta") self.assertEquals(pyfuzzy_set.a, a_param.get_value()) self.assertEquals(pyfuzzy_set.delta, delta_param.get_value())
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)