人工智能课程设计报告-罗马尼亚度假问题36

上传人:陈** 文档编号:97032418 上传时间:2022-05-26 格式:DOCX 页数:36 大小:162.50KB
返回 下载 相关 举报
人工智能课程设计报告-罗马尼亚度假问题36_第1页
第1页 / 共36页
人工智能课程设计报告-罗马尼亚度假问题36_第2页
第2页 / 共36页
人工智能课程设计报告-罗马尼亚度假问题36_第3页
第3页 / 共36页
点击查看更多>>
资源描述
人工智能课程设计报告 课 程:人工智能课程设计报告 班 级: 姓 名: 学 号: 指导教师:赵曼 2015年11月人工智能课程设计报告课程背景 人工智能(Artificial Intelligence),英文缩写为AI。它是研究、开发用于模拟、延伸和扩展人的智能的理论、方法、技术及应用系统的一门新的技术科学。 人工智能是计算机科学的一个分支,它企图了解智能的实质,并生产出一种新的能以人类智能相似的方式做出反应的智能机器,该领域的研究包括机器人、语言识别、图像识别、自然语言处理和专家系统等。人工智能从诞生以来,理论和技术日益成熟,应用领域也不断扩大,可以设想,未来人工智能带来的科技产品,将会是人类智慧的“容器”。人工智能是对人的意识、思维的信息过程的模拟。人工智能不是人的智能,但能像人那样思考、也可能超过人的智能。人工智能是一门极富挑战性的科学,从事这项工作的人必须懂得计算机知识,心理学和哲学。人工智能是包括十分广泛的科学,它由不同的领域组成,如机器学习,计算机视觉等等,总的说来,人工智能研究的一个主要目标是使机器能够胜任一些通常需要人类智能才能完成的复杂工作。但不同的时代、不同的人对这种“复杂工作”的理解是不同的。人工智能是计算机学科的一个分支,二十世纪七十年代以来被称为世界三大尖端技术之一(空间技术、能源技术、人工智能)。也被认为是二十一世纪三大尖端技术(基因工程、纳米科学、人工智能)之一。这是因为近三十年来它获得了迅速的发展,在很多学科领域都获得了广泛应用,并取得了丰硕的成果,人工智能已逐步成为一个独立的分支,无论在理论和实践上都已自成一个系统。人工智能是研究使计算机来模拟人的某些思维过程和智能行为(如学习、推理、思考、规划等)的学科,主要包括计算机实现智能的原理、制造类似于人脑智能的计算机,使计算机能实现更高层次的应用。人工智能将涉及到计算机科学、心理学、哲学和语言学等学科。可以说几乎是自然科学和社会科学的所有学科,其范围已远远超出了计算机科学的范畴,人工智能与思维科学的关系是实践和理论的关系,人工智能是处于思维科学的技术应用层次,是它的一个应用分支。从思维观点看,人工智能不仅限于逻辑思维,要考虑形象思维、灵感思维才能促进人工智能的突破性的发展,数学常被认为是多种学科的基础科学,数学也进入语言、思维领域,人工智能学科也必须借用数学工具,数学不仅在标准逻辑、模糊数学等范围发挥作用,数学进入人工智能学科,它们将互相促进而更快地发展。题目一:罗马利亚度假问题一. 问题描述分别用代价一致的宽度优先、有限制的深度优先(预设搜索层次)、贪婪算法和A*算法求解“罗马利亚度假问题”。即找到从初始地点 Arad到 目的地点 Bucharest 的一条路径。要求:分别用文件存储地图和启发函数表,用生成节点数比较几种算法在问题求解时的效率,并列表给出结果。数据如下:1、地图2、启发函数值Arad 366 Mehadia 241 Bucharest 0 Neamt 234 Craiova 160 Oradea 380 Doberta 242Pitesti 100 Eforie 161 Rimmicu_Vikea 193 Fagaras 176 Sibiu 253 Glurgiu 77Timisoara 329 Hirsova 151 Urziceni 80 Iasi 226 Vaslui 199 Lugoj 244 Zerind 3743、地图数据表0 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 140 1000 118 1000 1000 1000 1000 1000 751000 0 1000 1000 1000 1000 75 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 70 10001000 1000 0 1000 1000 1000 1000 101 1000 1000 211 1000 90 1000 1000 85 1000 1000 1000 10001000 1000 1000 0 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 87 1000 1000 10001000 1000 1000 1000 0 1000 120 138 1000 146 1000 1000 1000 1000 1000 1000 1000 1000 1000 10001000 1000 1000 1000 1000 0 1000 1000 1000 1000 1000 151 1000 1000 1000 1000 1000 1000 1000 711000 75 1000 1000 120 1000 0 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 10001000 1000 101 1000 138 1000 1000 0 1000 97 1000 1000 1000 1000 1000 1000 1000 1000 1000 10001000 1000 1000 1000 1000 1000 1000 1000 0 1000 1000 1000 1000 1000 86 1000 1000 1000 1000 10001000 1000 1000 1000 146 1000 1000 97 1000 0 1000 80 1000 1000 1000 1000 1000 1000 1000 10001000 1000 211 1000 1000 1000 1000 1000 1000 1000 0 99 1000 1000 1000 1000 1000 1000 1000 1000140 1000 1000 1000 1000 151 1000 1000 1000 80 99 0 1000 1000 1000 1000 1000 1000 1000 10001000 1000 90 1000 1000 1000 1000 1000 1000 1000 1000 1000 0 1000 1000 1000 1000 1000 1000 1000118 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 0 1000 1000 1000 1000 111 10001000 1000 1000 1000 1000 1000 1000 1000 86 1000 1000 1000 1000 1000 0 98 1000 1000 1000 10001000 1000 85 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 98 0 1000 1000 1000 10001000 1000 1000 87 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 0 92 1000 10001000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 92 0 1000 10001000 70 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 111 1000 1000 1000 1000 0 100075 1000 1000 1000 1000 71 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 0二.设计分析1.算法分析 1) 宽度优先搜索算法广度优先搜索使用队列(queue)来实现1、把根节点放到队列的末尾。2、每次从队列的头部取出一个元素,查看这个元素所有的下一级元素,把它们放到队列的末尾。并把这个元素记为它下一级元素的前驱。3、找到所要找的元素时结束程序。4、如果遍历整个图还没有找到,结束程序。2)深度优先搜索算法深度优先搜索用栈(stack)来实现,整个过程可以想象成一个倒立的树形:1、把根节点压入栈中。2、每次从栈中弹出一个元素,搜索所有在它下一级的元素,把这些元素压入栈中。并把这个元素记为它下一级元素的前驱。3、找到所要找的元素时结束程序。4、如果遍历整个树还没有找到,结束程序。3)贪婪算法1.建立数学模型来描述问题把求解的问题分成若干个子问题。对每一子问题求解,得到子问题的局部最优解。把子问题的解局部最优解合成原来解问题的一个解。实现该算法的过程:从问题的某一初始解出发;while 能朝给定总目标前进一步do求出可行解的一个解元素;由所有解元素组合成问题的一个可行解。4)A*算法A*1 (A-Star)算法是一种静态路网中求解最短路最有效的直接搜索方法。公式表示为: f(n)=g(n)+h(n),其中 f(n) 是从初始点经由节点n到目标点的估价函数,g(n) 是在状态空间中从初始节点到n节点的实际代价,h(n) 是从n到目标节点最佳路径的估计代价。保证找到最短路径(最优解的)条件,关键在于估价函数f(n)的选取:估价值h(n)实际值,搜索的点数少,搜索范围小,效率高,但不能保证得到最优解。2.数据结构1)图结构: 实现存储“罗马尼亚度假问题”的图空间; 抽象图结构的实现:typedef struct /图节点类型char cityname20;int value;int cost;Ver;class Graph /图结构public:Graph();Graph(); Ver VMaxV;int edgeMaxVMaxV;int numofedges; /注意这个变量的引用位置/读取地图节点信息void ReadVertex();/读取地图边关系信息void ReadEdge();/取与第V个节点的第一个邻接点int GetFirstVertex(int v);/找到第V1个节点的V2之后的下一个邻接节点int GetNextVertex(int v1, int v2);int GetVerValue(int index);/获取Vindex 的ver 的value值int GetVerCost(int index);/获取Vindex 的ver 的cost 值int GetEdge(int row, int col);/获取edgerowcol 的值void SetVerCost(int index,int cost);2)队列结构宽度优先算法以及A*算法 使用到。抽象队列结构实现:class SeqQueuepublic:SeqQueue();SeqQueue();void QueueInitiate();int QueueNotEmpty();int QueueAppend(int x);int QueueDelete(int *d);int QueueOrderAppend(int x, Graph &G);/A*算法使用int Queue_A_OrderAppend(int x, Graph &G);private:int queueMaxSize;int rear;int front;int count;3)栈结构深度优先算法使用;栈结构的抽象类型实现:class Stackpublic:Stack();Stack();bool StackNotFull();bool StakNotEmpty();void StackPop(Graph &G);void StackPush(int x, Graph &G);void PrintStack(Graph &G);int GetWeight();private:int a100;int top1;int weight;三.算法设计1) 宽度优先搜索算法/宽度优先算法void Romania_Trip:BroadFirstSearch(Graph &graph, int v)int u, w; i = 0;SeqCQuene queue;visitedv = 1;/访问节点count+;if (v = end)return;queue.QueueAppend( v);/入队列while (queue.QueueNotEmpty()/队列非空queue.QueueDelete(&u);/取队列节点w = graph.GetFirstVertex( u);while (w != -1) /有子节点的话if (!visitedw)/如果子节点未被访问,则访问子节点Visit(w, u);visitedw = 1;count+;if (w = end)/找到结果Print(graph, b, end, v);return;queue.QueueAppend(w);/节点压入队列w = graph.GetNextVertex(u, w);2)深度优先搜索算法/深度优先算法bool isOK = false;int level = 0;const int Level = 8;/预设的搜索层次void Romania_Trip:DepthFirstSearch(Graph &graph, int v, Stack &stack)int w; i = 0;if (isOK = true)return;if (level+1 Level)return;/大于搜索层次时不再深入level+;visitedv = 1;/访问该节点count+;stack.StackPush(v, graph);if (v = end | stack.GetWeight() = MaxWeight)w = -1;if (v = end&stack.GetWeight() = MaxWeight)cout stack.GetWeight()MaxWeight = stack.GetWeight();*/cout -路径长度为: stack.GetWeight() endl -访问节点数为: count endl-搜索层次:levelendl;isOK = true;elsew = graph.GetFirstVertex(v);/取当前节点的第一个子节点while (w != -1)if (!visitedw)DepthFirstSearch(graph, w, stack);/递归访问w = graph.GetNextVertex(v, w);/取当前节点的下一个子节点visitedv = 0;/返回时置该节点为未访问stack.StackPop( graph);/将该节点弹出栈,并根据graph 中weight 的值更改当前栈值level-;3)贪婪算法/贪婪算法void Romania_Trip:Greedy_Algorithms(Graph &graph, int v)int u, w;SeqCQuene queue;/队列存储图节点在图中的索引值,优先队列,value小的在队头visitedv = 1;if (v = end) return; queue.QueueOrderAppend( v, graph);/图节点按优先顺序入队列count+; /访问节点数+1while (queue.QueueNotEmpty()/宽度优先,循环queue.QueueDelete( &u);/删除队列头元素并返回删除的数值/cout u= u ;w = graph.GetFirstVertex(u);while (w != -1)if (!visitedw)Visit(w, u);/访问w节点,将way b 的指向更新if (w = end) /cout w=end;count+; return; queue.QueueOrderAppend( w, graph); /图节点按优先顺序入队列count+;w = graph.GetNextVertex(u, w);4)A*算法/A*算法void Romania_Trip:AStar_Algorithms(Graph &graph, int v)/i = 0; count = 0;int u, w;SeqCQuene queue;if (v = end) return;/到达终点queue.Queue_A_OrderAppend(v, graph); count+;while (queue.QueueNotEmpty()queue.QueueDelete( &u);if (u = end)cout -路径长度为: graph.GetVerCost(u) + graph.GetVerValue(u) endl -访问节点数为: count endl;return;w = graph.GetFirstVertex( u);while (w != -1)int cost=graph.GetVerCost(u) + graph.GetEdge(w,u);graph.SetVerCost(w, cost);/设置当前节点移动到目标节点的预估费用queue.Queue_A_OrderAppend( w, graph);/按预估费用优先入队列 count+;w = graph.GetNextVertex(u, w);四.运行结果及分析分析:节点数路径长度耗时msOptimality: Completeness: BFS1145016NoYESDFS 1260531NoNOGreedy845016NONOA*算法164180YESYES通过比较,Greedy搜索生成的结点数目最少,为8个,效率最高;A*算法生成的结点数目最多,为30个,效率最低。DFS(一般)、BFS和Greedy搜索找到的都不一定最优解, A*算法具有完备性且始终找到的是最优解。宽度优先虽然是完备的(如果分支因子有限的话),在任何情况下宽度优先都能找到一个解,但是,它找到的第一个解并非最优的,此外,最坏的情况是,当目标结点是第d层的最后一个被扩展的结点时,它将耗费大量的时间。宽度优先时间复杂度:(b为分支因子,d为深度);空间复杂度为所存储的节点的个数。DFS不是完备的(除非查找空间是有限的),同时,它也不能找到最优解。深度优先的时间复杂度:;空间复杂度:(b为分支因子,m为深度,仅有一枝需要存储);。贪婪算法不是完备的。同时,它找到的解也不一定是最优解。其时间复杂度:(b代表分支数,m为深度);空间复杂度为)。所以只有A*算法和DFS(回溯+剪枝)是完备的,且能够找到最优解;其时间复杂度:扩展节点的数目;空间复杂度:所有生成的结点。综合来看,BFS和贪婪算法的效率较高,但解并非最优,而A*算法的效率稍逊色,但解为最优;DFS(回溯+剪枝)搜索虽能找到最优解但效率最低。源代码/Graph.h#pragma onceusing namespace std;#define MaxV 20 /*#ifndef MY_DEBUG#define MY_DEBUG #endif*/typedef structchar cityname20;/城市名int value;/权值int cost;/A*算法中从当前节点移动到目标节点的预估费用Ver;class Graphpublic:Graph();Graph(); Ver VMaxV;int edgeMaxVMaxV;int numofedges; /注意这个变量的引用位置/读取地图节点信息void ReadVertex();/读取地图边关系信息void ReadEdge();/取与第V个节点的第一个邻接点int GetFirstVertex(int v);/找到第V1个节点的V2之后的下一个邻接节点int GetNextVertex(int v1, int v2);int GetVerValue(int index);/获取Vindex 的ver 的value值int GetVerCost(int index);/获取Vindex 的ver 的cost 值int GetEdge(int row, int col);/获取edgerowcol 的值void SetVerCost(int index,int cost);private:;/Queue.h#pragma once#include#include Stack.h#define MaxSize 30/*#ifndef MY_DEBUG#define MY_DEBUG #endif/*/class SeqQueuepublic:SeqQueue();SeqQueue();void QueueInitiate();int QueueNotEmpty();int QueueAppend(int x);int QueueDelete(int *d);int QueueOrderAppend(int x, Graph &G);/A*算法使用int Queue_A_OrderAppend(int x, Graph &G);private:int queueMaxSize;int rear;int front;int count;typedef SeqQueue SeqCQuene;/Romania_Trip.h#pragma once#include Queue.htypedef structint father;int me;way;class Romania_Trippublic:Romania_Trip();Romania_Trip();void Visit(int v, int u);void Print(Graph &graph, way *b, int end, int start);void BroadFirstSearch(Graph &graph, int v);void DepthFirstSearch(Graph &graph, int v,Stack &stack);void Greedy_Algorithms(Graph &graph, int v);void AStar_Algorithms(Graph &graph, int v);void ReSet();int GetCount();int GetMaxWeight();int GetEnd();way* GetB();private:way *b;int i;int end;int count;int visitedCity20;int MaxWeight;int visited20;/Stack.h#pragma once#includeGraph.h#includeusing namespace std;/*#ifndef MY_DEBUG#define MY_DEBUG #endif*/class Stackpublic:Stack();Stack();bool StackNotFull();bool StakNotEmpty();void StackPop(Graph &G);void StackPush(int x, Graph &G);void PrintStack(Graph &G);int GetWeight();private:int a100;int top1;int weight;/Graph.cpp#includeGraph.h#include#include#include#includeusing namespace std;Graph:Graph()numofedges = 0;Graph:Graph()void Graph:ReadVertex()int i=0, v;char ch20;fstream infile(启发式数值.txt, ios:in);while (infile ch & infile v)#ifdef MY_DEBUGprintf(%st%dn, ch, v);#endifVi.value = v;Vi.cost = 0;strcpy(Vi.cityname, ch);i+;void Graph:ReadEdge()int valu, i;fstream infile(地图数据表.txt, ios:in);i = 0;while (infile valu)edgei / 20i % 20 = valu;#ifdef MY_DEBUGif (i % 20 = 0)cout endl;coutedgei/20i%20t;#endifi+;/取与第V个节点的第一个邻接点int Graph:GetFirstVertex(int v)if (v= 20)return -1;for (int col = 0; col0 & edgevcol1000)return col;return -1;/找到第V1个节点的V2之后的下一个邻接节点int Graph:GetNextVertex(int v1, int v2)if (v1= 20 | v2= 20)return -1;for (int col= v2 + 1; col0 & edgev1col1000)return col;return -1;int Graph:GetVerValue(int index)/获取Vindex 的ver 的values值return Vindex.value;int Graph:GetVerCost(int index)/获取Vindex 的ver 的cost 值return Vindex.cost;int Graph:GetEdge(int row, int col)/获取edgerowcol 的值return edgerowcol;void Graph:SetVerCost(int index, int cost)Vindex.cost = cost;/Queue.cpp#includeQueue.h#include#include Stack.hSeqQueue:SeqQueue()rear = 0;front = 0;count = 0;SeqQueue:SeqQueue()int SeqQueue:QueueNotEmpty()if (count != 0)return 1;else return 0;int SeqQueue:QueueAppend( int x)if (count0 & rear = front)cout 队列已满 endl;return 0;elsequeuerear = x;rear = (rear + 1) % MaxSize;count+;return 1;int SeqQueue:QueueDelete( int *d)if (count = 0)cout 队列已空 0 & rear = front)cout 队列已满 = G.Vqueuerear - 1.value)/队尾插入queuerear = x;rear = (rear + 1) % MaxSize;count+;return 1;elseif (G.Vx.value G.Vqueueposition.value)position+;int i;for (i = front; i0 & rear = front)cout 队列已满 x2 )/队尾插入queuerear = x;rear = (rear + 1) % MaxSize;count+;return 1;else /队头插入if (G.Vx.value + G.Vx.cost G.Vqueueposition%MaxSize.value + G.Vqueueposition%MaxSize.cost)position+;int i;for (i = front; iposition; i+)queue(i - 1 + MaxSize) % MaxSize = queuei%MaxSize;queue(i - 1 + MaxSize) % MaxSize = x;front = (front - 1 + MaxSize) % MaxSize;count+;return 1;return 0;/Romania_Trip.cpp#includeRomania_Trip.h#include#include#includeusing namespace std;Romania_Trip:Romania_Trip()b = new way100;i = 0;end = 2;count = 0;MaxWeight = 10000;for (size_t i = 0; i 20; i+)visitedi = 0;void Romania_Trip:ReSet()if(NULL!=b)deleteb;b = new way20;i = 0;end = 2;count = 0;for (size_t i = 0; i 20; i+)visitedi = 0;Romania_Trip:Romania_Trip()if (NULL != b)deleteb;void Romania_Trip:Visit(int v, int u)bi.father = u;bi.me = v;i +;void Romania_Trip:Print(Graph &graph, way *b, int end, int start)int Bway = 0;vector CityName;string name = graph.Vend.cityname;CityName.push_back(name);while (1)for (int j = 0; ji; j+)if (bj.me = end)name = graph.Vbj.father.cityname;CityName.push_back(name);Bway += graph.edgebj.mebj.father;end = bj.father;if (end = start)break;cout 0; i-)cout CityNamei ;cout Bucharest endl;cout -路径长度为: Bway endl -访问节点数为: count Level)return;/大于搜索层次时不再深入level+;visitedv = 1;/访问该节点count+;stack.StackPush(v, graph);if (v = end | stack.GetWeight() = MaxWeight)w = -1;if (v = end&stack.GetWeight() = MaxWeight)cout stack.GetWeight()MaxWeight = stack.GetWeight();*/cout -路径长度为: stack.GetWeight() endl -访问节点数为: count endl-搜索层次:levelendl;isOK = true;elsew = graph.GetFirstVertex(v);/取当前节点的第一个子节点while (w != -1)if (!visitedw)DepthFirstSearch(graph, w, stack);/递归访问w = graph.GetNextVertex(v, w);/取当前节点的下一个子节点visitedv = 0;/返回时置该节点为未访问stack.StackPop( graph);/将该节点弹出栈,并根据graph 中weight 的值更改当前栈值level-;/贪婪算法void Romania_Trip:Greedy_Algorithms(Graph &graph, int v)int u, w;SeqCQuene queue;/队列存储图节点在图中的索引值,优先队列,value小的在队头visitedv = 1;if (v = end) return; queue.QueueOrderAppend( v, graph);/图节点按优先顺序入队列count+; /访问节点数+1while (queue.QueueNotEmpty()/宽度优先,循环queue.QueueDelete( &u);/删除队列头元素并返回删除的数值/cout u= u ;w = graph.GetFirstVertex(u);while (w != -1)if (!visitedw)Visit(w, u);/访问w节点,将way b 的指向更新if (w = end) /cout w=end;count+; return; queue.QueueOrderAppend( w, graph); /图节点按优先顺序入队列count+;w = graph.GetNextVertex(u, w);/A*算法void Romania_Trip:AStar_Algorithms(Graph &graph, int
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 管理文书 > 工作总结


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!