<h1 align="center">
<img src="https://github.com/UniversidadPolitecnicaAguascalientes/UPAFuzzySystems/raw/main/upa.png" width="300">
</h1><br>
[![PyPI Downloads](https://img.shields.io/pypi/dm/UPAFuzzySystems.svg?label=PyPI%20downloads)](
https://pypi.org/project/UPAFuzzySystems/)
# 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.
```python
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.
```python
#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:
<h1 align="center">
<img src="https://github.com/UniversidadPolitecnicaAguascalientes/UPAFuzzySystems/raw/main/InputUniverse.png" width="600">
</h1><br>
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.
```python
#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:
<h1 align="center">
<img src="https://github.com/UniversidadPolitecnicaAguascalientes/UPAFuzzySystems/raw/main/OutputUniverse.png" width="600">
</h1><br>
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.
```python
#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.
<h1 align="center">
<img src="https://github.com/UniversidadPolitecnicaAguascalientes/UPAFuzzySystems/raw/main/SurfaceResponse.png" width="600">
</h1><br>
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.
```python
#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.
```python
# 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.
```python
# 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.
<h1 align="center">
<img src="https://github.com/UniversidadPolitecnicaAguascalientes/UPAFuzzySystems/raw/main/ControllerResponse.png" width="600">
</h1><br>
For more details and other controllers check out the Jupyter Notebook notebook, PDF or HTML examples for using the UPAFuzzySystems library.
[Jupyter notebook](https://github.com/UniversidadPolitecnicaAguascalientes/UPAFuzzySystems/blob/main/examples/Examples-UPAFuzzySystems.ipynb)
[PDF](https://github.com/UniversidadPolitecnicaAguascalientes/UPAFuzzySystems/blob/main/examples/Examples-UPAFuzzySystems.pdf)
[HTML](https://github.com/UniversidadPolitecnicaAguascalientes/UPAFuzzySystems/blob/main/examples/Examples-UPAFuzzySystems.html)
[Other examples](https://github.com/UniversidadPolitecnicaAguascalientes/UPAFuzzySystems/tree/main/examples/other%20examples)
# 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
Raw data
{
"_id": null,
"home_page": "https://github.com/UniversidadPolitecnicaAguascalientes/UPAFuzzySystems/",
"name": "UPAFuzzySystems",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "python,fuzzy logic,fuzzy control,fuzzy inference systems,artificial intelligence",
"author": "Dr. Martin Montes Rivera (Universidad Polit\u00e9cnica de Aguascalientes)",
"author_email": "<martin.montes@upa.edu.mx>",
"download_url": "https://files.pythonhosted.org/packages/74/c1/79826b635af1ad023aff2d132b8175363dfbea387ca848064aa868170033/UPAFuzzySystems-0.2.4.tar.gz",
"platform": null,
"description": "<h1 align=\"center\">\r\n<img src=\"https://github.com/UniversidadPolitecnicaAguascalientes/UPAFuzzySystems/raw/main/upa.png\" width=\"300\">\r\n</h1><br>\r\n\r\n[![PyPI Downloads](https://img.shields.io/pypi/dm/UPAFuzzySystems.svg?label=PyPI%20downloads)](\r\nhttps://pypi.org/project/UPAFuzzySystems/)\r\n\r\n# UPAFuzzySystems\r\n\r\nUPAFuzzySystems 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.\r\n\r\n***Developed by Dr. Mart\u00edn Montes Rivera***\r\n\r\n# Installation\r\nFor installation, just run the command:\r\n\r\n```\r\npip install UPAFuzzySystems==0.2.4\r\n```\r\n# Documentation\r\n\r\nHere 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.\r\n\r\nFirst, we import the required libraries for working with the transfer function, numpy arrays, and plotting and defining the fuzzy controller.\r\n\r\n```python\r\nimport control\r\nimport numpy as np\r\nimport matplotlib.pyplot as plt\r\nimport UPAFuzzySystems as UPAfs\r\n```\r\nAfter importing the libraries, we define the error input universe. In this case the set of integers in ranges $[-100,100]$.\r\n\r\n$$\r\nError= \\lbrace x \u2208 Z : -100 \u2264 x \u2264 100 \\rbrace \\tag{1}\r\n$$\r\n\r\nThen, its fuzzy sets. \r\n\r\n$$\r\nNegativeError = trapezoid(x; -100,-100,-50,0) \\tag{2}\r\n$$\r\n\r\n$$\r\nZeroError = triangle(x; -1,0,1) \\tag{3}\r\n$$\r\n\r\n$$\r\nPositiveError = trapezoid(x; 0,50,100,100) \\tag{4}\r\n$$\r\n\r\nThe code shows how to define and plot the input universe and its fuzzy sets with UPAFuzzySystems.\r\n```python\r\n#Input Universe\r\nError_universe = UPAfs.fuzzy_universe('Error', np.arange(-100,101,1), 'continuous')\r\nError_universe.add_fuzzyset('negative','trapmf',[-100,-100,-50,0])\r\nError_universe.add_fuzzyset('zero','trimf',[-1,0,1])\r\nError_universe.add_fuzzyset('positive','trapmf',[0,50,100,100])\r\nError_universe.view_fuzzy()\r\n```\r\nThe plot obtained of the input universe is in the figure below:\r\n\r\n<h1 align=\"center\">\r\n<img src=\"https://github.com/UniversidadPolitecnicaAguascalientes/UPAFuzzySystems/raw/main/InputUniverse.png\" width=\"600\">\r\n</h1><br>\r\n\r\nSimilarly, we define the control output universe and its fuzzy sets. In this case defining a set of integers in ranges $[-20,20]$.\r\n\r\n$$\r\nControl= \\lbrace x \u2208 Z : -20 \u2264 x \u2264 20 \\rbrace \\tag{5}\r\n$$\r\n\r\nThen, its fuzzy sets. \r\n\r\n$$\r\nNegativeControl = trapezoid(x; -20,-20,-5,0) \\tag{6}\r\n$$\r\n\r\n$$\r\nZeroControl = triangle(x; -5,0,5) \\tag{7}\r\n$$\r\n\r\n$$\r\nPositiveControl = trapezoid(x; 0,5,20,20) \\tag{8}\r\n$$\r\n\r\nThe code shows how to define and plot the output universe and its fuzzy sets with UPAFuzzySystems.\r\n\r\n```python\r\n#Output Universe\r\nControl_universe = UPAfs.fuzzy_universe('Control', np.arange(-20,21,1), 'continuous')\r\nControl_universe.add_fuzzyset('negative','trapmf',[-20,-20,-5,0])\r\nControl_universe.add_fuzzyset('zero','trimf',[-5,-0,5])\r\nControl_universe.add_fuzzyset('positive','trapmf',[0,5,20,20])\r\nControl_universe.view_fuzzy()\r\n```\r\nThe plot obtained of the output universe is in the figure below:\r\n\r\n<h1 align=\"center\">\r\n<img src=\"https://github.com/UniversidadPolitecnicaAguascalientes/UPAFuzzySystems/raw/main/OutputUniverse.png\" width=\"600\">\r\n</h1><br>\r\n\r\nAfter defining error and control universes we define which is the premise and the consequence. Then we specify the rules of the inference system.\r\n\r\n$$\r\nNegativeError \\rightarrow NegativeControl\r\n$$\r\n$$\r\nZerroError \\rightarrow ZeroControl\r\n$$\r\n$$\r\nPositiveError \\rightarrow PositiveControl\r\n$$\r\n\r\nThe 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.\r\n\r\n```python\r\n \r\n#Defining Rules and Building Inference System\r\nMamdani1 = UPAfs.inference_system('Mamdani')\r\nMamdani1.add_premise(Error_universe)\r\nMamdani1.add_consequence(Control_universe)\r\nMamdani1.add_rule([['Error','negative']],[],[['Control','negative']])\r\nMamdani1.add_rule([['Error','zero']],[],[['Control','zero']])\r\nMamdani1.add_rule([['Error','positive']],[],[['Control','positive']])\r\n\r\nMamdani1.configure('Mamdani')\r\nMamdani1.build()\r\n\r\n#Testing Surface\r\nerror_values = np.arange(-100,100.1,0.1)\r\nMamdani1.surface_fuzzy_system([error_values])\r\n```\r\nThe figure below shows the obtained surface for the inference system.\r\n\r\n<h1 align=\"center\">\r\n<img src=\"https://github.com/UniversidadPolitecnicaAguascalientes/UPAFuzzySystems/raw/main/SurfaceResponse.png\" width=\"600\">\r\n</h1><br>\r\n\r\nAfter defining the inference system we define plant transfer function for controling the position in a DC motor in (9). \r\n\r\n\r\n$$\r\nTF(s) = \\frac{K}{s*((Js+b)*(Ls+R)+K^2)} \\tag{9} \r\n$$ \r\n\r\nThe code below specify the transfer function, its parameters and creates an input step for making the controller to folow a reference of 45\u00ba degrees.\r\n\r\n```python\r\n#Defining the system to control (Position of a DC motor)\r\n\r\nJ = 3.2284E-6\r\nb = 3.5077E-6\r\nK = 0.0274\r\nR = 4\r\nL = 2.75E-6\r\nte = 1.0\r\nns = 500\r\nT=np.linspace(0,te,ns)\r\nInput = [(np.radians(45)*min((t-0.25)/0.005,1)) if t> 0.25 else 0 for t in T]\r\n\r\ns = control.TransferFunction.s \r\nTF = K/(s*((J*s+b)*(L*s+R)+K**2))\r\n```\r\nThen we configure the controller for working with the transfer function using UPAFuzzySystems.\r\n\r\n```python\r\n# Building the Controller and obtaining system blocks for simulation\r\nMamdani1FuzzController = UPAfs.fuzzy_controller(Mamdani1,typec='Fuzzy1',tf=TF,DT = T[1])\r\nMamdani1FuzzController.build()\r\nMamdani1FuzzControllerBlock = Mamdani1FuzzController.get_controller()\r\nMamdani1FuzzSystemBlock = Mamdani1FuzzController.get_system()\r\n\r\n```\r\nOnce 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.\r\n\r\n```python\r\n# Performing simulation and showing results\r\n\r\nT, Theta = control.input_output_response(Mamdani1FuzzSystemBlock,T,Input,0)\r\n\r\nplt.plot(T,Theta)\r\nplt.plot(T,Input)\r\nplt.show()\r\n\r\n\r\n```\r\n\r\nThe results obtained for controlling position of a DC motor with a Mamdani fuzzy controller defined with UPAFuzzySystems are in the figure below.\r\n\r\n<h1 align=\"center\">\r\n<img src=\"https://github.com/UniversidadPolitecnicaAguascalientes/UPAFuzzySystems/raw/main/ControllerResponse.png\" width=\"600\">\r\n</h1><br>\r\n\r\nFor more details and other controllers check out the Jupyter Notebook notebook, PDF or HTML examples for using the UPAFuzzySystems library.\r\n\r\n[Jupyter notebook](https://github.com/UniversidadPolitecnicaAguascalientes/UPAFuzzySystems/blob/main/examples/Examples-UPAFuzzySystems.ipynb)\r\n\r\n[PDF](https://github.com/UniversidadPolitecnicaAguascalientes/UPAFuzzySystems/blob/main/examples/Examples-UPAFuzzySystems.pdf)\r\n\r\n[HTML](https://github.com/UniversidadPolitecnicaAguascalientes/UPAFuzzySystems/blob/main/examples/Examples-UPAFuzzySystems.html)\r\n\r\n[Other examples](https://github.com/UniversidadPolitecnicaAguascalientes/UPAFuzzySystems/tree/main/examples/other%20examples)\r\n\r\n# Please cite as:\r\nHave you found this software useful for your research? Please cite it as:\r\n\r\nMontes 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\r\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "UPAFuzzySystems package for definition and simulation of Fuzzy Inference Systems for general and control applications.",
"version": "0.2.4",
"project_urls": {
"Homepage": "https://github.com/UniversidadPolitecnicaAguascalientes/UPAFuzzySystems/"
},
"split_keywords": [
"python",
"fuzzy logic",
"fuzzy control",
"fuzzy inference systems",
"artificial intelligence"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "fa1ea9e7aa91b0721c52a7bbdb5e0c7fa612abf56b5fcbb5bbaf49b829aee5b3",
"md5": "e73a648252a63e8b166fcea85d8542b8",
"sha256": "ad29ec990d0d6e2592ecb2a888c3572f53ab2afbbe14c39b8827b059a68d82aa"
},
"downloads": -1,
"filename": "UPAFuzzySystems-0.2.4-py3-none-any.whl",
"has_sig": false,
"md5_digest": "e73a648252a63e8b166fcea85d8542b8",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 11015,
"upload_time": "2023-06-27T20:23:16",
"upload_time_iso_8601": "2023-06-27T20:23:16.707911Z",
"url": "https://files.pythonhosted.org/packages/fa/1e/a9e7aa91b0721c52a7bbdb5e0c7fa612abf56b5fcbb5bbaf49b829aee5b3/UPAFuzzySystems-0.2.4-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "74c179826b635af1ad023aff2d132b8175363dfbea387ca848064aa868170033",
"md5": "903d8ba1e2a22089237bbdd76e0541e1",
"sha256": "468eab22f35f0996862143a0fd4847897f379317fd00b7a6070fc1baa23038b5"
},
"downloads": -1,
"filename": "UPAFuzzySystems-0.2.4.tar.gz",
"has_sig": false,
"md5_digest": "903d8ba1e2a22089237bbdd76e0541e1",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 13690,
"upload_time": "2023-06-27T20:23:18",
"upload_time_iso_8601": "2023-06-27T20:23:18.278307Z",
"url": "https://files.pythonhosted.org/packages/74/c1/79826b635af1ad023aff2d132b8175363dfbea387ca848064aa868170033/UPAFuzzySystems-0.2.4.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-06-27 20:23:18",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "UniversidadPolitecnicaAguascalientes",
"github_project": "UPAFuzzySystems",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "upafuzzysystems"
}