一 实验原理
证据理论是Dempster于1967年首先提出,由他的学生Shafer于1976年进一步发展起来的一种不精确推理理论,也称为Dempster/Shafer 证据理论(D-S证据理论),属于人工智能范畴,最早应用于专家系统中,具有处理不确定信息的能力。作为一种不确定推理方法,证据理论的主要特点是:满足比贝叶斯概率论更弱的条件;具有直接表达“不确定”和“不知道”的能力。 在DS证据理论中,由互不相容的基本命题(假定)组成的完备集合称为识别框架,表示对某一问题的所有可能答案,但其中只有一个答案是正确的。该框架的子集称为命题。分配给各命题的信任程度称为基本概率分配(BPA,也称m函数),m(A)为基本可信数,反映着对A的信度大小。信任函数Bel(A)表示对命题A的信任程度,似然函数Pl(A)表示对命题A非假的信任程度,也即对A似乎可能成立的不确定性度量,实际上,[Bel(A),Pl(A)]表示A的不确定区间,[0,Bel(A)]表示命题A支持证据区间,[0,Pl(A)]表示命题A的拟信区间, [Pl(A),1]表示命题A的拒绝证据区间。设m1和m2是由两个独立的证据源(传感器)导出的基本概率分配函数,则Dempster组合规则可以计算这两个证据共同作用产生的反映融合信息的新的基本概率分配函数。
二· 案例描述
- 案例描述:
- 某个病人可能患有的病:[cold,flu,meningitis,indigestion],可以根据不同医生给出的证据推理出这个病人大概率患哪几种病。
- 问题描述:
- 在识别框架[cold,flu,meningitis,indigestion]下,
- 给定以下三个不同的证据:
- [[{'cold', 'flu'},0.6], [{'meningitis'},0.2], [{'indigestion'},0.1], [{''},0.1]]
- [[{'meningitis','indigestion'},0.7], [{''},0.3]]
- [[{'cold','flu','indigestion'},0.99], [{''},0.01]]
- ' '代表全集。分别计算:
- ['flu','indigestion','cold'],
- ['flu','cold'],
- ['meningitis','indigestion'],['indigestion'],['meningitis']
- 的信任区间。
- 具体步骤:
- (1) 定义mass融合函数,融合不同证据提供的mass函数得到组合mass函数
- (2) 定义信任函数和似然函数。
- (3) 整理结果得到最终信任区间
三 实验代码
#融合函数
def combination(d1, d2):
united = set(d1.keys()).union(set(d2.keys()))
result=dict.fromkeys(united,0)
for i in d1.keys():
for j in d2.keys():
if {''}==i and {''}==j:
result[i]+=d1[i]*d2[j]
else:
if {''}==i:
result[j]+=d1[i]*d2[j]
if {''}==j:
result[i]+=d1[i]*d2[j]
if {''}!=i and {''}!=j:
common = frozenset(i.intersection(j))
for k in result.keys():
if (len(common)!=0 and(common==k)):
result[k] += d1[i]*d2[j]
break
for i in result.keys():
result[i] = round(result[i],4)
f= sum(list(result.values()))
f=round(f,4)
for i in result.keys():
result[i] =round(result[i]/f,4)
return result
#mass函数
def get_mass(allLines):
mass = {}
previousLine = {}
currentLine = {}
previousLine = transformset(ast.literal_eval(allLines[0]))
for line in range(1,len(allLines)):
if allLines[line][0]!='#':
currentLine = transformset(ast.literal_eval(allLines[line]))
mass = combination(previousLine,currentLine)
previousLine=mass
return mass.copy()
#信任函数
def get_beliefs(masses):
belief = masses.copy()
for i in belief.keys():
for j in belief.keys():
if(i!=j):
if i.issuperset(j) and {''}!=i and {''}!=j:
belief[i]+=masses[j]
for i in belief.keys():
belief[i] = round(belief[i],4)
return belief
#似然函数
def get_plausibility(masses):
plausibility = masses.copy()
for i in plausibility.keys():
plausibility[i] = 0
for i in plausibility.keys():
for j in plausibility.keys():
if len(i.intersection(j))!=0 and {''}!=i:
plausibility[i]+=masses[j];
if {''}==j:
plausibility[i]+=masses[j]
for i in plausibility.keys():
plausibility[i] = round(plausibility[i],4)
return plausibility
#操作
import operator
def getkeys(structure):
return set([elem[0] for elem in structure])
import json
def transformset(input):
return dict((frozenset(elem[0]),elem[1]) for elem in input)
def filter_results(beliefs, plausibility):
finalSet = {}
for elem in beliefs.keys():
finalSet[elem] = beliefs[elem]
return finalSet
def get_final_result(elements, plausibility):
resultString = ''
for elem in elements:
setOfElems = elem[0]
movieTypes = str(setOfElems)
if elem[0] != {''}:
resultString += movieTypes[11:-2]+' ['+str(elem[1])+', '+str(plausibility[elem[0]])+']\n'
return resultString
#证据
s = '''[[{'cold', 'flu'},0.6], [{'meningitis'},0.2], [{'indigestion'},0.1], [{''},0.1]]
[[{'meningitis','indigestion'},0.7], [{''},0.3]]
[[{'cold','flu','indigestion'},0.99], [{''},0.01]]'''
#主函数
import io
import ast
f = io.StringIO(s)
mass={}
lines = f.readlines()
if len(lines)>1:
mass = get_mass(lines)
beliefs = get_beliefs(mass)
plausibility = get_plausibility(mass)
final_set = filter_results(beliefs, plausibility)
all_elements=sorted(final_set.items(), key=operator.itemgetter(1),reverse=True)
print("区间")
print(get_final_result(all_elements, plausibility))
区间 'flu', 'indigestion', 'cold' [0.9923, 0.9949] 'flu', 'cold' [0.4712, 0.5498] 'indigestion', 'meningitis' [0.4503, 0.5289] 'indigestion' [0.4433, 0.5237] 'meningitis' [0.0052, 0.0078]