




已阅读5页,还剩20页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
一、15数码问题的描述及其状态空间法表示(1)15数码问题描述15数码问题又叫移棋盘问题,是人工智能中的一个经典问题。所谓的15数码问题:就是在一个44的16宫格棋盘上,摆放有15个将牌,每一个将牌都刻有115中的某一个数码。棋盘中留有一个空格,允许其周围的某一个将牌向空格移动,这样通过移动将牌就可以不断改变将牌的布局。这种求解的问题是:给定一种初始的将牌布局或结构(称初始状态)和一个目标布局(称目标状态),问如何移动数码,实现从初始状态到目标状态的转变,如图1所示 。问题的实质就是寻找一个合法的动作序列512114136310142791158123456789101112131415(a)初始状态 (b)目标状态图1 15数码问题的一个实例(2)状态空间法表示人工智能问题的求解是以知识表示为基础的。如何将已获得的有关知识以计算机内部代码形式加以合理地描述、存储、有效地利用便是表示应解决的问题1。目前的知识表示方法有十余种,如:一阶谓词逻辑表示法、产生式表示法、状态空间表示法、语义网格表示法、框架表示法、脚本表示法、面向对象表示法等。任何一个给定的问题可能存在多种知识表示方法,人们可以根据待求解问题的领域知识选择适当的知识表示方法。这里我们只强调状态空间表示法。把求解的问题表示成问题状态、操作、约束、初始状态和目标状态。状态空间就是所有可能的状态的集合。求解一个问题就是从初始状态出发,不断应用可应用的操作,在满足约束的条件下达到目标状态。问题求解过程就可以看成是问题状态在状态空间的移动。状态是为描述某类不同事物间的差别而引入的一组最少变量q0,q1,qn的有序集合。问题的状态空间是一个表示该问题全部可能状态及其关系的图。记为三元状态(S、F、G),其中S所有可能的问题初始状态集合,F操作符集合,G目标状态集合。十五数码的状态空间法:初始状态S44=5,12,11,4,13,6,3,10,14,2,7,9,1,15,0,8;(0表示空格)目标状态G44=1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0;操作符集合F=空格上移,空格下移,空格左移,空格右移状态空间的一个解:是一个有限的操作算子序列,它使初始状态转化为目标状态:S0-f1-S1-f2-.fk-G。二、A* 算法的基本原理、算法步骤、流程图(1)A*算法基本原理A*算法是一种有序搜索算法,其特点在于对评价函数的定义上。对于一般的有序搜索,总是选择f值最小的节点作为扩展节点。因此,f是根据需要找到一条最小代价路径的观点来估算节点的,可考虑将每个节点n的估价函数值分解为两个分量:从起始节点到节点n的最小代价路径的代价与从节点n到目标节点的最小代价路径的代价之和,也就是说f(n)是约束通过节点n的一条最小代价路径的代价的一个估计。再定义一个函数f*,使得在任意一个节点n上的函数值f*(n)就是从节点S到节点n的一条最佳路径的实际代价加上从节点n到目标节点的一条最佳路径的代价之和,即:f*(n) =g* (n)+h * (n)评价函数f是f*的一个估计,这个估计可由下式给出:f(n)=g(n)+h(n)其中g是g*的估计,h是h*的估计。g* (n)的估计g(n)就是搜索树中从初始节点到当前节点n的这段路径的代价,这一代价可以由从初始节点到当前节点n寻找路径时,把遇到的各段路径的代价加起来给出。h* (n) 的估计h(n)依赖于有关问题的领域的启发信息,于是被称作启发函数。在启发函数中,应用的启发信息(问题知识)越多,扩展的节点就越少,这样就能更快地搜索到目标节点 。(2)A*算法基本步骤1)生成一个只包含开始节点n0的搜索图G,把n0放在一个叫OPEN的列表上。2)生成一个列表CLOSED,它的初始值为空。3)如果OPEN表为空,则失败退出。4)选择OPEN上的第一个节点,把它从OPEN中移入CLPSED,称该节点为n。5)如果n是目标节点,顺着G中,从n到n0的指针找到一条路径,获得解决方案,成功退出(该指针定义了一个搜索树,在第7步建立)。6)扩展节点n,生成其后继结点集M,在G中,n的祖先不能在M中。在G中安置M的成员,使他们成为n的后继。7)从M的每一个不在G中的成员建立一个指向n的指针(例如,既不在OPEN中,也不在CLOSED中)。把M1的这些成员加到OPEN中。对M的每一个已在OPEN中或CLOSED中的成员m,如果到目前为止找到的到达m的最好路径通过n,就把它的指针指向n。对已在CLOSED中的M的每一个成员,重定向它在G中的每一个后继,以使它们顺着到目前为止发现的最好路径指向它们的祖先。8)按递增f*值,重排OPEN(相同最小f*值可根据搜索树中的最深节点来解决)。9)返回第3步。在第7步中,如果搜索过程发现一条路径到达一个节点的代价比现存的路径代价低,就要重定向指向该节点的指针。已经在CLOSED中的节点子孙的重定向保存了后面的搜索结果,但是可能需要指数级的计算代价。(3)流程图如下所示:三、实例(1)本文采用C#基于对话框的程序设计,在图形界面上显示出十五数码格局并可以随意设置数码的位置。确定初始状态格局后,使用A*算法进行搜索。本方法实现的程序中用到的估价函数如下:f(n)=h(n)+g(n)。其中,h(n)代表搜索树中节点n的深度,根节点深度是0。启发函数g(n)定义为当前节点与其目标节点相应位置不相同元素的个数。点击初始化按钮,开始执行搜索,成功或失败后提示。(2)程序实例展示1)0步:初始状态,输入016的数码输入完毕后,点击初始化,呈下图所示:2)10步:初始态如下图所示:0数码经过上移一次、左移三次、下移三次,右移三次到达目标状态,如下图:(3)性能指标空格不计,则16宫图的某状态为数1到15的一个排列,记为n0nln2n3n4n5n6n7n8n9n10n11n12n13n14n15。两个状态之间是否可达可以通过计算两者的逆序数来判断,若两者逆序数的奇偶性相同则可达,否则不可达。也即对于任一个目标状态节点,有(1/2)16 !=10461394944000个状态可达该节点。据统计,单纯用A*算法要花费几个小时时间才能判断出不能达到目标状态,这时CLOSED表长度为10461394944000。但由于本程序缺陷甚多,所以算法的时间复杂度会更高。四、个人体会初学人工智能时,最先联想到的便是机器人,一直感觉机器人是非常智能且非常神秘的,这也令人工智能在我的思想里笼罩了一层非同寻常的面纱,非常迫切的想要了解它的内涵。经过十几学时的学习,我对人工智能已有了初步了解,也深深的被它吸引,尤其通过本次程序设计,对人工智能的学习兴趣更加浓厚了!15数码问题是人工智能的一个经典的问题。本文中通过设计一个基于A*算法的状态空间搜索程序,对于给定的初始状态,采用f(n)=h(n)+g(n)表示以当前节点与其目标节点相应位置不相同元素的个数与搜索深度之和作为启发函数的度量,并用可视化编程语言C#来实现该问题。在程序的设计与实现过程中,遇到了很多的问题。首先由于初学人工智能,理解上有一定的困难,对A*算法的深入学习是一个曲折的过程。其次,在程序真正的设计及实现过程中,的确需要花费大量的精力来思考,反复试验。所设计的程序能够运行,但缺陷还是非常之大的,如其中重排OPEN表时,没有进行真正意义上的重新排列,只是选出代价最小的放在最先的位置,这实际上对程序的运行效率有很大的影响。同时通过输入大量的初始状态和目标状态发现,在一般情况下都可以找到最优的动作序列。但对某些稍微复杂的初始状态虽能得到正确解却不能完全得到最短的搜索路径,对于某些极其复杂的状态,甚至得不到解。这是有待进一步学习并改进的地方。但本程序还是有些值得肯定之处。界面设计比较友好,容易操作。而且在程序开始时,就判断目标状态是否可达,这样可节约大量的时间。虽然很多地方设计的不尽如意,但这是针对十五数码这个具体问题的一点优化。附录:/Programusing System;using System.Collections.Generic;using System.Windows.Forms;namespace _15Digital static class Program / / 应用程序的主入口点。 / STAThread static void Main() Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1(); /Form1using System;using System.Collections;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Windows.Forms.ComponentModel;using System.Reflection;namespace _15Digital public partial class Form1 : Form public Form1() InitializeComponent(); private ArrayList result;/存储初始状态到目标状态的各个变换过程的状态 private static int currentIndex = 0;/记录初始状态到目标状态共需要的步数 / / 对初始状态和目标状态矩阵进行初始化,其中的16个数码为015 / private void initialize() /定义初始态和目标态矩阵 int Start_Matrix = new int4; int End_Matrix = new int4; for (int i = 0; i 4; i+) Start_Matrixi = new int4; End_Matrixi = new int4; /目标状态矩阵的赋值 for (int i = 0; i 4; i+) for (int j = 0; j 4; j+) End_Matrixij = 4 * i + j + 1; End_Matrix33 = 0; /获取初始状态矩阵 for (int i = 0; i 4; i+) for (int j = 0; j 4; j+) int k = 4 * i + j + 1; TextBox tbox = (TextBox)this.findTextbox(textBox + k); int k2 = 16 + 4 * i + j + 1; (TextBox)this.findTextbox(textBox + k2).Text= tbox.Text.ToString (); if (tbox.Text = ) MessageBox.Show(请将所有空格填写完整!); return; Start_Matrixij = Convert.ToInt32(tbox.Text); /计算能否在50飞、步之内搜索到目标状态 _15Digital fifteen = new _15Digital(Start_Matrix, End_Matrix); if (fifteen.searchObject() = true) MessageBox.Show(成功找到目标状态!); result = fifteen.findResult(); button1.Enabled = true; else MessageBox.Show(在50步以内没有您所要的结果!); /将初始状态矩阵与显示窗口中的16数码矩阵相对应 private TextBox findTextbox(string name) foreach (Control temp in this.Controls) if (temp is System.Windows.Forms.TextBox) TextBox tb = (TextBox)temp; if (tb.Name = name) return tb; return null; /在“目标态的显示窗口”中逐步实现,直到达到目标态为止 private void button1_Click(object sender, EventArgs e) _15DigitalNode currentNode = (_15DigitalNode)resultcurrentIndex; for (int i = 0; i 4; i+) for (int j = 0; j 4; j+) int k2 = 16 + 4 * i + j + 1; TextBox tbox2 = (TextBox)this.findTextbox(textBox + k2); tbox2.Text = currentNode.matrixij.ToString(); currentIndex = (+currentIndex) % result.Count; if (currentIndex = 0) this.button1.Enabled = false; /初始化 private void button2_Click(object sender, EventArgs e) initialize(); /Form1.Designernamespace _15Digital partial class Form1 / / 必需的设计器变量。 / private System.ComponentModel.IContainer components = null; / / 清理所有正在使用的资源。 / / 如果应释放托管资源,为 true;否则为 false。 protected override void Dispose(bool disposing) if (disposing & (components != null) components.Dispose(); base.Dispose(disposing); #region Windows 窗体设计器生成的代码 / / 设计器支持所需的方法 - 不要 / 使用代码编辑器修改此方法的内容。 / private void InitializeComponent() this.textBox1 = new System.Windows.Forms.TextBox(); this.textBox2 = new System.Windows.Forms.TextBox(); this.textBox3 = new System.Windows.Forms.TextBox(); this.textBox4 = new System.Windows.Forms.TextBox(); this.textBox5 = new System.Windows.Forms.TextBox(); this.textBox6 = new System.Windows.Forms.TextBox(); this.textBox7 = new System.Windows.Forms.TextBox(); this.textBox8 = new System.Windows.Forms.TextBox(); this.textBox9 = new System.Windows.Forms.TextBox(); this.textBox10 = new System.Windows.Forms.TextBox(); this.textBox11 = new System.Windows.Forms.TextBox(); this.textBox12 = new System.Windows.Forms.TextBox(); this.textBox13 = new System.Windows.Forms.TextBox(); this.textBox14 = new System.Windows.Forms.TextBox(); this.textBox15 = new System.Windows.Forms.TextBox(); this.textBox16 = new System.Windows.Forms.TextBox(); this.button1 = new System.Windows.Forms.Button(); this.button2 = new System.Windows.Forms.Button(); this.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.label3 = new System.Windows.Forms.Label(); this.label4 = new System.Windows.Forms.Label(); this.textBox17 = new System.Windows.Forms.TextBox(); this.textBox18 = new System.Windows.Forms.TextBox(); this.textBox19 = new System.Windows.Forms.TextBox(); this.textBox20 = new System.Windows.Forms.TextBox(); this.textBox21 = new System.Windows.Forms.TextBox(); this.textBox22 = new System.Windows.Forms.TextBox(); this.textBox23 = new System.Windows.Forms.TextBox(); this.textBox24 = new System.Windows.Forms.TextBox(); this.textBox25 = new System.Windows.Forms.TextBox(); this.textBox26 = new System.Windows.Forms.TextBox(); this.textBox27 = new System.Windows.Forms.TextBox(); this.textBox28 = new System.Windows.Forms.TextBox(); this.textBox29 = new System.Windows.Forms.TextBox(); this.textBox30 = new System.Windows.Forms.TextBox(); this.textBox31 = new System.Windows.Forms.TextBox(); this.textBox32 = new System.Windows.Forms.TextBox(); this.label8 = new System.Windows.Forms.Label(); this.label5 = new System.Windows.Forms.Label(); this.SuspendLayout(); / / textBox1 / this.textBox1.Location = new System.Drawing.Point(6, 145); this.textBox1.Name = textBox1; this.textBox1.Size = new System.Drawing.Size(21, 21); this.textBox1.TabIndex = 0; / / textBox2 / this.textBox2.Location = new System.Drawing.Point(27, 145); this.textBox2.Name = textBox2; this.textBox2.Size = new System.Drawing.Size(21, 21); this.textBox2.TabIndex = 1; / / textBox3 / this.textBox3.Location = new System.Drawing.Point(48, 145); this.textBox3.Name = textBox3; this.textBox3.Size = new System.Drawing.Size(21, 21); this.textBox3.TabIndex = 2; / / textBox4 / this.textBox4.Location = new System.Drawing.Point(69, 145); this.textBox4.Name = textBox4; this.textBox4.Size = new System.Drawing.Size(21, 21); this.textBox4.TabIndex = 3; / / textBox5 / this.textBox5.Location = new System.Drawing.Point(6, 166); this.textBox5.Name = textBox5; this.textBox5.Size = new System.Drawing.Size(21, 21); this.textBox5.TabIndex = 4; / / textBox6 / this.textBox6.Location = new System.Drawing.Point(27, 166); this.textBox6.Name = textBox6; this.textBox6.Size = new System.Drawing.Size(21, 21); this.textBox6.TabIndex = 5; / / textBox7 / this.textBox7.Location = new System.Drawing.Point(48, 166); this.textBox7.Name = textBox7; this.textBox7.Size = new System.Drawing.Size(21, 21); this.textBox7.TabIndex = 6; / / textBox8 / this.textBox8.Location = new System.Drawing.Point(69, 166); this.textBox8.Name = textBox8; this.textBox8.Size = new System.Drawing.Size(21, 21); this.textBox8.TabIndex = 7; / / textBox9 / this.textBox9.Location = new System.Drawing.Point(6, 187); this.textBox9.Name = textBox9; this.textBox9.Size = new System.Drawing.Size(21, 21); this.textBox9.TabIndex = 8; / / textBox10 / this.textBox10.Location = new System.Drawing.Point(27, 187); this.textBox10.Name = textBox10; this.textBox10.Size = new System.Drawing.Size(21, 21); this.textBox10.TabIndex = 9; / / textBox11 / this.textBox11.Location = new System.Drawing.Point(48, 187); this.textBox11.Name = textBox11; this.textBox11.Size = new System.Drawing.Size(21, 21); this.textBox11.TabIndex = 10; / / textBox12 / this.textBox12.Location = new System.Drawing.Point(69, 187); this.textBox12.Name = textBox12; this.textBox12.Size = new System.Drawing.Size(21, 21); this.textBox12.TabIndex = 11; / / textBox13 / this.textBox13.Location = new System.Drawing.Point(6, 208); this.textBox13.Name = textBox13; this.textBox13.Size = new System.Drawing.Size(21, 21); this.textBox13.TabIndex = 12; / / textBox14 / this.textBox14.Location = new System.Drawing.Point(27, 208); this.textBox14.Name = textBox14; this.textBox14.Size = new System.Drawing.Size(21, 21); this.textBox14.TabIndex = 13; / / textBox15 / this.textBox15.Location = new System.Drawing.Point(48, 208); this.textBox15.Name = textBox15; this.textBox15.Size = new System.Drawing.Size(21, 21); this.textBox15.TabIndex = 14; / / textBox16 / this.textBox16.Location = new System.Drawing.Point(69, 208); this.textBox16.Name = textBox16; this.textBox16.Size = new System.Drawing.Size(21, 21); this.textBox16.TabIndex = 15; / / button1 / this.button1.Enabled = false; this.button1.Location = new System.Drawing.Point(113, 185); this.button1.Name = button1; this.button1.Size = new System.Drawing.Size(87, 23); this.button1.TabIndex = 32; this.button1.Text = 逐步实现; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); / / button2 / this.button2.Location = new System.Drawing.Point(113, 156); this.button2.Name = button2; this.button2.Size = new System.Drawing.Size(87, 23); this.button2.TabIndex = 33; this.button2.Text = 初始化; this.button2.UseVisualStyleBackColor = true; this.button2.Click += new System.EventHandler(this.button2_Click); / / label1 / this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(10, 30); this.label1.Name = label1; this.label1.Size = new System.Drawing.Size(257, 12); this.label1.TabIndex = 34; this.label1.Text = 1、请您任意输入115个数字,剩余一位请输入0; / / label2 / this.label2.AutoSize = true; this.label2.Location = new System.Drawing.Point(10, 9); this.label2.Name = label2; this.label2.Size = new System.Drawing.Size(77, 12); this.label2.TabIndex = 35; this.label2.Text = 操作方法如下; / / label3 / this.label3.AutoSize = true; this.label3.Location = new System.Drawing.Point(10, 54); this.label3.Name = label3; this.label3.Size = new System.Drawing.Size(227, 12); this.label3.TabIndex = 36; this.label3.Text = 2、初入初始数据以后,请点击初始化按钮; / / label4 / this.label4.AutoSiz
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 二零二五年度高品质实木地板专业施工合同
- 二零二五版智能社区房地产开发经营合同规范
- 二零二五年度矿业权抵押担保合作协议范本
- 二零二五年度茶叶电商平台技术开发合同
- 二零二五年度房产项目股权转让与代建服务一体化协议
- 2025版新能源工程师岗位责任及绩效合同样本
- 二零二五年度餐饮企业员工绩效考核合同
- 2025防火门节能环保型安装工程合同
- 2025年海水淡化及水处理设备项目合作计划书
- 稚子弄冰说课课件
- 2025年医学高级职称-中医肛肠(医学高级)历年参考题库含答案解析(5套100道单选题合辑)
- 二手车辆买卖及后续维修服务协议
- 八不伤害专题培训
- 危废应急预案范本
- 绿化技师考试试题及答案
- 2025雷电防护装置检测部位及检测点确认技术规范
- 指挥、司索工安全交底
- 2025年血液透析室培训试题(附答案)
- 税务稽查程序培训
- 地理●甘肃卷丨2024年甘肃省普通高中学业水平等级性考试高考地理真题试卷及答案
- 全国公开课一等奖七年级历史统编版上册《第4课夏商西周王朝的更替》课件(内嵌视频)
评论
0/150
提交评论