【人工智能Ⅰ】实验1:谓词表示法与产生式知识表示

news/2024/7/5 3:27:15

实验1  谓词表示法与产生式知识表示

一、实验目的

1、熟悉谓词逻辑表示法;

2、理解和掌握产生式知识表示方法,实现产生式系统的规则库。

二、实验内容

要求通过C/C++/python语言编程实现:

1、猴子摘香蕉问题

2、动物识别系统

(1)建立识别七种动物识别系统的规则;

(2)确定需要识别的动物及其属性(7种动物,即:老虎、金钱豹、斑马、长颈鹿、企鹅、鸵鸟和信天翁)。

三、问题描述

1、房子里有一只猴子(即机器人),位于a处。在c处上方的天花板上有一串香蕉,猴子想吃,但摘不到。房间的b处还有一个箱子,如果猴子站到箱子上,就可以摸着天花板。如图1所示,对于上述问题,可以通过谓词逻辑表示法来描述知识。实现猴子摘香蕉问题的求解过程。

图1  猴子摘香蕉问题

2、建立一个动物识别系统的规则库,编写程序用以识别虎、豹、斑马、长颈鹿、企鹅、鸵鸟、信天翁等7种动物。

为了识别这些动物,可以根据动物识别的特征,建立包含下述规则库:            

R1:if 动物有毛发  then  动物是哺乳动物

    R2:if 动物有奶  then  动物是哺乳动物

    R3:if 动物有羽毛  then  动物是鸟

    R4:if 动物会飞  and  会生蛋 then  动物是鸟

    R5:if 动物吃肉 then 动物是食肉动物

    R6:if 动物有犀利牙齿 and 有爪 and 眼向前方 then 动物是食肉动物

    R7:if 动物是哺乳动物and有蹄then动物是有蹄类动物

    R8:if 动物是哺乳动物and反刍then动物是有蹄类动物

    R9:if 动物是哺乳动物and是食肉动物and有黄褐色 and 有暗斑点 then 动物是豹

    R10:if 动物是哺乳动物 and是食肉动物and有黄褐色 and有黑色条纹 then 动物是虎

    R11:if动物是有蹄类动物 and 有长脖子and有长腿and有暗斑点 then 动物是长颈鹿

    R12:if 动物是有蹄类动物 and有黑色条纹 then 动物是斑马

    R13:if 动物是鸟and不会飞 and有长脖子and有长腿 and有黑白二色 then 动物是鸵鸟

    R14:if 动物是鸟 and不会飞 and会游泳 and有黑白二色 then  动物是企鹅

    R15:if 动物是鸟 and善飞 then 动物是信天翁

四、编写程序并调试(源程序、实验过程与结果)

1:猴子摘香蕉问题

1.1:源程序

dict = {}

count = 0

while 1:

    if count == 3:

        break

    item = input("please input your item:")

    site = input("please input your site:")

    if item != 'monkey' and item != 'box' and item != 'banana':

        print("input error")

    else:

        dict.setdefault(item, site)

        count += 1

print() # change lines

step = 1 #initialzie step

climb = 0

gain = 0

# if monkey is not at box site

if dict['monkey'] != dict['box']:

    result = "step"+str(step)+": move monkey to box"

    print(result,end=" ")

    print(dict['monkey']+"->"+dict['box'])

    dict['monkey']=dict['box']

    step += 1

# if box is not at banana site

if dict['box'] != dict['banana']:

    result = "step"+str(step)+": move box to banana"

    print(result,end=" ")

    print(dict['box']+"->"+dict['banana'])

    dict['box']=dict['banana']

    dict['monkey']=dict['banana']

    step += 1

# monkey climbs onto the box

if climb == 0:

    result = "step"+str(step)+": monkey climbs on the box"

    print(result,end=" ")

    print(dict['monkey']+"->"+dict['box'])

    step += 1

    climb = 1

# monkey jumps to get the banana

if climb == 1 and gain == 0:

    result = "step"+str(step)+": monkey jumps to get the banana"

    print(result,end=" ")

    print(dict['box']+"->"+dict['banana'])

    step += 1

    gain = 1

 1.2:实验过程与结果


如下图所示,依次输入三个物体及其地点,例如猴子在a点、箱子在b点、香蕉在c点。输入完毕后,程序自动显示每一步的操作。

以图中的结果为例,第一步命令猴子移动到箱子处,即a点到b点;第二步命令猴子将箱子移动到香蕉处,即b点到c点;第三步命令猴子爬到箱子上,地点不变;第四步命令猴子跳起来拿到香蕉,地点不变。综上所述,通过上述4个步骤即可实现猴子拿到香蕉的结果。

 

2:动物识别系统

    2.1:源程序

dict = {}

dict.setdefault(1, "有毛发")

dict.setdefault(2, "有奶")

dict.setdefault(3, "有羽毛")

dict.setdefault(4, "会飞")

dict.setdefault(5, "会生蛋")

dict.setdefault(6, "吃肉")

dict.setdefault(7, "犀利牙齿")

dict.setdefault(8, "有爪")

dict.setdefault(9, "眼向前方")

dict.setdefault(10, "有蹄")

dict.setdefault(11, "反刍")

dict.setdefault(12, "有黄褐色")

dict.setdefault(13, "有暗斑点")

dict.setdefault(14, "有黑色条纹")

dict.setdefault(15, "有长脖子")

dict.setdefault(16, "有长腿")

dict.setdefault(17, "不会飞")

dict.setdefault(18, "有黑白二色")

dict.setdefault(19, "会游泳")

dict.setdefault(20, "善飞")

mammal = 0  # 哺乳动物

bird = 0    #

meat = 0    # 食肉动物

foot = 0    # 蹄类动物

print(dict)

print("please later input features according to this directory")

feature = []

n = int(input("please input the total number of your features: "))

# add int() to convert str into int, or error will occur

for i in range(n):

    t = int(input("please input your current feature number: "))

    feature.append(t)

# hash features

hash = []

for i in range(21):

    hash.append(0)

for item in feature:

    hash[item]=1

# rules

result = ""

check = 0

# rule 1 2

if hash[1] == 1 or hash[2] == 1:

    mammal = 1

# rule 3 4

if hash[3] == 1 or (hash[4] == 1 and hash[5] == 1):

    bird = 1

# rule 5 6

if hash[6] == 1 or (hash[7] == 1 and hash[8] == 1 and hash[9] == 1):

    meat = 1

# rule 7 8

if mammal == 1 and (hash[10] == 1 or hash[11] == 1):

    foot = 1

# rule 9

if mammal == 1 and meat == 1 and hash[12] == 1 and hash[13] == 1:

    if check == 0:

        check = 1

        result = ""

# rule 10

if mammal == 1 and meat == 1 and hash[12] == 1 and hash[14] == 1:

    if check == 0:

        check = 1

        result = ""

# rule 11

if foot == 1 and hash[15] == 1 and hash[16] == 1 and hash[13] == 1:

    if check == 0:

        check = 1

        result = "长颈鹿"

# rule 12

if foot == 1 and hash[14] == 1:

    if check == 0:

        check = 1

        result = "斑马"

# rule 13

if bird == 1 and hash[17] == 1 and hash[15] == 1 and hash[16] == 1 and hash[18] == 1:

    if check == 0:

        check = 1

        result = "鸵鸟"

# rule 14

if bird == 1 and hash[17] == 1 and hash[19] == 1 and hash[18] == 1:

    if check == 0:

        check = 1

        result = "企鹅"

# rule 15

if bird == 1 and hash[20] == 1:

    if check == 0:

        check = 1

        result = "信天翁"

if check == 1:

    print("判断的结果是:"+result)

else:

    print("无法判断")

    2.2:实验过程与结果

在实验过程中,首先编写供用户查阅编号和对应特征的字典,然后令用户根据提示输入特征的总数和每个特征的编号,中间使用哈希表将用户输入的特征所对应的表内位置设定为1,不涉及的特征设定为0,再通过if语句逐条判断规则是否成立,如果成立则将相应的判定结果进行标记,最后输出判断的结果。

值得注意的是,有可能出现无法判断的情况。以下将展示测试规则系统中的各类判断结果,并逐个列举用户所使用的特征。

用户字典:

·测试豹:1、2、6、7、8、9、12、13

·测试虎:1、2、6、7、8、9、12、14

·测试斑马:1、2、10、11、14、17、19

·测试长颈鹿:1、10、13、15、16

·测试企鹅:3、5、17、18、19

·测试鸵鸟:3、5、15、16、17、18


·测试信天翁:3、20


·测试无法判断:1、3、6

 综上所述,各类情况均判断完毕,结果如图所示。

五、遇到的问题和解决办法

问题1:

n = int(input("please input the total number of your features: "))

# add int() to convert str into int, or error will occur

for i in range(n):

    t = int(input("please input your current feature number: "))

    feature.append(t)

上述代码在没有加int()前,input默认输入的为字符串,导致在循环开头时,n不是整数类型而报错。

解决方法1:

将 n = input() 改为 n = int(input()) 即可。


http://lihuaxi.xjx100.cn/news/1717098.html

相关文章

长城首款MPV上市,能否迎来「高山」时刻?

作者 | Amy 编辑 | 德新 去年下半年起,自主品牌接二连三抢占高端MPV市场,腾势D9、岚图梦想家、极氪009以及传祺E9等车型接连亮相。 国泰君安证券研究报告显示,新能源MPV 2021年、2022年年销量分别为4.4万和9.2万辆,今年上半年销…

命令行输入sns.countplot(data[‘marital‘])报错

代码sns.countplot(data[marital])是用于绘制柱状图来显示分类变量的频数分布。但是,根据警告信息,从Seaborn 0.12版本开始,只有一个有效的位置参数data,其他参数必须作为关键字参数传递。因此,你需要将参数data[marit…

IDENTITY_INSERT 设置为 OFF 时,不能为表 ‘t_user‘ 中的标识列插入显式值

出现这个问题的原因 出现这个问题一般都是SQL server数据库,在创建表主键的时候双击修改标识规范默认自增,如果再插入显示的值就会出现这样的问题。这样的问题是非常常见的,通常会出现在大量数据插入表中,列如 解决的办法 在…

双非本两年经验,靠这套Java面试题拿下拿下阿里、百度、美团、滴滴、快手、拼多多等大厂offer

背景 博主是双非大学毕业,有两年的互联网经验 社招面试也是一样的流程:项目 八股 算法 项目: 公司项目,涉及的技术包括但不限: 管理域:DDD、CQRS、事件总线、命令总线 运行域:微内核、规则…

YOLO目标检测——行人数据集【含对应voc、coco和yolo三种格式标签+划分脚本】

实际项目应用:智能监控、人机交互、行为分析、安全防护数据集说明:行人检测数据集,真实场景的高质量图片数据,数据场景丰富标签说明:使用lableimg标注软件标注,标注框质量高,含voc(xml)、coco(j…

利用MATLAB创建栅格地图(代码可复制)

先做一个声明:文章是由我的个人公众号中的推送直接复制粘贴而来,因此对智能优化算法感兴趣的朋友,可关注我的个人公众号:启发式算法讨论。我会不定期在公众号里分享不同的智能优化算法,经典的,或者是近几年…

uni-app:解决异步请求返回值问题

可以使用 Promise 或者回调函数来处理异步请求的返回值。 方法一: Promise处理异步请求的返回值 使用 Promise 可以将异步请求的结果通过 resolve 和 reject 返回,然后通过 .then() 方法获取成功的结果,通过 .catch() 方法获取错误信息。 …

Kubernetes集群故障排查—使用 crictl 对 Kubernetes 节点进行调试

crictl 是 CRI 兼容的容器运行时命令行接口。 你可以使用它来检查和调试 Kubernetes 节点上的容器运行时和应用程序。 crictl 和它的源代码在 cri-tools 代码库。 一、准备开始 crictl 需要带有 CRI 运行时的 Linux 操作系统。 二、安装 crictl 你可以从 cri-tools 发布页面…