收藏 分销(赏)

人工智能-实验报告.doc

上传人:仙人****88 文档编号:6643908 上传时间:2024-12-19 格式:DOC 页数:26 大小:304.54KB
下载 相关 举报
人工智能-实验报告.doc_第1页
第1页 / 共26页
人工智能-实验报告.doc_第2页
第2页 / 共26页
人工智能-实验报告.doc_第3页
第3页 / 共26页
人工智能-实验报告.doc_第4页
第4页 / 共26页
人工智能-实验报告.doc_第5页
第5页 / 共26页
点击查看更多>>
资源描述

1、 实验一:知识表示方法一、实验目的状态空间表示法是人工智能领域最基本的知识表示方法之一,也是进一步学习状态空间搜索策略的基础,本实验通过牧师与野人渡河的问题,强化学生对知识表示的了解和应用,为人工智能后续环节的课程奠定基础。二、问题描述有n个牧师和n个野人准备渡河,但只有一条能容纳c个人的小船,为了防止野人侵犯牧师,要求无论在何处,牧师的人数不得少于野人的人数(除非牧师人数为0),且假定野人与牧师都会划船,试设计一个算法,确定他们能否渡过河去,若能,则给出小船来回次数最少的最佳方案。三、基本要求输入:牧师人数(即野人人数):n;小船一次最多载人量:c。输出:若问题无解,则显示Failed,否则

2、,显示Successed输出一组最佳方案。用三元组(X1, X2, X3)表示渡河过程中的状态。并用箭头连接相邻状态以表示迁移过程:初始状态-中间状态-目标状态。例:当输入n=2,c=2时,输出:221-110-211-010-021-000其中:X1表示起始岸上的牧师人数;X2表示起始岸上的野人人数;X3表示小船现在位置(1表示起始岸,0表示目的岸)。要求:写出算法的设计思想和源程序,并以图形用户界面实现人机交互,进行输入和输出结果,如:Please input n: 2 Please input c: 2Successed or Failed?: SuccessedOptimal Proc

3、edure: 221-110-211-010-021-000四、实验组织运行要求本实验采用集中授课形式,每个同学独立完成上述实验要求。五、实验条件每人一台计算机独立完成实验。六、实验代码Main.cpp#include #include RiverCrossing.husing namespace std;/主函数void main()RiverCrossing:ShowInfo();int n, c;coutn;coutc;RiverCrossing riverCrossing(n, c);riverCrossing.solve();system(pause);RiverCrossing.h

4、#pragma once#include /船class Boatpublic:static int c;int pastor;/牧师int savage;/野人Boat(int pastor, int savage);/河岸状态class Statepublic:static int n;int iPastor;/牧师数量int iSavage;/野人数量int iBoatAtSide;/船所在河岸State *pPrevious;/前一个状态State(int pastor, int savage, int boatAtSide);int getTotalCount();/获得此岸总人数b

5、ool check();/检查人数是否符合实际bool isSafe();/检查是否安全State operator + (Boat &boat);State operator - (Boat &boat);bool operator = (State &state);/过河问题class RiverCrossingprivate:std:list openList, closeList;State endState;bool move(State *nowState, Boat *boat);/进行一次决策State* findInList(std:list &listToCheck, St

6、ate &state);/检查某状态节点是否在列表中void print(State *endState);/打印结果public:static void ShowInfo();RiverCrossing(int n, int c);bool solve();/求解问题;RiverCrossing.cpp#include RiverCrossing.h#include #include #include using namespace std;/类静态变量定义int State:n = 0;int Boat:c = 0;/*=Methods for class Boat=*/Boat:Boat

7、(int pastor, int savage)this-pastor = pastor;this-savage = savage;/*=Methods for class State=*/构造函数State:State(int pastor, int savage, int boatAtSide)this-iPastor = pastor;this-iSavage = savage;this-iBoatAtSide = boatAtSide;this-pPrevious = NULL;/获取此岸总人数int State:getTotalCount()return iPastor + iSav

8、age;/检查人数是否在0到n之间bool State:check()return (iPastor =0 & iPastor = 0 & iSavage = x2/彼岸的安全:(n-x1) = 0 | (n-x1) = (n-x2)/将上述条件联立后得到如下条件return (iPastor = 0 | iPastor = n | iPastor = iSavage);/重载+符号,表示船开到此岸State State:operator+(Boat &boat)State ret(iPastor + boat.pastor, iSavage + boat.savage, iBoatAtSid

9、e + 1);ret.pPrevious = this;return ret;/重载-符号,表示船从此岸开走State State:operator-(Boat &boat)State ret(iPastor - boat.pastor, iSavage - boat.savage, iBoatAtSide - 1);ret.pPrevious = this;return ret;/重载=符号,比较两个节点是否是相同的状态bool State:operator=(State &state)return (this-iPastor = state.iPastor & this-iSavage =

10、 state.iSavage & this-iBoatAtSide = state.iBoatAtSide);/*=Methods for class RiverCrossing=*/显示信息void RiverCrossing:ShowInfo()cout*endl;cout 牧师与野人过河问题求解 endl;cout by 1040501211 陈嘉生 endl;cout*iBoatAtSide = 1) /船在此岸/过河的人越多越好,且野人优先int count = nowState-getTotalCount();count = (Boat:c = count ? count : Bo

11、at:c);for (int capticy = count; capticy = 1; -capticy) for (int i = 0; i iBoatAtSide = 0) /船在彼岸/把船开回来的人要最少,且牧师优先for (int capticy = 1; capticy = Boat:c; +capticy) for (int i = 0; i iBoatAtSide = 1) destState = new State(*nowState - *boat);/船离开此岸 else if (nowState-iBoatAtSide = 0) destState = new Stat

12、e(*nowState + *boat);/船开到此岸if (destState-check() /检查人数if (*destState = endState) /是否达到目标状态closeList.push_back(destState);print(destState);return true;/找到结果 else if (destState-isSafe() /检查是否安全if (!findInList(openList, *destState) & !findInList(closeList, *destState) /检查是否在表中/添加没出现过的状态节点到open表openList

13、.push_back(destState);return false;delete destState;return false;/检查给定状态是否存在于列表中State* RiverCrossing:findInList(list &listToCheck, State &state)for (list:iterator ite = listToCheck.begin(); ite != listToCheck.end(); +ite) if (*ite = state)return *ite;return NULL;/根据达到的目标状态,回溯打印出求解过程void RiverCrossin

14、g:print(State *endState)cout=endl;if (!endState) coutSearch failed!endl; else coutSearch successed!endl;coutOptimal Procedure: endl;State *pState = endState;stack st;/用栈将链表逆序,以便输出while (pState) st.push(pState);pState = pState-pPrevious;int count = 0;while (!st.empty() pState = st.top();st.pop();cout

15、iPastor,iSavage,iBoatAtSide;if (st.size() 0)cout ;if (+count % 5 = 0)/每五个步骤换行coutendl;coutendl;coutTotal move: count - 1endl;cout=endl;七、实验结果实验二:九宫重排一、实验目的A*算法是人工智能领域最重要的启发式搜索算法之一,本实验通过九宫重排问题,强化学生对A*算法的理解与应用,为人工智能后续环节的课程奠定基础。二、问题描述给定九宫格的初始状态,要求在有限步的操作内,使其转化为目标状态,且所得到的解是代价最小解(即移动的步数最少)。如:三、基本要求输入:九宫格

16、的初始状态和目标状态输出:重排的过程,即途径的状态四、实验组织运行要求本实验采用集中授课形式,每个同学独立完成上述实验要求。五、实验条件每人一台计算机独立完成实验。六、实验代码Main.cpp#include #include NineGrid.husing namespace std;/主函数void main()NineGrid:ShowInfo();string start, end;coutPlease input the initial state: (ex:134706582)start;coutPlease input the target state: (ex:12380476

17、5)end;NineGrid nineGrid(start, end);nineGrid.solve();system(pause);NineGrid.h#pragma once#include #include #include using namespace std;#define SPACE 0#define AT(s, x, y) (s)(x) * 3 + (y)enum Move UP = 0, DOWN = 1, LEFT = 2, RIGHT = 3;/九宫格状态class Statepublic:static State *pEndState;/指向目标状态,用于评价h的值st

18、ring grid;/用字符串保存当前棋盘状态int x, y;/空格所在位置int moves;/到此状态的移动次数int value;/价值State *pPrevious;/前一个状态State(string &grid, State *pPrevious = NULL);int getReversedCount();/获取逆序数void evaluate();/评价函数bool check(Move move);/检查是否可以移动State takeMove(Move move);/实施移动,生成子状态/重载=运算符,判断两个状态是否相等inline bool operator = (

19、State &state) return grid = state.grid; ;/九宫重排问题class NineGridprivate:vector openList, closeList;State startState, endState;clock_t startTime;bool compareReversed();/比较逆序数奇偶性是否相同bool takeMove(State *nowState, Move move);/进行一次决策State* findInList(vector &listToCheck, State &State);/检查某状态节点是否在列表中void p

20、rint(State *endState);/打印结果/用于排序static bool greater_than(const State *state1, const State *state2);public:static void ShowInfo();/显示信息NineGrid(string &start, string &dest);bool solve();/求解问题;NineGrid.cpp#include NineGrid.h#include #include #include using namespace std;State* State:pEndState = NULL;/

21、*=Methods for class State=*/构造函数State:State(string &grid, State *pPrevious)this-grid = grid;this-pPrevious = pPrevious;if (this-pPrevious)this-moves = pPrevious-moves + 1;elsethis-moves = 0;this-value = 0;evaluate();for (int i = 0; i 3; +i) for(int j = 0; j 3; +j) if (AT(grid, i, j) = SPACE) x = i;y

22、 = j;return;bool State:check(Move move)switch (move) case UP:if (x - 1 = 3)return false;break;case LEFT:if (y - 1 = 3)return false;break;return true;State State:takeMove(Move move)int destX, destY;switch (move) case UP:destX = x - 1;destY = y;break;case DOWN:destX = x + 1;destY = y;break;case LEFT:d

23、estX = x;destY = y - 1;break;case RIGHT:destX = x;destY = y + 1;break;string tGrid = grid;char t = AT(tGrid, destX, destY);AT(tGrid, destX, destY) = AT(tGrid, x, y);AT(tGrid, x, y) = t;return State(tGrid, this);void State:evaluate()if (!pEndState)return;int g = moves, h = 0;for (int i = 0; i 3; +i)

24、for (int j = 0; j grid, i, j)/+h;if(AT(grid, i, j) = SPACE)continue;for (int ii = 0; ii 3; +ii) for (int jj = 0; jj grid, ii, jj) h += abs(i - ii) + abs(j - jj);this-value = g + h;/求该状态的逆序数/逆序数定义为:/不计空格,将棋盘按顺序排列,/对于gridi,存在jgridi,即为逆序。/所有棋子的逆序总数为逆序数。int State:getReversedCount()int count = 0;for (int

25、 i = 0; i 9; +i) if(gridi = SPACE)continue;for (int j = 0; j gridj)+count;return count;/*=Methods for class NineGrid=*/显示信息void NineGrid:ShowInfo()cout*endl;cout 九宫重排问题求解 endl;cout by 1040501211 陈嘉生 endl;cout*endl;/构造函数NineGrid:NineGrid(string &start, string &dest): startState(start), endState(dest)

26、State:pEndState = &endState;endState.evaluate();/当初始状态和目标状态的逆序数的奇偶性相同时,问题才有解bool NineGrid:compareReversed()return startState.getReversedCount() % 2 = endState.getReversedCount() % 2;/解决问题bool NineGrid:solve()cout=endl;if (!compareReversed() cout初始状态和目标状态的逆序数的奇偶性不同,问题无解!endl; else coutStart searching

27、.endl;startTime = clock();/取得开始搜索的时间openList.push_back(new State(startState);while(!openList.empty() /获取一个状态为当前状态State *nowState = openList.back();openList.pop_back();closeList.push_back(nowState);/从当前状态开始决策for (int i = 0; i check(move) if (takeMove(nowState, move)return true;print(NULL);return fals

28、e;/实施一步决策,将得到的新状态添加到列表,返回是否达到目标状态bool NineGrid:takeMove(State *nowState, Move move)/获得下一个状态State *destState = new State(nowState-takeMove(move);if (*destState = endState) /是否达到目标状态closeList.push_back(destState);print(destState);return true;/找到结果 else if (!findInList(openList, *destState) & !findInLi

29、st(closeList, *destState) /添加没出现过的状态节点到open表openList.push_back(destState);sort(openList.begin(), openList.end(), greater_than);return false;delete destState;return false;/检查给定状态是否存在于列表中State* NineGrid:findInList(vector &listToCheck, State &state)for (vector:iterator ite = listToCheck.begin(); ite !=

30、 listToCheck.end(); +ite) if (*ite = state)return *ite;return NULL;/根据达到的目标状态,回溯打印出求解过程void NineGrid:print(State *endState)if (!endState) coutSearch failed!endl; else float elapsed = (float)clock() - startTime) / CLOCKS_PER_SEC * 1000;/取得搜索花费时间coutSearch successed!endl;coutElapsed time: elapsed(ms)e

31、ndl;coutTotal move: movesendl;coutOptimal Procedure: endl;State *pState = endState;stack st;/用栈将链表逆序,以便输出while (pState) st.push(pState);pState = pState-pPrevious;/3行一起输出,更直观一点string out3;int count = 0;while (!st.empty() pState = st.top();st.pop();for (int i = 0; i 3; +i) for(int j = 0; j grid, i, j)

32、 = SPACE)outi += ;elseouti += AT(pState-grid, i, j);outi += ;if (st.size() != 0) out0 += ;out1 += - ;out2 += ;if (+count % 5 = 0 | st.size() = 0) for (int i = 0; i 3; +i) coutoutiendl;outi = ;coutendl;cout=value state2-value;七、实验结果实验三:专家系统一、实验目的专家系统是人工智能的重要研究内容和组成部分之一,本实验通过设计一个简单的专家系统,加深学生对专家系统的组成结构

33、和构造原理的理解,并能转化为具体的应用。二问题描述设计一个简单的专家系统,可根据属性的输入值自动识别事物的具体类别,内容自拟。如一个动物专家系统可由以下11个属性组成,根据属性的对应值(Y或N),可判断动物的具体种类,运行结果如下图所示:三、实验组织运行要求本实验采用开放授课形式,每个同学独立完成上述实验要求。四、实验条件每人一台计算机独立完成实验。五、实验代码Main.cpp#include Expert.h#include using namespace std;void main()Expert:ShowInfo();Expert expert;if (expert.initDiseaseList() expert.diagnosis(); else cout初始化失败!endl;

展开阅读全文
部分上传会员的收益排行 01、路***(¥15400+),02、曲****(¥15300+),
03、wei****016(¥13200+),04、大***流(¥12600+),
05、Fis****915(¥4200+),06、h****i(¥4100+),
07、Q**(¥3400+),08、自******点(¥2400+),
09、h*****x(¥1400+),10、c****e(¥1100+),
11、be*****ha(¥800+),12、13********8(¥800+)。
相似文档                                   自信AI助手自信AI助手
搜索标签

当前位置:首页 > 教育专区 > 小学其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        获赠5币

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:4008-655-100  投诉/维权电话:4009-655-100

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :gzh.png    weibo.png    LOFTER.png 

客服