2016-10-20 17:57:59 +04:00
|
|
|
import numpy as np
|
|
|
|
from pyFTS import *
|
|
|
|
|
|
|
|
class IntervalFTS(hofts.HighOrderFTS):
|
|
|
|
def __init__(self,name):
|
|
|
|
super(IntervalFTS, self).__init__(name)
|
|
|
|
self.flrgs = {}
|
|
|
|
|
|
|
|
def getUpper(self,flrg):
|
2016-10-22 02:59:22 +04:00
|
|
|
if flrg.strLHS() in self.flrgs:
|
|
|
|
tmp = self.flrgs[ flrg.strLHS() ]
|
2016-10-25 21:52:44 +04:00
|
|
|
ret = max(np.array([self.setsDict[s].upper for s in tmp.RHS]))
|
2016-10-22 02:59:22 +04:00
|
|
|
else:
|
|
|
|
ret = flrg.LHS[-1].upper
|
2016-10-20 17:57:59 +04:00
|
|
|
return ret
|
|
|
|
|
|
|
|
def getLower(self,flrg):
|
2016-10-22 02:59:22 +04:00
|
|
|
if flrg.strLHS() in self.flrgs:
|
|
|
|
tmp = self.flrgs[ flrg.strLHS() ]
|
2016-10-25 21:52:44 +04:00
|
|
|
ret = min(np.array([self.setsDict[s].lower for s in tmp.RHS]))
|
2016-10-22 02:59:22 +04:00
|
|
|
else:
|
|
|
|
ret = flrg.LHS[-1].lower
|
2016-10-20 17:57:59 +04:00
|
|
|
return ret
|
|
|
|
|
|
|
|
def getSequenceMembership(self, data, fuzzySets):
|
|
|
|
mb = [ fuzzySets[k].membership( data[k] ) for k in np.arange(0,len(data)) ]
|
|
|
|
return mb
|
2016-10-21 17:23:55 +04:00
|
|
|
|
|
|
|
def buildTree(self,node, lags, level):
|
|
|
|
if level >= self.order:
|
|
|
|
return
|
|
|
|
|
2016-10-22 02:59:22 +04:00
|
|
|
for s in lags[level]:
|
2016-10-21 17:23:55 +04:00
|
|
|
node.appendChild(tree.FLRGTreeNode(s))
|
|
|
|
|
|
|
|
for child in node.getChildren():
|
|
|
|
self.buildTree(child,lags,level+1)
|
2016-10-20 17:57:59 +04:00
|
|
|
|
|
|
|
def forecast(self,data):
|
|
|
|
|
|
|
|
ndata = np.array(data)
|
|
|
|
|
|
|
|
l = len(ndata)
|
|
|
|
|
|
|
|
ret = []
|
|
|
|
|
2016-10-25 13:58:43 +04:00
|
|
|
for k in np.arange(self.order,l):
|
|
|
|
|
2016-10-22 02:59:22 +04:00
|
|
|
flrs = []
|
|
|
|
mvs = []
|
2016-10-20 17:57:59 +04:00
|
|
|
|
|
|
|
up = []
|
|
|
|
lo = []
|
|
|
|
|
|
|
|
# Achar os conjuntos que tem pert > 0 para cada lag
|
|
|
|
count = 0
|
|
|
|
lags = {}
|
2016-10-22 02:59:22 +04:00
|
|
|
if self.order > 1:
|
2016-10-25 13:58:43 +04:00
|
|
|
subset = ndata[k-self.order : k ]
|
2016-10-25 21:52:44 +04:00
|
|
|
|
2016-10-25 13:58:43 +04:00
|
|
|
for instance in subset:
|
2016-10-22 02:59:22 +04:00
|
|
|
mb = common.fuzzyInstance(instance, self.sets)
|
|
|
|
tmp = np.argwhere( mb )
|
|
|
|
idx = np.ravel(tmp) #flat the array
|
|
|
|
lags[count] = idx
|
|
|
|
count = count + 1
|
|
|
|
|
|
|
|
# Constrói uma árvore com todos os caminhos possíveis
|
2016-10-20 17:57:59 +04:00
|
|
|
|
2016-10-22 02:59:22 +04:00
|
|
|
root = tree.FLRGTreeNode(None)
|
2016-10-20 17:57:59 +04:00
|
|
|
|
2016-10-22 02:59:22 +04:00
|
|
|
self.buildTree(root,lags,0)
|
|
|
|
|
|
|
|
# Traça os possíveis caminhos e costrói as HOFLRG's
|
2016-10-20 17:57:59 +04:00
|
|
|
|
2016-10-22 02:59:22 +04:00
|
|
|
for p in root.paths():
|
|
|
|
path = list(reversed(list(filter(None.__ne__, p))))
|
|
|
|
flrg = hofts.HighOrderFLRG(self.order)
|
|
|
|
for kk in path: flrg.appendLHS(self.sets[ kk ])
|
2016-10-20 17:57:59 +04:00
|
|
|
|
2016-10-22 02:59:22 +04:00
|
|
|
flrs.append(flrg)
|
|
|
|
|
|
|
|
# Acha a pertinência geral de cada FLRG
|
2016-10-25 13:58:43 +04:00
|
|
|
mvs.append(min(self.getSequenceMembership(subset, flrg.LHS)))
|
2016-10-22 02:59:22 +04:00
|
|
|
else:
|
2016-10-21 17:23:55 +04:00
|
|
|
|
2016-10-22 02:59:22 +04:00
|
|
|
mv = common.fuzzyInstance(ndata[k],self.sets)
|
|
|
|
tmp = np.argwhere( mv )
|
|
|
|
idx = np.ravel(tmp)
|
|
|
|
for kk in idx:
|
|
|
|
flrg = hofts.HighOrderFLRG(self.order)
|
|
|
|
flrg.appendLHS(self.sets[ kk ])
|
|
|
|
flrs.append(flrg)
|
|
|
|
mvs.append(mv[kk])
|
2016-10-20 17:57:59 +04:00
|
|
|
|
2016-10-22 02:59:22 +04:00
|
|
|
count = 0
|
|
|
|
for flrg in flrs:
|
|
|
|
# achar o os bounds de cada FLRG, ponderados pela pertinência
|
|
|
|
up.append( mvs[count] * self.getUpper(flrg) )
|
|
|
|
lo.append( mvs[count] * self.getLower(flrg) )
|
|
|
|
count = count + 1
|
2016-10-20 17:57:59 +04:00
|
|
|
|
|
|
|
# gerar o intervalo
|
2016-10-22 02:59:22 +04:00
|
|
|
ret.append( [ sum(lo), sum(up) ] )
|
2016-10-20 17:57:59 +04:00
|
|
|
|
2016-10-22 02:59:22 +04:00
|
|
|
return ret
|