Skip to main content

UPAFuzzySystems package for definition and simulation of Fuzzy Inference Systems for general and control applications.

Project description


PyPI Downloads

UPAFuzzySystems

UPAFuzzySystems library that allows defining Fuzzy Inference Systems for different applications with continuous and discrete universes, it also deploys structures for the simulation of fuzzy control with transfer functions and state space models.

Developed by Dr. Martín Montes Rivera

Installation

For installation, just run the command:

pip install UPAFuzzySystems==0.2.4

Documentation

Here is an example for defining a one-input Mamdani controller, viewing the input and output fuzzy sets, obtaining its behavior surface, and simulating the controller with a transfer function defined by the Python control library.

First, we import the required libraries for working with the transfer function, numpy arrays, and plotting and defining the fuzzy controller.

import control
import numpy as np
import matplotlib.pyplot as plt
import UPAFuzzySystems as UPAfs

After importing the libraries, we define the error input universe. In this case the set of integers in ranges $[-100,100]$.

$$ Error= \lbrace x ∈ Z : -100 ≤ x ≤ 100 \rbrace \tag{1} $$

Then, its fuzzy sets.

$$ NegativeError = trapezoid(x; -100,-100,-50,0) \tag{2} $$

$$ ZeroError = triangle(x; -1,0,1) \tag{3} $$

$$ PositiveError = trapezoid(x; 0,50,100,100) \tag{4} $$

The code shows how to define and plot the input universe and its fuzzy sets with UPAFuzzySystems.

#Input Universe
Error_universe = UPAfs.fuzzy_universe('Error', np.arange(-100,101,1), 'continuous')
Error_universe.add_fuzzyset('negative','trapmf',[-100,-100,-50,0])
Error_universe.add_fuzzyset('zero','trimf',[-1,0,1])
Error_universe.add_fuzzyset('positive','trapmf',[0,50,100,100])
Error_universe.view_fuzzy()

The plot obtained of the input universe is in the figure below:


Similarly, we define the control output universe and its fuzzy sets. In this case defining a set of integers in ranges $[-20,20]$.

$$ Control= \lbrace x ∈ Z : -20 ≤ x ≤ 20 \rbrace \tag{5} $$

Then, its fuzzy sets.

$$ NegativeControl = trapezoid(x; -20,-20,-5,0) \tag{6} $$

$$ ZeroControl = triangle(x; -5,0,5) \tag{7} $$

$$ PositiveControl = trapezoid(x; 0,5,20,20) \tag{8} $$

The code shows how to define and plot the output universe and its fuzzy sets with UPAFuzzySystems.

#Output Universe
Control_universe = UPAfs.fuzzy_universe('Control', np.arange(-20,21,1), 'continuous')
Control_universe.add_fuzzyset('negative','trapmf',[-20,-20,-5,0])
Control_universe.add_fuzzyset('zero','trimf',[-5,-0,5])
Control_universe.add_fuzzyset('positive','trapmf',[0,5,20,20])
Control_universe.view_fuzzy()

The plot obtained of the output universe is in the figure below:


After defining error and control universes we define which is the premise and the consequence. Then we specify the rules of the inference system.

$$ NegativeError \rightarrow NegativeControl $$ $$ ZerroError \rightarrow ZeroControl $$ $$ PositiveError \rightarrow PositiveControl $$

The code below shows the how to configure a Mamdani inference system with UPAFuzzySystems for defining its premise, consequence, and rules. Additionally, we include the expected response of the inference system through its surface.

 
#Defining Rules and Building Inference System
Mamdani1 = UPAfs.inference_system('Mamdani')
Mamdani1.add_premise(Error_universe)
Mamdani1.add_consequence(Control_universe)
Mamdani1.add_rule([['Error','negative']],[],[['Control','negative']])
Mamdani1.add_rule([['Error','zero']],[],[['Control','zero']])
Mamdani1.add_rule([['Error','positive']],[],[['Control','positive']])

Mamdani1.configure('Mamdani')
Mamdani1.build()

#Testing Surface
error_values = np.arange(-100,100.1,0.1)
Mamdani1.surface_fuzzy_system([error_values])

The figure below shows the obtained surface for the inference system.


After defining the inference system we define plant transfer function for controling the position in a DC motor in (9).

$$ TF(s) = \frac{K}{s*((Js+b)*(Ls+R)+K^2)} \tag{9} $$

The code below specify the transfer function, its parameters and creates an input step for making the controller to folow a reference of 45º degrees.

#Defining the system to control (Position of a DC motor)

J = 3.2284E-6
b = 3.5077E-6
K = 0.0274
R = 4
L = 2.75E-6
te = 1.0
ns = 500
T=np.linspace(0,te,ns)
Input = [(np.radians(45)*min((t-0.25)/0.005,1)) if t> 0.25 else 0 for t in T]

s = control.TransferFunction.s 
TF = K/(s*((J*s+b)*(L*s+R)+K**2))

Then we configure the controller for working with the transfer function using UPAFuzzySystems.

# Building the Controller and obtaining system blocks for simulation
Mamdani1FuzzController = UPAfs.fuzzy_controller(Mamdani1,typec='Fuzzy1',tf=TF,DT = T[1])
Mamdani1FuzzController.build()
Mamdani1FuzzControllerBlock = Mamdani1FuzzController.get_controller()
Mamdani1FuzzSystemBlock = Mamdani1FuzzController.get_system()

Once obtained the blocks for simulation, you can simulate using the input_output_response method of the Python control library. Finally, we plot the obtained results.

# Performing simulation and showing results

T, Theta = control.input_output_response(Mamdani1FuzzSystemBlock,T,Input,0)

plt.plot(T,Theta)
plt.plot(T,Input)
plt.show()

The results obtained for controlling position of a DC motor with a Mamdani fuzzy controller defined with UPAFuzzySystems are in the figure below.


For more details and other controllers check out the Jupyter Notebook notebook, PDF or HTML examples for using the UPAFuzzySystems library.

Jupyter notebook

PDF

HTML

Other examples

Please cite as:

Have you found this software useful for your research? Please cite it as:

Montes Rivera, M.; Olvera-Gonzalez, E.; Escalante-Garcia, N. UPAFuzzySystems: A Python Library for Control and Simulation with Fuzzy Inference Systems. Machines 2023, 11, 572. https://doi.org/10.3390/machines11050572

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

UPAFuzzySystems-0.2.4.tar.gz (13.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

UPAFuzzySystems-0.2.4-py3-none-any.whl (11.0 kB view details)

Uploaded Python 3

File details

Details for the file UPAFuzzySystems-0.2.4.tar.gz.

File metadata

  • Download URL: UPAFuzzySystems-0.2.4.tar.gz
  • Upload date:
  • Size: 13.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.9

File hashes

Hashes for UPAFuzzySystems-0.2.4.tar.gz
Algorithm Hash digest
SHA256 468eab22f35f0996862143a0fd4847897f379317fd00b7a6070fc1baa23038b5
MD5 903d8ba1e2a22089237bbdd76e0541e1
BLAKE2b-256 74c179826b635af1ad023aff2d132b8175363dfbea387ca848064aa868170033

See more details on using hashes here.

File details

Details for the file UPAFuzzySystems-0.2.4-py3-none-any.whl.

File metadata

File hashes

Hashes for UPAFuzzySystems-0.2.4-py3-none-any.whl
Algorithm Hash digest
SHA256 ad29ec990d0d6e2592ecb2a888c3572f53ab2afbbe14c39b8827b059a68d82aa
MD5 e73a648252a63e8b166fcea85d8542b8
BLAKE2b-256 fa1ea9e7aa91b0721c52a7bbdb5e0c7fa612abf56b5fcbb5bbaf49b829aee5b3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page