Skip to content

Instantly share code, notes, and snippets.

@ibaaj ibaaj/fan.py
Created Nov 8, 2018

Embed
What would you like to do?
fan.py
import numpy as np
import skfuzzy as fuzz
import matplotlib.pyplot as plt
import sys
import matplotlib
matplotlib.rc('font', **{'sans-serif' : 'Arial',
'family' : 'sans-serif'})
x_temperature = np.arange(-15, 45, 0.5)
temperature_glaciale = fuzz.trapmf(x_temperature, [-15,-15,-5,0])
temperature_froide = fuzz.trimf(x_temperature, [-5, 10, 15])
temperature_moyenne = fuzz.trapmf(x_temperature, [10, 17, 20, 25])
temperature_chaude = fuzz.trapmf(x_temperature, [20,25, 30, 35])
temperature_caniculaire = fuzz.trapmf(x_temperature, [25, 40, 45, 45])
x_qualite_air = np.arange(0,10,0.5)
qualite_air_bonne = fuzz.trapmf(x_qualite_air, [0,0,2,5])
qualite_air_mediocre = fuzz.trimf(x_qualite_air, [4,6,8])
qualite_air_mauvaise = fuzz.trimf(x_qualite_air, [5,10,10])
x_humidite = np.arange(0,100,0.5)
humidite_basse = fuzz.trapmf(x_humidite, [0,0,30,60])
humidite_haute = fuzz.trapmf(x_humidite, [40,70,100,100])
x_surtension = np.arange(200,300,0.5)
surtension_presente = fuzz.trapmf(x_surtension, [220,270,300,300])
x_vitesse = np.arange(20,250,0.5)
vitesse_lente = fuzz.trapmf(x_vitesse, [20,20,75,120])
vitesse_moyenne = fuzz.trapmf(x_vitesse, [50,90,130,170])
vitesse_rapide = fuzz.trapmf(x_vitesse, [120,170,250,250])
# Visualize these universes and membership functions
fig, (ax0,ax1,ax2,ax3) = plt.subplots(nrows=4, figsize=(8, 9))
ax0.plot(x_temperature, temperature_glaciale, 'b', linewidth=1.5, label='Glaciale')
ax0.plot(x_temperature, temperature_froide, 'magenta', linewidth=1.5, label='Froide')
ax0.plot(x_temperature, temperature_moyenne, 'green', linewidth=1.5, label='Moyenne')
ax0.plot(x_temperature, temperature_chaude, 'orange', linewidth=1.5, label='Chaude')
ax0.plot(x_temperature, temperature_caniculaire, 'r', linewidth=1.5, label='Caniculaire')
ax0.set_xlim([-15,45])
ax0.set_ylim([0,1])
ax0.set_title('Température')
ax0.legend()
ax1.plot(x_qualite_air, qualite_air_bonne, 'lawngreen', linewidth=1.5, label='Bonne')
ax1.plot(x_qualite_air, qualite_air_mediocre, 'tan', linewidth=1.5, label='Médiocre')
ax1.plot(x_qualite_air, qualite_air_mauvaise, 'firebrick', linewidth=1.5, label='Mauvaise')
ax1.set_xlim([0,10])
ax1.set_ylim([0,1.1])
ax1.set_title("Qualité de l'air")
ax1.legend()
ax2.plot(x_humidite, humidite_basse, 'mediumseagreen', linewidth=1.5, label='Basse')
ax2.plot(x_humidite, humidite_haute, 'darkslategray', linewidth=1.5, label='Haute')
ax2.set_xlim([0,100])
ax2.set_ylim([0,1.1])
ax2.set_title("Humidité")
ax2.legend()
ax3.plot(x_surtension, surtension_presente, 'red', linewidth=1.5, label="Présente")
ax3.set_xlim([200,300])
ax3.set_ylim([0,1.1])
ax3.set_title('Surtension')
ax3.legend()
# Turn off top/right axes
for ax in (ax0, ax1, ax2, ax3):
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.get_xaxis().tick_bottom()
ax.get_yaxis().tick_left()
plt.tight_layout()
# Visualize this
fig, ax0 = plt.subplots(figsize=(8, 3))
ax0.plot(x_vitesse, vitesse_lente, 'gold', linewidth=1.5, label="Lente" )
ax0.plot(x_vitesse, vitesse_moyenne, 'orange', linewidth=1.5, label="Moyenne")
ax0.plot(x_vitesse, vitesse_rapide, 'red', linewidth=1.5, label="Rapide")
ax0.set_xlim([20,250])
ax0.set_ylim([0,1.1])
ax0.set_title("Vitesse du ventilateur")
ax0.legend()
# Turn off top/right axes
for ax in (ax0,):
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.get_xaxis().tick_bottom()
ax.get_yaxis().tick_left()
plt.tight_layout()
crisp_temperature = 7
crisp_qualite_air = 8
crisp_humidite = 50
crisp_surtension = 270
level_temperature_glaciale = fuzz.interp_membership(x_temperature, temperature_glaciale, crisp_temperature)
level_temperature_froide = fuzz.interp_membership(x_temperature, temperature_froide, crisp_temperature)
level_temperature_moyenne = fuzz.interp_membership(x_temperature, temperature_moyenne, crisp_temperature)
level_temperature_chaude = fuzz.interp_membership(x_temperature, temperature_chaude, crisp_temperature)
level_temperature_caniculaire = fuzz.interp_membership(x_temperature, temperature_caniculaire, crisp_temperature)
level_qualite_air_bonne = fuzz.interp_membership(x_qualite_air, qualite_air_bonne, crisp_qualite_air)
level_qualite_air_mediocre = fuzz.interp_membership(x_qualite_air, qualite_air_mediocre, crisp_qualite_air)
level_qualite_air_mauvaise = fuzz.interp_membership(x_qualite_air, qualite_air_mauvaise, crisp_qualite_air)
level_humidite_basse = fuzz.interp_membership(x_humidite, humidite_basse, crisp_humidite)
level_humidite_haute = fuzz.interp_membership(x_humidite, humidite_haute, crisp_humidite)
level_surtension_presente = fuzz.interp_membership(x_surtension, surtension_presente, crisp_surtension)
active_rule1 = np.fmin(np.fmax(level_temperature_caniculaire, level_temperature_chaude),level_humidite_haute)
active_rule2 = np.fmax(np.fmax(level_qualite_air_mediocre, level_qualite_air_mauvaise), level_temperature_moyenne)
active_rule3 = np.fmin(np.fmin(level_qualite_air_mauvaise, 1 - level_surtension_presente), 1 - np.fmax(level_temperature_glaciale, level_temperature_froide))
active_rule4 = np.fmin(np.fmax(level_temperature_glaciale, level_temperature_froide), 1 - level_surtension_presente)
active_rule5 = level_surtension_presente
vitesse_activation_lente = np.fmin(np.fmax(active_rule4,active_rule5), vitesse_lente)
vitesse_activation_moyenne = np.fmin(active_rule2, vitesse_moyenne)
vitesse_activation_rapide = np.fmin(np.fmax(active_rule1,active_rule3), vitesse_rapide)
vitesse0 = np.zeros_like(x_vitesse)
# Visualize this
fig, ax0 = plt.subplots(figsize=(8, 3))
ax0.fill_between(x_vitesse, vitesse0, vitesse_activation_lente, facecolor='gold', alpha=0.7)
ax0.plot(x_vitesse, vitesse_lente, 'gold', linewidth=0.5, linestyle='--', )
ax0.fill_between(x_vitesse,vitesse0, vitesse_activation_moyenne, facecolor='orange', alpha=0.7)
ax0.plot(x_vitesse, vitesse_moyenne, 'orange', linewidth=0.5, linestyle='--')
ax0.fill_between(x_vitesse,vitesse0, vitesse_activation_rapide, facecolor='red', alpha=0.7)
ax0.plot(x_vitesse, vitesse_rapide, 'red', linewidth=0.5, linestyle='--')
#ax0.set_title('Sortie')
# Turn off top/right axes
for ax in (ax0,):
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.get_xaxis().tick_bottom()
ax.get_yaxis().tick_left()
plt.tight_layout()
aggregated = np.fmax(vitesse_activation_lente,
np.fmax(vitesse_activation_moyenne, vitesse_activation_rapide))
vitesse = fuzz.defuzz(x_vitesse, aggregated, 'centroid')
vitesse_activation = fuzz.interp_membership(x_vitesse, aggregated, vitesse)
fig, ax0 = plt.subplots(figsize=(8, 3))
ax0.plot(x_vitesse, vitesse_lente, 'b', linewidth=0.5, linestyle='--', )
ax0.plot(x_vitesse, vitesse_moyenne, 'g', linewidth=0.5, linestyle='--')
ax0.plot(x_vitesse, vitesse_rapide, 'r', linewidth=0.5, linestyle='--')
ax0.fill_between(x_vitesse, vitesse0, aggregated, facecolor='Orange', alpha=0.7)
ax0.plot([vitesse, vitesse], [0, vitesse_activation], 'k', linewidth=1.5, alpha=0.9)
#ax0.set_title('Agrégation des sorties et résultat (ligne)')
print("LOG")
print("crisp_temperature =" + str(crisp_temperature ))
print("crisp_qualite_air =" + str(crisp_qualite_air ))
print("crisp_humidite =" + str(crisp_humidite ))
print("crisp_surtension =" + str(crisp_surtension ))
print("level_temperature_glaciale =" + str(level_temperature_glaciale ))
print("level_temperature_froide =" + str(level_temperature_froide ))
print("level_temperature_moyenne =" + str(level_temperature_moyenne ))
print("level_temperature_chaude =" + str(level_temperature_chaude ))
print("level_temperature_caniculaire =" + str(level_temperature_caniculaire ))
print("level_qualite_air_bonne =" + str(level_qualite_air_bonne ))
print("level_qualite_air_mediocre =" + str(level_qualite_air_mediocre ))
print("level_qualite_air_mauvaise =" + str(level_qualite_air_mauvaise ))
print("level_humidite_basse =" + str(level_humidite_basse ))
print("level_humidite_haute =" + str(level_humidite_haute ))
print("level_surtension_presente =" + str(level_surtension_presente ))
print("active_rule1 =" + str(active_rule1 ))
print("active_rule2 =" + str(active_rule2 ))
print("active_rule3 =" + str(active_rule3 ))
print("active_rule4 =" + str(active_rule4 ))
print("active_rule5 =" + str(active_rule5 ))
print("vitesse_activation =" + str(vitesse_activation))
print("vitesse = " + str(vitesse))
level_vitesse_lente = fuzz.interp_membership(x_vitesse, vitesse_lente, vitesse)
level_vitesse_moyenne = fuzz.interp_membership(x_vitesse, vitesse_moyenne, vitesse)
level_vitesse_rapide = fuzz.interp_membership(x_vitesse, vitesse_rapide, vitesse)
print("level_vitesse_lente=" + str(level_vitesse_lente))
print("level_vitesse_moyenne=" + str(level_vitesse_moyenne))
print("level_vitesse_rapide=" + str(level_vitesse_rapide))
# Turn off top/right axes
for ax in (ax0,):
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.get_xaxis().tick_bottom()
ax.get_yaxis().tick_left()
plt.tight_layout()
plt.show()
input("wait...")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.