Skip to content

Commit

Permalink
Fixing some bugs ...
Browse files Browse the repository at this point in the history
  • Loading branch information
Haghrah committed Aug 7, 2021
1 parent 66adae2 commit 04395b3
Show file tree
Hide file tree
Showing 8 changed files with 237 additions and 14 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ Or you can use pip3:
- Exception handling.
- Supporting Generalized Type 2 Fuzzy Sets and Systems.

### Some notes on version 0.7.9
- Some bugs (concerning EIASC algorithm) have been fixed in this version.

### Some notes on version 0.7.8
- Some bugs have been fixed in this version.

Expand Down
218 changes: 218 additions & 0 deletions examples/ex_5_0.7.0.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Jun 20 20:19:08 2019
@author: arslan
"""

from matplotlib.pyplot import figure, plot, legend, grid, show, xlabel, \
ylabel, savefig
from pyit2fls import IT2FS_Gaussian_UncertStd, IT2Mamdani, \
min_t_norm, product_t_norm, max_s_norm, IT2FS_plot
from numpy import linspace, array, trapz, where
from numpy import abs as npabs
from numpy import max as npmax
from ddeintlib import ddeint

# %%
def ITAE(a, b, t):
return trapz(t * npabs(a - b), t)

def os(y):
return npabs(npmax(y) - y[-1])

def ts(y, t):
ss = y[-1]
tol = 0.02 * ss
yy = npabs(y - ss)[::-1]
return t[t.size - where(yy>tol)[0][0]]

# %%
def u(t):
return 1. if t > 1. else 0.

def u_dot(t):
# return 0
a = 10
return (a if 1. < t < (1. + 1./a) else 0)

# %% Interval Type 2 Fuzzy PID Codes ...
domain = linspace(-1., 1., 201)

N = IT2FS_Gaussian_UncertStd(domain, [-1., 0.25, 0.05, 1.])
Z = IT2FS_Gaussian_UncertStd(domain, [0., 0.25, 0.05, 1.])
P = IT2FS_Gaussian_UncertStd(domain, [1., 0.25, 0.05, 1.])
IT2FS_plot(N, Z, P, legends=["Negative", "Zero", "Positive"], filename="delay_pid_input_sets")

NB = IT2FS_Gaussian_UncertStd(domain, [-1., 0.15, 0.025, 1.])
NM = IT2FS_Gaussian_UncertStd(domain, [-0.5, 0.15, 0.025, 1.])
ZZ = IT2FS_Gaussian_UncertStd(domain, [0., 0.15, 0.025, 1.])
PM = IT2FS_Gaussian_UncertStd(domain, [0.5, 0.15, 0.025, 1.])
PB = IT2FS_Gaussian_UncertStd(domain, [1., 0.15, 0.025, 1.])
IT2FS_plot(NB, NM, ZZ, PM, PB, legends=["Negative Big", "Negative Medium",
"Zero", "Positive Medium",
"Positive Big"], filename="delay_pid_output_sets")

def fuzzySystem(algorithm, algorithm_params=[]):
it2fls = IT2Mamdani(min_t_norm, max_s_norm, method="Centroid",
algorithm=algorithm, algorithm_params=algorithm_params)
it2fls.add_input_variable("I1") # E
it2fls.add_input_variable("I2") # dot E
it2fls.add_output_variable("O")

it2fls.add_rule([("I1", N), ("I2", N)], [("O", NB)])
it2fls.add_rule([("I1", N), ("I2", Z)], [("O", NM)])
it2fls.add_rule([("I1", N), ("I2", P)], [("O", ZZ)])
it2fls.add_rule([("I1", Z), ("I2", N)], [("O", NM)])
it2fls.add_rule([("I1", Z), ("I2", Z)], [("O", ZZ)])
it2fls.add_rule([("I1", Z), ("I2", P)], [("O", NM)])
it2fls.add_rule([("I1", P), ("I2", N)], [("O", ZZ)])
it2fls.add_rule([("I1", P), ("I2", Z)], [("O", PM)])
it2fls.add_rule([("I1", P), ("I2", P)], [("O", PB)])
return it2fls

it2fpid_KM = fuzzySystem("KM")
it2fpid_EIASC = fuzzySystem("EIASC")
it2fpid_WM = fuzzySystem("WM")
it2fpid_BMM = fuzzySystem("BMM", algorithm_params=(0.5, 0.5))
it2fpid_NT = fuzzySystem("NT")

def eval_IT2FPID_KM(i1, i2):
c, TR = it2fpid_KM.evaluate({"I1":i1, "I2":i2})
o = TR["O"]
return (o[0] + o[1]) / 2

def eval_IT2FPID_EIASC(i1, i2):
c, TR = it2fpid_EIASC.evaluate({"I1":i1, "I2":i2})
o = TR["O"]
return (o[0] + o[1]) / 2

def eval_IT2FPID_WM(i1, i2):
c, TR = it2fpid_WM.evaluate({"I1":i1, "I2":i2})
o = TR["O"]
return (o[0] + o[1]) / 2

def eval_IT2FPID_BMM(i1, i2):
c, y = it2fpid_BMM.evaluate({"I1":i1, "I2":i2})
return y["O"]

def eval_IT2FPID_NT(i1, i2):
c, y = it2fpid_NT.evaluate({"I1":i1, "I2":i2})
return y["O"]

# %% Overall system
def raw_sys(Y, t, K, T, L):
return array([(-1. / T) * Y[0](t) + (K / T) * u(t - L)])

def cl_sys(Y, t, K, T, L):
return array([(-1. / T) * Y[0](t) + (K / T) * (u(t - L) - Y[0](t - L))])

def model_fuzzy(Y, t, K, T, L, Ka, Kb, Ke, Kd, eval_func):
epsilon = 0.001

y2 = Y[1](t)

e1 = u(t - L) - Y[0](t - L)
de1 = u_dot(t - L) - Y[1](t - L)
xd1 = eval_func(min(max(Ke * e1, -1), 1), min(max(Kd * de1, -1), 1))


e2 = u(t - L - epsilon) - Y[0](t - L)
de2 = u_dot(t - L - epsilon) - Y[1](t - L)
xd2 = eval_func(min(max(Ke * e2, -1), 1), min(max(Kd * de2, -1), 1))



dxd = (xd1 - xd2) / epsilon



return array([y2, (-1./T) * y2 + (K * Ka / T) * dxd + (K * Kb / T) * xd1])

g1 = lambda t : 0
g2 = lambda t : 0

# %%
# Nominal
K = 1.
T = 1.
L = 0.2

# Perturbed 1.
# K = 1.3
# T = 1.9
# L = 0.4

#Perturbed 2.
#K = 1.1
#T = 1.3
#L = 0.45

tt = linspace(0., 40. ,2000)

y_raw = ddeint(raw_sys, [g1], tt, fargs=(K, T, L, ))
y_cl = ddeint(cl_sys, [g1], tt, fargs=(K, T, L, ))

print("KM evaluation start!")
y_it2fpid_KM = ddeint(model_fuzzy, [g1, g2], tt,
fargs=(K, T, L, 0.25, 4.25, 0.8, 0.5, eval_IT2FPID_KM, ))

print("EIASC evaluation start!")
y_it2fpid_EIASC = ddeint(model_fuzzy, [g1, g2], tt,
fargs=(K, T, L, 0.25, 4.25, 0.8, 0.5, eval_IT2FPID_EIASC, ))

print("WM evaluation start!")
y_it2fpid_WM = ddeint(model_fuzzy, [g1, g2], tt,
fargs=(K, T, L, 0.25, 4.25, 0.8, 0.5, eval_IT2FPID_WM, ))

print("BMM evaluation start!")
y_it2fpid_BMM = ddeint(model_fuzzy, [g1, g2], tt,
fargs=(K, T, L, 0.25, 4.25, 0.8, 0.5, eval_IT2FPID_BMM, ))

print("NT evaluation start!")
y_it2fpid_NT = ddeint(model_fuzzy, [g1, g2], tt,
fargs=(K, T, L, 0.25, 4.25, 0.8, 0.5, eval_IT2FPID_NT, ))

ref = array([u(t) for t in tt])

figure()
plot(tt, ref, label="Reference")
plot(tt, y_it2fpid_KM[:,0], label="KM", linewidth=1.)
plot(tt, y_it2fpid_EIASC[:,0], label="EIASC", linewidth=1.)
plot(tt, y_it2fpid_WM[:,0], label="WM", linewidth=1.)
plot(tt, y_it2fpid_BMM[:,0], label="BMM", linewidth=1.)
plot(tt, y_it2fpid_NT[:,0], label="NT", linewidth=1.)
legend()
xlabel("Time (s)")
ylabel("System response")
grid(True)
savefig("delay_pid_case1_comp.pdf", format="pdf", dpi=300, bbox_inches="tight")
show()



print("KM:")
print("ITAE:", ITAE(ref, y_it2fpid_KM[:,0], tt))
print("Overshoot:", 100 * os(y_it2fpid_KM[:,0]))
print("Settling time:", ts(y_it2fpid_KM[:,0], tt))

print("EIASC:")
print("ITAE:", ITAE(ref, y_it2fpid_EIASC[:,0], tt))
print("Overshoot:", 100 * os(y_it2fpid_EIASC[:,0]))
print("Settling time:", ts(y_it2fpid_EIASC[:,0], tt))

print("WM:")
print("ITAE:", ITAE(ref, y_it2fpid_WM[:,0], tt))
print("Overshoot:", 100 * os(y_it2fpid_WM[:,0]))
print("Settling time:", ts(y_it2fpid_WM[:,0], tt))

print("BMM:")
print("ITAE:", ITAE(ref, y_it2fpid_BMM[:,0], tt))
print("Overshoot:", 100 * os(y_it2fpid_BMM[:,0]))
print("Settling time:", ts(y_it2fpid_BMM[:,0], tt))

print("NT:")
print("ITAE:", ITAE(ref, y_it2fpid_NT[:,0], tt))
print("Overshoot:", 100 * os(y_it2fpid_NT[:,0]))
print("Settling time:", ts(y_it2fpid_NT[:,0], tt))
4 changes: 2 additions & 2 deletions pyit2fls/pyit2fls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2938,8 +2938,8 @@ def EIASC_algorithm(intervals, params=[]):
break
# Right calculations
intervals = intervals[intervals[:, 1].argsort()]
a_r = npsum(intervals[:, 1] * intervals[:, 3])
b_r = npsum(intervals[:, 3])
a_r = npsum(intervals[:, 1] * intervals[:, 2])
b_r = npsum(intervals[:, 2])
R = N - 1
while True:
d = intervals[R, 3] - intervals[R, 2]
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
long_description = f.read()

setup(name='pyit2fls',
version='0.7.8',
version='0.7.9',
description='Type 1 and Interval Type 2 Fuzzy Logic Systems in Python',
long_description=long_description,
long_description_content_type='text/markdown',
Expand Down
3 changes: 1 addition & 2 deletions typereduction/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
Typereduction
========

Typereduction library contains the Ctypes based implementations of the type reduction algorithms ONLY for IT2FSs. It should be noted that some parts of the original type reduction algorithms have been edited for improving the execution speed.

Typereduction library contains the Ctypes based implementations of type reduction algorithms ONLY for IT2FSs. It should be noted that some parts of the original type reduction algorithms have been edited for improving the execution speed.

### Connecting with PyIT2FLS

Expand Down
13 changes: 8 additions & 5 deletions typereduction/examples/ex_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
t_ = 0
for _ in range(M):
t = time()
typereduction.EIASC_algorithm(intervals)
eiasc = typereduction.EIASC_algorithm(intervals)
t_ += time() - t

print("EIASC C implementation:", t_)
Expand All @@ -60,7 +60,7 @@
t_ = 0
for _ in range(M):
t = time()
typereduction.KM_algorithm(intervals)
km = typereduction.KM_algorithm(intervals)
t_ += time() - t

print("KM C implementation:", t_)
Expand All @@ -76,7 +76,7 @@
t_ = 0
for _ in range(M):
t = time()
typereduction.EKM_algorithm(intervals)
ekm = typereduction.EKM_algorithm(intervals)
t_ += time() - t

print("EKM C implementation:", t_)
Expand All @@ -92,12 +92,15 @@
t_ = 0
for _ in range(M):
t = time()
typereduction.WM_algorithm(intervals)
wm = typereduction.WM_algorithm(intervals)
t_ += time() - t

print("WM C implementation:", t_)



print("KM:", km)
print("EKM:", ekm)
print("EIASC:", eiasc)
print("WM:", wm)


2 changes: 1 addition & 1 deletion typereduction/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
sources = ['typereduction/typereduction.c'])

setup(name='typereduction',
version='0.1.0',
version='0.2.0',
description='CTypes base type reduction algorithms for using with PyIT2FLS',
ext_modules = [typreduction],
url='https://github.com/Haghrah/PyIT2FLS/typereduction',
Expand Down
6 changes: 3 additions & 3 deletions typereduction/typereduction/typereduction.c
Original file line number Diff line number Diff line change
Expand Up @@ -382,9 +382,9 @@ void EIASC_algorithm(double *data, double *params, int size, double *result)
allZero = 0;
}
b_l += rawData[2];
b_r += rawData[3];
b_r += rawData[2];
a_l += rawData[0] * rawData[2];
a_r += rawData[1] * rawData[3];
a_r += rawData[1] * rawData[2];
rawData += 4;
}

Expand All @@ -409,7 +409,7 @@ void EIASC_algorithm(double *data, double *params, int size, double *result)
}
}

// Interval.a = Interval.b in IT2FSs!
// We know that Interval.a = Interval.b in IT2FSs!
//qsort(intervalArray, size, sizeof(Interval), compare_b);

for(R = size - 1; R > 0; R--)
Expand Down

0 comments on commit 04395b3

Please sign in to comment.