def test_eq(self, low, high, res): """This also tests Set.array(). This test can massively slow down hypothesis with even reasonably large/small values. """ assume(low < high) # to avoid MemoryError and runs that take forever.. assume(high - low <= 10) D1 = Domain("1", low, high, res=res) D1.s1 = fun.bounded_linear(low, high) D2 = Domain("2", low, high, res=res) D2.s2 = Set(fun.bounded_linear(low, high)) assert D1.s1 == D2.s2
def test_rating(): """Tom is surveying restaurants. He doesn't need fancy logic but rather uses a simple approach with weights. He went into a small, dirty bar that served some really good drink and food that wasn't nicely arranged but still yummmy. He rates the different factors on a scale from 1 to 10, uses a bounded_linear function to normalize over [0,1] and passes both the weights (how much each aspect should weigh in total) and the domain as parameters into weighted_sum. However, he can't just use Domain(value) because that would return a dict of memberships, instead he uses Domain.min(value) which returns the minimum of all memberships no matter how many sets there are. He creates a dict of membership values corresponding to the weights and passes that into the parametrized weighted_sum func as argument to get the final rating for this restaurant. """ R = Domain("rating", 1, 10, res=0.1) R.norm = bounded_linear(1, 10) weights = {"beverage": 0.3, "atmosphere": 0.2, "looks": 0.2, "taste": 0.3} w_func = weighted_sum(weights=weights, target_d=R) ratings = { "beverage": R.min(9), "atmosphere": R.min(5), "looks": R.min(4), "taste": R.min(8), } assert w_func(ratings) == 6.9
def test_basics(self): D = Domain("d", 0, 10) assert D._name == "d" assert D._low == 0 assert D._high == 10 assert D._res == 1 x = Set(lambda x: 1) D.s = x assert D.s == x assert D._sets == {"s": x} R = D(3) assert R == {D.s: 1}
def simple(): d = Domain("simple", 0, 10) d.low = S(0, 1) d.high = R(8, 10) return d
def temp(): d = Domain("temperature", -100, 100, res=0.1) # in Celsius d.cold = S(0, 15) # sic d.hot = Set(R(10, 30)) # sic d.warm = ~d.cold & ~d.hot return d
def test_complement(self): D = Domain("d", 0, 10, res=0.1) D.s1 = Set(fun.bounded_linear(3, 12)) D.s2 = ~~D.s1 assert all(np.isclose(D.s1.array(), D.s2.array()))
def test_sub_super_set(self): D = Domain("d", 0, 10, res=0.1) D.s = Set(fun.bounded_linear(3, 12)) D.x = D.s.normalized() assert D.x >= D.s assert D.s <= D.x
def test_normalized(self): D = Domain("d", 0, 10, res=0.1) D.s = Set(fun.bounded_linear(3, 12)) D.x = D.s.normalized() D.y = D.x.normalized() assert D.x == D.y
import numpy as np import helper import copy import fuzzylogic as fuzzy from fuzzylogic.classes import Domain import fuzzylogic.functions as ff DEGTORAD = math.pi/180.0 RADTODEG = 180.0/math.pi # Defining the main fuzzy domains Ang = Domain("diff_theta", -180, 180, res=1) Ang.zero = ff.triangular(-3,3) Ang.p_small = ff.trapezoid(0, 3, 5, 10) Ang.n_small = ff.trapezoid(-10,-5,-3,0) Ang.small = Ang.p_small | Ang.n_small Ang.p_med = ff.trapezoid(5, 10, 40, 50) Ang.n_med = ff.trapezoid(-50,-40,-10,-5) Ang.med = Ang.p_med | Ang.n_med Ang.p_big = ff.trapezoid(40, 50, 80, 100) Ang.n_big = ff.trapezoid(-100, -80, -50, -40)
from pathlib import Path src = str((Path(__file__).parent / "../src").resolve()) sys.path.insert(0, src) import numpy as np from fuzzylogic.classes import Domain from fuzzylogic.classes import Rule from fuzzylogic.classes import Set from fuzzylogic.functions import R from fuzzylogic.functions import S from fuzzylogic.functions import trapezoid temp = Domain("Temperatur", -30, 100, res=0.0001) # ,res=0.1) temp.kalt = S(-10, 30) temp.heiß = R(30, 70) temp.mittel = ~temp.heiß & ~temp.kalt tan = Domain("tandelta", 0, 1.3, res=0.0001) # ,res=0.1) tan.klein = S(0.1, 0.5) tan.groß = R(0.5, 0.9) tan.mittel = ~tan.groß & ~tan.klein gef = Domain("Gefahrenbewertung", -0.5, 1.5, res=0.0001) # ,res=0.1) gef.klein = trapezoid(-0.5, 0, 0, 0.5) gef.groß = trapezoid(0.5, 1, 1, 1.5) gef.mittel = trapezoid(0, 0.5, 0.5, 1) R1 = Rule({(temp.kalt, tan.klein): gef.klein})