2016-09-08 01:51:00 +04:00
|
|
|
from pyFTS import *
|
|
|
|
|
|
|
|
class ConventionalFLRG:
|
2016-09-02 22:55:55 +04:00
|
|
|
def __init__(self,premiss):
|
|
|
|
self.premiss = premiss
|
|
|
|
self.consequent = set()
|
|
|
|
|
|
|
|
def append(self,c):
|
|
|
|
self.consequent.add(c)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
tmp = self.premiss + " -> "
|
|
|
|
tmp2 = ""
|
|
|
|
for c in self.consequent:
|
|
|
|
if len(tmp2) > 0:
|
|
|
|
tmp2 = tmp2 + ","
|
|
|
|
tmp2 = tmp2 + c
|
|
|
|
return tmp + tmp2
|
|
|
|
|
|
|
|
|
2016-09-08 01:51:00 +04:00
|
|
|
class ConventionalFTS(fts.FTS):
|
2016-09-02 22:55:55 +04:00
|
|
|
def __init__(self,name):
|
2016-09-08 01:51:00 +04:00
|
|
|
super(ConventionalFTS, self).__init__(1,name)
|
2016-09-02 22:55:55 +04:00
|
|
|
|
|
|
|
def defuzzy(self,data):
|
|
|
|
|
|
|
|
actual = self.fuzzy(data)
|
|
|
|
|
|
|
|
if actual["fuzzyset"] not in self.flrgs:
|
|
|
|
return self.sets[actual["fuzzyset"]].centroid
|
|
|
|
|
|
|
|
flrg = self.flrgs[actual["fuzzyset"]]
|
|
|
|
|
|
|
|
count = 0.0
|
|
|
|
denom = 0.0
|
|
|
|
|
|
|
|
for s in flrg.consequent:
|
|
|
|
denom = denom + self.sets[s].centroid
|
|
|
|
count = count + 1.0
|
|
|
|
|
|
|
|
return denom/count
|
|
|
|
|
|
|
|
def learn(self, data, sets):
|
|
|
|
last = {"fuzzyset":"", "membership":0.0}
|
|
|
|
actual = {"fuzzyset":"", "membership":0.0}
|
|
|
|
|
|
|
|
for s in sets:
|
|
|
|
self.sets[s.name] = s
|
|
|
|
|
|
|
|
self.flrgs = {}
|
|
|
|
count = 1
|
|
|
|
for inst in data:
|
|
|
|
actual = self.fuzzy(inst)
|
|
|
|
|
|
|
|
if count > self.order:
|
|
|
|
if last["fuzzyset"] not in self.flrgs:
|
2016-09-08 01:51:00 +04:00
|
|
|
self.flrgs[last["fuzzyset"]] = ConventionalFLRG(last["fuzzyset"])
|
2016-09-02 22:55:55 +04:00
|
|
|
|
|
|
|
self.flrgs[last["fuzzyset"]].append(actual["fuzzyset"])
|
|
|
|
count = count + 1
|
|
|
|
last = actual
|
|
|
|
|