1 | initial version |
To create a dynamic CTRL.Rule() using Scikit-Fuzzy, you can follow the following steps:
import numpy as np
import skfuzzy as fuzz
from skfuzzy import control as ctrl
# Input variable
length = ctrl.Antecedent(np.arange(0, 11, 1), 'length')
# Output variable
width = ctrl.Consequent(np.arange(0, 11, 1), 'width')
# Membership functions for length
length['short'] = fuzz.trimf(length.universe, [0, 0, 5])
length['medium'] = fuzz.trimf(length.universe, [0, 5, 10])
length['long'] = fuzz.trimf(length.universe, [5, 10, 10])
# Membership functions for width
width['narrow'] = fuzz.trimf(width.universe, [0, 0, 5])
width['medium'] = fuzz.trimf(width.universe, [2, 5, 8])
width['wide'] = fuzz.trimf(width.universe, [5, 10, 10])
rule1 = ctrl.Rule(length['short'] & ~length['medium'], width['narrow'])
rule2 = ctrl.Rule(length['medium'], width['medium'])
rule3 = ctrl.Rule(length['long'] | length['medium'], width['wide'])
# Create a control system using the defined rules
width_control = ctrl.ControlSystem([rule1, rule2, rule3])
In the above code, rule1 corresponds to "If length is short and not medium, then width is narrow", rule2 corresponds to "If length is medium, then width is medium", and rule3 corresponds to "If length is long or medium, then width is wide".
# Create a control simulation object
width_simulation = ctrl.ControlSystemSimulation(width_control)
# Provide input to the control system
width_simulation.input['length'] = 6
# Compute the result of the simulation
width_simulation.compute()
# Get the output value
width_val = width_simulation.output['width']
print("Width: ", width_val)
The above code will print the width value obtained as per the given input length value.
In this way, dynamic CTRL.Rule() can be created using Scikit-Fuzzy to implement fuzzy control systems for various real-world applications.