From d803452c03be2e88b56b7f7638364fc4b98c9cc0 Mon Sep 17 00:00:00 2001 From: haghrah Date: Wed, 27 Nov 2024 19:40:40 +0330 Subject: [PATCH] T1TSK and T1Mamdani ML Classes ... --- examples/ex_19.py | 20 +++++++++++++++----- examples/ex_20.py | 4 ++-- pyit2fls/learning.py | 11 ++++++----- 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/examples/ex_19.py b/examples/ex_19.py index 6e96d37..47b9a8f 100644 --- a/examples/ex_19.py +++ b/examples/ex_19.py @@ -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 @@ -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) @@ -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()) @@ -44,7 +47,7 @@ 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) @@ -52,7 +55,14 @@ 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) diff --git a/examples/ex_20.py b/examples/ex_20.py index 8a5d3a7..50ffaea 100644 --- a/examples/ex_20.py +++ b/examples/ex_20.py @@ -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) @@ -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) diff --git a/pyit2fls/learning.py b/pyit2fls/learning.py index 143ac9a..083162c 100644 --- a/pyit2fls/learning.py +++ b/pyit2fls/learning.py @@ -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]) @@ -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: @@ -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: @@ -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)