rename (t|q)(a|c|d) to (t|q)(1|2|) in traptraj
[liba.git] / python / test / mf.py
blob3282bc945b19f5929b40550cceec5aa1b29a8451
1 #!/usr/bin/env python
2 import os, sys
4 sys.path.insert(0, os.getcwd())
5 prefix = os.path.join(sys.path[0], "build")
6 if not os.path.exists(prefix):
7 os.mkdir(prefix)
8 try:
9 import liba as a
10 import numpy as np
11 import matplotlib.pyplot as plt
12 except Exception as e:
13 print(e)
14 exit()
16 title = "Gaussian membership function"
17 x = np.arange(-3, 3, 0.001)
18 y = a.mf.gauss(x, 1, 0)
19 plt.figure(title)
20 plt.title(title)
21 plt.plot(x, y)
22 plt.grid(True)
23 plt.savefig(os.path.join(prefix, "mf_gauss.png"))
25 title = "Gaussian combination membership function"
26 x = np.arange(-3, 3, 0.001)
27 y = a.mf.gauss2(x, 1, -1, 1, +1)
28 plt.figure(title)
29 plt.title(title)
30 plt.plot(x, y)
31 plt.grid(True)
32 plt.savefig(os.path.join(prefix, "mf_gauss2.png"))
34 title = "Generalized bell-shaped membership function"
35 x = np.arange(-3, 3, 0.001)
36 y = a.mf.gbell(x, 2, 4, 0)
37 plt.figure(title)
38 plt.title(title)
39 plt.plot(x, y)
40 plt.grid(True)
41 plt.savefig(os.path.join(prefix, "mf_gbell.png"))
43 title = "Sigmoidal membership function"
44 x = np.arange(-3, 3, 0.001)
45 y = a.mf.sig(x, 2, 0)
46 plt.figure(title)
47 plt.title(title)
48 plt.plot(x, y)
49 plt.grid(True)
50 plt.savefig(os.path.join(prefix, "mf_sig.png"))
52 title = "Difference between two sigmoidal membership functions"
53 x = np.arange(-3, 3, 0.001)
54 y = a.mf.dsig(x, 5, -2, +5, 2)
55 plt.figure(title)
56 plt.title(title)
57 plt.plot(x, y)
58 plt.grid(True)
59 plt.savefig(os.path.join(prefix, "mf_dsig.png"))
61 title = "Product of two sigmoidal membership functions"
62 x = np.arange(-3, 3, 0.001)
63 y = a.mf.psig(x, 5, -2, -5, 2)
64 plt.figure(title)
65 plt.title(title)
66 plt.plot(x, y)
67 plt.grid(True)
68 plt.savefig(os.path.join(prefix, "mf_psig.png"))
70 title = "Triangular membership function"
71 x = np.arange(0, 2, 0.001)
72 y = a.mf.tri(x, 0, 1, 2)
73 plt.figure(title)
74 plt.title(title)
75 plt.plot(x, y)
76 plt.grid(True)
77 plt.savefig(os.path.join(prefix, "mf_tri.png"))
79 y = []
80 title = "Trapezoidal membership function"
81 x = np.arange(0, 3, 0.001)
82 y = a.mf.trap(x, 0, 1, 2, 3)
83 plt.figure(title)
84 plt.title(title)
85 plt.plot(x, y)
86 plt.grid(True)
87 plt.savefig(os.path.join(prefix, "mf_trap.png"))
89 title = "Linear s-shaped saturation membership function"
90 x = np.arange(0, 3, 0.001)
91 y = a.mf.lins(x, 1, 2)
92 plt.figure(title)
93 plt.title(title)
94 plt.plot(x, y)
95 plt.grid(True)
96 plt.savefig(os.path.join(prefix, "mf_lins.png"))
98 title = "Linear z-shaped saturation membership function"
99 x = np.arange(0, 3, 0.001)
100 y = a.mf.linz(x, 1, 2)
101 plt.figure(title)
102 plt.title(title)
103 plt.plot(x, y)
104 plt.grid(True)
105 plt.savefig(os.path.join(prefix, "mf_linz.png"))
107 title = "S-shaped membership function"
108 x = np.arange(0, 3, 0.001)
109 y = a.mf.s(x, 1, 2)
110 plt.figure(title)
111 plt.title(title)
112 plt.plot(x, y)
113 plt.grid(True)
114 plt.savefig(os.path.join(prefix, "mf_s.png"))
116 title = "Z-shaped membership function"
117 x = np.arange(0, 3, 0.001)
118 y = a.mf.z(x, 1, 2)
119 plt.figure(title)
120 plt.title(title)
121 plt.plot(x, y)
122 plt.grid(True)
123 plt.savefig(os.path.join(prefix, "mf_z.png"))
125 y = []
126 title = "Pi-shaped membership function"
127 x = np.arange(0, 3, 0.001)
128 y = a.mf.pi(x, 0, 1, 2, 3)
129 plt.figure(title)
130 plt.title(title)
131 plt.plot(x, y)
132 plt.grid(True)
133 plt.savefig(os.path.join(prefix, "mf_pi.png"))
135 S = 1
136 params = [
137 (a.mf.TRI, -3 * S, -3 * S, -2 * S),
138 (a.mf.TRI, -3 * S, -2 * S, -1 * S),
139 (a.mf.TRI, -2 * S, -1 * S, +0 * S),
140 (a.mf.TRI, -1 * S, +0 * S, +1 * S),
141 (a.mf.TRI, +0 * S, +1 * S, +2 * S),
142 (a.mf.TRI, +1 * S, +2 * S, +3 * S),
143 (a.mf.TRI, +2 * S, +3 * S, +3 * S),
146 title = "membership function"
147 x = np.arange(-3 * S, +3 * S, 0.001)
148 plt.figure(title)
149 plt.title(title)
150 for param in params:
151 plt.plot(x, a.mf()(param[0], x, param[1:]))
152 plt.savefig(os.path.join(prefix, "mf.png"))