2016-10-18 15:54:49 +04:00
|
|
|
import numpy as np
|
|
|
|
from pyFTS import *
|
|
|
|
|
|
|
|
class SeasonalFLRG(fts.FTS):
|
|
|
|
def __init__(self,seasonality):
|
|
|
|
self.LHS = seasonality
|
|
|
|
self.RHS = []
|
|
|
|
|
|
|
|
def append(self,c):
|
|
|
|
self.RHS.append(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 SeasonalFTS(fts.FTS):
|
|
|
|
def __init__(self,name):
|
2016-10-18 16:09:36 +04:00
|
|
|
super(SeasonalFTS, self).__init__(1,name)
|
2016-10-18 15:54:49 +04:00
|
|
|
|
2016-10-18 16:09:36 +04:00
|
|
|
def forecast(self,data):
|
2016-10-18 15:54:49 +04:00
|
|
|
|
|
|
|
actual = self.fuzzy(data)
|
|
|
|
|
|
|
|
if actual["fuzzyset"] not in self.flrgs:
|
|
|
|
return self.sets[actual["fuzzyset"]].centroid
|
|
|
|
|
|
|
|
flrg = self.flrgs[actual["fuzzyset"]]
|
|
|
|
|
|
|
|
mi = np.array([self.sets[s].centroid for s in flrg.RHS])
|
|
|
|
|
|
|
|
return mi.dot( flrg.weights() )
|
|
|
|
|
2016-10-18 16:09:36 +04:00
|
|
|
def train(self, data, sets):
|
2016-10-18 15:54:49 +04:00
|
|
|
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-10-18 16:09:36 +04:00
|
|
|
self.flrgs[last["fuzzyset"]] = SeasonalFLRG(last["fuzzyset"])
|
2016-10-18 15:54:49 +04:00
|
|
|
|
|
|
|
self.flrgs[last["fuzzyset"]].append(actual["fuzzyset"])
|
|
|
|
count = count + 1
|
|
|
|
last = actual
|