pyFTS/pifts.py

145 lines
3.8 KiB
Python
Raw Normal View History

2016-10-25 20:04:37 +04:00
import numpy as np
from pyFTS import *
2016-10-25 21:52:44 +04:00
class ProbabilisticIntervalFLRG(hofts.HighOrderFLRG):
2016-10-25 20:04:37 +04:00
def __init__(self,order):
2016-10-25 21:52:44 +04:00
super(ProbabilisticIntervalFLRG, self).__init__(order)
self.RHS = {}
2016-10-25 20:04:37 +04:00
self.frequencyCount = 0
2016-10-25 21:52:44 +04:00
2016-10-25 20:04:37 +04:00
def appendRHS(self,c):
self.frequencyCount = self.frequencyCount + 1
2016-10-25 21:52:44 +04:00
if c.name in self.RHS:
self.RHS[c.name] = self.RHS[c.name] + 1
2016-10-25 20:04:37 +04:00
else:
2016-10-25 21:52:44 +04:00
self.RHS[c.name] = 1
def getProbability(self,c):
return self.RHS[c] / self.frequencyCount
2016-10-25 20:04:37 +04:00
def __str__(self):
2016-10-25 21:52:44 +04:00
tmp2 = ""
for c in sorted(self.RHS):
if len(tmp2) > 0:
tmp2 = tmp2 + ","
tmp2 = tmp2 + c + "(" + str(round(self.RHS[c]/self.frequencyCount,3)) + ")"
return self.strLHS() + " -> " + tmp2
2016-10-25 20:04:37 +04:00
class ProbabilisticIntervalFTS(ifts.IntervalFTS):
def __init__(self,name):
super(ProbabilisticIntervalFTS, self).__init__("PIFTS")
self.name = "Probabilistic Interval FTS"
self.detail = "Silva, P.; Guimarães, F.; Sadaei, H."
2016-10-25 20:04:37 +04:00
self.flrgs = {}
self.globalFrequency = 0
self.isInterval = True
2016-10-25 20:04:37 +04:00
def generateFLRG(self, flrs):
flrgs = {}
l = len(flrs)
for k in np.arange(self.order +1, l):
flrg = ProbabilisticIntervalFLRG(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)
self.globalFrequency = self.globalFrequency + 1
return (flrgs)
2016-10-25 21:52:44 +04:00
def getProbability(self, flrg):
return flrg.frequencyCount / self.globalFrequency
def getUpper(self,flrg):
if flrg.strLHS() in self.flrgs:
tmp = self.flrgs[ flrg.strLHS() ]
ret = sum(np.array([ tmp.getProbability(s) * self.setsDict[s].upper for s in tmp.RHS]))
else:
ret = flrg.LHS[-1].upper
return ret
def getLower(self,flrg):
if flrg.strLHS() in self.flrgs:
tmp = self.flrgs[ flrg.strLHS() ]
ret = sum(np.array([ tmp.getProbability(s) * self.setsDict[s].lower for s in tmp.RHS]))
else:
ret = flrg.LHS[-1].lower
return ret
2016-10-25 20:04:37 +04:00
def forecast(self,data):
ndata = np.array(data)
l = len(ndata)
ret = []
for k in np.arange(self.order,l):
print(k)
flrs = []
mvs = []
up = []
lo = []
# Achar os conjuntos que tem pert > 0 para cada lag
count = 0
lags = {}
if self.order > 1:
subset = ndata[k-self.order : k ]
print(subset)
for instance in subset:
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
root = tree.FLRGTreeNode(None)
self.buildTree(root,lags,0)
# Traça os possíveis caminhos e costrói as HOFLRG's
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-25 21:52:44 +04:00
##
flrs.append( self.flrgs[ flrg.strLHS() ] )
2016-10-25 20:04:37 +04:00
# Acha a pertinência geral de cada FLRG
mvs.append(min(self.getSequenceMembership(subset, flrg.LHS)))
else:
2016-10-25 21:52:44 +04:00
mv = common.fuzzyInstance(ndata[k],self.sets) # get all membership values
tmp = np.argwhere( mv ) # get the indices of values > 0
idx = np.ravel(tmp) # flatten the array
2016-10-25 20:04:37 +04:00
for kk in idx:
flrg = hofts.HighOrderFLRG(self.order)
flrg.appendLHS(self.sets[ kk ])
2016-10-25 21:52:44 +04:00
flrs.append( self.flrgs[ flrg.strLHS() ] )
2016-10-25 20:04:37 +04:00
mvs.append(mv[kk])
count = 0
for flrg in flrs:
# achar o os bounds de cada FLRG, ponderados pela pertinência
2016-10-25 21:52:44 +04:00
up.append( self.getProbability(flrg) * mvs[count] * self.getUpper(flrg) )
lo.append( self.getProbability(flrg) * mvs[count] * self.getLower(flrg) )
2016-10-25 20:04:37 +04:00
count = count + 1
# gerar o intervalo
ret.append( [ sum(lo), sum(up) ] )
return ret