在网络可靠性中,一种较为经典且在实践中更为常用的可靠度计算便是二终端可靠度,即给定网络拓扑结构与边可靠度(假定节点完全可靠),计算网络中指定的两个节点之间的连通可靠度。
import itertools
def min_path_sets(init_matrix,index_start,index_end):
import re
num_point = init_matrix.shape[0]
min_path_list = []
for i in range(num_point-1):
temp = init_matrix**(i+1)
item = expand(temp[index_start-1,index_end-1])
list_given = re.sub('[ *123456789]',"",str(item)).split("+")
#删除指定阶数下,路径长度不等于阶数的路
index_to_delete = []
for j in range(len(list_given)):
if len(list_given[j])!=(i+1) or list_given[j]=='0':
index_to_delete.append(j)
for counter, index in enumerate(index_to_delete):
index = index - counter
list_given.pop(index)
min_path_list.extend(list_given)
return min_path_list
def str_de_duplication(pstr):
a = ''
for i in range(len(pstr)):
if pstr[i] not in a:
a+=pstr[i]
return a
def product_symbol(pstr,my_dict):
import numpy as np
value_list = []
for i in pstr:
value_list.append(my_dict[i])
return np.prod(value_list)
def generate_label(path_sets,my_dict):
import numpy as np
all_result = []
for exp_num in range(len(path_sets)):
item_Combination = list(itertools.combinations(path_sets, exp_num+1))
item_list = list(map(lambda x: str_de_duplication("".join(x)),item_Combination))
value_list = list(map(lambda x: product_symbol(x,my_dict),item_list))
all_result.append(np.sum(value_list)*(-1)**(exp_num))
return np.sum(all_result)
def Matrix_label(init_matrix,my_dict,index_start,index_end):
path_sets = min_path_sets(init_matrix,index_start,index_end)
pro_value = generate_label(path_sets,my_dict)
return pro_value
from sympy import *
from sympy.abc import A,B,C,D,E,F
index_start = 2
index_end = 1
data = Matrix([[0,A,B],
[A,0,C],
[B,C,0]])
my_dict = {'A':0.8,
'B':0.9,
'C':0.9}
Matrix_label(data,my_dict,index_start,index_end)
在前部分,主要定义了几个函数以便求出最小路集以及利用容斥原理计算二终端可靠度,最终外层函数为Matrix_label(data,my_dict,index_start,index_end):