__Introduction:__

__Introduction:__

**Fuzzy logic is a powerful tool for dealing with imprecise or uncertain data. It is widely used in control systems to regulate parameters such as temperature, pressure, and speed.**

**We will learn how to create a fuzzy logic controller system for regulating AC temperature based on room temperature feedback using Python.**

**Scikit-fuzzy package, is a Python library for fuzzy logic control, to define the fuzzy sets, create the rules, and simulate the system.**

**This post will cover step-by-step instructions on how to create a fuzzy logic control system, and we will test it using different input temperatures to see how it reacts.**

__Setting up the environment:__

__Setting up the environment:__

**You can install the skfuzzy module using the following pip command:**

**Import the modules.**

**We start with defining the fuzzy sets: –**

**The antecedent and Consequent methods of the skfuzzy nodule are used to set the input and output respectively to the control system.**

__create the membership functions view the graphical representation__

__create the membership functions view the graphical representation__

**The automf function is a part of the scikit-fuzzy package, which is a Python library for fuzzy logic control.**

**It is used to automatically generate the membership functions for a given variable.**

**Membership functions are used to define how much an input belongs to a particular fuzzy set.**

**It creates these membership functions automatically based on the number of sets specified as an argument.**

__Room Temperature Graph:__

__Room Temperature Graph:__

**Here, we call the automf function with the argument 3, so it will create three fuzzy sets.**

**By default we have – poor, average, good**

**The temp.view() and ac_temp.view() functions create graphical representations of the membership functions generated by the automf function. The output of these functions is a plot that shows the membership function curves for each fuzzy set, with the x-axis representing the range of input values and the y-axis representing the degree of membership in the set.**

**For the temp input variable, the temp.view() function generates a plot with three fuzzy sets: poor, average, and good. The fuzzy sets are defined by triangular membership functions, with each function centered at a specific temperature value. The ranges of the fuzzy sets are as follows:**

__Fuzzy Sets:__

__Fuzzy Sets:__

**poor: This fuzzy set has a range of 15 to 23.3 degrees Celsius, with a peak membership value of 1 at 15 degrees and a gradually decreasing membership value as the temperature increases towards the center of the triangular function at 23.3 degrees.**

**average: This fuzzy set has a range of 18.3 to 31.7 degrees Celsius, with a peak membership value of 1 at 25 degrees and decreasing membership values towards the edges of the triangular function.**

**good: This fuzzy set has a range of 26.7 to 45 degrees Celsius, with a peak membership value of 1 at 45 degrees and decreasing membership values towards the center of the triangular function at 26.7 degrees.**

__AC Temperature Graph:__

__AC Temperature Graph:__

**For the ac_temp output variable, the ac_temp.view() function generates a plot with three fuzzy sets: poor, average, and good. The fuzzy sets are defined by triangular membership functions, with each function centered at a specific temperature value. The ranges of the fuzzy sets are as follows:**

__Fuzzy Sets:__

__Fuzzy Sets:__

**poor: This fuzzy set has a range of 15 to 20 degrees Celsius, with a peak membership value of 1 at 15 degrees and decreasing membership values towards the center of the triangular function at 20 degrees.**

**average: This fuzzy set has a range of 17.5 to 27.5 degrees Celsius, with a peak membership value of 1 at 22.5 degrees and decreasing membership values towards the edges of the triangular function.**

**good: This fuzzy set has a range of 25 to 30 degrees Celsius, with a peak membership value of 1 at 30 degrees and decreasing membership values towards the center of the triangular function at 25 degrees.**

**Rules and control System:**

**The rules for the control system are set using the ctrl.Rule() function, where the antecedent is the current room temperature (represented by the temp object), and the consequent is the desired AC temperature (represented by the ac_temp object).**

**In this case, three rules are defined:**

**If the room temperature is “poor,” set the AC temperature to “good.”**

**If the room temperature is “average,” set the AC temperature to “average.”**

**If the room temperature is “good,” set the AC temperature to “poor.”**

**The ctrl.ControlSystem() function is then used to create the control system, which takes in the defined rules as its argument.**

**The ctrl.ControlSystemSimulation() function creates a simulation of the control system, which can be used to test different input values and observe the resulting output.**

**Finally, we test the system by giving the room temperature as input.**

**Each simulation is done by setting the detect_temp.input[‘temp’] value to a different room temperature, and then using the detect_temp.compute() function to calculate the corresponding AC temperature output based on the defined rules and membership functions.**

**Finally, the ac_temp.view(sim=detect_temp) function is used to plot the graph of the output AC temperature based on the simulated input value.**

**When the room temperature is set to 45°C, the output AC temperature is adjusted to the “poor” range, which is around 17.5°C in the membership function plot.**

**If it is set to 30°C, the output AC temperature is adjusted to the “average” range, which is around 22.5°C in the membership function plot.**

**When the temperature is set to 15°C, the output AC temperature is adjusted to the “good” range, which is around 27.5°C in the membership function plot.**

**Overall, the simulation shows that the fuzzy control system is able to adjust the AC temperature based on the input room temperature and the defined rules and membership functions.**

__Full code:__

__Full code:__

**#importing the numpy and skfuzzy modules**

**import numpy as np**

**import skfuzzy as fuzz**

**from skfuzzy import control as ctrl**

**#array which contains the room temperature**

**temperature = np.arange(15,45)**

**#set the input**

**temp = ctrl.Antecedent(temperature, ‘temp’)**

**#array to represent the possible AC temperatures**

**#based on the defined temperature range**

**ac_temperature = np.arange(15,31)**

**#set the output of the system**

**ac_temp = ctrl.Consequent(ac_temperature, ‘ac_temp’)**

**#automatically generate the membership functions**

**temp.automf(3)**

**ac_temp.automf(3)**

**#graphical representation**

**temp.view()**

**ac_temp.view()**

**#setting rules**

**rule1 = ctrl.Rule(temp[‘poor’], ac_temp[‘good’])**

**rule2 = ctrl.Rule(temp[‘average’], ac_temp[‘average’])**

**rule3 = ctrl.Rule(temp[‘good’], ac_temp[‘poor’])**

**#Creating control system**

**temperature_ctrl = ctrl.ControlSystem([rule1,rule2,rule3])**

**detect_temp = ctrl.ControlSystemSimulation(temperature_ctrl)**

**#testing**

**detect_temp.input[‘temp’] = 45**

**detect_temp.compute()**

**temp = detect_temp.output[‘ac_temp’]**

**print(“\nWhen room temperature is 45°C :”)**

**print(“The AC temperature is adjusted to”,temp, “°C”)**

**ac_temp.view(sim = detect_temp)**

**detect_temp.input[‘temp’] = 30**

**detect_temp.compute()**

**temp = detect_temp.output[‘ac_temp’]**

**print(“\nWhen room temperature is 27°C :”)**

**print(“The AC temperature is adjusted to”,temp, “°C”)**

**detect_temp.input[‘temp’] = 15**

**detect_temp.compute()**

**temp = detect_temp.output[‘ac_temp’]**

**print(“\nWhen room temperature is 15°C :”)**

**print(“The AC temperature is adjusted to”,temp, “°C”)**

**ac_temp.view(sim = detect_temp)**

__Output:__

__Output:__