【AI】基于产生式规则的动物识别系统

一、实验目的

【实验内容】
开发一个可以识别虎、金钱豹、斑马、长颈鹿、企鹅、鸵鸟、信天翁等7种动物的产生式系统。
【规则库】
IF 有毛发 THEN 哺乳动物
IF 有奶 THEN 哺乳动物
IF 有羽毛 THEN 鸟
IF 会飞 AND 会下蛋 THEN 鸟
IF 吃肉 THEN 食肉动物
IF 有犬齿 AND 有爪 AND 眼盯前方 THEN 食肉动物
IF 哺乳动物 AND 有蹄 THEN 有蹄类动物
IF 哺乳动物 AND 反刍动物 THEN 有蹄类动物
IF 哺乳动物 AND 食肉动物 AND 黄褐色 AND 暗斑点 THEN 金钱豹
IF 哺乳动物 AND 食肉动物 AND 黄褐色 AND 黑色条纹 THEN 虎
IF 有蹄类动物 AND 长脖子 AND 长腿 AND 暗斑点 THEN 长颈鹿
IF 蹄类动物 AND 黑色条纹 THEN 斑马
IF 鸟 AND 长脖子 AND 长腿 AND 不会飞 AND 黑白二色 THEN 鸵鸟
IF 鸟 AND 会游泳 AND 不会飞 AND 黑白二色 THEN 企鹅
IF 鸟 AND 善飞 THEN 信天翁

二、实验原理

产生式系统用来描述若干个不同的以一个基本概念为基础的系统。这个基本概念就是产生式规则或产生式条件和操作对象的概念。在产生式系统中,知识分为两部分:用事实表示静态知识;用产生式规则表示推理过程和行为。

三、算法设计

1.建立知识库。可将规则中的每一个前提条件、中间结论及最终结论转换为一个对应的唯一的数字,如:“1 有毛发 2 产奶 …… 23 食肉类 …… 31 信天翁”等,并将其用字典形式储存起来。其中键为1,值为有毛发,以此类推。
2.建立规则库。可依据书中给出的规则结合条件或结论的对应数字序号对产生式规则进行转换,如1→21,2→21……20、22→31等,并将其写入列表。
3.建立事实库。本实验中的事实库可在程序开始直接输入,用户根据需要选择,要求用户输入动物特征的数字序号,进行识别。如果未识别出来,可以重新选择或者退出。
4.运行推理。

四、系统代码设计

'''system designed by MoBrook_Susie'''

# 事实库
# fact = [12, 14, 15, 2, 10]
fact = list(map(int, input("请输入动物特征编号:").strip().split(" ")))

# 知识库
knowledge = {1:"有毛发", 2:"有奶", 3:"有羽毛", 4:"会飞", 5:"会下蛋",6:"吃肉",7:"有犬齿",8:"有爪", 9:"眼盯前方", 10:"有蹄", 11:"黄褐色" 12:"暗斑点", 13:"黑色条纹", 14:"长脖子", 15:"长腿", 16:"不会飞", 17:"黑白二色", 18:"会游泳", 19:"善飞",
             20:"哺乳动物", 21:"鸟", 22:"食肉动物", 23:"有蹄类动物", 24:"反刍动物",
             25:"金钱豹", 26:"虎", 27:"长颈鹿", 28:"斑马", 29:"鸵鸟", 30:"企鹅", 31:"信天翁", }

# 规则库
def regulation(fact):
    if 1 in fact and 20 not in fact:  #确保20不在事实库的话,才执行,否则会一直添加20
        fact.append(20)
    if 2 in fact and 20 not in fact:
        fact.append(20)
    if 3 in fact and 21 not in fact:
        fact.append(21)
    if 4 in fact and 5 in fact and 21 not in fact:
        fact.append(21)
    if 6 in fact and 22 not in fact:
        fact.append(22)
    if 7 in fact and 8 in fact and 9 in fact and 22 not in fact:
        fact.append(22)
    if 20 in fact and 10 in fact and 23 not in fact:
        fact.append(23)
    if 20 in fact and 24 in fact and 23 not in fact:
        fact.append(23)
    if 20 in fact and 22 in fact and 11 in fact and 12 in fact and 25 not in fact:
        fact.append(25)
    if 20 in fact and 22 in fact and 11 in fact and 13 in fact and 26 not in fact:
        fact.append(26)
    if 10 in fact and 14 in fact and 15 in fact and 12 in fact and 27 not in fact:
        fact.append(27)
    if 23 in fact and 13 in fact and 28 not in fact:
        fact.append(28)
    if 21 in fact and 14  in fact and 15 in fact and 16 in fact and 17 in fact and 29 not in fact:
        fact.append(29)
    if 21 in fact and 18 in fact and 16 in fact and 17 in fact and 30 not in fact:
        fact.append(30)
    if 21 in fact and 19 in fact and 31 not in fact:
        fact.append(31)

if __name__ == '__main__':
    print("该动物的特征有:"+"、".join(map(lambda x:knowledge[x],fact)))
    while(True):
        star_fact = len(fact)
        regulation(fact)
        end_fact = len(fact)
        if star_fact == end_fact or fact[-1]>24:  # 若无变化或已得出具体动物(25~31),则停止
            print("这个动物是"+knowledge[fact[-1]])
            break
  • 0
    点赞
  • 47
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值