Correções em HOFTS, IFTS e PIFTS

This commit is contained in:
Petrônio Cândido de Lima e Silva 2016-10-25 15:52:44 -02:00
parent 62ca5efc1b
commit ce5c267505
3 changed files with 61 additions and 50 deletions

View File

@ -4,12 +4,13 @@ from pyFTS import *
class HighOrderFLRG: class HighOrderFLRG:
def __init__(self,order): def __init__(self,order):
self.LHS = [] self.LHS = []
self.RHS = [] self.RHS = {}
self.order = order self.order = order
self.strlhs = "" self.strlhs = ""
def appendRHS(self,c): def appendRHS(self,c):
self.RHS.append(c) if c.name not in self.RHS:
self.RHS[c.name] = c
def strLHS(self): def strLHS(self):
if len(self.strlhs) == 0: if len(self.strlhs) == 0:
@ -24,16 +25,17 @@ class HighOrderFLRG:
def __str__(self): def __str__(self):
tmp = "" tmp = ""
for c in sorted(self.RHS, key=lambda s: s.name): for c in sorted(self.RHS):
if len(tmp) > 0: if len(tmp) > 0:
tmp = tmp + "," tmp = tmp + ","
tmp = tmp + c.name tmp = tmp + c
return self.strLHS() + " -> " + tmp return self.strLHS() + " -> " + tmp
class HighOrderFTS(fts.FTS): class HighOrderFTS(fts.FTS):
def __init__(self,name): def __init__(self,name):
super(HighOrderFTS, self).__init__(1,name) super(HighOrderFTS, self).__init__(1,name)
self.order = 1 self.order = 1
self.setsDict = {}
def generateFLRG(self, flrs): def generateFLRG(self, flrs):
flrgs = {} flrgs = {}
@ -54,9 +56,14 @@ class HighOrderFTS(fts.FTS):
def train(self, data, sets, order): def train(self, data, sets, order):
self.order = order self.order = order
self.sets = sets self.sets = sets
for s in self.sets: self.setsDict[s.name] = s
tmpdata = common.fuzzySeries(data,sets) tmpdata = common.fuzzySeries(data,sets)
flrs = common.generateRecurrentFLRs(tmpdata) flrs = common.generateRecurrentFLRs(tmpdata)
self.flrgs = self.generateFLRG(flrs) self.flrgs = self.generateFLRG(flrs)
def getMidpoints(self,flrg):
ret = np.array([self.setsDict[s].centroid for s in flrg.RHS])
return ret
def forecast(self,data): def forecast(self,data):

10
ifts.py
View File

@ -1,8 +1,6 @@
import numpy as np import numpy as np
from pyFTS import * from pyFTS import *
class IntervalFTS(hofts.HighOrderFTS): class IntervalFTS(hofts.HighOrderFTS):
def __init__(self,name): def __init__(self,name):
super(IntervalFTS, self).__init__(name) super(IntervalFTS, self).__init__(name)
@ -11,7 +9,7 @@ class IntervalFTS(hofts.HighOrderFTS):
def getUpper(self,flrg): def getUpper(self,flrg):
if flrg.strLHS() in self.flrgs: if flrg.strLHS() in self.flrgs:
tmp = self.flrgs[ flrg.strLHS() ] tmp = self.flrgs[ flrg.strLHS() ]
ret = max(np.array([s.upper for s in tmp.RHS])) ret = max(np.array([self.setsDict[s].upper for s in tmp.RHS]))
else: else:
ret = flrg.LHS[-1].upper ret = flrg.LHS[-1].upper
return ret return ret
@ -19,7 +17,7 @@ class IntervalFTS(hofts.HighOrderFTS):
def getLower(self,flrg): def getLower(self,flrg):
if flrg.strLHS() in self.flrgs: if flrg.strLHS() in self.flrgs:
tmp = self.flrgs[ flrg.strLHS() ] tmp = self.flrgs[ flrg.strLHS() ]
ret = min(np.array([s.lower for s in tmp.RHS])) ret = min(np.array([self.setsDict[s].lower for s in tmp.RHS]))
else: else:
ret = flrg.LHS[-1].lower ret = flrg.LHS[-1].lower
return ret return ret
@ -48,8 +46,6 @@ class IntervalFTS(hofts.HighOrderFTS):
for k in np.arange(self.order,l): for k in np.arange(self.order,l):
print(k)
flrs = [] flrs = []
mvs = [] mvs = []
@ -61,7 +57,7 @@ class IntervalFTS(hofts.HighOrderFTS):
lags = {} lags = {}
if self.order > 1: if self.order > 1:
subset = ndata[k-self.order : k ] subset = ndata[k-self.order : k ]
print(subset)
for instance in subset: for instance in subset:
mb = common.fuzzyInstance(instance, self.sets) mb = common.fuzzyInstance(instance, self.sets)
tmp = np.argwhere( mb ) tmp = np.argwhere( mb )

View File

@ -1,45 +1,33 @@
import numpy as np import numpy as np
from pyFTS import * from pyFTS import *
class ProbabilisticIntervalFLRG: class ProbabilisticIntervalFLRG(hofts.HighOrderFLRG):
def __init__(self,order): def __init__(self,order):
self.LHS = [] super(ProbabilisticIntervalFLRG, self).__init__(order)
self.RHS = [] self.RHS = {}
self.RHSfreqs = {}
self.order = order
self.frequencyCount = 0 self.frequencyCount = 0
self.strlhs = ""
def appendRHS(self,c):
self.RHS.append(c)
self.frequencyCount = self.frequencyCount + 1
if c.name in self.RHSfreqs:
self.RHSfreqs[c.name] = self.RHSfreqs[c.name] + 1
else:
self.RHSfreqs[c.name] = 1
def strLHS(self): def appendRHS(self,c):
if len(self.strlhs) == 0: self.frequencyCount = self.frequencyCount + 1
for c in self.LHS: if c.name in self.RHS:
if len(self.strlhs) > 0: self.RHS[c.name] = self.RHS[c.name] + 1
self.strlhs = self.strlhs + "," else:
self.strlhs = self.strlhs + c.name self.RHS[c.name] = 1
return self.strlhs
def getProbability(self,c):
def appendLHS(self,c): return self.RHS[c] / self.frequencyCount
self.LHS.append(c)
def __str__(self): def __str__(self):
tmp = "" tmp2 = ""
for c in sorted(self.RHS, key=lambda s: s.name): for c in sorted(self.RHS):
if len(tmp) > 0: if len(tmp2) > 0:
tmp = tmp + "," tmp2 = tmp2 + ","
tmp = tmp + c.name tmp2 = tmp2 + c + "(" + str(round(self.RHS[c]/self.frequencyCount,3)) + ")"
return self.strLHS() + " -> " + tmp return self.strLHS() + " -> " + tmp2
class ProbabilisticIntervalFTS(ifts.IntervalFTS): class ProbabilisticIntervalFTS(ifts.IntervalFTS):
def __init__(self,name): def __init__(self,name):
super(IntervalFTS, self).__init__(1,name) super(ProbabilisticIntervalFTS, self).__init__(name)
self.flrgs = {} self.flrgs = {}
self.globalFrequency = 0 self.globalFrequency = 0
@ -60,6 +48,25 @@ class ProbabilisticIntervalFTS(ifts.IntervalFTS):
self.globalFrequency = self.globalFrequency + 1 self.globalFrequency = self.globalFrequency + 1
return (flrgs) return (flrgs)
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
def forecast(self,data): def forecast(self,data):
@ -105,26 +112,27 @@ class ProbabilisticIntervalFTS(ifts.IntervalFTS):
flrg = hofts.HighOrderFLRG(self.order) flrg = hofts.HighOrderFLRG(self.order)
for kk in path: flrg.appendLHS(self.sets[ kk ]) for kk in path: flrg.appendLHS(self.sets[ kk ])
flrs.append(flrg) ##
flrs.append( self.flrgs[ flrg.strLHS() ] )
# Acha a pertinência geral de cada FLRG # Acha a pertinência geral de cada FLRG
mvs.append(min(self.getSequenceMembership(subset, flrg.LHS))) mvs.append(min(self.getSequenceMembership(subset, flrg.LHS)))
else: else:
mv = common.fuzzyInstance(ndata[k],self.sets) mv = common.fuzzyInstance(ndata[k],self.sets) # get all membership values
tmp = np.argwhere( mv ) tmp = np.argwhere( mv ) # get the indices of values > 0
idx = np.ravel(tmp) idx = np.ravel(tmp) # flatten the array
for kk in idx: for kk in idx:
flrg = hofts.HighOrderFLRG(self.order) flrg = hofts.HighOrderFLRG(self.order)
flrg.appendLHS(self.sets[ kk ]) flrg.appendLHS(self.sets[ kk ])
flrs.append(flrg) flrs.append( self.flrgs[ flrg.strLHS() ] )
mvs.append(mv[kk]) mvs.append(mv[kk])
count = 0 count = 0
for flrg in flrs: for flrg in flrs:
# achar o os bounds de cada FLRG, ponderados pela pertinência # achar o os bounds de cada FLRG, ponderados pela pertinência
up.append( mvs[count] * self.getUpper(flrg) ) up.append( self.getProbability(flrg) * mvs[count] * self.getUpper(flrg) )
lo.append( mvs[count] * self.getLower(flrg) ) lo.append( self.getProbability(flrg) * mvs[count] * self.getLower(flrg) )
count = count + 1 count = count + 1
# gerar o intervalo # gerar o intervalo