def test_abs(): x = phuzzy.Trapezoid(alpha0=[-3, 5], alpha1=[1, 2], name="x", number_of_alpha_levels=3) y = abs(x) print(y.df.values.tolist()) assert np.allclose(y.df.values.tolist(), [[0.0, 0.0, 5.0], [0.5, 0.0, 3.5], [1.0, 1.0, 2.0]]) x = phuzzy.Trapezoid(alpha0=[1, 5], alpha1=[2, 3], name="x", number_of_alpha_levels=3) y = abs(x) print(y.df.values.tolist()) assert np.allclose(y.df.values.tolist(), [[0.0, 1.0, 5.0], [0.5, 1.5, 4.0], [1.0, 2.0, 3.0]]) x = phuzzy.Trapezoid(alpha0=[-5, -1], alpha1=[-3, -2], name="x", number_of_alpha_levels=3) y = abs(x) print(y.df.values.tolist()) assert np.allclose(y.df.values.tolist(), [[0.0, 1.0, 5.0], [0.5, 1.5, 4.0], [1.0, 2.0, 3.0]]) y = x.abs() print(y.df.values.tolist()) assert np.allclose(y.df.values.tolist(), [[0.0, 1.0, 5.0], [0.5, 1.5, 4.0], [1.0, 2.0, 3.0]])
def test_mul(): t = phuzzy.TruncNorm(alpha0=[1, 3], alpha1=[2], number_of_alpha_levels=3, name="t") print(t) print(t.df.values.tolist()) assert len(t.df) == 3 p = phuzzy.Trapezoid(alpha0=[1, 4], alpha1=[2, 3], number_of_alpha_levels=3, name="p") print(p) assert len(p.df) == 3 a = t * p a.name = "t*p" print(a) print(a.df.values.tolist()) assert np.allclose( a.df.values.tolist(), [[0.0, 1.0, 12.0], [0.5, 2.4112937437280677, 8.373647931301177], [1.0, 4.0, 6.0]]) b = t * 1. print(b) print(b.df) print(b.df.values.tolist()) assert np.allclose( b.df.values.tolist(), [[0.0, 1.0, 3.0], [0.5, 1.6075291624853785, 2.392470837514622], [1.0, 2.0, 2.0]]) b = t * 2. print(b) print(b.df) print(b.df.values.tolist()) assert np.allclose( b.df.values.tolist(), [[0.0, 2.0, 6.0], [0.5, 3.215058324970757, 4.784941675029244], [1.0, 4.0, 4.0]]) b = 2. * t print(b) print(b.df) print(b.df.values.tolist()) assert np.allclose( b.df.values.tolist(), [[0.0, 2.0, 6.0], [0.5, 3.215058324970757, 4.784941675029244], [1.0, 4.0, 4.0]]) p = phuzzy.Trapezoid(alpha0=[-1, 4], alpha1=[2, 3], number_of_alpha_levels=3, name="p") z = p * p print(z.df)
def test_poor_mens_alpha_optimization_pow_fuzzy(): p = phuzzy.Trapezoid(alpha0=[-2, 2], alpha1=[-1, 1]) e = phuzzy.Trapezoid(alpha0=[2, 2], alpha1=[2, 2]) y = p**e print(y.df) # mix_mpl(y) # y.plot(show=True) assert np.isclose(y.min(), 0) assert np.isclose(y.max(), 4) assert np.isclose(y.df.iloc[-1][["l", "r"]].max(), 1)
def test_gt(): x = phuzzy.Trapezoid(alpha0=[1, 5], alpha1=[2, 3], name="x", number_of_alpha_levels=3) assert x > -1 assert not x > 2 y = phuzzy.Trapezoid(alpha0=[-5, -1], alpha1=[-3, -2], name="x", number_of_alpha_levels=3) print(y.max(), x.min()) assert x > y
def test_max(): x = phuzzy.Trapezoid(alpha0=[-3, 5], alpha1=[1, 2], name="x", number_of_alpha_levels=3) print(x.max()) assert np.isclose(x.max(), 5) y = phuzzy.Trapezoid(alpha0=[-5, -1], alpha1=[-3, -2], name="x", number_of_alpha_levels=3) print(y.df) print(y.max()) assert np.isclose(y.max(), -1)
def atest_div(): t = phuzzy.TruncNorm(alpha0=[2, 3], alpha1=[], number_of_alpha_levels=15, name="t") print(t) assert len(t.df) == 15 p = phuzzy.Trapezoid(alpha0=[0, 4], alpha1=[2, 3], number_of_alpha_levels=5, name="p") print(p) assert len(p.df) == 5 a = t ** p a = p ** t a.name = "t**p" print(a) print(a.df) mix_mpl(t) mix_mpl(p) mix_mpl(a) t.plot() p.plot() a.plot() print(a.df) # a.df.iloc[10, a.df.columns.get_loc("max")] = 8.5 # a.make_convex() a.name += "!" print(a.df) a.plot(show=True)
def test_alo(): v1 = phuzzy.Triangle(alpha0=[0, 4], alpha1=[1], number_of_alpha_levels=5) v2 = phuzzy.Superellipse(alpha0=[-1, 2.], alpha1=None, m=1.0, n=.5, number_of_alpha_levels=6) v3 = phuzzy.TruncGenNorm(alpha0=[1, 4], alpha1=[2, 3], number_of_alpha_levels=5, beta=3.) v4 = phuzzy.Trapezoid(alpha0=[0, 4], alpha1=[2, 3], number_of_alpha_levels=5) v5 = phuzzy.TruncNorm(alpha0=[1, 3], number_of_alpha_levels=5, name="y") v6 = phuzzy.Triangle(alpha0=[1, 4], alpha1=[3], number_of_alpha_levels=5) obj_function = '-1*((x[0] - 1) ** 2 + (x[1] + .1) ** 2 + .1 - (x[2] + 2) ** 2 - (x[3] - 0.1) ** 2 - (x[4] * x[5]) ** 2)' name = 'Opti_Test' kwargs = { 'var1': v1, 'var2': v2, 'var3': v3, 'var4': v4, 'var5': v5, 'var6': v6, 'obj_function': obj_function, 'name': name } # alo = phuzzy.analysis.alo.Alpha_Level_Optimization(name="test", obj_function=obj_function, vars=[v1, v2, v3, v4, v5, v6]) alo = phuzzy.analysis.alo.Alpha_Level_Optimization(**kwargs) alo.calculation() print(alo)
def test_min(): x = phuzzy.Trapezoid(alpha0=[-3, 5], alpha1=[1, 2], name="x", number_of_alpha_levels=3) print(x.min()) assert np.isclose(x.min(), -3)
def test_poor_mens_alpha_optimization_pow_fuzzy2(): t = phuzzy.TruncNorm(alpha0=[2, 3], alpha1=[], number_of_alpha_levels=5, name="t") p = phuzzy.Trapezoid(alpha0=[0, 4], alpha1=[2, 3], number_of_alpha_levels=5, name="p") # a = t ** p a = p**t a.name = "t**p" print(a.df.values.tolist()) print(a) print(a.df) assert np.allclose( a.df.values.tolist(), [[0.0, 0.0, 64.0], [0.25, 0.125, 52.734375], [0.5, 1.0, 42.875], [0.75, 2.25, 34.328125], [1.0, 4.0, 27.0]]) # mix_mpl(a) # a.plot(show=True) print(a.df.values.tolist()) a = t**p print(a.df.values.tolist()) # mix_mpl(a) # a.plot(show=True) assert np.allclose(a.df.values.tolist(), [[0.0, 1.0, 81.0], [0.25, 1.0, 59.51536637372663], [0.5, 1.0, 52.84832630002708], [0.75, 1.0, 47.58354751779905], [1.0, 1.0, 39.0625]])
def plot_sub(): H = 100. # mm B = 300. # mm fig, axs = plt.subplots(1, 3, dpi=90, facecolor='w', edgecolor='k', figsize=(B / 25.4, H / 25.4)) x = phuzzy.Trapezoid(alpha0=[0, 4], alpha1=[2, 3], number_of_alpha_levels=5) mix_mpl(x) x.plot(ax=axs[0]) y = phuzzy.TruncNorm(alpha0=[1, 3], number_of_alpha_levels=15, name="y") mix_mpl(y) y.plot(ax=axs[1]) z = x - y z.name = "x-y" mix_mpl(z) z.plot(ax=axs[2]) fig.tight_layout() fig.savefig("x-y.png") plt.show()
def atest_sub(): t = phuzzy.TruncNorm(alpha0=[1, 3], alpha1=[2], number_of_alpha_levels=15, name="t") print(t) assert len(t.df) == 15 p = phuzzy.Trapezoid(alpha0=[1, 4], alpha1=[2, 3], number_of_alpha_levels=5, name="p") print(p) assert len(p.df) == 5 a = t - p a.name = "t-p" print(a) print(a.df) mix_mpl(t) mix_mpl(p) mix_mpl(a) t.plot() p.plot() a.plot() print(a.df) a.df.iloc[10, a.df.columns.get_loc("r")] = 1.5 a.make_convex() a.name += "!" print(a.df) a.plot(show=True)
def test_defuzzification(): p = phuzzy.Trapezoid(alpha0=[-1, 6], alpha1=[1, 5], number_of_alpha_levels=5)**2 print(p) x = p.defuzzification(method='alpha_one') print(x)
def test_mean(): x = phuzzy.Trapezoid(alpha0=[-2, 4], alpha1=[-1, 3], name="x", number_of_alpha_levels=3) print(x.mean()) assert np.isclose(x.mean(), 1)
def test_poor_mens_alpha_optimization_neg_pow_fuzzy(): p = phuzzy.Trapezoid(alpha0=[-2, 2], alpha1=[-1, 1]) e = phuzzy.Trapezoid(alpha0=[0, 0], alpha1=[0, 0]) y = p**e print(y.df) # mix_mpl(y) # y.plot(show=True) assert np.isclose(y.min(), 1) assert np.isclose(y.max(), 1) p = phuzzy.Trapezoid(alpha0=[0, 4], alpha1=[2, 3], number_of_alpha_levels=2, name="p") print(p) assert len(p.df) == 2
def test_unify(): x = phuzzy.Uniform(alpha0=[1, 4], number_of_alpha_levels=5) y = phuzzy.Trapezoid(alpha0=[1, 4], alpha1=[2, 3], number_of_alpha_levels=7) a, b = x._unify(y) assert len(a.df) == 7 assert len(b.df) == 7
def test_contains(): x = phuzzy.Trapezoid(alpha0=[1, 5], alpha1=[2, 3], name="x", number_of_alpha_levels=3) assert 2 in x y = phuzzy.Trapezoid(alpha0=[2, 4], alpha1=[2, 3], name="y", number_of_alpha_levels=3) z = phuzzy.Trapezoid(alpha0=[2, 7], alpha1=[2, 3], name="z", number_of_alpha_levels=3) assert y in x assert not x in y assert not z in x
def test_trapz(): x = np.linspace(-2, 5, 11) p = phuzzy.Trapezoid(alpha0=[1, 4], alpha1=[2, 3], number_of_alpha_levels=5) P = p.cdf(x) print(P)
def atest_add(): t = phuzzy.TruncNorm(alpha0=[1, 3], alpha1=[2], number_of_alpha_levels=15) print(t) assert len(t.df) == 15 p = phuzzy.Trapezoid(alpha0=[1, 4], alpha1=[2, 3], number_of_alpha_levels=5) print(p) assert len(p.df) == 5 a = t + p print(a) print(a.df)
def test_neg(): p = phuzzy.Trapezoid(alpha0=[0, 4], alpha1=[2, 3], number_of_alpha_levels=3, name="p") print(p) print(p.df.values.tolist()) assert len(p.df) == 3 b = -p print(b.df.values.tolist()) assert np.allclose( b.df.values.tolist(), [[0.0, -4.0, -1e-10], [0.5, -3.5, -1.00000000005], [1.0, -3.0, -2.0]])
def test_pow_zero(): # p = phuzzy.Trapezoid(alpha0=[-1, 4], alpha1=[2, 3], number_of_alpha_levels=5, name="p") # y = p ** 0 x = phuzzy.Trapezoid(alpha0=[-1, 4], alpha1=[.5, .8], number_of_alpha_levels=5, name="p") y = x**0 mix_mpl(y) y.plot() print(y.df.values.tolist()) assert np.isclose(y.min(), 1) assert np.isclose(y.max(), 1)
def test_div(): t = phuzzy.TruncNorm(alpha0=[2, 3], alpha1=[], number_of_alpha_levels=3, name="t") print(t.df.values.tolist()) print(t) assert len(t.df) == 3 p = phuzzy.Trapezoid(alpha0=[0, 4], alpha1=[2, 3], number_of_alpha_levels=3, name="p") print(p) assert len(p.df) == 3 # a = t / p a = p / t a.name = "t/p" print(a.df.values.tolist()) assert np.allclose( a.df.values.tolist(), [[0.0, 3.3333333333333335e-11, 2.0], [0.5, 0.37088749487272066, 1.519252456825272], [1.0, 0.8, 1.2]]) print(a) print(a.df) print("_" * 80) b = t / 1. print(b) print(b.df) print(b.df.values.tolist()) assert np.allclose( b.df.values.tolist(), [[0.0, 2.0, 3.0], [0.5, 2.3037645812426892, 2.6962354187573108], [1.0, 2.5, 2.5]]) print(a) print(a.df) print("_" * 80) b = t / 2. print(b) print(b.df) print(b.df.values.tolist()) assert np.allclose( b.df.values.tolist(), [[0.0, 1.0, 1.5], [0.5, 1.1518822906213446, 1.3481177093786554], [1.0, 1.25, 1.25]])
def test_import_export(): y = phuzzy.Trapezoid(alpha0=[1, 4], alpha1=[2, 3], number_of_alpha_levels=7) e = y.export_csv() print(e) if is_py2: e = e.decode() fh = StringIO(e) fh.seek(0) z = phuzzy.FuzzyNumber(alpha0=[1, 4], alpha1=[2, 3], number_of_alpha_levels=2) z.import_csv(fh) assert len(z.df) == 7
def test_trapezoid(): p = phuzzy.Trapezoid(alpha0=[1, 4], alpha1=[2, 3], number_of_alpha_levels=5) print(p) print("number_of_alpha_levels", p.number_of_alpha_levels) print(p.df) print(len(p.df)) assert len(p.df) == 5 print(p.get_01) p.discretize(alpha0=[1, 4], alpha1=[2, 3], alpha_levels=5) s = p.to_str() print(s) pdf = p.pdf([2]) print(pdf) cdf = p.cdf([2]) print(cdf) p.discretize(alpha0=[1, 3], alpha1=[2, 3], alpha_levels=5)
def test_add(): t = phuzzy.TruncNorm(alpha0=[1, 3], alpha1=[2], number_of_alpha_levels=3) print(t) assert len(t.df) == 3 p = phuzzy.Trapezoid(alpha0=[1, 4], alpha1=[2, 3], number_of_alpha_levels=5) print(p) assert len(p.df) == 5 a = t + p print(a) print(a.df) print(a.df.values.tolist()) assert np.allclose( a.df.values.tolist(), [[0.0, 2.0, 7.0], [0.25, 2.5537645812426892, 6.446235418757311], [0.5, 3.1075291624853785, 5.892470837514622], [0.75, 3.5537645812426892, 5.446235418757311], [1.0, 4.0, 5.0]]) # mix_mpl(t) # mix_mpl(p) # mix_mpl(a) # t.plot() # p.plot() # a.plot(show=True) b = t + 4. print(b) print(b.df) print(b.df.values.tolist()) assert np.allclose( b.df.values.tolist(), [[0.0, 5.0, 7.0], [0.5, 5.6075291624853785, 6.392470837514622], [1.0, 6.0, 6.0]]) b = 4 - t print(b) print(b.df) print(b.df.values.tolist()) assert np.allclose( b.df.values.tolist(), [[0.0, -3.0, -1.0], [0.5, -2.3924708375146215, -1.607529162485378], [1.0, -2.0, -2.0]])
def test_sub(): t = phuzzy.TruncNorm(alpha0=[1, 3], alpha1=[2], number_of_alpha_levels=3, name="t") print(t) assert len(t.df) == 3 p = phuzzy.Trapezoid(alpha0=[1, 4], alpha1=[2, 3], number_of_alpha_levels=3, name="p") print(p) assert len(p.df) == 3 a = t - p a.name = "t-p" print(a.df) print(a.df.values.tolist()) assert np.allclose( a.df.values.tolist(), [[0.0, -3.0, 2.0], [0.5, -1.8924708375146215, 0.892470837514622], [1.0, -1.0, 0.0]]) b = t - 4. print(b) print(b.df) print(b.df.values.tolist()) assert np.allclose( b.df.values.tolist(), [[0.0, -3.0, -1.0], [0.5, -2.3924708375146215, -1.607529162485378], [1.0, -2.0, -2.0]]) b = 4. - t print(b) print(b.df) print(b.df.values.tolist()) assert np.allclose( b.df.values.tolist(), [[0.0, -3.0, -1.0], [0.5, -2.3924708375146215, -1.607529162485378], [1.0, -2.0, -2.0]])
def test_operation_class(): tra = phuzzy.Trapezoid(alpha0=[1, 4], alpha1=[2, 3], number_of_alpha_levels=5) tri = phuzzy.Triangle(alpha0=[1, 4], alpha1=[2, 3], number_of_alpha_levels=5) uni = phuzzy.Uniform(alpha0=[1, 4], alpha1=[2, 3], number_of_alpha_levels=5) x = tra + tra assert isinstance(x, phuzzy.Trapezoid) x = uni + uni assert isinstance(x, phuzzy.Uniform) x = tri + uni assert isinstance(x, phuzzy.Triangle) x = uni + tri print(x.__class__) assert isinstance(x, phuzzy.Triangle) x = tra + uni assert isinstance(x, phuzzy.Trapezoid) x = uni + tra assert isinstance(x, phuzzy.Trapezoid) x = tra + tri assert isinstance(x, phuzzy.Trapezoid) x = tri + tra assert isinstance(x, phuzzy.Trapezoid) x = uni + tri + tra assert isinstance(x, phuzzy.Trapezoid) x = uni + 2. assert isinstance(x, phuzzy.Uniform)
def test_fuzzy(): n = phuzzy.FuzzyNumber() print(n) print(n.__class__.__name__) assert hasattr(n, "name") assert hasattr(n, "df") assert hasattr(n, "number_of_alpha_levels") t = phuzzy.FuzzyNumber.from_data(data=[1, 3, 5], number_of_alpha_levels=3) print(t.df) assert len(t.df) == 3 print(t.__class__.__name__) s = t.to_str() print(s) pdf = t.pdf([2]) print(pdf) cdf = t.cdf([2]) print(cdf) n = phuzzy.FuzzyNumber() with pytest.raises(NotImplementedError) as exp: s = n.to_str() print(s) with pytest.raises(NotImplementedError) as exp: s = phuzzy.FuzzyNumber.from_str("!") print(s) t = phuzzy.Triangle(alpha0=[1, 3], alpha1=[2], number_of_alpha_levels=15) p = phuzzy.Trapezoid(alpha0=[1, 4], alpha1=[2, 3], number_of_alpha_levels=5) n = t + p pdf = n.pdf([2]) print(pdf) cdf = n.cdf([2]) print(cdf)
def plot_fuzzynumber_cdf(): import matplotlib.pyplot as plt H = 100. # mm B = 300. # mm fig, axs = plt.subplots(1, 2, dpi=90, facecolor='w', edgecolor='k', figsize=(B / 25.4, H / 25.4)) x = np.linspace(0, 5, 200) t = phuzzy.Trapezoid(alpha0=[1, 5], alpha1=[2, 3], number_of_alpha_levels=5) p = t.pdf(x) P = t.cdf(x) axs[0].plot(x, p, label="pdf", lw=1) axs[1].plot(x, P, label="cdf", lw=1) f = phuzzy.FuzzyNumber(alpha0=[1, 2], alpha1=[1.1, 1.4], number_of_alpha_levels=5) f.df = t.df.copy() print(f.df) pf = f.pdf(x) Pf = f.cdf(x) mix_mpl(f) f.plot(show=False) axs[0].plot(x, pf, label="pdf", lw=3, c="r", alpha=.5) axs[1].plot(x, Pf, label="cdf", lw=3, c="r", alpha=.5) axs[0].legend(loc="best") axs[1].legend(loc="best") plt.show()
def plot_traz_cdf(): import matplotlib.pyplot as plt H = 100. # mm B = 300. # mm fig, axs = plt.subplots(1, 2, dpi=90, facecolor='w', edgecolor='k', figsize=(B / 25.4, H / 25.4)) x = np.linspace(0, 6, 200) f = phuzzy.Trapezoid(alpha0=[1, 5], alpha1=[2, 3], number_of_alpha_levels=5) p = f.pdf(x) P = f.cdf(x) axs[0].plot(x, p, label="pdf", lw=2) axs[1].plot(x, P, label="cdf", lw=2) axs[0].legend(loc="best") axs[1].legend(loc="best") plt.show()
import streamlit as st import phuzzy from phuzzy.mpl import mix_mpl import matplotlib.pyplot as plt add_selectbox = st.sidebar.selectbox("phuzzy numbers", ("Triangle", "Uniform", "Trapezoid")) st.text('phuzzy') p1_slot = st.empty() uniform = phuzzy.Uniform(alpha0=[1, 2]) mix_mpl(uniform) trapezoid = phuzzy.Trapezoid(alpha0=[1, 2], alpha1=[1.2, 1.5]) mix_mpl(trapezoid) triangle = phuzzy.Triangle(alpha0=[1, 2], alpha1=[1.8]) mix_mpl(triangle) p_dict = {"Triangle": triangle, "Uniform": uniform, "Trapezoid": trapezoid} print(add_selectbox) p = p_dict.get(add_selectbox, triangle) def plot_phuzzy_number(p): H = 170. #mm B = 270. #mm fig, ax = plt.subplots(dpi=90,