Skip to main content

Pacote de inferência fuzzy

Project description

Table of Contents

To-Do

  • implementar Cauchyset [Done]
    1. Try with many codes []
    2. 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]
    1. Verify the Tnorm product
    2. Create code example of Tnorm product
    3. Verify the Tconorm probabilistic sum
    4. Create code example of probabilistic sum
    5. verify the Tnorm min max
    6. 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 fuzzysets
pip install git+https://github.com/thigs0/fuzzysets.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)

TriangularFuzzyNumber

>>> 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")

CauchyFuzzyNumber

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)

CauchyFuzzyNumbertnorm

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


Download files

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

Source Distribution

initfuzzy-0.0.1.tar.gz (10.2 kB view details)

Uploaded Source

Built Distribution

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

initfuzzy-0.0.1-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

Details for the file initfuzzy-0.0.1.tar.gz.

File metadata

  • Download URL: initfuzzy-0.0.1.tar.gz
  • Upload date:
  • Size: 10.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for initfuzzy-0.0.1.tar.gz
Algorithm Hash digest
SHA256 c420ec729daecf0842ea0d18b164f0389263063beac79ec6c3a5e309a26c5ea3
MD5 22398414c2ee3168abcf7bf7acc4b76b
BLAKE2b-256 bca45ceabcf91cc512b1576358d0799ca756e8bf43cfe6df424e7b724ee1f54a

See more details on using hashes here.

File details

Details for the file initfuzzy-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: initfuzzy-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 4.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for initfuzzy-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 128772d200880e52ed901f8ff9b295f9887a99711e34abca0502f5e98e400b28
MD5 65d2d5b256157b7553ba58a30e4d5a7a
BLAKE2b-256 8f9b87ac355264bcd3f7db09f620a0731e73304b819740834c5bbf4db8df5c83

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