Pacote de inferência fuzzy
Project description
Table of Contents
To-Do
- implementar Cauchyset [Done]
- Try with many codes []
- create a code with example at README []
- implementar tnorma( DeMorgan tripdrástica, produto e soma probabilista, min max ) obs: qualquer definição de uma tnorma -> automaticamente no t-conorma [Done]
- Verify the Tnorm product
- Create code example of Tnorm product
- Verify the Tconorm probabilistic sum
- Create code example of probabilistic sum
- verify the Tnorm min max
- Create code with Tnorm min and Tconorm max
- implementar takagi-sugeno, mandanam -> dados de saída são em dataframe com todas as variáveis
fuzzysets
fuzzysets is a simple Python package that is well suited for interactive experiments with fundamental concepts in fuzzy set theory.
Installation
The package is available on PYPI and can be installed with pip:
pip install initfuzzy
pip install git+https://github.com/thigs0/initfuzzy.git
Notation
In the following sections, code snippets will be highlighted like this.
import fuzzysets as fs
Examples involving a read-evaluate-print loop will show both the Python statement, which will be preceded by a >>> prompt, and its output, if any.
Triangular fuzzy numbers
One of the concepts implemented by fuzzysets is a triangular fuzzy number (TFN), represented with the TriangularFuzzyNumber class.
Each TFN can be uniquely represented as a 3-tuple of real numbers (left, peak, right) (l, n and r below) where:
- peak is the number whose membership degree is 1, that is, the number being modeled
- left (< peak) and right (> peak) determine the fuzzy number's membership function:
- mu(x) = 0, x ∈ (-inf, l) U (r, +inf)
- mu(x) = (x - l) / (n - l), l <= x <= n
- mu(x) = (r - x) / (r - n), n <= x <= r
The TriangularFuzzyNumber class offers a more complex abstraction than that.
We first need to import the fuzzysets package:
>>> import fuzzysets as fs
The class is available through an alias - TFN. The default value of the class is 0:
import matplotlib.pyplot as plt
import numpy as np
from tfn import TriangularFuzzyNumber as tfn
plt.style.use('ggplot')
a = tfn()
b = tfn(3,2,4)
plt.plot(x, a.mu(x), label="Triangular Fuzzy Set (a)", color="red")
plt.plot(x, b.mu(x), label="Cauchy Fuzzy Set (b)", color="blue")
x = np.linspace(-5,10,200)
>>> fs.TFN()
TriangularFuzzyNumber(l=-1.0, n=0.0, r=1.0)
As you can see, the left and right properties are offset by 1 by default. We could create a TFN that models any number like this:
>>> fs.TFN(11.5)
TriangularFuzzyNumber(l=10.5, n=11.5, r=12.5)
It is also possible to set one or both of the other properties:
>>> fs.TFN(10, r=12.8)
TriangularFuzzyNumber(l=9.0, n=10.0, r=12.8)
>>> fs.TFN(10.6, 8, 12)
TriangularFuzzyNumber(l=8.0, n=10.6, r=12.0)
We can also obtain the alpha cut of a TFN:
>>> n
TriangularFuzzyNumber(l=1.0, n=2.0, r=4.0)
>>> n.alpha_cut
AlphaCut(1.0 + alpha * 1.0, 4.0 + alpha * -2.0)
It can be converted to a more user-friendly string than the one above and can compute the interval for a fixed alpha:
>>> cut = n.alpha_cut
>>> str(cut)
'[1.0 + alpha * 1.0, 4.0 + alpha * -2.0]'
>>> cut.for_alpha(0.5)
(1.5, 3.0)
Cauchy fuzzy Set
import matplotlib.pyplot as plt
import numpy as np
import CauchyFuzzyNumber as cfn
a = cfs(1,2,3)
b = cfs(2,4,5)
x = np.linspace(-5,10,200)
plt.plot(x, a.mu(x))
plt.style.use('ggplot')
plt.plot(x, a.mu(x), label="Cauchy Fuzzy Set (a)", color="red")
plt.plot(x, b.mu(x), label="Cauchy Fuzzy Set (b)", color="blue")
Tnorm and Tconorm
Example
import matplotlib.pyplot as plt
import numpy as np
from cauchyfuzzyset import CauchyFuzzySet as cfn
from tfn import TriangularFuzzyNumber as tfn
from tnorm import Tnorm
plt.style.use('ggplot')
c = cfn(1,2,3)
d = cfn(2,4,5)
x = np.linspace(-1,10,200)
sets = [c,d]
colors = ["yellow", "green"]
for i in range(2): plt.plot(x, [sets[i].mu(j) for j in x ], label=f"Fuzzy Set)", color=colors[i])
out = Tnorm("minimum", c,d)
plt.plot(x, [out.tnorm(i) for i in x], "r--", label="Tnorm")
plt.plot(x, [out.tconorm(i) for i in x], "*", label="tconorm", color="gray")
plt.title("tnorm and tconorm about two cauchy fuzzy set")
plt.legend(loc="center left")
plt.savefig('test.png', dpi=300)
Mandani inference method
Example
import numpy as np
from typing import Callable, List, Tuple
from cauchyfuzzyset import CauchyFuzzySet
from tfn import TriangularFuzzyNumber
from tnorm import Tnorm, AND
from inference import MamdaniInference
#heavy of cloths
ml = TriangularFuzzyNumber(0, -20, 20)
l = TriangularFuzzyNumber(30,10, 50)
p = TriangularFuzzyNumber(65, 40, 90)
mp = TriangularFuzzyNumber(90, 75,100)
#dirt cloths
ql = TriangularFuzzyNumber(0, -20, 20)
s = TriangularFuzzyNumber(30, 10, 50)
ms = TriangularFuzzyNumber(70, 40, 100)
es = TriangularFuzzyNumber(100, 80, 120)
# Define regras com funções anônimas ou conjuntos fuzzy
muito_pouco = TriangularFuzzyNumber(10, 0, 20)
pouco = TriangularFuzzyNumber(30, 20, 40)
moderado = TriangularFuzzyNumber(50, 40 ,60)
exagerado = TriangularFuzzyNumber(70, 60 ,80)
maximo = TriangularFuzzyNumber(100, 80 ,120)
#tnorm
p1,p2 = 10,15
ps = [p1, p2]
w1 = AND([ml, ql])
w2 = AND([ml, s])
w3 = AND([ml, ms])
w4 = AND([ml, es])
w5 = AND([l, ql])
w6 = AND([l, s])
w7 = AND([l, ms])
w8 = AND([l, es])
w9 = AND([p, ql])
w10 = AND([p, s])
w11 = AND([p, ms])
w12 = AND([p, es])
w13 = AND([mp, ql])
w14 = AND([mp, s])
w15 = AND([mp, ms])
w16 = AND([mp, es])
# Sistema de inferência
mi = MamdaniInference()
mi.add_rule(antecedent = w1, consequent = muito_pouco)
mi.add_rule(antecedent = w2, consequent = pouco)
mi.add_rule(antecedent = w3, consequent = moderado)
mi.add_rule(antecedent = w4, consequent = moderado)
mi.add_rule(antecedent = w5, consequent = pouco)
mi.add_rule(antecedent = w6, consequent = pouco)
mi.add_rule(antecedent = w7, consequent = moderado)
mi.add_rule(antecedent = w8, consequent = exagerado)
mi.add_rule(antecedent = w9, consequent = moderado)
mi.add_rule(antecedent = w10, consequent = moderado)
mi.add_rule(antecedent = w11, consequent = exagerado)
mi.add_rule(antecedent = w12, consequent = exagerado)
mi.add_rule(antecedent = w13, consequent = moderado)
mi.add_rule(antecedent = w14, consequent = exagerado)
mi.add_rule(antecedent = w15, consequent = maximo)
mi.add_rule(antecedent = w16, consequent = maximo)
# Inferência
saida = mi.infer(ps, np.linspace(0, 100, 1000))
print("Saída defuzzificada:", saida)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file initfuzzy-0.0.2.tar.gz.
File metadata
- Download URL: initfuzzy-0.0.2.tar.gz
- Upload date:
- Size: 17.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2e69cc6a014b1086f8ad35ab09f11bc7e816d5cb8000aa17ba0901c3218c9965
|
|
| MD5 |
c0466f9678131e9d07605981d6487e3c
|
|
| BLAKE2b-256 |
43119ced5f58e32eb0a1c4b1fa3d5067b8d9a12896f0ea0d513b2a14abbe35a6
|
File details
Details for the file initfuzzy-0.0.2-py3-none-any.whl.
File metadata
- Download URL: initfuzzy-0.0.2-py3-none-any.whl
- Upload date:
- Size: 13.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2be31fe934a1bd4a63caf6662bb5ca0d351f4902e555f968f7f4c8bb492bbad3
|
|
| MD5 |
8896c8e1290a2bcaa2caf5479e72ec8d
|
|
| BLAKE2b-256 |
f95a39a27da21c1c96c3efd2051e1f4b1d2257f5d3168dccd1e84a72bcaef1bc
|