2017-01-14 03:42:00 +04:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf8 -*-
|
|
|
|
|
2016-10-20 17:57:59 +04:00
|
|
|
import numpy as np
|
2016-12-22 20:36:50 +04:00
|
|
|
from pyFTS.common import FuzzySet,FLR
|
2017-01-11 00:05:51 +04:00
|
|
|
from pyFTS import hofts, fts, tree
|
2016-12-22 20:36:50 +04:00
|
|
|
|
2016-10-20 17:57:59 +04:00
|
|
|
|
|
|
|
class IntervalFTS(hofts.HighOrderFTS):
|
2017-05-05 22:33:27 +04:00
|
|
|
"""High Order Interval Fuzzy Time Series"""
|
2017-05-03 00:16:49 +04:00
|
|
|
def __init__(self, name, **kwargs):
|
2017-05-02 03:56:47 +04:00
|
|
|
super(IntervalFTS, self).__init__(order=1, name="IFTS " + name)
|
2016-12-22 20:36:50 +04:00
|
|
|
self.shortname = "IFTS " + name
|
|
|
|
self.name = "Interval FTS"
|
|
|
|
self.detail = "Silva, P.; Guimarães, F.; Sadaei, H. (2016)"
|
|
|
|
self.flrgs = {}
|
2017-05-02 18:32:03 +04:00
|
|
|
self.has_point_forecasting = False
|
|
|
|
self.has_point_forecasting = True
|
|
|
|
self.is_high_order = True
|
2016-12-22 20:36:50 +04:00
|
|
|
|
|
|
|
def getUpper(self, flrg):
|
|
|
|
if flrg.strLHS() in self.flrgs:
|
|
|
|
tmp = self.flrgs[flrg.strLHS()]
|
|
|
|
ret = max(np.array([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 = min(np.array([self.setsDict[s].lower for s in tmp.RHS]))
|
|
|
|
else:
|
|
|
|
ret = flrg.LHS[-1].lower
|
|
|
|
return ret
|
|
|
|
|
|
|
|
def getSequenceMembership(self, data, fuzzySets):
|
|
|
|
mb = [fuzzySets[k].membership(data[k]) for k in np.arange(0, len(data))]
|
|
|
|
return mb
|
|
|
|
|
|
|
|
def buildTree(self, node, lags, level):
|
|
|
|
if level >= self.order:
|
|
|
|
return
|
|
|
|
|
|
|
|
for s in lags[level]:
|
|
|
|
node.appendChild(tree.FLRGTreeNode(s))
|
|
|
|
|
|
|
|
for child in node.getChildren():
|
|
|
|
self.buildTree(child, lags, level + 1)
|
|
|
|
|
2017-04-15 02:57:39 +04:00
|
|
|
def forecastInterval(self, data, **kwargs):
|
2016-12-22 20:36:50 +04:00
|
|
|
|
2017-01-30 03:59:50 +04:00
|
|
|
ndata = np.array(self.doTransformations(data))
|
2016-12-22 20:36:50 +04:00
|
|
|
|
|
|
|
l = len(ndata)
|
|
|
|
|
|
|
|
ret = []
|
|
|
|
|
|
|
|
for k in np.arange(self.order - 1, l):
|
|
|
|
|
|
|
|
affected_flrgs = []
|
|
|
|
affected_flrgs_memberships = []
|
|
|
|
|
|
|
|
up = []
|
|
|
|
lo = []
|
|
|
|
|
|
|
|
# Achar os conjuntos que tem pert > 0 para cada lag
|
|
|
|
count = 0
|
|
|
|
lags = {}
|
|
|
|
if self.order > 1:
|
|
|
|
subset = ndata[k - (self.order - 1): k + 1]
|
|
|
|
|
|
|
|
for instance in subset:
|
|
|
|
mb = FuzzySet.fuzzyInstance(instance, self.sets)
|
|
|
|
tmp = np.argwhere(mb)
|
|
|
|
idx = np.ravel(tmp) # flat the array
|
2017-03-22 06:17:06 +04:00
|
|
|
|
|
|
|
if idx.size == 0: # the element is out of the bounds of the Universe of Discourse
|
|
|
|
if instance <= self.sets[0].lower:
|
|
|
|
idx = [0]
|
|
|
|
elif instance >= self.sets[-1].upper:
|
|
|
|
idx = [len(self.sets) - 1]
|
|
|
|
else:
|
|
|
|
raise Exception(instance)
|
|
|
|
|
|
|
|
|
2016-12-22 20:36:50 +04:00
|
|
|
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])
|
|
|
|
|
|
|
|
affected_flrgs.append(flrg)
|
|
|
|
|
|
|
|
# Acha a pertinência geral de cada FLRG
|
|
|
|
affected_flrgs_memberships.append(min(self.getSequenceMembership(subset, flrg.LHS)))
|
|
|
|
else:
|
|
|
|
|
|
|
|
mv = FuzzySet.fuzzyInstance(ndata[k], self.sets)
|
|
|
|
tmp = np.argwhere(mv)
|
|
|
|
idx = np.ravel(tmp)
|
2017-03-22 06:17:06 +04:00
|
|
|
|
|
|
|
if idx.size == 0: # the element is out of the bounds of the Universe of Discourse
|
|
|
|
if ndata[k] <= self.sets[0].lower:
|
|
|
|
idx = [0]
|
|
|
|
elif ndata[k] >= self.sets[-1].upper:
|
|
|
|
idx = [len(self.sets) - 1]
|
|
|
|
else:
|
|
|
|
raise Exception(ndata[k])
|
|
|
|
|
2016-12-22 20:36:50 +04:00
|
|
|
for kk in idx:
|
|
|
|
flrg = hofts.HighOrderFLRG(self.order)
|
|
|
|
flrg.appendLHS(self.sets[kk])
|
|
|
|
affected_flrgs.append(flrg)
|
|
|
|
affected_flrgs_memberships.append(mv[kk])
|
|
|
|
|
|
|
|
count = 0
|
|
|
|
for flrg in affected_flrgs:
|
|
|
|
# achar o os bounds de cada FLRG, ponderados pela pertinência
|
|
|
|
up.append(affected_flrgs_memberships[count] * self.getUpper(flrg))
|
|
|
|
lo.append(affected_flrgs_memberships[count] * self.getLower(flrg))
|
|
|
|
count = count + 1
|
|
|
|
|
|
|
|
# gerar o intervalo
|
|
|
|
norm = sum(affected_flrgs_memberships)
|
2017-01-30 03:59:50 +04:00
|
|
|
lo_ = self.doInverseTransformations(sum(lo) / norm, params=[data[k - (self.order - 1): k + 1]])
|
|
|
|
up_ = self.doInverseTransformations(sum(up) / norm, params=[data[k - (self.order - 1): k + 1]])
|
2017-01-27 14:26:47 +04:00
|
|
|
ret.append([lo_, up_])
|
2016-12-22 20:36:50 +04:00
|
|
|
|
|
|
|
return ret
|