人工智能实验报告
具体实现的算法为:
move(state(X,X,G,C),state(Y,Y,G,C)):-opp(X,Y).
move(state(X,W,X,C),state(Y,W,Y,C)):-opp(X,Y).
move(state(X,W,G,X),state(Y,W,G,Y)):-opp(X,Y).
move(state(X,W,G,C),state(Y,W,G,C)):-opp(X,Y).
path(Open_queue,Closed_set,Goal):-
dequeue([State,Parent],Open_queue,Rest_open_queue),
get_children(State,Rest_open_queue,Closed_set,Children),
add_list_to_queue(Children,Rest_open_queue,New_open_queue),
add_if_not_in_set(X,S,S):-member(X,S),!.
add_if_not_in_set(X,S,[X|S]).
3.四皇后问题,在类中封装方法,bool bCanPlace(int k),oid BackTrack(int k),void PrintX(void),实现实现对皇后位置的检测,最后打印出结果为每行皇后的位置的列即就是为一个有效的序列。
union([[State,Parent]],Closed_set,New_closed_set),
path(New_open_queue,New_closed_set,Goal).
get_children(State,Rest_open_queue,Closed_set,Children):-
bagof(Child,moves(State,Rest_open_queue,Closed_set,Child),Children).
move(squre(Row,Column),squre(New_Row,New_Column)):-
Row>=2,New_Row is Row-1,Column=<6,New_Column is Column+2.
move(squre(Row,Column),squre(New_Row,New_Column)):-
主要方法:
class Queen{
friend int nQueen(int);
private:
bool bCanPlace(int k);
void BackTrack(int k);
void PrintX(void);
int n,*x,sum;
};
bool Queen:: bCanPlace(int k)//判断是否合法
empty_set(Closed_set),
path(Open_queue,Closed_set,Goal).
path(Open_queue,_,_):-empty_queue(Open_queue),
write("No solution found with these rules").
path(Open_queue,Closed_set,Goal):-
{
bool bOk = true;
for(int i =1 ; i < k; i++)
{
if ((abs(k-i) == abs( x[k] - x[i])) || (x[k] == x[i]) )
{
bOk = false;
break;
}
}
return bOk;
}
4.极大极小值求井字棋问题,void Init()棋盘初始化函数,void PrintQP()打印棋盘函数void UserInput()用户输入落子位置函数,这三份方法程序中当前的棋局进行读写操作;nt IsWin(State s)判断当前棋局是否有一方获胜,并判断哪一方获胜的函数,int e_fun(State s)评估函数值计算函数,当前的棋局进行判断;int AutoDone()极大极小值算法主函数,计算机决定在哪个位置落子所采用的核心算法,并且可以判断计算机落子前后棋局的状态,如果在搜索树的深度范围内能判断哪一方必胜,则可提前打印输赢信息,并结束本棋。
实验要求采用PROLOG编程求解8*8骑士周游问题以及农夫、狼、羊、菜问题。采用熟悉的高级语言编程实现“过河问题”、“九宫格”等问题的求解。
二、所用仪器、设备
计算机、TRINC-PROLOG及高级语言程序设计环境。
三、实验原理
1.8*8骑士周游问题求解,以squre(x,y)表示骑士的位置,然后寻找一条是否存在的路径判断是否存在此路径的周游方法。通过x与y的值的范围,判断是否可以向左右方向移动,达到求解周游的问题。
Row>=2,New_Row is Row-1,Column>=3,New_Column is Column-2.
been(squre(0,0)).
path(X,X).
path(X,Y):-move(X,Z),not(been(Z)),dynamic(knight_tour,been/1),asserta(knight_tour,been(Z)),path(Z,Y).
Row=<7,New_Row is Row+1,Column=<6,New_Column is Column+2.
move(squre(Row,Column),squre(New_Row,New_Column)):-
Row=<7,New_Row is Row+1,Column>=3,New_Column is Column-2.
moves(State,Rest_open_queue,Closed_set,[Next,State]):-
move(State,Next),not(unsafe(Next)),
not(member_queue([Next,_],Rest_open_queue)),
not(member_set([Next,_],Closed_set)).
当path(x,y)的x与y参数相同是则退出。
实现算法:
move(squre(Row,Column),squre(New_Row,New_Column)):-
Row=<6,New_Row is Row+2,Column=<7,New_Column is Column+1.
move(squre(Row,Column),squre(New_Row,New_Column)):-
member_set([Parent,Grandparent],Closed_set),
printsolution([Parent,Grandparent],Closed_set),
write(State),nl.
empty_set([]).
member_set([State,Parent],[[State, Parent]|_]).
dequeue([State,Parent],Open_queue,_),State=Goal,
write("A solution is found:"),nl,
printsolution([State,Parent],Closed_set).
%printsolution1(Closed_set).
Row=<6,New_Row is Row+2,Column>=2,New_Column is Column-1.
move(squre(Row,Column),squre(New_Row,New_Column)):-
Row>=3,New_Row is Row-2,Column=<7,New_Column is Column+1.
add_list_to_queue(List,Queue,New_queue):-append(Queue,List,New_queue).
append(X,Y,Z):-X=[],Z=Y.
append(X,Y,Z):-X=[A|B],Z=[A|W],append(B,Y,W).
member(X,[X|T]).
move(squre(Row,Column),squre(New_Row,New_Column)):-
Row>=3,New_Row is Row-2,Column>=2,New_Column is Column-1.
move(squre(Row,Column),squre(New_Row,New_Column)):-
华北电力大学
实验报告
|
|Hale Waihona Puke 实验名称图搜索问题求解课程名称人工智能及应用
|
|
专业班级:学生姓名:
学号:成绩:
指导教师:李继荣实验日期:2014.5
一、实验目的及要求
熟悉PROLOG语言的特点和某种PROLOG编程环境;掌握编写与调试简单的PROLOG程序的方法。通过设计、编写和调试了解PROLOG编译器;掌握PROLOG语言中常量、变量的表示方法和PROLOG进行事实库、规则库的编写方法。加深对逻辑程序运行机理的理解,掌握PROLOG语言的特点,熟悉其编程环境。针对实际应用问题,分析题目背景,利用编程实现图搜索技术的问题求解方法,以牢固掌握图搜索技术的基本原理和常用算法,加深对图搜索技术的理解。
2.农夫、狼、羊、菜问题求解,采用宽度优先搜索算法,寻找一条安全的路径,农夫把三物品从河的一岸送到对岸,设计状态state(x,y,z,w)表示当前四物所处在的状态,按照算法寻找出最后的路径。
3.四皇后问题解决,封装Queen类,包括皇后个数,以及皇后位置是否正确而的判断方法,然后再主方法中调用方法即可。