In [1]:
# !pip install ipywidgets
# !pip install -U scikit-fuzzy
In [2]:
import numpy as np
from skfuzzy import control as ctrl
from skfuzzy import membership as mf
from IPython.display import display
In [3]:
# Define input variables
data_redundancy = ctrl.Antecedent(np.arange(0, 11, 1), 'Data Redundancy')
degradation_level = ctrl.Antecedent(np.arange(0, 5.1, 0.1), 'Degradation Level')
error_history = ctrl.Antecedent(np.arange(0, 11, 1), 'Error History')
data_redundancy.automf(3)
degradation_level.automf(3)
error_history.automf(3)
# Output Variable
error_likelihood = ctrl.Consequent(np.arange(0, 1.1, 0.1), 'Error Likelihood')
In [4]:
# Fuzzification
data_redundancy['Low'] = mf.trimf(data_redundancy.universe, [0, 5, 10])
data_redundancy['Medium'] = mf.trimf(data_redundancy.universe, [2.5, 5.0, 7.5])
data_redundancy['High'] = mf.trimf(data_redundancy.universe, [5, 10, 10])
# Define membership functions for the degradation_level
degradation_level['Low'] = mf.trimf(degradation_level.universe, [0, 0, 2.5])
degradation_level['Medium'] = mf.trimf(degradation_level.universe, [0, 2.5, 5])
degradation_level['High'] = mf.trimf(degradation_level.universe, [2.5, 5, 5])
# Membership functions for error_history
error_history['Low'] = mf.trimf(error_history.universe, [0, 0, 5])
error_history['Moderate'] = mf.trimf(error_history.universe, [0, 5, 10])
error_history['High'] = mf.trimf(error_history.universe, [5, 10, 10])
# Membership functions for the output variable (error_likelihood)
error_likelihood['Low'] = mf.trimf(error_likelihood.universe, [0, 0, 0.5])
error_likelihood['Medium'] = mf.trimf(error_likelihood.universe, [0, 0.5, 1])
error_likelihood['High'] = mf.trimf(error_likelihood.universe, [0.5, 1, 1])
In [5]:
data_redundancy.view()
degradation_level.view()
error_history.view()
error_likelihood.view()
C:\Users\Dell\anaconda3\Lib\site-packages\skfuzzy\control\fuzzyvariable.py:122: UserWarning: Matplotlib is currently using module://matplotlib_inline.backend_inline, which is a non-GUI backend, so cannot show the figure. fig.show()
In [9]:
# Rule Base
rule1 = ctrl.Rule(data_redundancy['High'] & degradation_level['Low'] & error_history['Moderate'], error_likelihood['Low'])
rule2 = ctrl.Rule(data_redundancy['Medium'] & degradation_level['Medium'] & error_history['Moderate'], error_likelihood['Medium'])
rule3 = ctrl.Rule(data_redundancy['Low'] & degradation_level['High'] & error_history['High'], error_likelihood['High'])
rule4 = ctrl.Rule(data_redundancy['High'] & degradation_level['Medium'] & error_history['Low'], error_likelihood['Medium'])
rule5 = ctrl.Rule(data_redundancy['Low'] & degradation_level['Low'] & error_history['Moderate'], error_likelihood['Low'])
rule6 = ctrl.Rule(data_redundancy['Medium'] & degradation_level['High'] & error_history['High'], error_likelihood['High'])
rule7 = ctrl.Rule(data_redundancy['High'] & degradation_level['Medium'] & error_history['High'], error_likelihood['High'])
rule8 = ctrl.Rule(data_redundancy['Medium'] & degradation_level['Low'] & error_history['Low'], error_likelihood['Low'])
rule9 = ctrl.Rule(data_redundancy['Low'] & degradation_level['High'] & error_history['Moderate'], error_likelihood['Medium'])
# Create the control system
error_detection_system = ctrl.ControlSystem([rule1, rule2, rule3, rule4, rule5, rule6, rule7, rule8, rule9])
error_detector = ctrl.ControlSystemSimulation(error_detection_system)
In [10]:
from matplotlib import pyplot as plt
error_detector.input['Data Redundancy'] = 2
error_detector.input['Degradation Level'] = 2
error_detector.input['Error History'] = 4
error_detector.compute()
error_likelihood.view(sim=error_detector)
# error_detector.output['Error Likelihood'].view()
In [11]:
# Run this code block again separately after executing the whole file for the first time
from IPython.core.display_functions import clear_output
from ipywidgets import widgets
# User Interface
data_redundancy_slider = widgets.FloatSlider(value=8, min=0, max=10, step=1, description='Data Redundancy')
degradation_level_slider = widgets.FloatSlider(value=2, min=0, max=5, step=0.1, description='Degradation Level')
error_history_slider = widgets.FloatSlider(value=7, min=0, max=10, step=1, description='Error History')
calculate_button = widgets.Button(description="Calculate Error")
output_text = widgets.Output()
def calculate_likelihood(b):
error_detector.input['Data Redundancy'] = data_redundancy_slider.value
error_detector.input['Degradation Level'] = degradation_level_slider.value
error_detector.input['Error History'] = error_history_slider.value
error_detector.compute()
with output_text:
clear_output(wait=True)
print("Error Likelihood:", error_detector.output['Error Likelihood'])
error_likelihood.view(sim=error_detector)
calculate_button.on_click(calculate_likelihood)
def generate_graph(c):
error_detector.input['Data Redundancy'] = data_redundancy_slider.value
error_detector.input['Degradation Level'] = degradation_level_slider.value
error_detector.input['Error History'] = error_history_slider.value
error_detector.compute()
print(error_detector.output['error_likelihood'])
error_likelihood.view(sim=error_detector)
# Display the user interface
display(data_redundancy_slider, degradation_level_slider, error_history_slider, calculate_button, output_text)
FloatSlider(value=8.0, description='Data Redundancy', max=10.0, step=1.0)
FloatSlider(value=2.0, description='Degradation Level', max=5.0)
FloatSlider(value=7.0, description='Error History', max=10.0, step=1.0)
Button(description='Calculate Error', style=ButtonStyle())
Output()
In [13]:
from IPython.core.display_functions import clear_output
from ipywidgets import widgets
# User Interface
data_redundancy_slider = widgets.FloatSlider(value=8, min=0, max=10, step=1, description='Data Redundancy')
degradation_level_slider = widgets.FloatSlider(value=2, min=0, max=5, step=0.1, description='Degradation Level')
error_history_slider = widgets.FloatSlider(value=7, min=0, max=10, step=1, description='Error History')
calculate_button = widgets.Button(description="Calculate Error")
output_text = widgets.Output()
# Variable to store error_likelihood
stored_error_likelihood = None
def classify_error_likelihood(error_likelihood_value):
# Classify the error likelihood into categories
if error_likelihood_value <= 3:
return "Low"
elif 3 < error_likelihood_value <= 7:
return "Medium"
else:
return "High"
def calculate_likelihood(b):
global stored_error_likelihood # Declare the variable as global
error_detector.input['Data Redundancy'] = data_redundancy_slider.value
error_detector.input['Degradation Level'] = degradation_level_slider.value
error_detector.input['Error History'] = error_history_slider.value
error_detector.compute()
with output_text:
clear_output(wait=True)
error_likelihood_value = error_detector.output['Error Likelihood']
print("Error Likelihood:", error_likelihood_value)
# Classify and print the category
error_category = classify_error_likelihood(error_likelihood_value)
print("Error Category:", error_category)
# Store the value in the variable
stored_error_likelihood = error_likelihood_value
calculate_button.on_click(calculate_likelihood)
# Display the user interface
display(data_redundancy_slider, degradation_level_slider, error_history_slider, calculate_button, output_text)
FloatSlider(value=8.0, description='Data Redundancy', max=10.0, step=1.0)
FloatSlider(value=2.0, description='Degradation Level', max=5.0)
FloatSlider(value=7.0, description='Error History', max=10.0, step=1.0)
Button(description='Calculate Error', style=ButtonStyle())
Output()
In [ ]: