def __init__(self): # INPUTY self.buildingType = ctrl.Antecedent(np.arange(0, 400, 1), "Technologia budynku") self.wearWater = ctrl.Antecedent(np.arange(0, 500, 0.1), "Zuzycie wody na osobe") self.zone = ctrl.Antecedent(np.arange(15, 25, 0.01), "Strefa klimatyczna") self.isolation = ctrl.Antecedent(np.arange(1, 100, 1), "Procentowa termoizolacja budynku") self.surface = ctrl.Antecedent(np.arange(20, 150, 0.01), "Powierzchnia grzewcza") # OUTPUTY self.typeFurnance = ctrl.Consequent(np.arange(1, 100, 1), "Rodzaj pieca") # MEMBERSHIPS FUN self.buildingType['starebudownictwo'] = fuzz.trapmf(self.buildingType.universe, [170, 350, 400, 400]) self.buildingType['70-85'] = fuzz.gaussmf(self.buildingType.universe, 260, 25) self.buildingType['86-92'] = fuzz.gaussmf(self.buildingType.universe, 200, 20) self.buildingType['93-97'] = fuzz.gaussmf(self.buildingType.universe, 160, 15) self.buildingType['98-07'] = fuzz.gaussmf(self.buildingType.universe, 120, 15) self.buildingType['energooszczedny'] = fuzz.gaussmf(self.buildingType.universe, 80, 15) self.buildingType['niskoenegetyczny'] = fuzz.gaussmf(self.buildingType.universe, 45, 10) self.buildingType['pasywny'] = fuzz.trapmf(self.buildingType.universe, [0, 0, 15, 100]) # ---------------------- self.wearWater['1 osoba'] = fuzz.zmf(self.wearWater.universe, 20, 80) self.wearWater['2 osoba'] = fuzz.gaussmf(self.wearWater.universe, 100, 30) self.wearWater['3 osoba'] = fuzz.gaussmf(self.wearWater.universe, 150, 30) self.wearWater['4 osoba'] = fuzz.gaussmf(self.wearWater.universe, 200, 40) self.wearWater['5 osoba'] = fuzz.gaussmf(self.wearWater.universe, 250, 50) self.wearWater['6 osoba'] = fuzz.gaussmf(self.wearWater.universe, 300, 60) self.wearWater['7 osoba'] = fuzz.gaussmf(self.wearWater.universe, 350, 60) self.wearWater['8 osoba'] = fuzz.smf(self.wearWater.universe, 310, 470) # ---------------------- self.zone['Strefa 1'] = fuzz.gaussmf(self.zone.universe, 16, 0.5) self.zone['Strefa 2'] = fuzz.gaussmf(self.zone.universe, 18, 0.9) self.zone['Strefa 3'] = fuzz.gaussmf(self.zone.universe, 20, 1.5) self.zone['Strefa 4'] = fuzz.gaussmf(self.zone.universe, 22, 1.2) self.zone['Strefa 5'] = fuzz.gaussmf(self.zone.universe, 24, 0.45) # --------------------- self.surface['kawalerka'] = fuzz.zmf(self.surface.universe, 25, 65) self.surface['mieszkanie'] = fuzz.gaussmf(self.surface.universe, 75, 30) self.surface['apartament'] = fuzz.gaussmf(self.surface.universe, 111, 8) self.surface['dom_jednorodzinny'] = fuzz.gaussmf(self.surface.universe, 130, 3.5) self.surface['willa'] = fuzz.smf(self.surface.universe, 130, 150) # -------------------- self.isolation['brak'] = fuzz.zmf(self.isolation.universe, 5, 15) self.isolation['slabo'] = fuzz.gaussmf(self.isolation.universe, 20, 7) self.isolation['srednio'] = fuzz.gaussmf(self.isolation.universe, 50, 20) self.isolation['dobrze'] = fuzz.gaussmf(self.isolation.universe, 70, 10) self.isolation['doskonale'] = fuzz.smf(self.isolation.universe, 75, 95) self.typeFurnance['weglowy'] = fuzz.zmf(self.typeFurnance.universe, 4, 40) self.typeFurnance['gazowy'] = fuzz.gbellmf(self.typeFurnance.universe, 23, 2.5, 50) self.typeFurnance['pompa ciepla'] = fuzz.smf(self.typeFurnance.universe, 70, 100) # ----------------------- # RULES rules = [ ctrl.Rule(self.buildingType['pasywny'] & self.wearWater['4 osoba'] & self.surface['dom_jednorodzinny'] & self.isolation['doskonale'], self.typeFurnance['pompa ciepla']), ctrl.Rule(self.buildingType['starebudownictwo'] & self.wearWater['8 osoba'] & self.isolation['brak'] & self.surface['dom_jednorodzinny'], self.typeFurnance['weglowy']), ctrl.Rule(self.buildingType['starebudownictwo'] & self.wearWater['7 osoba'] & self.isolation['slabo'] & self.surface['dom_jednorodzinny'], self.typeFurnance['weglowy']), ctrl.Rule(self.buildingType['starebudownictwo'] & self.wearWater['6 osoba'] & self.isolation['slabo'] & self.surface['dom_jednorodzinny'], self.typeFurnance['weglowy']), ctrl.Rule(self.buildingType['70-85'] & self.wearWater['6 osoba'] & self.isolation['slabo'] & self.surface[ 'dom_jednorodzinny'], self.typeFurnance['weglowy']), ctrl.Rule(self.buildingType['niskoenegetyczny'] & self.wearWater['5 osoba'] & self.isolation['dobrze'] & self.surface['dom_jednorodzinny'] | self.surface['willa'], self.typeFurnance['pompa ciepla']), ctrl.Rule(self.buildingType['energooszczedny'] & self.wearWater['5 osoba'] & self.isolation['dobrze'] & self.surface['dom_jednorodzinny'] | self.surface['willa'], self.typeFurnance['pompa ciepla']), ctrl.Rule(self.buildingType['energooszczedny'] & self.wearWater['4 osoba'] & self.isolation['dobrze'] & self.surface['dom_jednorodzinny'] | self.surface['willa'], self.typeFurnance['pompa ciepla']), ctrl.Rule(self.buildingType['niskoenegetyczny'] & self.wearWater['5 osoba'] & self.isolation['dobrze'] & self.surface['dom_jednorodzinny'] | self.surface['willa'], self.typeFurnance['pompa ciepla']), ctrl.Rule(self.buildingType['93-97'] & self.wearWater['2 osoba'] & self.isolation['srednio'] & self.surface[ 'kawalerka'], self.typeFurnance['gazowy']), ctrl.Rule(self.buildingType['93-97'] & self.wearWater['3 osoba'] & self.isolation['srednio'] & self.surface[ 'mieszkanie'], self.typeFurnance['gazowy']), ctrl.Rule(self.buildingType['93-97'] & self.wearWater['4 osoba'] & self.isolation['srednio'] & self.surface[ 'mieszkanie'], self.typeFurnance['gazowy']), ctrl.Rule(self.buildingType['98-07'] & self.wearWater['2 osoba'] & self.isolation['srednio'] & self.surface[ 'kawalerka'], self.typeFurnance['gazowy']), ctrl.Rule(self.buildingType['98-07'] & self.wearWater['3 osoba'] & self.isolation['srednio'] & self.surface[ 'mieszkanie'], self.typeFurnance['gazowy']), ctrl.Rule(self.buildingType['98-07'] & self.wearWater['4 osoba'] & self.isolation['srednio'] & self.surface[ 'mieszkanie'], self.typeFurnance['gazowy']), ctrl.Rule(self.buildingType['98-07'] & self.wearWater['5 osoba'] & self.isolation['srednio'] & self.surface[ 'apartament'], self.typeFurnance['gazowy']), ] # Controll System self.typeControl = ctrl.ControlSystem(rules) self.type = ctrl.ControlSystemSimulation(self.typeControl)
def membership_f(mf, x, abcd): """ Returns y values corresponding to type of type of Membership fn. arguments: mf - string containing type of Membership function x - x axis values """ if mf == "zmf": return fuzz.zmf(x, abcd[0], abcd[1]) # zmf(x, a, b) elif mf == "trimf": return fuzz.trimf(x, abcd[0:3]) # trimf(x, abc) elif mf == "dsigmf": return fuzz.dsigmf(x, abcd[0], abcd[1], abcd[2], abcd[3]) # dsigmf(x, b1, c1, b2, c2) elif mf == "gauss2mf": return fuzz.gauss2mf(x, abcd[0], abcd[1], abcd[2], abcd[3]) # gauss2mf(x, mean1, sigma1, mean2, sigma2) elif mf == "gaussmf": return fuzz.gaussmf(x, abcd[0], abcd[1]) # gaussmf(x, mean, sigma) elif mf == "gbellmf": return fuzz.gbellmf(x, abcd[0], abcd[1], abcd[2]) # gbellmf(x, a, b, c) elif mf == "piecemf": return fuzz.piecemf(x, abcd[0:3]) # piecemf(x, abc) elif mf == "pimf": return fuzz.pimf(x, abcd[0], abcd[1], abcd[2], abcd[3]) # pimf(x, a, b, c, d) elif mf == "psigmf": return fuzz.psigmf(x, abcd[0], abcd[1], abcd[2], abcd[3]) # psigmf(x, b1, c1, b2, c2) elif mf == "sigmf": return fuzz.sigmf(x, abcd[0], abcd[1]) # sigmf(x, b, c) elif mf == "smf": return fuzz.smf(x, abcd[0], abcd[1]) # smf(x, a, b) elif mf == "trapmf": return fuzz.trapmf(x, abcd) # trapmf(x, abcd)
def membership_f(mf, x, abcd): """ Returns y values corresponding to type of type of Membership fn. arguments: mf - string containing type of Membership function x - x axis values """ if mf == 'zmf': return fuzz.zmf(x, abcd[0], abcd[1]) # zmf(x, a, b) elif mf == 'trimf': return fuzz.trimf(x, abcd[0:3]) # trimf(x, abc) elif mf == 'dsigmf': return fuzz.dsigmf(x, abcd[0], abcd[1], abcd[2], abcd[3]) # dsigmf(x, b1, c1, b2, c2) elif mf == 'gauss2mf': return fuzz.gauss2mf( x, abcd[0], abcd[1], abcd[2], abcd[3]) # gauss2mf(x, mean1, sigma1, mean2, sigma2) elif mf == 'gaussmf': return fuzz.gaussmf(x, abcd[0], abcd[1]) # gaussmf(x, mean, sigma) elif mf == 'gbellmf': return fuzz.gbellmf(x, abcd[0], abcd[1], abcd[2]) # gbellmf(x, a, b, c) elif mf == 'piecemf': return fuzz.piecemf(x, abcd[0:3]) # piecemf(x, abc) elif mf == 'pimf': return fuzz.pimf(x, abcd[0], abcd[1], abcd[2], abcd[3]) # pimf(x, a, b, c, d) elif mf == 'psigmf': return fuzz.psigmf(x, abcd[0], abcd[1], abcd[2], abcd[3]) # psigmf(x, b1, c1, b2, c2) elif mf == 'sigmf': return fuzz.sigmf(x, abcd[0], abcd[1]) # sigmf(x, b, c) elif mf == 'smf': return fuzz.smf(x, abcd[0], abcd[1]) # smf(x, a, b) elif mf == 'trapmf': return fuzz.trapmf(x, abcd) # trapmf(x, abcd)
def plotMF(self, x, inputVar): from skfuzzy import gaussmf, gbellmf, sigmf for mf in range(len(self.memFuncs[inputVar])): if self.memFuncs[inputVar][mf][0] == 'gaussmf': y = gaussmf(x, **self.memClass.MFList[inputVar][mf][1]) elif self.memFuncs[inputVar][mf][0] == 'gbellmf': y = gbellmf(x, **self.memClass.MFList[inputVar][mf][1]) elif self.memFuncs[inputVar][mf][0] == 'sigmf': y = sigmf(x, **self.memClass.MFList[inputVar][mf][1]) plt.plot(x, y, 'r') plt.show()
def plotMF(self, x, inputVar): import matplotlib.pyplot as plt from skfuzzy import gaussmf, gbellmf, sigmf for mf in range(len(self.memFuncs[inputVar])): if self.memFuncs[inputVar][mf][0] == 'gaussmf': y = gaussmf(x,**self.memClass.MFList[inputVar][mf][1]) elif self.memFuncs[inputVar][mf][0] == 'gbellmf': y = gbellmf(x,**self.memClass.MFList[inputVar][mf][1]) elif self.memFuncs[inputVar][mf][0] == 'sigmf': y = sigmf(x,**self.memClass.MFList[inputVar][mf][1]) plt.plot(x,y,'r') plt.show()
def membership_f(mf, x, abc = [0,0,0], a = 1, b = 2, c = 3, d = 4, abcd = [0,0,0,0]): return { 'trimf' : fuzz.trimf(x, abc), # trimf(x, abc) 'dsigmf' : fuzz.dsigmf(x, a, b, c, d), # dsigmf(x, b1, c1, b2, c2) 'gauss2mf': fuzz.gauss2mf(x, a, b, c, d), # gauss2mf(x, mean1, sigma1, mean2, sigma2) 'gaussmf' : fuzz.gaussmf(x, a, b), # gaussmf(x, mean, sigma) 'gbellmf' : fuzz.gbellmf(x, a, b, c), # gbellmf(x, a, b, c) 'piecemf' : fuzz.piecemf(x, abc), # piecemf(x, abc) 'pimf' : fuzz.pimf(x, a, b, c, d), # pimf(x, a, b, c, d) 'psigmf' : fuzz.psigmf(x, a, b, c, d), # psigmf(x, b1, c1, b2, c2) 'sigmf' : fuzz.sigmf(x, a, b), # sigmf(x, b, c) 'smf' : fuzz.smf(x, a, b), # smf(x, a, b) 'trapmf' : fuzz.trapmf(x, abcd), # trapmf(x, abcd) 'zmf' : fuzz.zmf(x, a, b), # zmf(x, a, b) }[mf]
def plotMF(self, x, inputNumber): """plotMF(self, x, inputNumber) Parameters ---------- x : 1d array or iterable 表示したいmfの横軸の値 inputNumber : int 入力するデータの種類を配列の添字で示したもの """ import matplotlib.pyplot as plt from skfuzzy import gaussmf, gbellmf, sigmf """gaussmf() Parameters ---------- x : 1d array or iterable 横軸の値 mean : float 中央値 sigma : float 標準偏差 Returns ------- y : 1d array 関数の出力 """ for mf in range(len(self.memFuncs[inputNumber])): if self.memFuncs[inputNumber][mf][0] == 'gaussmf': y = gaussmf(x, **self.memClass.MFList[inputNumber][mf][1]) elif self.memFuncs[inputNumber][mf][0] == 'gbellmf': y = gbellmf(x, **self.memClass.MFList[inputNumber][mf][1]) elif self.memFuncs[inputNumber][mf][0] == 'sigmf': y = sigmf(x, **self.memClass.MFList[inputNumber][mf][1]) plt.plot(x, y, 'r') plt.show()
def load_rules(self, filename): with open(filename, 'r') as jsonfile: rules = json.loads(jsonfile.read()) self.actions = rules['actions'] self.state = rules['state'] self.rules = rules['results'] self.x = np.arange(self.state['min'], self.state['max']) for subset, parts in self.rules.items(): aggregated = [] points = [] for n, v in parts.items(): print n if v['mf'] == 'tri': mf = fuzz.trimf(self.x, v['shp']) if v['mf'] == 'trap': mf = fuzz.trapmf(self.x, v['shp']) elif v['mf'] == 'gbell': mf = fuzz.gbellmf(self.x, v['shp']) aggregated.append(mf) points.append(v['pts']) self.rules[subset] = (aggregated, points)
def membership_f(mf, x, abc=[0, 0, 0], a=1, b=2, c=3, d=4, abcd=[0, 0, 0, 0]): """ Returns y values corresponding to type of type of Membership fn. arguments: mf - string containing type of Membership function x - x axis values abc - list containing triangular edge point x-values """ return { "trimf": fuzz.trimf(x, abc), # trimf(x, abc) "dsigmf": fuzz.dsigmf(x, a, b, c, d), # dsigmf(x, b1, c1, b2, c2) "gauss2mf": fuzz.gauss2mf(x, a, b, c, d), # gauss2mf(x, mean1, sigma1, mean2, sigma2) "gaussmf": fuzz.gaussmf(x, a, b), # gaussmf(x, mean, sigma) "gbellmf": fuzz.gbellmf(x, a, b, c), # gbellmf(x, a, b, c) "piecemf": fuzz.piecemf(x, abc), # piecemf(x, abc) "pimf": fuzz.pimf(x, a, b, c, d), # pimf(x, a, b, c, d) "psigmf": fuzz.psigmf(x, a, b, c, d), # psigmf(x, b1, c1, b2, c2) "sigmf": fuzz.sigmf(x, a, b), # sigmf(x, b, c) "smf": fuzz.smf(x, a, b), # smf(x, a, b) "trapmf": fuzz.trapmf(x, abcd), # trapmf(x, abcd) "zmf": fuzz.zmf(x, a, b), # zmf(x, a, b) }[mf]
def membership_f(mf, x, abc = [0,0,0], a = 1, b = 2, c = 3, d = 4, abcd = [0,0,0,0]): """ Returns y values corresponding to type of type of Membership fn. arguments: mf - string containing type of Membership function x - x axis values abc - list containing triangular edge point x-values """ return { 'trimf' : fuzz.trimf(x, abc), # trimf(x, abc) 'dsigmf' : fuzz.dsigmf(x, a, b, c, d), # dsigmf(x, b1, c1, b2, c2) 'gauss2mf': fuzz.gauss2mf(x, a, b, c, d), # gauss2mf(x, mean1, sigma1, mean2, sigma2) 'gaussmf' : fuzz.gaussmf(x, a, b), # gaussmf(x, mean, sigma) 'gbellmf' : fuzz.gbellmf(x, a, b, c), # gbellmf(x, a, b, c) 'piecemf' : fuzz.piecemf(x, abc), # piecemf(x, abc) 'pimf' : fuzz.pimf(x, a, b, c, d), # pimf(x, a, b, c, d) 'psigmf' : fuzz.psigmf(x, a, b, c, d), # psigmf(x, b1, c1, b2, c2) 'sigmf' : fuzz.sigmf(x, a, b), # sigmf(x, b, c) 'smf' : fuzz.smf(x, a, b), # smf(x, a, b) 'trapmf' : fuzz.trapmf(x, abcd), # trapmf(x, abcd) 'zmf' : fuzz.zmf(x, a, b), # zmf(x, a, b) }[mf]
def move_fuzzy(distance_in): """ A simple fuzzy movement algorithm. Hit the ball with the center of the paddle. """ # Convert the distance from this game's 550 unit tall area to # the 200 unit tall are this fuzzy system was made for in MATLAB distance_in = distance_in/2.75 ### Inputs ### # Input variable domain dist = np.arange(-100, 100, 1) # Input Membership Functions far_below = fuzz.gbellmf(dist, 79.68, 24.6, -90) close_below = fuzz.gaussmf(dist, -6.57989, 3.17) on_target = fuzz.gaussmf(dist, 0, 2) close_above = fuzz.gaussmf(dist, 6.58, 3.17) far_above = fuzz.gbellmf(dist, 79.68, 24.6, 90) # Fuzzifying the inputs def dist_category(dist_in): dist_cat_far_below = fuzz.interp_membership(dist, far_below, dist_in) dist_cat_close_below = fuzz.interp_membership(dist, close_below, dist_in) dist_cat_on_target = fuzz.interp_membership(dist, on_target, dist_in) dist_cat_close_above = fuzz.interp_membership(dist, close_above, dist_in) dist_cat_far_above = fuzz.interp_membership(dist, far_above, dist_in) return dict(fbelow = dist_cat_far_below, cbelow = dist_cat_close_below, target = dist_cat_on_target, cabove = dist_cat_close_above, fabove = dist_cat_far_above) ### Outputs ### # Output Variable Domain move = np.arange(-1, 1, 1) # Output Membership Functions down_fast = fuzz.trapmf(move, [-1.45, -1.05, -0.2, -0.1]) down_slow = fuzz.trimf(move, [-0.2, -0.1, 0]) none = fuzz.trimf(move, [-0.1, 0, 0.1]) up_slow = fuzz.trimf(move, [0, 0.1, 0.2]) up_fast = fuzz.trapmf(move, [0.1, 0.2, 1.14, 1.38]) ### Rules ### this_move = dist_category(float(distance_in)) rule1 = this_move['fbelow'] rule2 = this_move['cbelow'] rule3 = this_move['target'] rule4 = this_move['cabove'] rule5 = this_move['fabove'] # Apply implication operator (Mandami) connecting rules to # output mfs imp1 = np.fmin(rule1, down_fast) imp2 = np.fmin(rule2, down_slow) imp3 = np.fmin(rule3, none) imp4 = np.fmin(rule4, up_slow) imp5 = np.fmin(rule5, up_fast) # Aggregate outputs using max agg_memb = np.fmax(imp1, np.fmax(imp2, np.fmax(imp3, np.fmax(imp4, imp5)))) # Defuzzify using centroid and return the result move_result = fuzz.defuzz(move, agg_memb, 'centroid') return move_result
In = ctrl.Consequent(influenza, 'In') # linguistic values diagno = ['PP', 'Po', 'X', 'Pr' 'MP'] Co.automf(5, names=diagno) Re.automf(5, names=diagno) Al.automf(5, names=diagno) In.automf(5, names=diagno) # ------------------------------------- # Generate fuzzy membership functions # ------------------------------------- # Covid-19 Co['PP'] = fuzz.sigmf(Co.universe, -50, 0.2) Co['Po'] = fuzz.gbellmf(Co.universe, 0.14, 2.5, 0.33) Co['Pr'] = fuzz.gbellmf(Co.universe, 0.14, 2.5, 0.66) Co['MP'] = fuzz.sigmf(Co.universe, 50, 0.8) # Resfriado Re['PP'] = fuzz.sigmf(Re.universe, -50, 0.2) Re['Po'] = fuzz.gbellmf(Re.universe, 0.14, 2.5, 0.33) Re['Pr'] = fuzz.gbellmf(Re.universe, 0.14, 2.5, 0.66) Re['MP'] = fuzz.sigmf(Re.universe, 50, 0.8) # Alergia Al['PP'] = fuzz.sigmf(Al.universe, -50, 0.2) Al['Po'] = fuzz.gbellmf(Al.universe, 0.14, 2.5, 0.33) Al['Pr'] = fuzz.gbellmf(Al.universe, 0.14, 2.5, 0.66) Al['MP'] = fuzz.sigmf(Al.universe, 50, 0.8)
############################################ #### Scaling champions stats to 0.0-1.0 ############################################ for i, maxstat in enumerate(maxStatsList): df[df.columns[i + 1]] = df[df.columns[i + 1]] / maxstat print(df[df.columns[i + 1]]) print("The end of this stat") fHP = ctrl.Antecedent(np.arange(0, 1.01, 0.01), 'fHP') fMEANHP = ctrl.Antecedent(np.arange(0, 1.01, 0.01), 'fMEANHP') fPoints = ctrl.Consequent(np.arange(0, 1.01, 0.01), 'fPoints') fHP['poor'] = fuzz.gbellmf(fHP.universe, 0.025, 0.95, 0.2) fHP['mediocre'] = fuzz.gbellmf(fHP.universe, 0.025, 0.95, 0.4) fHP['average'] = fuzz.gbellmf(fHP.universe, 0.025, 0.95, 0.6) fHP['decent'] = fuzz.gbellmf(fHP.universe, 0.025, 0.95, 0.8) fHP['good'] = fuzz.gbellmf(fHP.universe, 0.025, 0.95, 1.0) fMEANHP['poor'] = fuzz.gbellmf(fMEANHP.universe, 0.025, 0.95, 0.2) fMEANHP['mediocre'] = fuzz.gbellmf(fMEANHP.universe, 0.025, 0.95, 0.4) fMEANHP['average'] = fuzz.gbellmf(fMEANHP.universe, 0.025, 0.95, 0.6) fMEANHP['decent'] = fuzz.gbellmf(fMEANHP.universe, 0.025, 0.95, 0.8) fMEANHP['good'] = fuzz.gbellmf(fMEANHP.universe, 0.025, 0.95, 1.0) # fSuits['hearts'] = fuzz.gbellmf(fSuits.universe, 0.025, 0.95, float(counterPreferencesHearts.get())) # fSuits['tiles'] = fuzz.gbellmf(fSuits.universe, 0.025, 0.95, float(counterPreferencesTiles.get())) # fSuits['clovers'] = fuzz.gbellmf(fSuits.universe, 0.025, 0.95, float(counterPreferencesClovers.get())) # fSuits['pikes'] = fuzz.gbellmf(fSuits.universe, 0.025, 0.95, float(counterPreferencesPikes.get()))