63 lines
1.3 KiB
Python
63 lines
1.3 KiB
Python
from pyFTS import *
|
|
|
|
class ConventionalFLRG:
|
|
def __init__(self,LHS):
|
|
self.LHS = LHS
|
|
self.RHS = set()
|
|
|
|
def append(self,c):
|
|
self.RHS.add(c)
|
|
|
|
def __str__(self):
|
|
tmp = self.LHS + " -> "
|
|
tmp2 = ""
|
|
for c in self.RHS:
|
|
if len(tmp2) > 0:
|
|
tmp2 = tmp2 + ","
|
|
tmp2 = tmp2 + c
|
|
return tmp + tmp2
|
|
|
|
|
|
class ConventionalFTS(fts.FTS):
|
|
def __init__(self,name):
|
|
super(ConventionalFTS, self).__init__(1,name)
|
|
|
|
def forecast(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.RHS:
|
|
denom = denom + self.sets[s].centroid
|
|
count = count + 1.0
|
|
|
|
return denom/count
|
|
|
|
def train(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:
|
|
self.flrgs[last["fuzzyset"]] = ConventionalFLRG(last["fuzzyset"])
|
|
|
|
self.flrgs[last["fuzzyset"]].append(actual["fuzzyset"])
|
|
count = count + 1
|
|
last = actual
|
|
|