From 921dd5d1466684e6680e53600bc6e83b01f1e99d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=C3=B4nio=20C=C3=A2ndido?= Date: Wed, 27 Sep 2017 22:26:03 -0300 Subject: [PATCH] - Non Stationary Fuzzy Sets --- pyFTS/common/Membership.py | 13 ++++- pyFTS/common/NonStationary.py | 94 +++++++++++++++++++++++++++-------- 2 files changed, 86 insertions(+), 21 deletions(-) diff --git a/pyFTS/common/Membership.py b/pyFTS/common/Membership.py index 4960dea..79c00a3 100644 --- a/pyFTS/common/Membership.py +++ b/pyFTS/common/Membership.py @@ -52,12 +52,23 @@ def gaussmf(x, parameters): :return: the membership value of x given the parameters """ return math.exp((-(x - parameters[0])**2)/(2 * parameters[1]**2)) - #return math.exp(-0.5 * ((x - parameters[0]) / parameters[1]) ** 2) def bellmf(x, parameters): + """ + Bell shaped membership function + :param x: + :param parameters: + :return: + """ return 1 / (1 + abs((x - parameters[2]) / parameters[0]) ** (2 * parameters[1])) def sigmf(x, parameters): + """ + Sigmoid / Logistic membership function + :param x: + :param parameters: an list with 2 real values (smoothness and midpoint) + :return: + """ return 1 / (1 + math.exp(-parameters[0] * (x - parameters[1]))) diff --git a/pyFTS/common/NonStationary.py b/pyFTS/common/NonStationary.py index 3e5e21b..8eccb43 100644 --- a/pyFTS/common/NonStationary.py +++ b/pyFTS/common/NonStationary.py @@ -3,6 +3,76 @@ from pyFTS import * from pyFTS.common import FuzzySet, Membership +class NonStationaryMembershipFunction(object): + def __init__(self, name, mf, parameters, **kwargs): + self.mf = mf + self.parameters = parameters + self.location = kwargs.get("location", None) + self.location_params = kwargs.get("location_params", None) + self.width = kwargs.get("width", None) + self.width_params = kwargs.get("width_params", None) + self.noise = kwargs.get("noise", None) + self.noise_params = kwargs.get("noise_params", None) + + def perform_location(self, t, param): + if self.location is None: + return param + + inc = self.location(t, self.location_params) + + if self.mf == Membership.gaussmf: + #changes only the mean parameter + return [param[0] + inc, param[1]] + elif self.mf == Membership.sigmf: + #changes only the midpoint parameter + return [param[0], param[1] + inc] + elif self.mf == Membership.bellmf: + return [param[0], param[1], param[2] + inc] + else: + #translate all parameters + return [k + inc for k in param] + + def perform_width(self, t, param): + if self.width is None: + return param + + inc = self.width(t, self.width_params) + + if self.mf == Membership.gaussmf: + #changes only the variance parameter + return [param[0], param[1] + inc] + elif self.mf == Membership.sigmf: + #changes only the smooth parameter + return [param[0] + inc, param[1]] + elif self.mf == Membership.trimf: + return [param[0] + inc, param[1], param[2] - inc] + elif self.mf == Membership.trapmf: + l = (param[3]-param[0]) + rab = (param[1] - param[0]) / l + rcd = (param[3] - param[2]) / l + return [param[0] + inc, param[1] + inc*rab, param[2] - inc*rcd, param[3] - inc] + else: + return param + + def membership(self, x, t): + """ + Calculate the membership value of a given input + :param x: input value + :return: membership value of x at this fuzzy set + """ + + param = self.parameters + param = self.perform_location(t, param) + param = self.perform_width(t, param) + + tmp = self.mf(x, param) + + if self.noise is not None: + tmp += self.noise(t, self.noise_params) + + return tmp + + class NonStationaryFuzzySet(FuzzySet.FuzzySet): """ Non Stationary Fuzzy Sets @@ -11,28 +81,12 @@ class NonStationaryFuzzySet(FuzzySet.FuzzySet): IEEE Transactions on Fuzzy Systems, v. 16, n. 4, p. 1072-1086, 2008. """ - def __init__(self, name, mf, parameters): + def __init__(self, name, mf, **kwargs): """ Constructor :param name: Fuzzy Set name - :param mf: Membership Function - :param pf: Pertubation Function - :param parameters: initial parameters of the membership function - :param pf_parameters: parameters of the membership pertubation function + :param mf: NonStationary Membership Function """ - super(FuzzySet, self).__init__(order=1, name=name, **kwargs) - self.name = name - self.mf = mf - self.parameters = parameters - self.pf = [] - self.pf_parameters = [] + super(FuzzySet, self).__init__(name=name, mf=mf, parameters=None, centroid=None) + - def appendPertubation(self, pf, pf_parameters): - """ - Append a pertubation function to the non-stationary fuzzy set - :param pf: - :param pf_parameters: - :return: - """ - self.pf.append(pf) - self.pf_parameters.append(pf_parameters)