Skip to content

Commit

Permalink
T1TSK and T1Mamdani ML Classes ...
Browse files Browse the repository at this point in the history
  • Loading branch information
Haghrah committed Nov 27, 2024
1 parent 0313953 commit d803452
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
20 changes: 15 additions & 5 deletions examples/ex_19.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from pyit2fls import (T1TSK_ML, )
from pyit2fls import (T1TSK_ML, T1FS_plot, )
from numpy import (linspace, array, abs, pi, sin, cos, meshgrid, zeros_like, )
from scipy.optimize import (Bounds, )
import matplotlib.pyplot as plt
Expand All @@ -15,8 +15,10 @@
y.append(sin(x1) + cos(x2))
X = array(X)

myTSK = T1TSK_ML(2, 16, (-4., 4.), algorithm="PSO",
algorithm_params=[20, 200, 0.3, 0.3, 2.4])
M = 2
N = 4
myTSK = T1TSK_ML(M, N, (-4., 4.), algorithm="PSO",
algorithm_params=[200, 200, 0.3, 0.3, 2.4])
print(myTSK.fit(X, y))

x1, x2 = meshgrid(X1, X2)
Expand All @@ -26,6 +28,7 @@
for j in range(10):
y2[i, j] = myTSK.score(array([X1[j], X2[i], ]))


fig = plt.figure()
ax = fig.add_subplot(111, projection="3d")
original = ax.plot_surface(x1, x2, y1, cmap="viridis", vmin=y1.min(), vmax=y1.max())
Expand All @@ -44,15 +47,22 @@

fig = plt.figure()
ax = fig.add_subplot(111, projection="3d")
error_surface = ax.plot_surface(x1, x2, abs(y2 - y1), cmap="Greens", alpha=0.3)
error_surface = ax.plot_surface(x1, x2, abs(y2 - y1), cmap="Greens", alpha=0.8)
fig.colorbar(error_surface, ax=ax, shrink=0.5, aspect=10,
label="Error Magnitude")
ax.plot_surface(x1, x2, y1, cmap="Blues", alpha=0.7)
ax.set_title("3D Surface Plot")
plt.show()



achievedSystem = myTSK.get_T1TSK()
for r, rule in zip(range(N), achievedSystem.rules):
sets = []
labels = []
for i in range(M):
sets.append(rule[0][i][1])
labels.append(f"X{i}")
T1FS_plot(*sets, title=f"Rule {r+1}", legends=labels)



Expand Down
4 changes: 2 additions & 2 deletions examples/ex_20.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
M = 2
N = 4
myMamdani = T1Mamdani_ML(M, N, (-4., 4.), algorithm="GA",
algorithm_params=[50, 200, 100, 100, ])
algorithm_params=[200, 200, 100, 100, 0.05])
print(myMamdani.fit(X, y))

x1, x2 = meshgrid(X1, X2)
Expand Down Expand Up @@ -62,7 +62,7 @@
labels.append(f"X{i}")
sets.append(rule[1][0][1])
labels.append("Y")
T1FS_plot(*sets, title="Rule {r}", legends=labels)
T1FS_plot(*sets, title=f"Rule {r+1}", legends=labels)



Expand Down
11 changes: 6 additions & 5 deletions pyit2fls/learning.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ def crossover(self, parent1, parent2):
b = rand(self.M)
return (a * parent1.solution + b * parent2.solution) / (a + b)

def iterate(self, mutation_num, crossover_num):
parent_list = self.tournament_selection(2 * crossover_num, 1)
def iterate(self, mutation_num, crossover_num, tp):
parent_list = self.tournament_selection(2 * crossover_num, 1.0)

for i, j in zip(parent_list[::2], parent_list[1::2]):
child_solution = self.crossover(self.population[i], self.population[j])
Expand All @@ -111,7 +111,7 @@ def iterate(self, mutation_num, crossover_num):
self.population[j].solution = child_solution.copy()
self.population[j].fitness = self.func(child_solution, *self.args)

parent_list = self.tournament_selection(mutation_num, 0.1)
parent_list = self.tournament_selection(mutation_num, tp)
for i in parent_list:
mutated_solution = self.mutate(self.population[i])
if self.func(mutated_solution, *self.args) < self.population[i].fitness:
Expand Down Expand Up @@ -246,7 +246,8 @@ def fit(self, X, y):
self.Bounds[0], args=(X, y, ))
for i in range(self.algorithm_params[1]):
myGA.iterate(self.algorithm_params[2],
self.algorithm_params[3], )
self.algorithm_params[3],
self.algorithm_params[4], )
print("Iteration ", i+1, ".", myGA.population[0].fitness)
self.params = myGA.population[0].solution
else:
Expand Down Expand Up @@ -325,7 +326,7 @@ def get_T1TSK(self, std=1., ):
T1FS(domain, gaussian_mf,
params=[self.model.p[i][j][0], self.model.p[i][j][1], 1., ])))

consequent = [("Y", lambda **X: self.model.q[i]),
consequent = [("Y", lambda *X: self.model.q[i]),
]
generated_T1TSK.add_rule(antecedent, consequent)

Expand Down

0 comments on commit d803452

Please sign in to comment.