2016-09-08 16:03:32 +04:00
|
|
|
import numpy as np
|
|
|
|
from pyFTS import *
|
|
|
|
|
|
|
|
def differential(original):
|
|
|
|
n = len(original)
|
|
|
|
diff = [ original[t-1]-original[t] for t in np.arange(1,n) ]
|
|
|
|
diff.insert(0,0)
|
|
|
|
return np.array(diff)
|
|
|
|
|
2016-09-02 22:55:55 +04:00
|
|
|
def trimf(x,parameters):
|
2016-11-08 20:08:06 +04:00
|
|
|
xx = round(x,3)
|
|
|
|
if(xx < parameters[0]):
|
2016-09-02 22:55:55 +04:00
|
|
|
return 0
|
2016-11-08 20:08:06 +04:00
|
|
|
elif(xx >= parameters[0] and xx < parameters[1]):
|
2016-09-02 22:55:55 +04:00
|
|
|
return (x-parameters[0])/(parameters[1]-parameters[0])
|
2016-11-08 20:08:06 +04:00
|
|
|
elif(xx >= parameters[1] and xx <= parameters[2]):
|
|
|
|
return (parameters[2]-xx)/(parameters[2]-parameters[1])
|
2016-09-02 22:55:55 +04:00
|
|
|
else:
|
|
|
|
return 0
|
|
|
|
|
|
|
|
def trapmf(x, parameters):
|
|
|
|
if(x < parameters[0]):
|
|
|
|
return 0
|
|
|
|
elif(x >= parameters[0] and x < parameters[1]):
|
|
|
|
return (x-parameters[0])/(parameters[1]-parameters[0])
|
|
|
|
elif(x >= parameters[1] and x <= parameters[2]):
|
|
|
|
return 1
|
|
|
|
elif(x >= parameters[2] and x <= parameters[3]):
|
|
|
|
return (parameters[3]-x)/(parameters[3]-parameters[2])
|
|
|
|
else:
|
|
|
|
return 0
|
|
|
|
|
|
|
|
def gaussmf(x,parameters):
|
|
|
|
return math.exp(-0.5*((x-parameters[0]) / parameters[1] )**2)
|
|
|
|
|
|
|
|
|
|
|
|
def bellmf(x,parameters):
|
|
|
|
return 1 / (1 + abs((xx - parameters[2])/parameters[0])**(2*parameters[1]))
|
|
|
|
|
|
|
|
|
|
|
|
def sigmf(x,parameters):
|
|
|
|
return 1 / (1 + math.exp(-parameters[0] * (x - parameters[1])))
|
|
|
|
|
|
|
|
|
|
|
|
class FuzzySet:
|
|
|
|
def __init__(self,name,mf,parameters,centroid):
|
|
|
|
self.name = name
|
|
|
|
self.mf = mf
|
|
|
|
self.parameters = parameters
|
|
|
|
self.centroid = centroid
|
2016-10-18 21:45:07 +04:00
|
|
|
self.lower = min(parameters)
|
|
|
|
self.upper = max(parameters)
|
2016-09-02 22:55:55 +04:00
|
|
|
|
|
|
|
def membership(self,x):
|
|
|
|
return self.mf(x,self.parameters)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.name + ": " + str(self.mf) + "(" + str(self.parameters) + ")"
|
|
|
|
|
2016-10-18 21:45:07 +04:00
|
|
|
class FLR:
|
|
|
|
def __init__(self,LHS,RHS):
|
|
|
|
self.LHS = LHS
|
|
|
|
self.RHS = RHS
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return str(self.LHS) + " -> " + str(self.RHS)
|
2016-09-02 22:55:55 +04:00
|
|
|
|
2016-10-18 21:45:07 +04:00
|
|
|
def fuzzyInstance(inst, fuzzySets):
|
2016-10-19 20:45:01 +04:00
|
|
|
mv = np.array([ fs.membership(inst) for fs in fuzzySets])
|
|
|
|
return mv
|
2016-10-22 02:59:22 +04:00
|
|
|
|
|
|
|
|
|
|
|
def getMaxMembershipFuzzySet(inst, fuzzySets):
|
|
|
|
mv = fuzzyInstance(inst,fuzzySets)
|
|
|
|
return fuzzySets[ np.argwhere(mv == max(mv) )[0,0] ]
|
2016-10-18 21:45:07 +04:00
|
|
|
|
|
|
|
|
|
|
|
def fuzzySeries(data,fuzzySets):
|
|
|
|
fts = []
|
|
|
|
for item in data:
|
2016-10-22 02:59:22 +04:00
|
|
|
fts.append(getMaxMembershipFuzzySet(item,fuzzySets))
|
2016-10-18 21:45:07 +04:00
|
|
|
return fts
|
|
|
|
|
|
|
|
|
|
|
|
def generateNonRecurrentFLRs(fuzzyData):
|
|
|
|
flrs = {}
|
|
|
|
for i in range(2,len(fuzzyData)):
|
|
|
|
tmp = FLR(fuzzyData[i-1],fuzzyData[i])
|
|
|
|
flrs[str(tmp)] = tmp
|
|
|
|
ret = [value for key, value in flrs.items()]
|
|
|
|
return ret
|
|
|
|
|
|
|
|
def generateRecurrentFLRs(fuzzyData):
|
|
|
|
flrs = []
|
2016-10-18 23:44:03 +04:00
|
|
|
for i in np.arange(1,len(fuzzyData)):
|
|
|
|
flrs.append(FLR(fuzzyData[i-1],fuzzyData[i]))
|
2016-10-18 21:45:07 +04:00
|
|
|
return flrs
|