在網(wǎng)絡(luò)可靠性中,一種較為經(jīng)典且在實(shí)踐中更為常用的可靠度計(jì)算便是二終端可靠度,即給定網(wǎng)絡(luò)拓?fù)浣Y(jié)構(gòu)與邊可靠度(假定節(jié)點(diǎn)完全可靠),計(jì)算網(wǎng)絡(luò)中指定的兩個(gè)節(jié)點(diǎn)之間的連通可靠度。
在此,筆者依據(jù)最小路集思想給出此方法的python代碼實(shí)現(xiàn),該代碼可以依據(jù)給定的輸入矩陣、節(jié)點(diǎn)序號(hào)等設(shè)定值算出兩節(jié)點(diǎn)間的連通可靠度。
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("+")
#刪除指定階數(shù)下,路徑長(zhǎng)度不等于階數(shù)的路
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)
在前部分,主要定義了幾個(gè)函數(shù)以便求出最小路集以及利用容斥原理計(jì)算二終端可靠度,最終外層函數(shù)為Matrix_label(data,my_dict,index_start,index_end):
以上就是使用Python實(shí)現(xiàn)二終端網(wǎng)絡(luò)可靠度的詳細(xì)內(nèi)容,更多關(guān)于二終端網(wǎng)絡(luò)可靠度 的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!