Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To create a dynamic CTRL.Rule() using Scikit-Fuzzy, you can follow the following steps:

  1. Import the necessary libraries:
import numpy as np
import skfuzzy as fuzz
from skfuzzy import control as ctrl
  1. Define the input and output variables using ctrl.Antecedent() and ctrl.Consequent() methods:
# Input variable
length = ctrl.Antecedent(np.arange(0, 11, 1), 'length')

# Output variable
width = ctrl.Consequent(np.arange(0, 11, 1), 'width')
  1. Define the membership functions for the input and output variables using the methods available in Scikit-Fuzzy:
# 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])
  1. Create a CTRL.Rule() object by defining the antecedent and consequent variables using the methods available in Scikit-Fuzzy:
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".

  1. Create a control simulation by setting the control system and inputs using Simulation() and input['variable_name'] methods:
# Create a control simulation object
width_simulation = ctrl.ControlSystemSimulation(width_control)

# Provide input to the control system
width_simulation.input['length'] = 6
  1. Obtain the output of the simulation using the compute() method:
# 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.