def test_get_alpha_cut(fn_a: FuzzyNumber): assert fn_a.get_alpha_cut(0) == IntervalFactory.two_values(1, 3) assert fn_a.get_alpha_cut(0.25) == IntervalFactory.two_values(1.25, 2.75) assert fn_a.get_alpha_cut(0.5) == IntervalFactory.two_values(1.5, 2.5) assert fn_a.get_alpha_cut(0.75) == IntervalFactory.two_values(1.75, 2.25) assert fn_a.get_alpha_cut(1) == IntervalFactory.two_values(2, 2)
def test_creation_errors(): with pytest.raises(TypeError, match="must be a list"): FuzzyNumber(1, []) with pytest.raises(TypeError, match="must be a list"): FuzzyNumber([], 1) with pytest.raises( ValueError, match="Lists `alphas` and `alpha_cuts` must be of same length."): FuzzyNumber([1, 2, 3], [1, 2]) with pytest.raises(ValueError, match="All elements of `alphas` must be from range"): FuzzyNumber([0, 0.5, 1, 1.1], [None] * 4) with pytest.raises(ValueError, match="All elements of `alphas` must be int or float"): FuzzyNumber([0, 0.5, 1, "1.1"], [None] * 4) with pytest.raises(ValueError, match="Values in `alphas` are not unique"): FuzzyNumber([0, 0.5, 1, 0.5], [None] * 4) with pytest.raises(ValueError, match="`alphas` must contain both 0 and 1 alpha value"): FuzzyNumber([0, 0.5, 0.9], [None] * 3) with pytest.raises(ValueError, match="`alphas` must contain both 0 and 1 alpha value"): FuzzyNumber([0.1, 0.5, 1], [None] * 3) with pytest.raises(TypeError, match="All elements of `alpha_cuts` must be Interval"): FuzzyNumber([0, 1], [IntervalFactory.two_values(0, 1), 5]) with pytest.raises( ValueError, match= "Interval on lower alpha level has to contain the higher level alpha cuts" ): FuzzyNumber([0, 1], [ IntervalFactory.two_values(0, 1), IntervalFactory.two_values(2, 2) ]) with pytest.raises(ValueError, match="The fuzzy number is invalid"): FuzzyNumberFactory.triangular(5, 4, 3) with pytest.raises(ValueError, match="The fuzzy number is invalid"): FuzzyNumberFactory.trapezoidal(5, 1, 4, 3)
def test_get_alpha_cut_values(): assert FuzzyNumber.get_alpha_cut_values( 6, precision=4) == [0, 0.2, 0.4, 0.6, 0.8, 1] assert FuzzyNumber.get_alpha_cut_values(2, precision=4) == [0, 1] assert FuzzyNumber.get_alpha_cut_values(11, precision=8) == [ 0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1 ] with pytest.raises( ValueError, match="`number_of_cuts` has to be integer and higher than 1"): FuzzyNumber.get_alpha_cut_values("str", precision=2) with pytest.raises( ValueError, match="`number_of_cuts` has to be integer and higher than 1"): FuzzyNumber.get_alpha_cut_values(5.0, precision=2) with pytest.raises( ValueError, match="`number_of_cuts` has to be integer and higher than 1"): FuzzyNumber.get_alpha_cut_values(1, precision=2)
def test_membership(fn_a: FuzzyNumber, fn_d: FuzzyNumber, fn_e: FuzzyNumber): diff = 0.000000001 assert fn_a.membership(0) == 0 assert fn_a.membership(0.999) == 0 assert fn_a.membership(3.001) == 0 assert fn_a.membership(99) == 0 assert fn_a.membership(2) == 1 assert fn_a.membership(1.5) == 0.5 assert fn_a.membership(2.5) == 0.5 assert fn_a.membership(1.25) == 0.25 assert fn_a.membership(1.75) == 0.75 assert fn_a.membership(2.25) == 0.75 assert fn_a.membership(2.75) == 0.25 assert fn_d.membership(2.5) == 1 assert fn_d.membership(2) == 1 assert fn_d.membership(3) == 1 assert fn_d.membership(1) == 0 assert fn_d.membership(4) == 0 assert fn_e.membership(1.5).membership == pytest.approx(0.5, diff) assert fn_e.membership(2.5).membership == pytest.approx(0.5, diff)
def test_str(i_a: FuzzyNumber): assert isinstance(i_a.__str__(), str)
import math from FuzzyMath.class_interval import Interval from FuzzyMath.class_fuzzy_number import FuzzyNumber # define two fuzzy numbers a = FuzzyNumber(alphas=[0, 0.5, 1], alpha_cuts=[ Interval.infimum_supremum(1, 3), Interval.infimum_supremum(1.5, 2.5), Interval.infimum_supremum(2, 2) ]) b = FuzzyNumber(alphas=[0, 0.3, 0.6, 1], alpha_cuts=[ Interval.infimum_supremum(0, 4), Interval.infimum_supremum(1, 3), Interval.infimum_supremum(1, 3), Interval.infimum_supremum(2, 2) ]) # one fuzzy number functions print("--- representation a ---") print(a) print(repr(a)) print("\n") print("--- in operator ---") print(f"-1 in a: {-1 in a}") print(f"0 in a: {0 in a}") print(f"1 in a: {1 in a}")