2016-10-18 15:50:27 +04:00
|
|
|
import numpy as np
|
2016-09-08 16:03:32 +04:00
|
|
|
from pyFTS import *
|
|
|
|
|
2016-09-02 22:55:55 +04:00
|
|
|
class ImprovedWeightedFLRG:
|
2016-10-18 15:50:27 +04:00
|
|
|
def __init__(self,LHS):
|
|
|
|
self.LHS = LHS
|
|
|
|
self.RHS = {}
|
2016-09-02 22:55:55 +04:00
|
|
|
self.count = 0.0
|
|
|
|
|
|
|
|
def append(self,c):
|
2016-10-19 20:45:01 +04:00
|
|
|
if c.name not in self.RHS:
|
|
|
|
self.RHS[c.name] = 1.0
|
2016-09-02 22:55:55 +04:00
|
|
|
else:
|
2016-10-19 20:45:01 +04:00
|
|
|
self.RHS[c.name] = self.RHS[c.name] + 1.0
|
2016-09-02 22:55:55 +04:00
|
|
|
self.count = self.count + 1.0
|
|
|
|
|
|
|
|
def weights(self):
|
2016-10-18 15:50:27 +04:00
|
|
|
return np.array([ self.RHS[c]/self.count for c in self.RHS.keys() ])
|
2016-09-02 22:55:55 +04:00
|
|
|
|
|
|
|
def __str__(self):
|
2016-10-19 20:45:01 +04:00
|
|
|
tmp = self.LHS.name + " -> "
|
2016-09-02 22:55:55 +04:00
|
|
|
tmp2 = ""
|
2016-10-19 20:45:01 +04:00
|
|
|
for c in sorted(self.RHS):
|
2016-09-02 22:55:55 +04:00
|
|
|
if len(tmp2) > 0:
|
|
|
|
tmp2 = tmp2 + ","
|
2016-10-18 15:50:27 +04:00
|
|
|
tmp2 = tmp2 + c + "(" + str(round(self.RHS[c]/self.count,3)) + ")"
|
2016-09-02 22:55:55 +04:00
|
|
|
return tmp + tmp2
|
|
|
|
|
|
|
|
|
2016-09-08 16:03:32 +04:00
|
|
|
class ImprovedWeightedFTS(fts.FTS):
|
2016-09-02 22:55:55 +04:00
|
|
|
def __init__(self,name):
|
|
|
|
super(ImprovedWeightedFTS, self).__init__(1,name)
|
2016-10-19 20:45:01 +04:00
|
|
|
self.setsDict = {}
|
2016-10-18 23:48:02 +04:00
|
|
|
|
|
|
|
def generateFLRG(self, flrs):
|
|
|
|
flrgs = {}
|
|
|
|
for flr in flrs:
|
2016-10-19 20:45:01 +04:00
|
|
|
if flr.LHS.name in flrgs:
|
|
|
|
flrgs[flr.LHS.name].append(flr.RHS)
|
2016-10-18 23:48:02 +04:00
|
|
|
else:
|
2016-10-19 20:45:01 +04:00
|
|
|
flrgs[flr.LHS.name] = ImprovedWeightedFLRG(flr.LHS);
|
|
|
|
flrgs[flr.LHS.name].append(flr.RHS)
|
2016-10-18 23:48:02 +04:00
|
|
|
return (flrgs)
|
|
|
|
|
|
|
|
def train(self, data, sets):
|
|
|
|
self.sets = sets
|
2016-10-19 20:45:01 +04:00
|
|
|
|
|
|
|
for s in self.sets: self.setsDict[s.name] = s
|
|
|
|
|
|
|
|
tmpdata = common.fuzzySeries(data,self.sets)
|
2016-10-18 23:48:02 +04:00
|
|
|
flrs = common.generateRecurrentFLRs(tmpdata)
|
|
|
|
self.flrgs = self.generateFLRG(flrs)
|
2016-10-19 20:45:01 +04:00
|
|
|
|
|
|
|
def getMidpoints(self,flrg):
|
|
|
|
ret = np.array([self.setsDict[s].centroid for s in flrg.RHS])
|
|
|
|
return ret
|
2016-09-02 22:55:55 +04:00
|
|
|
|
2016-10-18 16:09:36 +04:00
|
|
|
def forecast(self,data):
|
2016-10-19 17:27:57 +04:00
|
|
|
l = 1
|
2016-09-02 22:55:55 +04:00
|
|
|
|
2016-10-19 17:27:57 +04:00
|
|
|
ndata = np.array(data)
|
|
|
|
|
|
|
|
l = len(ndata)
|
|
|
|
|
|
|
|
ret = []
|
2016-09-02 22:55:55 +04:00
|
|
|
|
2016-10-25 19:53:00 +04:00
|
|
|
for k in np.arange(0,l):
|
2016-10-19 17:27:57 +04:00
|
|
|
|
|
|
|
mv = common.fuzzyInstance(ndata[k], self.sets)
|
|
|
|
|
|
|
|
actual = self.sets[ np.argwhere( mv == max(mv) )[0,0] ]
|
|
|
|
|
|
|
|
if actual.name not in self.flrgs:
|
|
|
|
ret.append(actual.centroid)
|
|
|
|
else:
|
|
|
|
flrg = self.flrgs[actual.name]
|
|
|
|
mp = self.getMidpoints(flrg)
|
|
|
|
|
2016-10-19 20:45:01 +04:00
|
|
|
ret.append( mp.dot( flrg.weights() ))
|
2016-10-19 17:27:57 +04:00
|
|
|
|
|
|
|
return ret
|