pyFTS/common.py

62 lines
1.6 KiB
Python

def trimf(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 (parameters[2]-x)/(parameters[2]-parameters[1])
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
def membership(self,x):
return self.mf(x,self.parameters)
def __str__(self):
return self.name + ": " + str(self.mf) + "(" + str(self.parameters) + ")"
def GridPartitionerTrimf(data,npart,names = None,prefix = "A"):
sets = []
dmax = max(data)
dmin = min(data)
dlen = dmax - dmin
partlen = dlen / npart
partition = dmin
for c in range(npart):
sets.append( FuzzySet(prefix+str(c),trimf,[partition-partlen, partition, partition+partlen], partition ) )
partition = partition + partlen
return sets