Esempio n. 1
0
def test_when_then_rule_cuts_output_at_input_membership():
    small_mf = Gaussian(150, 4)
    cheap_center = 175
    cheap_mf = Triangle(150, cheap_center, 200)
    input_variable = FuzzyVariable("size", {
        "small": FuzzySet(small_mf),
        "large": FuzzySet(Gaussian(180, 4))
    })
    output_variable = FuzzyVariable(
        "clothing cost", {
            "cheap": FuzzySet(cheap_mf),
            "expansive": FuzzySet(Triangle(200, 250, 400))
        })

    rule = when("size", "small").then("clothing cost", "cheap").compile(
        {"size": input_variable}, {"clothing cost": output_variable})

    assert rule({
        "size": 150
    }).membership(cheap_center) == pytest.approx(small_mf(150))
    assert rule({
        "size": 20
    }).membership(cheap_center) == pytest.approx(small_mf(20))
    assert rule({
        "size": 140
    }).membership(cheap_center) == pytest.approx(small_mf(140))
Esempio n. 2
0
def test_r_sets_example():
    #  R Sets is R library

    system = MamdaniSystem.empty()
    system.add_input(
        "service", {
            "poor": Gaussian(0, 1.5),
            "good": Gaussian(5, 1.5),
            "excellent": Gaussian(10, 1.5),
        })
    system.add_input("food", {
        "rancid": Trapezoid(-2, 0, 2, 4),
        "delicious": Trapezoid(7, 9, 11, 13),
    })

    system.add_output(
        "tip", {
            "cheap": Triangle(0, 5, 10),
            "average": Triangle(7.5, 12.5, 17.5),
            "generous": Triangle(15, 20, 25),
        })
    system.add_rule(
        when("service", "poor").or_is("food", "rancid").then("tip", "cheap"))
    system.add_rule(when("service", "good").then("tip", "average"))
    system.add_rule(
        when("service",
             "excellent").or_is("food", "delicious").then("tip", "generous"))

    system_output = system.run({"service": 3, "food": 8}, (0, 25))
    assert system_output["tip"] == pytest.approx(14.89, 0.1)
Esempio n. 3
0
def draw_system(logic, title):
    mf1 = Triangle(0, 3, 6)
    mf2 = Gaussian(4.5, 1)
    mf3 = Triangle(3, 6, 9)

    mf1_or2_or3 = logic.t_conorm(logic.t_conorm(mf1, mf2), mf3)
    mf1_and2_and3 = logic.t_norm(logic.t_norm(mf1, mf2), mf3)

    universe = np.linspace(0, 10, 1000)

    plt.plot(universe, [mf1(x) for x in universe], "--")
    plt.plot(universe, [mf2(x) for x in universe], "--")
    plt.plot(universe, [mf3(x) for x in universe], "--")
    plt.plot(universe, [mf1_or2_or3(x) for x in universe], label="T-Conorm")
    plt.plot(universe, [mf1_and2_and3(x) for x in universe], label="T-Norm")

    plt.xlabel("x")
    plt.ylabel(r"$\mu$(x)")
    plt.title(title)
    plt.legend()
Esempio n. 4
0
from yvain.fuzzy_system import MamdaniSystem, when
from yvain.membership_functions import Gaussian, Trapezoid, Triangle

if __name__ == '__main__':
    system = MamdaniSystem.empty()
    system.add_input(
        "service", {
            "poor": Gaussian(0, 1.5),
            "good": Gaussian(5, 1.5),
            "excellent": Gaussian(10, 1.5),
        })
    system.add_input("food", {
        "rancid": Trapezoid(-2, 0, 2, 4),
        "delicious": Trapezoid(7, 9, 11, 13),
    })

    system.add_output(
        "tip", {
            "cheap": Triangle(0, 5, 10),
            "average": Triangle(7.5, 12.5, 17.5),
            "generous": Triangle(15, 20, 25),
        })
    system.add_rule(
        when("service", "poor").or_is("food", "rancid").then("tip", "cheap"))
    system.add_rule(when("service", "good").then("tip", "average"))
    system.add_rule(
        when("service",
             "excellent").or_is("food", "delicious").then("tip", "generous"))

    print(system.run({"service": 3, "food": 8}, (0, 25)))
Esempio n. 5
0
from docs.source.plots.membership_functions.membership_plot import draw_mf
from yvain.membership_functions import Gaussian
import matplotlib.pyplot as plt

if __name__ == '__main__':
    mu, sigma = 5, 2
    draw_mf(Gaussian(mu=5, sigma=2), 0, 10, f"Gaussian(mu={mu}, sigma={sigma})")
    plt.vlines(mu, ymin=0, ymax=1, colors="gray",
               linestyles="--")
Esempio n. 6
0
import matplotlib.pyplot as plt
import numpy as np

from yvain.logical_systems import Zadeh
from yvain.membership_functions import Triangle, Gaussian

if __name__ == '__main__':
    mf1 = Triangle(0, 3, 6)
    mf2 = Gaussian(4.5, 1)
    mf3 = Triangle(3, 6, 9)

    logic = Zadeh()
    mf1_or2_or3 = logic.t_conorm(logic.t_conorm(mf1, mf2), mf3)
    mf1_and2_and3 = logic.t_norm(logic.t_norm(mf1, mf2), mf3)

    universe = np.linspace(0, 10, 1000)

    plt.subplot(2, 1, 1)
    plt.plot(universe, [mf1(x) for x in universe])
    plt.plot(universe, [mf2(x) for x in universe])
    plt.plot(universe, [mf3(x) for x in universe])

    plt.ylabel(r"$\mu$(x)")

    plt.subplot(2, 1, 2)
    plt.plot(universe, [mf1(x) for x in universe], "--")
    plt.plot(universe, [mf2(x) for x in universe], "--")
    plt.plot(universe, [mf3(x) for x in universe], "--")
    plt.plot(universe, [mf1_or2_or3(x) for x in universe], label="T-Conorm")
    plt.plot(universe, [mf1_and2_and3(x) for x in universe], label="T-Norm")
Esempio n. 7
0
import matplotlib.pyplot as plt
import numpy as np

from yvain.logical_systems import Zadeh
from yvain.membership_functions import Gaussian

if __name__ == '__main__':
    mf = Gaussian(4.5, 1)

    logic = Zadeh()
    mf_negated = logic.complement(mf)

    universe = np.linspace(0, 10, 1000)
    plt.plot(universe, [mf(x) for x in universe], label="gaussian mf")
    plt.plot(universe, [mf_negated(x) for x in universe], "--", label="complementary mf")

    plt.xlabel("x")
    plt.ylabel(r"$\mu$(x)")
    plt.legend()
Esempio n. 8
0
from yvain.logical_systems import *
from yvain.membership_functions import Triangle, Gaussian
import pytest

_TRIANGLE_UNIVERSE = [
    Triangle(0, 5, 10),
    Triangle(5, 10, 15),
    Triangle(10, 15, 20)
]

_GAUSSIAN_UNIVERSE = [Gaussian(5, 2), Gaussian(7, 1), Gaussian(10, 4)]

_LOGICS = [
    Zadeh(),
    Drastic(),
    Product(),
    Lukasiewicz(),
    Fodor(),
    Frank(2),
    ShweizerSklar(2),
    Yager(2),
    Dombi(2)
]


@pytest.mark.parametrize("logic", _LOGICS)
def test_demorgan_triple(logic):
    ors = _TRIANGLE_UNIVERSE[0]
    ands = logic.complement(_TRIANGLE_UNIVERSE[0])

    for mf in _TRIANGLE_UNIVERSE[1:]: