Refatoração dos códigos para padronizar com a rfts e HOFTS

This commit is contained in:
Petrônio Cândido de Lima e Silva 2016-10-19 14:45:01 -02:00
parent 30ccb6a88f
commit a04eabf9ed
6 changed files with 41 additions and 33 deletions

View File

@ -10,12 +10,12 @@ class ConventionalFLRG:
self.RHS.add(c)
def __str__(self):
tmp = self.LHS + " -> "
tmp = self.LHS.name + " -> "
tmp2 = ""
for c in self.RHS:
for c in sorted(self.RHS, key=lambda s: s.name):
if len(tmp2) > 0:
tmp2 = tmp2 + ","
tmp2 = tmp2 + c
tmp2 = tmp2 + c.name
return tmp + tmp2

View File

@ -65,8 +65,8 @@ class FLR:
return str(self.LHS) + " -> " + str(self.RHS)
def fuzzyInstance(inst, fuzzySets):
mv = np.array([ fs.membership(inst) for fs in fuzzySets])
return mv
mv = np.array([ fs.membership(inst) for fs in fuzzySets])
return mv
def fuzzySeries(data,fuzzySets):

2
fts.py
View File

@ -31,6 +31,6 @@ class FTS:
def __str__(self):
tmp = self.name + ":\n"
for r in self.flrgs.keys():
for r in sorted(self.flrgs):
tmp = tmp + str(self.flrgs[r]) + "\n"
return tmp

View File

@ -8,19 +8,19 @@ class ImprovedWeightedFLRG:
self.count = 0.0
def append(self,c):
if c not in self.RHS:
self.RHS[c] = 1.0
if c.name not in self.RHS:
self.RHS[c.name] = 1.0
else:
self.RHS[c] = self.RHS[c] + 1.0
self.RHS[c.name] = self.RHS[c.name] + 1.0
self.count = self.count + 1.0
def weights(self):
return np.array([ self.RHS[c]/self.count for c in self.RHS.keys() ])
def __str__(self):
tmp = self.LHS + " -> "
tmp = self.LHS.name + " -> "
tmp2 = ""
for c in self.RHS.keys():
for c in sorted(self.RHS):
if len(tmp2) > 0:
tmp2 = tmp2 + ","
tmp2 = tmp2 + c + "(" + str(round(self.RHS[c]/self.count,3)) + ")"
@ -30,22 +30,30 @@ class ImprovedWeightedFLRG:
class ImprovedWeightedFTS(fts.FTS):
def __init__(self,name):
super(ImprovedWeightedFTS, self).__init__(1,name)
self.setsDict = {}
def generateFLRG(self, flrs):
flrgs = {}
for flr in flrs:
if flr.LHS in flrgs:
flrgs[flr.LHS].append(flr.RHS)
if flr.LHS.name in flrgs:
flrgs[flr.LHS.name].append(flr.RHS)
else:
flrgs[flr.LHS] = ImprovedWeightedFLRG(flr.LHS);
flrgs[flr.LHS].append(flr.RHS)
flrgs[flr.LHS.name] = ImprovedWeightedFLRG(flr.LHS);
flrgs[flr.LHS.name].append(flr.RHS)
return (flrgs)
def train(self, data, sets):
self.sets = sets
tmpdata = common.fuzzySeries(data,sets)
for s in self.sets: self.setsDict[s.name] = s
tmpdata = common.fuzzySeries(data,self.sets)
flrs = common.generateRecurrentFLRs(tmpdata)
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):
l = 1
@ -68,6 +76,6 @@ class ImprovedWeightedFTS(fts.FTS):
flrg = self.flrgs[actual.name]
mp = self.getMidpoints(flrg)
ret.append( mi.dot( flrg.weights() ))
ret.append( mp.dot( flrg.weights() ))
return ret

View File

@ -18,42 +18,42 @@ class ExponentialyWeightedFLRG:
return np.array([ k/tot for k in wei ])
def __str__(self):
tmp = self.LHS + " -> "
tmp = self.LHS.name + " -> "
tmp2 = ""
cc = 0
wei = [ self.c**k for k in np.arange(0.0,self.count,1.0)]
tot = sum( wei )
for c in self.RHS:
for c in sorted(self.RHS, key=lambda s: s.name):
if len(tmp2) > 0:
tmp2 = tmp2 + ","
tmp2 = tmp2 + c + "(" + str(wei[cc]/tot) + ")"
tmp2 = tmp2 + c.name + "(" + str(wei[cc]/tot) + ")"
cc = cc + 1
return tmp + tmp2
class ExponentialyWeightedFTS(fts.FTS):
def __init__(self,name):
super(ExponentialyWeightedFTS, self).__init__(1,name)
this.c = 1
self.c = 1
def generateFLRG(self, flrs, c):
flrgs = {}
for flr in flrs:
if flr.LHS in flrgs:
flrgs[flr.LHS].append(flr.RHS)
if flr.LHS.name in flrgs:
flrgs[flr.LHS.name].append(flr.RHS)
else:
flrgs[flr.LHS] = ExponentialyWeightedFLRG(flr.LHS, c);
flrgs[flr.LHS].append(flr.RHS)
flrgs[flr.LHS.name] = ExponentialyWeightedFLRG(flr.LHS, c);
flrgs[flr.LHS.name].append(flr.RHS)
return (flrgs)
def train(self, data, sets, c):
this.c = c
self.c = c
self.sets = sets
tmpdata = common.fuzzySeries(data,sets)
flrs = common.generateRecurrentFLRs(tmpdata)
self.flrgs = self.generateFLRG(flrs,c)
def forecast(self,data):
l = 1
l = 1
ndata = np.array(data)
@ -73,7 +73,7 @@ class ExponentialyWeightedFTS(fts.FTS):
flrg = self.flrgs[actual.name]
mp = self.getMidpoints(flrg)
ret.append( mi.dot( flrg.weights() ))
ret.append( mp.dot( flrg.weights() ))
return ret

10
yu.py
View File

@ -16,14 +16,14 @@ class WeightedFLRG(fts.FTS):
return np.array([ k/tot for k in np.arange(1.0,self.count,1.0) ])
def __str__(self):
tmp = self.LHS + " -> "
tmp = self.LHS.name + " -> "
tmp2 = ""
cc = 1.0
tot = sum( np.arange(1.0,self.count,1.0) )
for c in self.RHS:
for c in sorted(self.RHS, key=lambda s: s.name):
if len(tmp2) > 0:
tmp2 = tmp2 + ","
tmp2 = tmp2 + c + "(" + str(round(cc/tot,3)) + ")"
tmp2 = tmp2 + c.name + "(" + str(round(cc/tot,3)) + ")"
cc = cc + 1.0
return tmp + tmp2
@ -49,7 +49,7 @@ class WeightedFTS(fts.FTS):
self.flrgs = self.generateFLRG(flrs)
def forecast(self,data):
l = 1
l = 1
ndata = np.array(data)
@ -69,6 +69,6 @@ class WeightedFTS(fts.FTS):
flrg = self.flrgs[actual.name]
mp = self.getMidpoints(flrg)
ret.append( mi.dot( flrg.weights() ))
ret.append( mp.dot( flrg.weights() ))
return ret