人工智能课内实验报告2Word格式.docx
- 文档编号:17339089
- 上传时间:2022-12-01
- 格式:DOCX
- 页数:16
- 大小:32.50KB
人工智能课内实验报告2Word格式.docx
《人工智能课内实验报告2Word格式.docx》由会员分享,可在线阅读,更多相关《人工智能课内实验报告2Word格式.docx(16页珍藏版)》请在冰豆网上搜索。
把其中不是节点n先辈的那些子节点记做集合M,并把这些子节点作为节点n的子节点加入G中;
6.
针对M中子节点的不同情况,分别进行如下处理:
(1)对于那些未曾在G中出现过的M成员设置一个指向父节点(即节点n)
的指针,并把它们放入OPEN表;
(不在OPEN表)
(2)对于那些先前已经在G中出现过的M成员,确定是否需要修改它指向父
节点的指针;
(在OPEN表中)
(3)
对于那些先前已在G中出现并且已经扩展了的M成员,确定是否需要修
改其后继节点指向父节点的指针;
(在CLOSE表中)
7.按某种搜索策略对OPEN表中的节点进行排序;
8.
转第2步。
(二)A*算法描述:
(在OPEN表中,对g(x)进行更新)
(3)对于那些先前已在G中出现并且已经扩展了的M成员,确定是否需要修
(在CLOSE表中,
对节点n子节点的子节点更新g(x)
)
7.对OPEN表中的节点按估价函数进行排序;
转第2步。
4、A*算法实验程序及运行结果
程序如下:
#include"
iostream.h"
#include<
time.h>
stdio.h>
dos.h>
conio.h>
staticinttarget[9]={1,2,3,8,0,4,7,6,5};
//classdefinition
classeight_num
{
private:
intnum[9];
intnot_in_position_num;
intdeapth;
inteva_function;
public:
eight_num*parent;
eight_num*leaf_next;
eight_num*leaf_pre;
eight_num(intinit_num[9]);
eight_num(intnum1,intnum2,intnum3,intnum4,intnum5,intnum6,intnum7,intnum8,intnum9)
{
num[0]=num1;
num[1]=num2;
num[2]=num3;
num[3]=num4;
num[4]=num5;
num[5]=num6;
num[6]=num7;
num[7]=num8;
num[8]=num9;
}
eight_num(void)
for(inti=0;
i<
9;
i++)
num[i]=i;
voidcul_para(void);
voidget_numbers_to(intother_num[9]);
intget_nipn(void)
{returnnot_in_position_num;
}
intget_deapth(void)
{returndeapth;
intget_evafun(void)
{returneva_function;
voidset_num(intother_num[9]);
voidshow(void);
eight_num&
operator=(eight_num&
);
operator=(intother_num[9]);
intoperator==(eight_num&
intoperator==(intother_num[9]);
};
//计算启发函数g(n)的值
voideight_num:
:
cul_para(void)
inti;
inttemp_nipn=0;
for(i=0;
if(num[i]!
=target[i])
temp_nipn++;
not_in_position_num=temp_nipn;
if(this->
parent==NULL)
deapth=0;
else
deapth=this->
parent->
deapth+1;
eva_function=not_in_position_num+deapth;
//构造函数1
eight_num:
eight_num(intinit_num[9])
for(inti=0;
num[i]=init_num[i];
//显示当前节点的状态
show()
cout<
<
num[0];
"
"
;
num[1];
num[2];
\n"
num[3];
num[4];
num[5];
num[6];
num[7];
num[8];
//复制当前节点状态到一个另数组中
get_numbers_to(intother_num[9])
other_num[i]=num[i];
//设置当前节点状态(欲设置的状态记录的other数组中)
set_num(intother_num[9])
num[i]=other_num[i];
eight_num&
eight_num:
operator=(eight_num&
another_8num)
num[i]=another_8num.num[i];
not_in_position_num=another_8num.not_in_position_num;
deapth=another_8num.deapth+1;
return*this;
operator=(intother_num[9])
inteight_num:
operator==(eight_num&
intmatch=1;
if(num[i]!
=another_8num.num[i])
{
match=0;
break;
}
if(match==0)
return0;
else
return1;
operator==(intother_num[9])
=other_num[i])
//classdefinitionover
//空格向上移
intmove_up(intnum[9])
if(num[i]==0)
if(i<
3)
num[i]=num[i-3];
num[i-3]=0;
//空格向下移
intmove_down(intnum[9])
if(i>
5)
num[i]=num[i+3];
num[i+3]=0;
//空格向左移
intmove_left(intnum[9])
if(i==0||i==3||i==6)
num[i]=num[i-1];
num[i-1]=0;
//空格向右移
intmove_right(intnum[9])
if(i==2||i==5||i==8)
num[i]=num[i+1];
num[i+1]=0;
//判断可否解出
inticansolve(intnum[9],inttarget[9])
inti,j;
intcount_num,count_target;
for(j=0;
j<
i;
j++)
if(num[j]<
num[i]&
&
num[j]!
=0)
count_num++;
if(target[j]<
target[i]&
target[j]!
count_target++;
if((count_num+count_target)%2==0)
return1;
else
return0;
//判断有无重复
intexisted(intnum[9],eight_num*where)
eight_num*p;
for(p=where;
p!
=NULL;
p=p->
parent)
if(*p==num)
return0;
//寻找估价函数最小的叶子节点
eight_num*find_OK_leaf(eight_num*start)
eight_num*p,*OK;
p=OK=start;
intmin=start->
get_evafun();
for(p=start;
leaf_next)
if(min>
p->
get_evafun())
OK=p;
min=p->
returnOK;
//主函数开始
intmain(void)
doubletime;
clock_tStart,Finish;
intmemery_used=0,step=0;
intflag=0;
//是否输入错误标志,1表示输入错误
intbingo=0;
//是否查找成功标志,1表示成功
//cout<
重排九宫\n\n"
初始状态:
flag=0;
cin>
>
num[i];
for(j=0;
if(num[i]==num[j])
flag=1;
if(num[i]<
0||num[i]>
8||flag==1)
i--;
cout<
包含无效的数字!
eight_numS(num),Target(target);
S.parent=S.leaf_next=S.leaf_pre=NULL;
S.cul_para();
memery_used++;
初始状态为:
S.show();
目标状态为:
Target.show();
if(!
icansolve(num,target))
cout<
输入的初始状态无法到达目标状态!
else{cout<
变换过程为:
Start=clock();
eight_num*OK_leaf=&
S,*leaf_start=&
S,*new_8num,*p;
while(OK_leaf!
=NULL&
bingo!
=1)
OK_leaf=find_OK_leaf(leaf_start);
if(*OK_leaf==Target)
bingo=1;
p=OK_leaf->
leaf_pre;
OK_leaf->
get_numbers_to(num);
if(move_up(num)&
!
existed(num,OK_leaf))
new_8num=neweight_num;
new_8num->
set_num(num);
parent=OK_leaf;
cul_para();
leaf_pre=p;
if(p==NULL)
leaf_start=new_8num;
else
p->
leaf_next=new_8num;
p=new_8num;
memery_used++;
if(move_down(num)&
if(move_left(num)&
if(move_right(num)&
new_8num=neweight_num;
p->
leaf_next=OK_leaf->
leaf_next;
if(OK_leaf->
leaf_next!
=NULL)
OK_leaf->
leaf_next->
leaf_pre=NULL;
Finish=clock();
if(bingo==1)
time=(double)(Finish-Start)*1000/CLOCKS_PER_SEC;
eight_num*p;
for(p=OK_leaf->
parent;
^\n"
p->
show();
step++;
}
Timecost:
time;
ms\n"
Totalycoveredsteps:
step;
Failtofind!
输入矩阵:
283
104
765
程序运行截图如下:
5、实验体会和感想
本次实验有很多不尽如人意的地方,编写程序时遇到种种困难,在老师和同学的帮助下,经过努力最终还是完成了实验,收获很大。
认识到自己在编程方面还有所欠缺,今后还要更加努力!
另外,这次实验也加深了我对A*算法的认识和理解,A*搜索算法的效率较高,是一种最好优先算法。
总之这次实验让我深入了解了A*搜索方法的具体使用方法,也扩宽了关于人工智能知识使用的范围,锻炼了思考问题的能力。
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 人工智能 实验 报告