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) self.RHS.add(c)
def __str__(self): def __str__(self):
tmp = self.LHS + " -> " tmp = self.LHS.name + " -> "
tmp2 = "" tmp2 = ""
for c in self.RHS: for c in sorted(self.RHS, key=lambda s: s.name):
if len(tmp2) > 0: if len(tmp2) > 0:
tmp2 = tmp2 + "," tmp2 = tmp2 + ","
tmp2 = tmp2 + c tmp2 = tmp2 + c.name
return tmp + tmp2 return tmp + tmp2

View File

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

2
fts.py
View File

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

View File

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

View File

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