support for installing liba.pyi during pip install
[liba.git] / python / test / 3rd / mf.py
blobb9e92b2ae4bf0c96606c4671bb37bdd61780c8b0
1 #!/usr/bin/env python
2 import os, sys
4 base = os.path.dirname(__file__)
5 path = os.path.dirname(base)
6 path = os.path.dirname(path)
7 sys.path.insert(0, path)
8 try:
9 import numpy as np
10 import matplotlib.pyplot as plt
11 except Exception as e:
12 print(e)
13 exit()
15 import liba # type: ignore
17 title = "Gaussian membership function"
18 x = np.arange(-3, 3, 0.001)
19 y = liba.mf.gauss(x, 1, 0)
20 plt.figure(title)
21 plt.title(title)
22 plt.plot(x, y)
23 plt.grid(True)
24 plt.savefig(os.path.join(base, "mf_gauss.png"))
26 title = "Gaussian combination membership function"
27 x = np.arange(-3, 3, 0.001)
28 y = liba.mf.gauss2(x, 1, -1, 1, +1)
29 plt.figure(title)
30 plt.title(title)
31 plt.plot(x, y)
32 plt.grid(True)
33 plt.savefig(os.path.join(base, "mf_gauss2.png"))
35 title = "Generalized bell-shaped membership function"
36 x = np.arange(-3, 3, 0.001)
37 y = liba.mf.gbell(x, 2, 4, 0)
38 plt.figure(title)
39 plt.title(title)
40 plt.plot(x, y)
41 plt.grid(True)
42 plt.savefig(os.path.join(base, "mf_gbell.png"))
44 title = "Sigmoidal membership function"
45 x = np.arange(-3, 3, 0.001)
46 y = liba.mf.sig(x, 2, 0)
47 plt.figure(title)
48 plt.title(title)
49 plt.plot(x, y)
50 plt.grid(True)
51 plt.savefig(os.path.join(base, "mf_sig.png"))
53 title = "Difference between two sigmoidal membership functions"
54 x = np.arange(-3, 3, 0.001)
55 y = liba.mf.dsig(x, 5, -2, +5, 2)
56 plt.figure(title)
57 plt.title(title)
58 plt.plot(x, y)
59 plt.grid(True)
60 plt.savefig(os.path.join(base, "mf_dsig.png"))
62 title = "Product of two sigmoidal membership functions"
63 x = np.arange(-3, 3, 0.001)
64 y = liba.mf.psig(x, 5, -2, -5, 2)
65 plt.figure(title)
66 plt.title(title)
67 plt.plot(x, y)
68 plt.grid(True)
69 plt.savefig(os.path.join(base, "mf_psig.png"))
71 title = "Triangular membership function"
72 x = np.arange(0, 2, 0.001)
73 y = liba.mf.tri(x, 0, 1, 2)
74 plt.figure(title)
75 plt.title(title)
76 plt.plot(x, y)
77 plt.grid(True)
78 plt.savefig(os.path.join(base, "mf_tri.png"))
80 y = []
81 title = "Trapezoidal membership function"
82 x = np.arange(0, 3, 0.001)
83 y = liba.mf.trap(x, 0, 1, 2, 3)
84 plt.figure(title)
85 plt.title(title)
86 plt.plot(x, y)
87 plt.grid(True)
88 plt.savefig(os.path.join(base, "mf_trap.png"))
90 title = "Linear s-shaped saturation membership function"
91 x = np.arange(0, 3, 0.001)
92 y = liba.mf.lins(x, 1, 2)
93 plt.figure(title)
94 plt.title(title)
95 plt.plot(x, y)
96 plt.grid(True)
97 plt.savefig(os.path.join(base, "mf_lins.png"))
99 title = "Linear z-shaped saturation membership function"
100 x = np.arange(0, 3, 0.001)
101 y = liba.mf.linz(x, 1, 2)
102 plt.figure(title)
103 plt.title(title)
104 plt.plot(x, y)
105 plt.grid(True)
106 plt.savefig(os.path.join(base, "mf_linz.png"))
108 title = "S-shaped membership function"
109 x = np.arange(0, 3, 0.001)
110 y = liba.mf.s(x, 1, 2)
111 plt.figure(title)
112 plt.title(title)
113 plt.plot(x, y)
114 plt.grid(True)
115 plt.savefig(os.path.join(base, "mf_s.png"))
117 title = "Z-shaped membership function"
118 x = np.arange(0, 3, 0.001)
119 y = liba.mf.z(x, 1, 2)
120 plt.figure(title)
121 plt.title(title)
122 plt.plot(x, y)
123 plt.grid(True)
124 plt.savefig(os.path.join(base, "mf_z.png"))
126 y = []
127 title = "Pi-shaped membership function"
128 x = np.arange(0, 3, 0.001)
129 y = liba.mf.pi(x, 0, 1, 2, 3)
130 plt.figure(title)
131 plt.title(title)
132 plt.plot(x, y)
133 plt.grid(True)
134 plt.savefig(os.path.join(base, "mf_pi.png"))
136 S = 1
137 params = [
138 (liba.mf.TRI, -3 * S, -3 * S, -2 * S),
139 (liba.mf.TRI, -3 * S, -2 * S, -1 * S),
140 (liba.mf.TRI, -2 * S, -1 * S, +0 * S),
141 (liba.mf.TRI, -1 * S, +0 * S, +1 * S),
142 (liba.mf.TRI, +0 * S, +1 * S, +2 * S),
143 (liba.mf.TRI, +1 * S, +2 * S, +3 * S),
144 (liba.mf.TRI, +2 * S, +3 * S, +3 * S),
147 title = "membership function"
148 x = np.arange(-3 * S, +3 * S, 0.001)
149 plt.figure(title)
150 plt.title(title)
151 for param in params:
152 plt.plot(x, liba.mf()(param[0], x, param[1:]))
153 plt.savefig(os.path.join(base, "mf.png"))
154 plt.show()