2017-05-07 11:41:31 -03:00
|
|
|
|
"""
|
|
|
|
|
First Order Exponentialy Weighted Fuzzy Time Series by Sadaei et al. (2013)
|
|
|
|
|
|
|
|
|
|
H. J. Sadaei, R. Enayatifar, A. H. Abdullah, and A. Gani, “Short-term load forecasting using a hybrid model with a
|
|
|
|
|
refined exponentially weighted fuzzy time series and an improved harmony search,” Int. J. Electr. Power Energy Syst., vol. 62, no. from 2005, pp. 118–129, 2014.
|
|
|
|
|
"""
|
|
|
|
|
|
2016-10-18 09:50:27 -02:00
|
|
|
|
import numpy as np
|
2018-02-26 13:29:11 -03:00
|
|
|
|
from pyFTS.common import FuzzySet,FLR,fts, flrg
|
2016-09-08 09:03:32 -03:00
|
|
|
|
|
2018-02-27 18:30:20 -03:00
|
|
|
|
default_c = 1.1
|
|
|
|
|
|
2017-05-02 11:32:03 -03:00
|
|
|
|
|
2017-10-06 14:08:46 -03:00
|
|
|
|
class ExponentialyWeightedFLRG(flrg.FLRG):
|
2017-05-05 15:33:27 -03:00
|
|
|
|
"""First Order Exponentialy Weighted Fuzzy Logical Relationship Group"""
|
2017-10-06 14:08:46 -03:00
|
|
|
|
def __init__(self, LHS, **kwargs):
|
|
|
|
|
super(ExponentialyWeightedFLRG, self).__init__(1, **kwargs)
|
2016-12-22 14:36:50 -02:00
|
|
|
|
self.LHS = LHS
|
|
|
|
|
self.RHS = []
|
|
|
|
|
self.count = 0.0
|
2018-02-27 18:30:20 -03:00
|
|
|
|
self.c = kwargs.get("c",default_c)
|
|
|
|
|
self.w = None
|
2016-12-22 14:36:50 -02:00
|
|
|
|
|
2018-03-03 20:07:50 -03:00
|
|
|
|
def append_rhs(self, c, **kwargs):
|
2018-12-17 21:49:48 -02:00
|
|
|
|
count = kwargs.get('count', 1.0)
|
2016-12-22 14:36:50 -02:00
|
|
|
|
self.RHS.append(c)
|
2018-12-17 21:49:48 -02:00
|
|
|
|
self.count += count
|
2016-12-22 14:36:50 -02:00
|
|
|
|
|
|
|
|
|
def weights(self):
|
2018-02-27 18:30:20 -03:00
|
|
|
|
if self.w is None:
|
|
|
|
|
wei = [self.c ** k for k in np.arange(0.0, self.count, 1.0)]
|
|
|
|
|
tot = sum(wei)
|
|
|
|
|
self.w = np.array([k / tot for k in wei])
|
|
|
|
|
return self.w
|
2016-12-22 14:36:50 -02:00
|
|
|
|
|
|
|
|
|
def __str__(self):
|
2018-03-03 20:07:50 -03:00
|
|
|
|
tmp = self.LHS + " -> "
|
2016-12-22 14:36:50 -02:00
|
|
|
|
tmp2 = ""
|
|
|
|
|
cc = 0
|
|
|
|
|
wei = [self.c ** k for k in np.arange(0.0, self.count, 1.0)]
|
|
|
|
|
tot = sum(wei)
|
2018-03-03 20:07:50 -03:00
|
|
|
|
for c in sorted(self.RHS):
|
2016-12-22 14:36:50 -02:00
|
|
|
|
if len(tmp2) > 0:
|
|
|
|
|
tmp2 = tmp2 + ","
|
2018-03-03 20:07:50 -03:00
|
|
|
|
tmp2 = tmp2 + c + "(" + str(wei[cc] / tot) + ")"
|
2016-12-22 14:36:50 -02:00
|
|
|
|
cc = cc + 1
|
|
|
|
|
return tmp + tmp2
|
|
|
|
|
|
2017-02-24 13:29:55 -03:00
|
|
|
|
def __len__(self):
|
|
|
|
|
return len(self.RHS)
|
|
|
|
|
|
2016-12-22 14:36:50 -02:00
|
|
|
|
|
2016-09-08 09:03:32 -03:00
|
|
|
|
class ExponentialyWeightedFTS(fts.FTS):
|
2017-05-05 15:33:27 -03:00
|
|
|
|
"""First Order Exponentialy Weighted Fuzzy Time Series"""
|
2018-05-08 17:59:53 -03:00
|
|
|
|
def __init__(self, **kwargs):
|
|
|
|
|
super(ExponentialyWeightedFTS, self).__init__(order=1, name="EWFTS", **kwargs)
|
2016-12-22 14:36:50 -02:00
|
|
|
|
self.name = "Exponentialy Weighted FTS"
|
|
|
|
|
self.detail = "Sadaei"
|
2018-02-27 18:30:20 -03:00
|
|
|
|
self.c = kwargs.get('c', default_c)
|
2016-12-22 14:36:50 -02:00
|
|
|
|
|
2018-02-27 18:30:20 -03:00
|
|
|
|
def generate_flrg(self, flrs, c):
|
2016-12-22 14:36:50 -02:00
|
|
|
|
for flr in flrs:
|
2018-03-03 20:07:50 -03:00
|
|
|
|
if flr.LHS in self.flrgs:
|
|
|
|
|
self.flrgs[flr.LHS].append_rhs(flr.RHS)
|
2016-12-22 14:36:50 -02:00
|
|
|
|
else:
|
2018-03-03 20:07:50 -03:00
|
|
|
|
self.flrgs[flr.LHS] = ExponentialyWeightedFLRG(flr.LHS, c=c);
|
|
|
|
|
self.flrgs[flr.LHS].append_rhs(flr.RHS)
|
2016-12-22 14:36:50 -02:00
|
|
|
|
|
2018-02-27 18:30:20 -03:00
|
|
|
|
def train(self, data, **kwargs):
|
2019-04-09 16:24:04 -03:00
|
|
|
|
tmpdata = self.partitioner.fuzzyfy(data, method='maximum', mode='sets')
|
2018-02-27 12:56:05 -03:00
|
|
|
|
flrs = FLR.generate_recurrent_flrs(tmpdata)
|
2018-03-02 19:20:21 -03:00
|
|
|
|
self.generate_flrg(flrs, self.c)
|
2016-12-22 14:36:50 -02:00
|
|
|
|
|
2018-04-11 01:12:55 -03:00
|
|
|
|
def forecast(self, ndata, **kwargs):
|
2018-10-29 16:48:05 -03:00
|
|
|
|
|
|
|
|
|
explain = kwargs.get('explain', False)
|
2016-12-22 14:36:50 -02:00
|
|
|
|
|
2018-05-08 17:59:53 -03:00
|
|
|
|
if self.partitioner is not None:
|
|
|
|
|
ordered_sets = self.partitioner.ordered_sets
|
|
|
|
|
else:
|
2019-06-20 17:38:36 -03:00
|
|
|
|
ordered_sets = FuzzySet.set_ordered(self.partitioner.sets)
|
2018-03-03 20:07:50 -03:00
|
|
|
|
|
2018-04-24 12:57:40 -03:00
|
|
|
|
data = np.array(ndata)
|
2017-01-27 08:26:47 -02:00
|
|
|
|
|
2016-12-22 14:36:50 -02:00
|
|
|
|
l = len(ndata)
|
|
|
|
|
|
|
|
|
|
ret = []
|
|
|
|
|
|
|
|
|
|
for k in np.arange(0, l):
|
|
|
|
|
|
2019-06-20 17:38:36 -03:00
|
|
|
|
actual = FuzzySet.get_maximum_membership_fuzzyset(ndata[k], self.partitioner.sets, ordered_sets)
|
2016-12-22 14:36:50 -02:00
|
|
|
|
|
2018-10-29 16:48:05 -03:00
|
|
|
|
if explain:
|
|
|
|
|
print("Fuzzyfication:\n\n {} -> {} \n".format(ndata[k], actual.name))
|
|
|
|
|
|
2016-12-22 14:36:50 -02:00
|
|
|
|
if actual.name not in self.flrgs:
|
|
|
|
|
ret.append(actual.centroid)
|
2018-10-29 16:48:05 -03:00
|
|
|
|
|
|
|
|
|
if explain:
|
|
|
|
|
print("Rules:\n\n {} -> {} (Naïve)\t Midpoint: {} \n\n".format(actual.name, actual.name,actual.centroid))
|
|
|
|
|
|
2016-12-22 14:36:50 -02:00
|
|
|
|
else:
|
|
|
|
|
flrg = self.flrgs[actual.name]
|
2019-06-20 17:38:36 -03:00
|
|
|
|
mp = flrg.get_midpoints(self.partitioner.sets)
|
2016-12-22 14:36:50 -02:00
|
|
|
|
|
2018-10-29 16:48:05 -03:00
|
|
|
|
final = mp.dot(flrg.weights())
|
|
|
|
|
|
|
|
|
|
ret.append(final)
|
|
|
|
|
|
|
|
|
|
if explain:
|
|
|
|
|
print("Rules:\n\n {} \n\n ".format(str(flrg)))
|
|
|
|
|
print("Midpoints: \n\n {}\n\n".format(mp))
|
|
|
|
|
|
|
|
|
|
print("Deffuzyfied value: {} \n".format(final))
|
2016-12-22 14:36:50 -02:00
|
|
|
|
|
|
|
|
|
return ret
|