人工智能原理
实验报告
学号:
姓名:
******
****
班级:计算机科学与技术
目录
人工智能原理............................................................................................................................................1
实验报告....................................................................................................................................................1
2017217783......................................................................................................................... 1
学号:
彭梦昊..............................................................................................................................1
姓名:
班级:计算机科学与技术 17-3............................................................................................................... 1
实验一:猴子摘香蕉问题的 python 编程实现...................................................................................... 3
一、实验目的:....................................................................................................................................3
一、 实验内容:..................................................................................................................................3
二、 实验环境:..................................................................................................................................3
三、 实验原理:..................................................................................................................................3
四、 实验源码:..................................................................................................................................4
六、实验结果:....................................................................................................................................5
七、心得体会:....................................................................................................................................5
实验三:搜索算法求解 8 数码问题........................................................................................................6
一、 实验目的:..................................................................................................................................6
二、 实验内容:..................................................................................................................................6
三、BFS 遍历搜索算法:.....................................................................................................................6
四、实验源码:....................................................................................................................................6
五、实验结果:....................................................................................................................................9
六、心得体会:....................................................................................................................................9
实验四:子句集消解实验......................................................................................................................10
一、 实验目的:................................................................................................................................10
二、 实验内容:................................................................................................................................10
(1)消去蕴涵符号........................................................................................................................... 10
(2) 缩小否定符号的作用范围 (辖域)................................................................................... 10
(3) 变量命名标准化..................................................................................................................... 10
(4) 消去存在量词( Skolem 化 )................................................................................................ 10
(5) 化为前束范式......................................................................................................................... 10
(6) 消去全称量词......................................................................................................................... 11
(7) 化为合取范式......................................................................................................................... 11
(8) 将公式用子句集合表示......................................................................................................... 11
三、 实验源码:................................................................................................................................11
四、 实验结果:................................................................................................................................20
五、 心得体会:................................................................................................................................20
实验六:蚁群算法在 TSP 问题中的实现..............................................................................................21
一、 实验目的:................................................................................................................................21
二、 实验内容:................................................................................................................................21
三、 实验环境:................................................................................................................................21
四、旅行商问题的蚁群优化求解:..................................................................................................21
五、实验源码:..................................................................................................................................21
六、 实验结果:................................................................................................................................26
七、 心得体会:................................................................................................................................26
实验一:猴子摘香蕉问题的 python 编程实现
一、实验目的:
(1)、熟悉谓词逻辑表示法
(2)、掌握人工智能谓词逻辑中的经典例子——猴子摘香蕉问题的编程实现。
一、实验内容:
房间里有一只猴子(机器人),位于 a 处。在 c 处的上方的天花板上有一串
香蕉,猴子想吃,但是摘不到。房间的 b 处还有一个箱子,如果猴子站到箱子上
就可以摸到天花板。对于上述问题,可以通过谓词表示法来描述知识。要求通过
python 语言编程实现猴子摘香蕉问题的求解过程。
天花板
a
b
c
二、实验环境:
Windows 10 微信小程序
Python 语言
三、实验原理:
定义描述状态的谓词:
AT(x, y):x 在 y 处
ONBOX:猴子在箱子上
GB:猴子得到香蕉
个体域:
x:{monkey, box, banana}
Y:{a, b, c}
问题的初始状态
AT(monkey, a)
AT(box, b)
ONBOX,
GB
问题的目标状态
AT(monkey, c), AT(box, c)
ONBOX, GB
ClimbBox( )
条件: ONBOX, AT(monkey, w),AT(box,w)
动作:删除: ONBOX
添加:ONBOX
Grasp( )
条件:ONBOX,AT(box, c)
动作:删除: GB
添加:GB
四、实验源码:
#全局变量 i
i=0
def Monkey_go_box(x,y):
global i
i=i+1
print('step:',i,'monkey 从',x,'走到'+y)
def Monkey_move_box(x,y):
global i
i = i + 1
print('step:', i, 'monkey 把箱子从', x, '运到' + y)
def Monkey_on_box():
global i
i = i + 1
print('step:', i, 'monkey 爬上箱子')
def Monkey_get_banana():
global i
i = i + 1
print('step:', i, 'monkey 摘到香蕉')
import sys
print('请用‘a’、‘b’、‘c’表示猴子香蕉箱子的位置')
#读取输入的运行参数
codeIn=sys.stdin.read()
codeInList=codeIn.split()
#将运行参数赋值给 monkey、banana、box
monkey=codeInList[0]
banana=codeInList[1]
box=codeInList[2]
print('操作步骤如下:')
#请用最少步骤完成猴子摘香蕉任务
###########开始#############
if monkey != box:
Monkey_go_box(monkey, box)
if box != banana:
Monkey_move_box(box, banana)
Monkey_on_box()
Monkey_get_banana()
###########结束#############
六、实验结果:
七、心得体会:
通过本次实验,使我更加对人工智能这个学科有了更加深刻的了解,并且对
其产生了极大的兴趣。并且由于实验需要,让我尝试自己去学习 python 语言的
而一些基本知识,增强自己学习新知识的能力,也扩大了自己的知识储备。
实验三:搜索算法求解 8 数码问题
一、实验目的:
(1)、熟悉人工智系统中的问题求解过程
(2)、熟悉状态空间中的盲目搜索策略
(3)、掌握盲目搜索算法,重点是宽度优先搜索和深度优先搜索算法。
二、实验内容:
用 python 语言编程,采用宽度优先搜索和深度优先搜索的方法,求解 8 数
码问题。
(1)采用宽度优先算法,运行程序,要求输入初始状态
假定输入状态如下: 2 8 3
1 6 4
7 0 5
目标状态为: 2 1 6
4 0 8
7 5 3
每次选扩展结点时,从数组的最后一个生成的节点开始找,找出一个没有
扩展的节点,这样也需要对节点添加一个是否被扩展过的标志。
三、BFS 遍历搜索算法:
从初始状态节点 S 出发广度优先搜索遍历图的算法 bfs(S):
1) 访问 S
2) 依次访问 S 的各邻接点
3) 设最近一层访问序列为 vi1,vi2,…,vik,则依次访问 vi1,vi2,…,
vik 未被访问过的邻接点。
4) 重复(3),直到找不到未被访问的邻接点为止。
四、实验源码:
import numpy as np
class State:
def __init__(self, state, directionFlag=None, parent=None):
self.state = state
# state is a ndarray with a shape(3,3) to storage the state
self.direction = ['up', 'down', 'right', 'left']
if directionFlag:
self.direction.remove(directionFlag)
# record the possible directions to generate the sub-states
self.parent = parent
def showInfo(self):
for i in range(3):
for j in range(3):
print(self.state[i, j], end='
')
print("\n")
print('->')
return
def getEmptyPos(self):
postion = np.where(self.state == self.symbol)
return postion
def generateSubStates(self):#产生子节点
if not self.direction:
return []
subStates = []
boarder = len(self.state) - 1
# the maximum of the x,y
row, col = self.getEmptyPos()
if 'left' in self.direction and col > 0:#向左移动
s = self.state.copy()
#标志位 symbol=0 向左移动,产生新的状态节点,加入到 subStates 中
temp = s.copy()
s[row, col] = s[row, col-1]
s[row, col-1] = temp[row, col]
news = State(s, directionFlag='right', parent=self)
subStates.append(news)
if 'up' in self.direction and row > 0:
s = self.state.copy()
# 标志位 symbol=0 向上移动,产生新的状态节点,加入到 subStates 中
temp = s.copy()
s[row, col] = s[row-1, col]
s[row-1, col] = temp[row, col]
news = State(s, directionFlag='down', parent=self)
subStates.append(news)
if 'down' in self.direction and row < boarder:
#it can move to down
place
s = self.state.copy()
# 标志位 symbol=0 向下移动,产生新的状态节点,加入到 subStates 中
temp = s.copy()
s[row, col] = s[row+1, col]
s[row+1, col] = temp[row, col]
news = State(s, directionFlag='up', parent=self)
subStates.append(news)
if self.direction.count('right') and col < boarder:
#it can move to
right place
s = self.state.copy()
# 标志位 symbol=0 向右移动,产生新的状态节点,加入到 subStates 中
temp = s.copy()
s[row, col] = s[row, col+1]
s[row, col+1] = temp[row, col]
news = State(s, directionFlag='left', parent=self)
subStates.append(news)
#end1
return subStates
def BFS(self):
#存放状态的地方
# generate a empty openTable
openTable = []
# append the origin state to the openTable
openTable.append(self)#将初始状态加入
steps = 1#步骤
while len(openTable) > 0:
n = openTable.pop(0)#pop() 函数用于移除列表中的一个元素(默认最后一
个元素),并且返回该元素的值。
subStates = n.generateSubStates()
# 查看子状态中有没有最终状态,如果有则输出之前的父状态到 path 中,输
出 step+1
path = []
for s in subStates:
if (s.state == s.answer).all():
while s.parent and s.parent !=s1:
path.append(s.parent)
s = s.parent
path.reverse()