动物式产生推理系统

动物式产生推理系统

产生式系统
把一组领域相关的产生式(或称规则)放在一起,让它们互相配合、协同动作,一个产生式生成的结论一般可供另一个(或一些)产生式作为前提或前提的一部分来使用,以这种方式求得问题之解决,这样的一组产生式被称为产生式系统
产生式系统的构成

  1. 规则
    每条规则分为左部(或称前提、前件)和右部(或称结论、动作、后件)。通常左部表示条件,核查左部条件是否得到满足一般采用匹配方第 3 页法,即查看数据基DB(Data Base)中是否存在左部所指明的情况,若存在则认为匹配成功,否则认为匹配失败。一般说来,匹配成功则执行右部所规定的动作,例如:添加、修改和删除等。
  2. 数据集–目标数据库TB
    DB 中存放的数据是产生式作用的对象
    在这里插入图片描述
  3. 一个推理程序
    它负责整个产生式系统的运行,包括:规则左部与 DB 匹配;从匹配成功的规则中,选出一条将在下一步执行的规则 R*,执行 R*右部规定的动作;掌握时间结束产生式系统的运行。
  4. 知识库–规则数据库RB

在这里插入图片描述
5.运行结果----综合数据库DB
在这里插入图片描述
Github地址:
https://github.com/okeyxyw/Animal_production_reasoning_system
python代码


import numpy as np

class productionSystem:
    def __init__(self):
        f=open("目标数据库TB.txt","r",encoding='utf8')
        self.productions=[]
        self.charicters=[]
        self.myclass=(f.readline().strip("最终类别为:").strip()).split(",")
        f.close()
        f=open("规则数据库RB.txt","r")
        for line in f.readlines():
            if(line!='\n'):
                line=line.replace("->"," ")
                line=line.replace(","," ")
                line=line.split()
                self.productions.append(line)
                for ch in line:
                    if (ch not in self.myclass) and (ch not in self.charicters):
                        self.charicters.append(ch)
        f.close()
    def showProduction(self):
        print()
    def match(self,x):
        for ch in self.productions[x][:-1]:
            if(ch in self.work):
                continue
            else:
                return False
        return True
    def classify(self,work):
        used=np.zeros(len(self.productions))
        counter=len(self.work)
        #self.s=[]
        f=open('综合数据库DB.txt','a')
        f.write('\n推理规则:\n')
        while(True):
            for i in range(len(self.productions)):
                if(used[i]==0 and self.match(i)):
                    used[i]=1
                    # print ('IF ')
                    #print((self.productions[i][:-1]),'-->',self.productions[i][-1])
                    STR=''
                    for J in self.productions[i][:-1]:
                        STR+=J
                        if J!=self.productions[i][-2]:
                            STR+=' AND '
                    f.write('   IF '+STR+' THEN '+self.productions[i][-1]+'\n')
                    print(' IF ',STR,' THEN ',self.productions[i][-1])
                    self.work.append(self.productions[i][-1])
                    if(self.work[-1] in self.myclass):
                        return True
            tmp=len(self.work)
            if(tmp==counter):
                return False
            else:
                counter=tmp
        #f.close()
    def dowork(self):
        print("目标数据库TB:所有类别有:")
        for t in self.myclass:
            print('{:<10}'.format(t),end="")
        print()
        print("所有属性有:")
        counter=0
        for t in self.charicters:
            counter+=1
            if(counter%5==0):
                print('{:<10}'.format(t))
            else:
                print('{:<10}'.format(t),end="")
        self.work=input("\n综合数据库DB-请输入属性(属性之间用空格隔开):\n")
        self.work=self.work.split()
        st='属性'+str(self.work)
        f = open('综合数据库DB.txt', 'a')
        f.write('\n\n\n')
        f.write(st)
        f.close()
        if(self.classify(self.work)):
            f = open('综合数据库DB.txt', 'a')
            print("该物种为:"+self.work[-1])
            f.write("该物种为:"+self.work[-1]+'\n')
            f.close()
        else:
            f = open('综合数据库DB.txt', 'a')
            print("抱歉,未能识别出该物种\n")
            f.write("抱歉,未能识别出该物种\n")
            f.close()
def main():
    p=productionSystem()
    p.dowork()
if __name__=="__main__":
    print('动物式产生推理系统open!!!!')
    c=0
    # f=open('综合数据库DB.txt','w')
    # while(True):
        # print("-----------------------------------------")
        # s=str(c)+'\n\n'
        # print(s)
        # f.write(s)
    main()
    # c+=1
    print('-----------------------------------------')
    print('写入"综合数据库DB.txt"成功!!!!!')
    print('-----------------------------------------')
    print('end!!')
    # f.close()
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值