2016-10-18 21:45:07 +04:00
|
|
|
import numpy as np
|
2016-09-08 01:51:00 +04:00
|
|
|
from pyFTS import *
|
|
|
|
|
|
|
|
class ConventionalFLRG:
|
2016-10-18 15:50:27 +04:00
|
|
|
def __init__(self,LHS):
|
|
|
|
self.LHS = LHS
|
|
|
|
self.RHS = set()
|
2016-09-02 22:55:55 +04:00
|
|
|
|
|
|
|
def append(self,c):
|
2016-10-18 15:50:27 +04:00
|
|
|
self.RHS.add(c)
|
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, key=lambda s: s.name):
|
2016-09-02 22:55:55 +04:00
|
|
|
if len(tmp2) > 0:
|
|
|
|
tmp2 = tmp2 + ","
|
2016-10-19 20:45:01 +04:00
|
|
|
tmp2 = tmp2 + c.name
|
2016-09-02 22:55:55 +04:00
|
|
|
return tmp + tmp2
|
|
|
|
|
|
|
|
|
2016-09-08 01:51:00 +04:00
|
|
|
class ConventionalFTS(fts.FTS):
|
2016-09-02 22:55:55 +04:00
|
|
|
def __init__(self,name):
|
2016-09-08 01:51:00 +04:00
|
|
|
super(ConventionalFTS, self).__init__(1,name)
|
2016-10-18 21:45:07 +04:00
|
|
|
self.flrgs = {}
|
2016-10-18 23:44:03 +04:00
|
|
|
|
2016-10-19 17:27:57 +04:00
|
|
|
def generateFLRG(self, flrs):
|
2016-10-18 23:44:03 +04:00
|
|
|
flrgs = {}
|
|
|
|
for flr in flrs:
|
2016-10-19 17:27:57 +04:00
|
|
|
if flr.LHS.name in flrgs:
|
|
|
|
flrgs[flr.LHS.name].append(flr.RHS)
|
2016-10-18 23:44:03 +04:00
|
|
|
else:
|
2016-10-19 17:27:57 +04:00
|
|
|
flrgs[flr.LHS.name] = ConventionalFLRG(flr.LHS);
|
|
|
|
flrgs[flr.LHS.name].append(flr.RHS)
|
2016-10-18 23:44:03 +04:00
|
|
|
return (flrgs)
|
|
|
|
|
|
|
|
def train(self, data, sets):
|
|
|
|
self.sets = sets
|
|
|
|
tmpdata = common.fuzzySeries(data,sets)
|
|
|
|
flrs = common.generateNonRecurrentFLRs(tmpdata)
|
|
|
|
self.flrgs = self.generateFLRG(flrs)
|
2016-10-19 17:27:57 +04:00
|
|
|
|
2016-10-18 16:09:36 +04:00
|
|
|
def forecast(self,data):
|
2016-10-18 21:45:07 +04:00
|
|
|
|
2016-10-19 17:27:57 +04:00
|
|
|
ndata = np.array(data)
|
|
|
|
|
|
|
|
l = len(ndata)
|
|
|
|
|
|
|
|
ret = []
|
|
|
|
|
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)
|
2016-10-18 21:45:07 +04:00
|
|
|
|
2016-10-19 17:27:57 +04:00
|
|
|
actual = self.sets[ np.argwhere( mv == max(mv) )[0,0] ]
|
2016-09-02 22:55:55 +04:00
|
|
|
|
2016-10-19 17:27:57 +04:00
|
|
|
if actual.name not in self.flrgs:
|
|
|
|
ret.append(actual.centroid)
|
|
|
|
else:
|
|
|
|
flrg = self.flrgs[actual.name]
|
|
|
|
mp = self.getMidpoints(flrg)
|
|
|
|
|
|
|
|
ret.append(sum(mp)/len(mp))
|
|
|
|
|
|
|
|
return ret
|
2016-09-02 22:55:55 +04:00
|
|
|
|
2016-10-18 23:44:03 +04:00
|
|
|
|
2016-10-18 21:45:07 +04:00
|
|
|
|