def main(): e_univ = np.linspace(-2.57, 2.57, 1000) e_fuzzy_set_1 = [-1.285, 0.5, 'l_inf'] e_fuzzy_set_2 = [0, 0.5, 'none'] e_fuzzy_set_3 = [1.285, 0.5, 'r_inf'] e_dot_univ = np.linspace(-4.15, 4.15, 1000) e_dot_fuzzy_set_1 = [-1.57, 0.5, 'l_inf'] e_dot_fuzzy_set_2 = [0, 0.5, 'none'] e_dot_fuzzy_set_3 = [1.57, 0.5, 'r_inf'] i_univ = np.linspace(-30, 30, 1000) i_fuzzy_set_1 = [-15, 0.25, 'none'] i_fuzzy_set_2 = [0, 0.25, 'none'] i_fuzzy_set_3 = [15, 0.25, 'none'] e_fuzz_set = [e_fuzzy_set_1, e_fuzzy_set_2, e_fuzzy_set_3] e_dot_fuzz_set = [e_dot_fuzzy_set_1, e_dot_fuzzy_set_2, e_dot_fuzzy_set_3] i_fuzz_set = [i_fuzzy_set_1, i_fuzzy_set_2, i_fuzzy_set_3] names = ["N", "Z", "P"] e_memship = np.zeros((3, ), dtype=object) e_dot_memship = np.zeros((3, ), dtype=object) i_memship = np.zeros((3, ), dtype=object) for i in range(len(e_fuzz_set)): e_memship[i] = fuzz.membership('gauss', e_fuzz_set[i], e_univ, names[i]) e_dot_memship[i] = fuzz.membership('gauss', e_dot_fuzz_set[i], e_dot_univ, names[i]) i_memship[i] = fuzz.membership('gauss', i_fuzz_set[i], i_univ, names[i]) params = [e_memship, e_dot_memship, i_memship] x0 = [1.57, 0] t0 = 0 t1 = 10 dt = 0.001 state = np.zeros((10001, 2)) i = 0 t = [] r = ode(model).set_integrator('vode').set_f_params( params).set_initial_value(x0, t0) while r.successful() and r.t < t1: r.integrate(r.t + dt) xdes = (math.pi / 2) + math.sin(math.pi * r.t) state[i] = r.y state[i, 0] = xdes - state[i, 0] t.append(r.t) i += 1 plt.plot(t, state[:, 0]) plt.show()
def main(): # create fuzzy system # initial fuzzy set parameters b_0 = np.random.rand(5, 1) * 15.0 c_0 = np.random.rand(5, 1) * 6.0 sig_0 = np.random.rand(5, 1) * 2.0 # create fuzzy sets memship = [] for i in range(len(b_0)): p = [c_0[i, 0], sig_0[i, 0], "none"] memship.append(fuzz.membership("gauss", p, x, "none")) b, c, sig = simulate(memship, b_0, c_0, sig_0) memship = update_fuzzy_sets(memship, c, sig) b, c, sig = simulate(memship, b, c, sig) memship = update_fuzzy_sets(memship, c, sig) b, c, sig = simulate(memship, b, c, sig) memship = update_fuzzy_sets(memship, c, sig) b, c, sig = simulate(memship, b, c, sig) memship = update_fuzzy_sets(memship, c, sig) b, c, sig = simulate(memship, b, c, sig) memship = update_fuzzy_sets(memship, c, sig) b, c, sig = simulate(memship, b, c, sig) memship = update_fuzzy_sets(memship, c, sig) compare(b, c, sig, memship)
def setup_fuzz_sys(): # define universes of input variables e_phi_univ = np.linspace(-math.pi / 2.0, math.pi / 2.0, 1000) e_theta_univ = np.linspace(-math.pi / 2.0, math.pi / 2.0, 1000) e_psi_univ = np.linspace(-math.pi, math.pi, 1000) de_phi_univ = np.linspace(-1.74, 1.74, 1000) de_theta_univ = np.linspace(-1.74, 1.74, 1000) de_psi_univ = np.linspace(-1.74, 1.74, 1000) e_univ = [e_phi_univ, e_theta_univ, e_psi_univ] de_univ = [de_phi_univ, de_theta_univ, de_psi_univ] # define centres for each input e_phi_c = np.array([-0.785, -0.3925, 0, 0.3925, 0.785]) e_theta_c = np.array([-0.785, -0.3925, 0, 0.3925, 0.785]) e_psi_c = np.array([-math.pi / 2, -0.785, 0, 0.785, math.pi / 2]) de_phi_c = np.array([-0.872, -0.436, 0, 0.436, 0.785]) de_theta_c = np.array([-0.872, -0.436, 0, 0.436, 0.785]) de_psi_c = np.array([-0.872, -0.436, 0, 0.436, 0.785]) e_c = [e_phi_c, e_theta_c, e_psi_c] de_c = [de_phi_c, de_theta_c, de_psi_c] # define spreads sig = 0.184 # make memberships e_membership = [] de_membership = [] for i in range(n_angles): temp1 = [] temp2 = [] # get parameters for input c = e_c[i] dc = de_c[i] for j in range(n_sets): # make parameter vectors for sets p1 = [c[j], sig, "none"] p2 = [dc[j], sig, "none"] temp1.append(fuzz.membership("gauss", p1, e_univ[i], "none")) temp2.append(fuzz.membership("gauss", p2, de_univ[i], "none")) e_membership.append(temp1) de_membership.append(temp2) return [e_membership, de_membership]
def main(): x1_univ = np.linspace(-1000, 1000, 10000) x1_fuzz_set_1 = [-1, -1, 0] x1_fuzz_set_2 = [-1, 0, 1] x1_fuzz_set_3 = [0, 1, 1] x2_univ = np.linspace(-1000, 1000, 10000) x2_fuzz_set_1 = [-1, -1, 0] x2_fuzz_set_2 = [-1, 0, 1] x2_fuzz_set_3 = [0, 1, 1] names = ["left", "center", "right"] x1_fuzz_set = [x1_fuzz_set_1, x1_fuzz_set_2, x1_fuzz_set_3] x2_fuzz_set = [x2_fuzz_set_1, x2_fuzz_set_2, x2_fuzz_set_3] x1_fuzz_mem = [] x2_fuzz_mem = [] for i in range(len(x1_fuzz_set)): x1_fuzz_mem.append( fuzz.membership('trimf', x1_fuzz_set[i], x1_univ, names[i])) x2_fuzz_mem.append( fuzz.membership('trimf', x2_fuzz_set[i], x2_univ, names[i])) params = [x1_fuzz_mem, x2_fuzz_mem] x0 = [0, 0] t0 = 0 t1 = 8 dt = 0.001 r = ode(model).set_integrator('vode').set_f_params( params).set_initial_value(x0, t0) state = np.zeros((8000, 2)) t = [] i = 0 while r.successful() and r.t < t1: r.integrate(r.t + dt) state[i] = r.y t.append(r.t) i += 1 plt.plot(t, state[:, 0], t, state[:, 1]) plt.show()
def make_fuzzy_sets(): # define universe of e e_univ = np.linspace(-3, 3, 1000) # centres and spreads c = [-1.5, 0, 1.5] sig = 0.484 # make the fuzzy sets names = ["F1", "F2", "F3"] memship = [] for i in range(len(c)): p = [c[i], sig, "none"] memship.append(fuzz.membership("gauss", p, e_univ, names[i])) return memship
import fuzzy_tools as fuzz import numpy as np p1 = [-1, 0.5] p2 = [0, 0.5] p3 = [1, 0.5] p = [p1, p2, p3] univ = np.linspace(-3, 3, 1000) a = [] for x in p: a.append(fuzz.membership('gauss', x, univ, 'test')) for x in a: fuzz.make_memship(x) fuzz.plot_memship(a)
def main(): e_mem = [] edot_mem = [] v_mem = [] e_fuzz_set_1 = [-0.5, 0.5, 'l_inf'] e_fuzz_set_2 = [-0.25, 0.5, 'none'] e_fuzz_set_3 = [0, 0.5, 'none'] e_fuzz_set_4 = [0.25, 0.5, 'none'] e_fuzz_set_5 = [0.5, 0.5, 'r_inf'] e_univ = np.linspace(-20.5, 20.5, 120) edot_fuzz_set_1 = [-4, 0.5, 'l_inf'] edot_fuzz_set_3 = [-2, 0.5, 'none'] edot_fuzz_set_4 = [0, 0.5, 'none'] edot_fuzz_set_5 = [2, 0.5, 'none'] edot_fuzz_set_2 = [4, 0.5, 'r_inf'] edot_univ = np.linspace(-30, 30, 100) v_fuzz_set_1 = [-10.0] v_fuzz_set_2 = [-5.0] v_fuzz_set_3 = [0.0] v_fuzz_set_4 = [5.0] v_fuzz_set_5 = [10.0] v_univ = np.linspace(-30, 30, 200) e_fuzz_set = [ e_fuzz_set_1, e_fuzz_set_2, e_fuzz_set_3, e_fuzz_set_4, e_fuzz_set_5 ] edot_fuzz_set = [ edot_fuzz_set_1, edot_fuzz_set_2, edot_fuzz_set_3, edot_fuzz_set_4, edot_fuzz_set_5 ] v_fuzz_set = [ v_fuzz_set_1, v_fuzz_set_2, v_fuzz_set_3, v_fuzz_set_4, v_fuzz_set_5 ] names = ['NL', 'NS', 'Z', 'PS', 'PL'] for i in range(len(edot_fuzz_set)): e_mem.append(fuzz.membership('gauss', e_fuzz_set[i], e_univ, names[i])) edot_mem.append( fuzz.membership('gauss', edot_fuzz_set[i], edot_univ, names[i])) v_mem.append( fuzz.membership('singleton', v_fuzz_set[i], v_univ, names[i])) params = [e_mem, edot_mem, v_mem] x0 = [-0.12, 0.0] t0 = 0 t1 = 10 dt = 0.01 r = ode(model).set_integrator('vode').set_initial_value( x0, t0).set_f_params(params) state = np.zeros((1001, 2)) t = [] i = 0 while r.successful() and r.t < t1: r.integrate(r.t + dt) state[i] = r.y t.append(r.t) i += 1 plt.plot(t, state[:, 0]) plt.grid() plt.show()
g_cap = np.zeros_like(x1_cap) for i in range(len(x1_cap)): g_cap[i] = sin(x1_cap[i]) * ((cos(x2_cap[i]))**2) # fuzzy sets centres = [-1.0, -0.5, 0, 0.5, 1] spread = 0.2123 option = 'none' params = [] names = 'none' memship_1 = [] memship_2 = [] for x in centres: params.append([x, spread, option]) for x in params: memship_1.append(fuzz.membership('gauss', x, x1, names)) memship_2.append(fuzz.membership('gauss', x, x2, names)) # fuzzified value set fuzz_val_set_1 = np.zeros((len(x1_cap), len(memship_1))) fuzz_val_set_2 = np.zeros((len(x2_cap), len(memship_2))) temp = np.zeros((len(memship_1), )) for i in range(len(x1_cap)): for j in range(len(memship_1)): fuzz.fuzzify(x1_cap[i], memship_1[j]) temp[j] = memship_1[j].fuzz_val fuzz_val_set_1[i] = temp for i in range(len(x2_cap)): for j in range(len(memship_2)): fuzz.fuzzify(x2_cap[i], memship_2[j]) temp[j] = memship_2[j].fuzz_val
# universe x = np.linspace(0, 6, 100) # no of iterations n_points = 200 # fuzzy sets p1 = [0, 1.184, "none"] p2 = [2, 1.184, "none"] p3 = [4, 1.184, "none"] p4 = [6, 1.184, "none"] params = [p1, p2, p3, p4] input_fuzz_set = [] for z in params: input_fuzz_set.append(fuzz.membership("gauss", z, x, "none")) # to get parameter estimate def get_estimate(data_point_x, prev_lam, prev_theta): # getting fuzzified value fuzz_val = [] for mem in input_fuzz_set: fuzz.fuzzify(data_point_x, mem) fuzz_val.append(mem.fuzz_val) # premise values premise_val = np.zeros((len(fuzz_val),)) for i in range(len(fuzz_val)): premise_val[i] = fuzz_val[i] # implied fuzzy set membership equals the premise values as we are usign singleton sets
g = x - cos(1.5*x) + sin(0.4*x) # datapoints for getting estimated g(x) x_estimate = [0, 1, 2, 3, 4, 5, 6] g_estimate = [-1.0, 1.3187, 3.7073, 4.1428, 4.0394, 5.5627, 7.5866] # fuzzy sets p1 = [0, 1.184, 'none'] p2 = [2, 1.184, 'none'] p3 = [4, 1.184, 'none'] p4 = [6, 1.184, 'none'] params = [p1, p2, p3, p4] memship = [] names = 'none' for z in params: memship.append(fuzz.membership('gauss', z, x, 'none')) # premise memship and fuzzy basis fuzz_val_set = np.zeros((len(x_estimate), len(memship))) temp = np.zeros((len(memship),)) for i in range(len(x_estimate)): for j in range(len(memship)): fuzz.fuzzify(x_estimate[i], memship[j]) temp[j] = memship[j].fuzz_val fuzz_val_set[i] = temp # LSE phi = np.zeros_like(fuzz_val_set) for i in range(len(x_estimate)): a1 = np.sum(fuzz_val_set[i]) b1 = (fuzz_val_set[i])/a1