From a04eabf9ed7939a40580bb0910e0171547c070e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=C3=B4nio=20C=C3=A2ndido=20de=20Lima=20e=20Silva?= Date: Wed, 19 Oct 2016 14:45:01 -0200 Subject: [PATCH] =?UTF-8?q?Refatora=C3=A7=C3=A3o=20dos=20c=C3=B3digos=20pa?= =?UTF-8?q?ra=20padronizar=20com=20a=20rfts=20e=20HOFTS?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- chen.py | 6 +++--- common.py | 4 ++-- fts.py | 2 +- ismailefendi.py | 30 +++++++++++++++++++----------- sadaei.py | 22 +++++++++++----------- yu.py | 10 +++++----- 6 files changed, 41 insertions(+), 33 deletions(-) diff --git a/chen.py b/chen.py index 160a5cb..df3e88e 100644 --- a/chen.py +++ b/chen.py @@ -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 diff --git a/common.py b/common.py index da41ced..6cde86f 100644 --- a/common.py +++ b/common.py @@ -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): diff --git a/fts.py b/fts.py index 6317f03..186676f 100644 --- a/fts.py +++ b/fts.py @@ -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 diff --git a/ismailefendi.py b/ismailefendi.py index dd6b46c..0474a40 100644 --- a/ismailefendi.py +++ b/ismailefendi.py @@ -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 diff --git a/sadaei.py b/sadaei.py index 0f2c69c..b18fc7e 100644 --- a/sadaei.py +++ b/sadaei.py @@ -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 diff --git a/yu.py b/yu.py index 60823f6..74d2d64 100644 --- a/yu.py +++ b/yu.py @@ -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