Skip to content

Instantly share code, notes, and snippets.

@Pascal66
Created October 6, 2022 08:23
Show Gist options
  • Save Pascal66/0e0b5da5395cb1cc41c58acc90d35dd3 to your computer and use it in GitHub Desktop.
Save Pascal66/0e0b5da5395cb1cc41c58acc90d35dd3 to your computer and use it in GitHub Desktop.
"""
Rules:
IF room_temperature IS LOW AND humidity IS LOW AND number_of_people IS FEW THEN thermostat_temperature IS VERY HIGH
IF room_temperature IS LOW AND humidity IS LOW AND number_of_people IS ALOT THEN thermostat_temperature IS HIGH
IF room_temperature IS LOW AND humidity IS LOW THEN thermostat_temperature IS HIGH
IF room_temperature IS LOW AND humidity IS HIGH THEN thermostat_temperature IS QUITE HIGH
IF room_temperature IS LOW AND humidity IS LOW AND number_of_people IS FEW THEN thermostat_temperature IS QUITE LOW
IF room_temperature IS LOW AND humidity IS LOW AND number_of_people IS ALOT THEN thermostat_temperature IS LOW
IF room_temperature IS LOW AND humidity IS LOW THEN thermostat_temperature IS VERY LOW
IF room_temperature IS LOW AND humidity IS HIGH AND number_of_people IS FEW THEN thermostat_temperature IS LOW
IF room_temperature IS LOW AND humidity IS HIGH AND number_of_people IS ALOT THEN thermostat_temperature IS QUITE LOW
IF room_temperature IS HIGH AND humidity IS LOW AND number_of_people IS FEW THEN thermostat_temperature IS QUITE LOW
IF room_temperature IS HIGH AND humidity IS LOW AND number_of_people IS ALOT THEN thermostat_temperature IS LOW
IF room_temperature IS HIGH AND humidity IS LOW THEN thermostat_temperature IS LOW
IF room_temperature IS HIGH AND humidity IS HIGH AND number_of_people IS FEW THEN thermostat_temperature IS LOW
IF room_temperature IS HIGH AND humidity IS HIGH AND number_of_people IS ALOTTHEN thermostat_temperature IS VERY LOW
"""
import numpy as np
import skfuzzy as fuzz
from skfuzzy import control as ctrl
def sqrt(num):
return num**0.5
def square(num):
return num**2
# Generate universe from fuzzy set as numpy array
Temperature = ctrl.Antecedent(np.arange(15, 36, 1), 'Temperature')
Humidity = ctrl.Antecedent(np.arange(0, 101, 1), 'Humidity')
People = ctrl.Antecedent(np.arange(0, 31, 1), 'People')
thermostat = ctrl.Consequent(np.arange(15, 36, 1), 'Thermostat')
# Generate membership function values with trim to create a triangular membership function
Temperature['LOW'] = fuzz.trimf(Temperature.universe, [15, 15, 25])
Temperature['LOW'] = fuzz.trimf(Temperature.universe, [23, 25, 29])
Temperature['HIGH'] = fuzz.trimf(Temperature.universe, [27, 35, 36])
Humidity['LOW'] = fuzz.trimf(Humidity.universe, [0, 0, 41])
Humidity['LOW'] = fuzz.trimf(Humidity.universe, [35, 55, 76])
Humidity['HIGH'] = fuzz.trimf(Humidity.universe, [70, 100, 101])
People['FEW'] = fuzz.trimf(People.universe, [0, 0, 21])
People['ALOT'] = fuzz.trimf(People.universe, [10, 30, 31])
thermostat['QUITE LOW'] = np.array(list(map(sqrt, fuzz.trimf(Temperature.universe, [15, 15, 25]))))
thermostat['LOW'] = fuzz.trimf(Temperature.universe, [15, 15, 25])
thermostat['VERY LOW'] = np.array(list(map(square, fuzz.trimf(Temperature.universe, [15, 15, 25]))))
thermostat['QUITE LOW'] = np.array(list(map(sqrt, fuzz.trimf(Temperature.universe, [23, 26, 30]))))
thermostat['LOW'] = fuzz.trimf(Temperature.universe, [23, 26, 30])
thermostat['VERY LOW'] = np.array(list(map(square, fuzz.trimf(Temperature.universe, [23, 26, 30]))))
thermostat['QUITE HIGH'] = np.array(list(map(sqrt, fuzz.trimf(Temperature.universe, [27, 35, 36]))))
thermostat['HIGH'] = fuzz.trimf(Temperature.universe, [27, 35, 36])
thermostat['VERY HIGH'] = np.array(list(map(square, fuzz.trimf(Temperature.universe, [27, 35, 36]))))
# Buat list of fuzzy rules menggunakan ctrl.Rule
rulebase = [ctrl.Rule(Temperature['LOW'] & Humidity['LOW'] & People['FEW'] , thermostat['VERY HIGH']),
ctrl.Rule(Temperature['LOW'] & Humidity['LOW'] & People['ALOT'] , thermostat['HIGH']),
ctrl.Rule(Temperature['LOW'] & Humidity['LOW'] , thermostat['HIGH']),
ctrl.Rule(Temperature['LOW'] & Humidity['HIGH'] , thermostat['QUITE HIGH']),
ctrl.Rule(Temperature['LOW'] & Humidity['LOW'] & People['FEW'] , thermostat['QUITE LOW']),
ctrl.Rule(Temperature['LOW'] & Humidity['LOW'] & People['ALOT'] , thermostat['LOW']),
ctrl.Rule(Temperature['LOW'] & Humidity['LOW'] , thermostat['VERY LOW']),
ctrl.Rule(Temperature['LOW'] & Humidity['HIGH'] & People['FEW'] , thermostat['LOW']),
ctrl.Rule(Temperature['LOW'] & Humidity['HIGH'] & People['ALOT'] , thermostat['QUITE LOW']),
ctrl.Rule(Temperature['HIGH'] & Humidity['LOW'] & People['FEW'] , thermostat['QUITE LOW']),
ctrl.Rule(Temperature['HIGH'] & Humidity['LOW'] & People['ALOT'] , thermostat['LOW']),
ctrl.Rule(Temperature['HIGH'] & Humidity['LOW'] , thermostat['LOW']),
ctrl.Rule(Temperature['HIGH'] & Humidity['HIGH'] & People['FEW'] , thermostat['LOW']),
ctrl.Rule(Temperature['HIGH'] & Humidity['HIGH'] & People['ALOT'] , thermostat['VERY LOW'])]
# Create a Control System for Consequent and Simulation variables
thermostat_ctrl = ctrl.ControlSystem(rulebase)
thermostat_ctrl_sim = ctrl.ControlSystemSimulation(thermostat_ctrl)
# Accept input
temperatureInput = float(input('room_temperature (15-35) : '))
humidityInput = float(input('humidity (0-100) : '))
peopleInput = float(input('number_of_people (0-30) : '))
# Enter the input into the simulation
thermostat_ctrl_sim.input['Temperature'] = temperatureInput
thermostat_ctrl_sim.input['Humidity'] = humidityInput
thermostat_ctrl_sim.input['People'] = peopleInput
# Do the calculations
thermostat_ctrl_sim.compute()
# Show value of defuzzification result (centroid method)
print('thermostat_temperature : {:.2f}'.format(thermostat_ctrl_sim.output['thermostat']))
thermostat.view(sim = thermostat_ctrl_sim)
input('Press any button to exit...')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment