根据g_i (x,a_i,b_i,c_i,d_i)={█(0,x<a_i@〖((x-a_i)/(b_i-a_i))〗^2,a_i⩽x<b_i@1,b_i⩽x<c_i@〖((d_i-x)/(d_i-c_i))〗^2,c_i⩽x<d_i@0,x⩾d_i )隶属度函数
时间: 2023-12-07 15:06:04 浏览: 40
该隶属度函数是一个梯形隶属度函数,用于模糊逻辑中的模糊集合表示。其中,x是输入变量,a_i、b_i、c_i和d_i是该隶属度函数的参数。该隶属度函数将输入变量x映射到隶属度值,表示x属于该模糊集合的程度。
具体来说,该隶属度函数可以表示为:
g_i (x,a_i,b_i,c_i,d_i) = {
0, x < a_i
((x-a_i)/(b_i-a_i))^2, a_i <= x < b_i
1, b_i <= x < c_i
((d_i-x)/(d_i-c_i))^2, c_i <= x < d_i
0, x >= d_i
}
当x小于a_i时,该隶属度函数的隶属度值为0,表示x不属于该模糊集合。当x在a_i和b_i之间时,隶属度值逐渐增加,用二次函数进行插值。当x在b_i和c_i之间时,隶属度值始终为1,表示x完全属于该模糊集合。当x在c_i和d_i之间时,隶属度值逐渐减小,用二次函数进行插值。当x大于等于d_i时,隶属度值为0,表示x不属于该模糊集合。
相关问题
root = tk.Tk() root.withdraw() f_path = filedialog.askopenfilename() I0 = cv2.imread(f_path ) b, g, r = cv2.split(I0) m, n = r.shape flag = False mode = 0 def abc(x): global flag a = keyboard.KeyboardEvent(event_type='down', scan_code=2, name='1') b = keyboard.KeyboardEvent(event_type='down', scan_code=3, name='2') c = keyboard.KeyboardEvent(event_type='down', scan_code=4, name='3') if x.event_type == a.event_type and x.scan_code == a.scan_code: print("迭代式阈值选择算法") mode = 1 flag = True if x.event_type == b.event_type and x.scan_code == b.scan_code: print("大律算法") flag = True mode = 2 if x.event_type == c.event_type and x.scan_code == c.scan_code: print("三角算法") flag = True mode = 3 keyboard.hook(abc) if flag == False: time.sleep(5) # 等待5秒 if mode == 1: i_b = b.ravel() mea1_b = np.mean(i_b) mea = np.zeros(shape=(1, 1)) while True: mea1 = mea[0] i_b1 = np.where(i_b > mea1_b) mea2_b = np.mean(i_b[i_b1]) i_b2 = np.where(i_b < mea1_b) mea3_b = np.mean(i_b[i_b2]) mea1_b = (mea3_b + mea2_b) / 2 if mea1_b == mea1: break else: mea[0] = mea1_b I_B = b I_B[I_B > mea1_b] = 255 I_B[I_B < mea1_b] = 0 area_b = np.where(I_B == 255) print(mea1_b) i_r = r.ravel() mea1_r = np.mean(i_r) mea = np.zeros(shape=(1, 1)) while True: mea1 = mea[0] i_r1 = np.where(i_r > mea1_r) mea2_r = np.mean(i_r[i_r1]) i_r2 = np.where(i_r < mea1_r) mea3_r = np.mean(i_r[i_r2]) mea1_r = (mea3_r + mea2_r) / 2 if mea1_r == mea1: break else: mea[0] = mea1_r I_R = r I_R[I_R > mea1_r] = 255 I_R[I_R < mea1_r] = 0 area_r = np.where(I_R == 255) I_Rx = area_r[0] I_Ry = area_r[1] print(mea1_r)哪里出现了问题
在函数 `abc(x)` 中,变量 `mode` 在函数内部被赋值,但没有使用 `global` 声明为全局变量。因此,在后面的条件语句中,`mode` 始终为 0,导致程序一直执行 `mode == 1` 的分支,而不是根据用户输入的按键执行相应的阈值选择算法。解决方法是在函数 `abc(x)` 中,将 `mode` 声明为全局变量,即在函数开头加上 `global mode`。
import networkx as nx import numpy as np import pandas as pd import matplotlib.pyplot as plt import networkx as nx import random df=pd.read_csv("D:\级联失效\edges.csv") G=nx.from_pandas_edgelist(df,'from','to',create_using=nx.Graph()) nx.draw(G,node_size=300,with_labels=True) As=nx.adjacency_matrix(G) A=As.todense() def f(x): F=4*x*(1-x) return F n=len(A) r=2 ohxs=0.4 step=10 d=np.zeros([n,step]) for i in range(n): d[i,0]=np.sum(A[i]) x_intial=np.zeros([n,step]) for i in range(n): x_intial[i,0]=random.random() np.set_printoptions(precision=5) h_a=100 H=np.zeros([n,step]) D=np.zeros([n,step]) for i in range(n): Deg=0 for k in range(n): if k!=i: Deg=Deg+d[k,0] D[i,0]=Deg H[i,0]=d[i,0]/D[i,0]/h_a fail_scale=np.zeros(step) fail_scale[0]=1 node_rand_id=random.randint(0,n) r=2 x_intial[node_rand_id,0]=x_intial[node_rand_id,0]+r print(x_intial) fail_node=np.zeros(n) fail_node[node_rand_id]=1 print(fail_node) np.seterr(divide='ignore',invalid='ignore') for t in range(1,step): fail_node_id=[idx for (idx,val) in enumerate(fail_node) if val ==1] for i in range(n): sum=0 for j in range(n): sum = sum+A[i,j]*f(x_intial[j,t-1])/d[i] if i in fail_node_id: x_intial[i,t-1]=0 A[i,:]=0 A[:,i]=0 else: x_intial[i,t]=H[i,t-1]*abs((1-ohxs)*f(x_intial[i,t-1])+ohxs*sum) d[i,t]=np.sum(A[i]) Deg=0 for k in range(n): if k!=i: Deg=Deg+d[i,t] D[i,t]=Deg H[i,t]=d[i,t]/D[i,t]/h_a new_fail_id=[idx for (idx,val) in enumerate(x_intial[:,t]) if val>=1] fail_scale[t]=fail_scale[t-1]+len(new_fail_id) fail_node[new_fail_id]=1 x_intial[new_fail_id,t]=x_intial[new_fail_id,t]+r print(H[i,t]) print(fail_node) print(x_intial) plt.plot(fail_scale) plt.show()
这是一个使用 Python 编写的网络模型,使用了 networkx 库来构建网络。代码中从 csv 文件中读取了网络的边,然后使用 from_pandas_edgelist 方法将边转化为图。接着定义了一个 f(x) 函数,对应于网络中每个节点的失效概率。代码中还定义了一些变量和参数,如节点个数 n、失效概率增长率 r、节点失效后的修复时间 step 等。接下来的循环中,初始化了网络中每个节点的失效概率,并定义了一些辅助变量。然后对网络进行模拟,计算每个节点的失效概率,并记录失效节点的数量。最后,将失效节点的数量用 matplotlib 绘制成图表。
阅读全文