Building a Simple Fuzzy Inference System
To build a fuzzy inference system, you start by defining the problem and identifying the input and output variables. Suppose you want to control the speed of a fan in a room using two inputs: temperature and humidity. The output variable is fan speed. The steps include:
- Defining fuzzy sets for each variable;
- Fuzzifying the crisp inputs;
- Applying fuzzy rules;
- Aggregating the results;
- Finally defuzzifying to get a crisp output.
You first create membership functions for temperature (such as Cool, Warm, Hot) and humidity (Dry, Humid). The output, fan speed, might have fuzzy sets like Low, Medium, and High. Next, you define rules like:
- If temperature is Hot OR humidity is Humid, then fan speed is High;
- If temperature is Cool AND humidity is Dry, then fan speed is Low.
Given specific values for temperature and humidity, you fuzzify these inputs, evaluate the rules, aggregate the outcomes, and produce a final fan speed value.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960import numpy as np # Membership functions for temperature def temp_cool(x): return max(0, min(1, (25 - x) / 10)) def temp_hot(x): return max(0, min(1, (x - 20) / 10)) # Membership functions for humidity def hum_dry(x): return max(0, min(1, (60 - x) / 30)) def hum_humid(x): return max(0, min(1, (x - 40) / 30)) # Membership functions for fan speed (output) def speed_low(x): return max(0, min(1, (50 - x) / 30)) def speed_high(x): return max(0, min(1, (x - 30) / 30)) # Fuzzify inputs temperature = 28 # crisp input humidity = 65 # crisp input temp_cool_val = temp_cool(temperature) temp_hot_val = temp_hot(temperature) hum_dry_val = hum_dry(humidity) hum_humid_val = hum_humid(humidity) # Rule evaluation # Rule 1: If temperature is Hot OR humidity is Humid, then fan speed is High rule1_strength = max(temp_hot_val, hum_humid_val) # Rule 2: If temperature is Cool AND humidity is Dry, then fan speed is Low rule2_strength = min(temp_cool_val, hum_dry_val) # Aggregation and Defuzzification (Centroid method) x_range = np.linspace(0, 80, 100) aggregated = np.zeros_like(x_range) for i, x in enumerate(x_range): high_val = min(rule1_strength, speed_high(x)) low_val = min(rule2_strength, speed_low(x)) aggregated[i] = max(high_val, low_val) if np.sum(aggregated) == 0: fan_speed = 0 else: fan_speed = np.sum(x_range * aggregated) / np.sum(aggregated) print("Fuzzified Inputs:") print(f" Temperature Cool: {temp_cool_val:.2f}, Hot: {temp_hot_val:.2f}") print(f" Humidity Dry: {hum_dry_val:.2f}, Humid: {hum_humid_val:.2f}") print("\nRule Strengths:") print(f" Rule 1 (Hot OR Humid): {rule1_strength:.2f}") print(f" Rule 2 (Cool AND Dry): {rule2_strength:.2f}") print(f"\nDefuzzified Fan Speed: {fan_speed:.2f}")
The output of the system demonstrates approximate reasoning: instead of producing a binary high or low fan speed, the fuzzy inference system blends the influence of both rules based on how well the inputs match the fuzzy sets. This approach allows the fan to respond smoothly and proportionally to varying degrees of temperature and humidity, rather than switching abruptly between fixed speeds. The result is a more natural and adaptive control, closely reflecting the way you might reason about comfort in everyday situations.
Danke für Ihr Feedback!
Fragen Sie AI
Fragen Sie AI
Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen
Großartig!
Completion Rate verbessert auf 6.67
Building a Simple Fuzzy Inference System
Swipe um das Menü anzuzeigen
To build a fuzzy inference system, you start by defining the problem and identifying the input and output variables. Suppose you want to control the speed of a fan in a room using two inputs: temperature and humidity. The output variable is fan speed. The steps include:
- Defining fuzzy sets for each variable;
- Fuzzifying the crisp inputs;
- Applying fuzzy rules;
- Aggregating the results;
- Finally defuzzifying to get a crisp output.
You first create membership functions for temperature (such as Cool, Warm, Hot) and humidity (Dry, Humid). The output, fan speed, might have fuzzy sets like Low, Medium, and High. Next, you define rules like:
- If temperature is Hot OR humidity is Humid, then fan speed is High;
- If temperature is Cool AND humidity is Dry, then fan speed is Low.
Given specific values for temperature and humidity, you fuzzify these inputs, evaluate the rules, aggregate the outcomes, and produce a final fan speed value.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960import numpy as np # Membership functions for temperature def temp_cool(x): return max(0, min(1, (25 - x) / 10)) def temp_hot(x): return max(0, min(1, (x - 20) / 10)) # Membership functions for humidity def hum_dry(x): return max(0, min(1, (60 - x) / 30)) def hum_humid(x): return max(0, min(1, (x - 40) / 30)) # Membership functions for fan speed (output) def speed_low(x): return max(0, min(1, (50 - x) / 30)) def speed_high(x): return max(0, min(1, (x - 30) / 30)) # Fuzzify inputs temperature = 28 # crisp input humidity = 65 # crisp input temp_cool_val = temp_cool(temperature) temp_hot_val = temp_hot(temperature) hum_dry_val = hum_dry(humidity) hum_humid_val = hum_humid(humidity) # Rule evaluation # Rule 1: If temperature is Hot OR humidity is Humid, then fan speed is High rule1_strength = max(temp_hot_val, hum_humid_val) # Rule 2: If temperature is Cool AND humidity is Dry, then fan speed is Low rule2_strength = min(temp_cool_val, hum_dry_val) # Aggregation and Defuzzification (Centroid method) x_range = np.linspace(0, 80, 100) aggregated = np.zeros_like(x_range) for i, x in enumerate(x_range): high_val = min(rule1_strength, speed_high(x)) low_val = min(rule2_strength, speed_low(x)) aggregated[i] = max(high_val, low_val) if np.sum(aggregated) == 0: fan_speed = 0 else: fan_speed = np.sum(x_range * aggregated) / np.sum(aggregated) print("Fuzzified Inputs:") print(f" Temperature Cool: {temp_cool_val:.2f}, Hot: {temp_hot_val:.2f}") print(f" Humidity Dry: {hum_dry_val:.2f}, Humid: {hum_humid_val:.2f}") print("\nRule Strengths:") print(f" Rule 1 (Hot OR Humid): {rule1_strength:.2f}") print(f" Rule 2 (Cool AND Dry): {rule2_strength:.2f}") print(f"\nDefuzzified Fan Speed: {fan_speed:.2f}")
The output of the system demonstrates approximate reasoning: instead of producing a binary high or low fan speed, the fuzzy inference system blends the influence of both rules based on how well the inputs match the fuzzy sets. This approach allows the fan to respond smoothly and proportionally to varying degrees of temperature and humidity, rather than switching abruptly between fixed speeds. The result is a more natural and adaptive control, closely reflecting the way you might reason about comfort in everyday situations.
Danke für Ihr Feedback!