2016-10-19 20:45:11 +04:00
|
|
|
import numpy as np
|
|
|
|
from pyFTS import *
|
|
|
|
|
|
|
|
class HighOrderFLRG:
|
|
|
|
def __init__(self,order):
|
|
|
|
self.LHS = []
|
2016-10-25 21:52:44 +04:00
|
|
|
self.RHS = {}
|
2016-10-19 20:45:11 +04:00
|
|
|
self.order = order
|
|
|
|
self.strlhs = ""
|
|
|
|
|
|
|
|
def appendRHS(self,c):
|
2016-10-25 21:52:44 +04:00
|
|
|
if c.name not in self.RHS:
|
|
|
|
self.RHS[c.name] = c
|
2016-10-19 20:45:11 +04:00
|
|
|
|
|
|
|
def strLHS(self):
|
|
|
|
if len(self.strlhs) == 0:
|
|
|
|
for c in self.LHS:
|
|
|
|
if len(self.strlhs) > 0:
|
2016-10-26 19:01:30 +04:00
|
|
|
self.strlhs = self.strlhs + ", "
|
2016-10-19 20:45:11 +04:00
|
|
|
self.strlhs = self.strlhs + c.name
|
|
|
|
return self.strlhs
|
|
|
|
|
|
|
|
def appendLHS(self,c):
|
|
|
|
self.LHS.append(c)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
tmp = ""
|
2016-10-25 21:52:44 +04:00
|
|
|
for c in sorted(self.RHS):
|
2016-10-19 20:45:11 +04:00
|
|
|
if len(tmp) > 0:
|
|
|
|
tmp = tmp + ","
|
2016-10-25 21:52:44 +04:00
|
|
|
tmp = tmp + c
|
2016-10-19 20:45:11 +04:00
|
|
|
return self.strLHS() + " -> " + tmp
|
|
|
|
|
|
|
|
class HighOrderFTS(fts.FTS):
|
|
|
|
def __init__(self,name):
|
2016-10-27 23:14:17 +04:00
|
|
|
super(HighOrderFTS, self).__init__(1,"HOFTS" + name)
|
2016-10-25 22:21:32 +04:00
|
|
|
self.name = "High Order FTS"
|
|
|
|
self.detail = "Chen"
|
2016-10-19 20:45:11 +04:00
|
|
|
self.order = 1
|
2016-10-25 21:52:44 +04:00
|
|
|
self.setsDict = {}
|
2016-10-19 20:45:11 +04:00
|
|
|
|
|
|
|
def generateFLRG(self, flrs):
|
|
|
|
flrgs = {}
|
|
|
|
l = len(flrs)
|
|
|
|
for k in np.arange(self.order +1, l):
|
|
|
|
flrg = HighOrderFLRG(self.order)
|
|
|
|
|
|
|
|
for kk in np.arange(k - self.order, k):
|
|
|
|
flrg.appendLHS( flrs[kk].LHS )
|
|
|
|
|
|
|
|
if flrg.strLHS() in flrgs:
|
|
|
|
flrgs[flrg.strLHS()].appendRHS(flrs[k].RHS)
|
|
|
|
else:
|
|
|
|
flrgs[flrg.strLHS()] = flrg;
|
|
|
|
flrgs[flrg.strLHS()].appendRHS(flrs[k].RHS)
|
|
|
|
return (flrgs)
|
|
|
|
|
|
|
|
def train(self, data, sets, order):
|
|
|
|
self.order = order
|
|
|
|
self.sets = sets
|
2016-10-25 21:52:44 +04:00
|
|
|
for s in self.sets: self.setsDict[s.name] = s
|
2016-10-19 20:45:11 +04:00
|
|
|
tmpdata = common.fuzzySeries(data,sets)
|
|
|
|
flrs = common.generateRecurrentFLRs(tmpdata)
|
|
|
|
self.flrgs = self.generateFLRG(flrs)
|
2016-10-25 21:52:44 +04:00
|
|
|
|
|
|
|
def getMidpoints(self,flrg):
|
|
|
|
ret = np.array([self.setsDict[s].centroid for s in flrg.RHS])
|
|
|
|
return ret
|
2016-10-19 20:45:11 +04:00
|
|
|
|
|
|
|
def forecast(self,data):
|
|
|
|
|
|
|
|
ret = []
|
|
|
|
|
|
|
|
l = len(data)
|
|
|
|
|
|
|
|
if l <= self.order:
|
|
|
|
return data
|
|
|
|
|
|
|
|
for k in np.arange(self.order, l):
|
|
|
|
tmpdata = common.fuzzySeries(data[k-self.order : k],self.sets)
|
|
|
|
tmpflrg = HighOrderFLRG(self.order)
|
|
|
|
|
|
|
|
for s in tmpdata: tmpflrg.appendLHS(s)
|
|
|
|
|
|
|
|
if tmpflrg.strLHS() not in self.flrgs:
|
|
|
|
ret.append(tmpdata[-1].centroid)
|
|
|
|
else:
|
|
|
|
flrg = self.flrgs[tmpflrg.strLHS()]
|
|
|
|
mp = self.getMidpoints(flrg)
|
|
|
|
|
|
|
|
ret.append(sum(mp)/len(mp))
|
|
|
|
|
|
|
|
return ret
|
|
|
|
|