归结反演实验

归结反演实验(鲁滨逊归结原理)

实验原理:
归结反演实验_第1张图片
代码

# 输入命题集合
 # P(A)∨P(B)∨P(C) ┐P(A)∨P(B)∨P(C) ┐P(B)∨P(C) ┐P(B)
 # P(A)∨P(B)∨P(C) ┐P(A)∨P(B)∨P(C) ┐P(B)∨P(C) ┐P(C)


# 输入函数
def inputset():
    lists=[]
    print("请输入命题集合:两个命题之间用空格分割开,∨ ∧  ┐分别表示析取、合取、非")
    listset=input()
    lists= listset.split(' ')
    return lists

# 字符换数字
def changeinto1(listc):
    listnum=[]
    for k in range(len(listc)):
        if (listc[k]=='P(A)'):
            listnum.append(1)
        elif(listc[k]=='P(B)'):
            listnum.append(2)
        elif (listc[k] == 'P(C)'):
            listnum.append(3)
        elif (listc[k] == '┐P(A)'):
            listnum.append(-1)
        elif (listc[k] == '┐P(B)'):
            listnum.append(-2)
        elif (listc[k] == '┐P(C)'):
            listnum.append(-3)
    return listnum

# 匹配互补
def matchclose(list1,list2,k,l):
    for i in range(len(list1)):
        for j in range(len(list2)):
            if(list1[i]+list2[j]==0):
                if(list1[i]==1):
                    print('第' , k , '组的P(A)与第' , l , '组的┐P(A)互补')
                elif(list1[i]==-1):
                    print("第" , k , "组的┐P(A)与第" , l , "组的P(A)互补")
                elif (list1[i] == 2):
                    print("第" , k , "组的P(B)与第" , l , "组的┐P(B)互补")
                elif (list1[i] == -2):
                    print("第" , k , "组的┐P(B)与第" , l , "组的P(B)互补")
                elif (list1[i] == 3):
                    print("第" , k , "组的P(C)与第" , l , "组的┐P(C)互补")
                elif (list1[i]== -3):
                    print("第" , k , "组的┐P(C)与第" , l , "组的P(C)互补")

# 匹配归结
def matchclose1(list1,list2,count):
    list1=sorted(list1)
    list2=sorted(list2)
    flag=0
    # 判断子句是否相同
    for i in range(len(list2)):
        if(list1[i+count]!=list2[i] and list1[i+count]+list2[i]!=0):
            flag=1
    if(flag==1):
        return 0
    else:
        list3=[]
        if(count!=0):
            for i in range(count):
                list3.append(0)
        for i in range(len(list2)):
            if(list1[i+count]==list2[i]):
                list3.append(list2[i])
            else:
                if(list2[i]==1):
                    print("┐P(A)与P(A)互补")
                elif(list2[i]==-1):
                    print("P(A)与┐P(A)互补")
                elif (list2[i] == 2):
                    print("┐P(B)与P(B)互补")
                elif (list2[i] == -2):
                    print("P(B)与┐P(B)互补")
                elif (list2[i] == 3):
                    print("┐P(C)与P(C)互补")
                elif (list2[i] == -3):
                    print("P(C)与┐P(C)互补")
                list3.append(0)
    return list3


# 归结演算
def matchanswer(list1,list2,g):
    count=0
    for i in range(len(list1)):
        if(list1[i]==0):
            count=count+1
    i1=len(list1)-count
    i2=len(list2)
    list3=list1
    if(i1==i2):
        list1=matchclose1(list1,list2,count)
        if(list1!=0):
            print("在第",g+1,"组归结")
            return list1
        else:
            print("\n无法归结!!!")
            return list3

lists=inputset()
print("命题集合展示:")
listchar1=[]
for i in range(len(lists)):
    print(lists[i])
    listchar1.append(lists[i].split('∨'))
print("\n")

#     将命题通过析取符号进行划分,在转换成数字
print("通过析取式划分后的结果:")
listnum=[]
for j in range(len(listchar1)):
    print(listchar1[j])
    listnum.append(changeinto1(listchar1[j]))
print("\n")
print("转化成相应的数字后的结果:")
for k in range(len(listnum)):
    print(listnum[k])

# 将命题集合排序
print("\n")
print("按命题式子的长度排序(从长到短)后的结果为:")
listnum=sorted(listnum, key=len,reverse = True )
print(listnum)
# 得到命题集合的长度
# print(len(listnum))
# 双重循环判断互补文字
print("\n")
print("式子互补输出:")
for i in range(len(listnum)-1):
    for j in range(i+1,len(listnum)):
        matchclose(listnum[i],listnum[j],i+1,j+1)

print("\n")
print("归结演算过程:")
listjudge=listnum[0]
for i in range(1,len(listnum)):
    listjudge=matchanswer(listjudge,listnum[i],i)
    print(listjudge)

# 验证是否成功
if(listjudge!=[0,0,0]):
    print("证明错误!")
else:
    print("证明成功!!!")

小结:大部分还是通过if-else实现的,它可以帮助我们去简化一个式子得到最简,并且我们可以利用它的思想去思考如何将其他方面的运算减少,这样以来增大了我们的运算速度,二来可以简化问题。

你可能感兴趣的:(人工智能,python,人工智能)