四、个人体会
初学人工智能时,最先联想到的便是机器人,一直感觉机器人是非常智能且非常神秘的,这也令人工智能在我的思想里笼罩了一层非同寻常的面纱,非常迫切的想要了解它的内涵。经过十几学时的学习,我对人工智能已有了初步了解,也深深的被它吸引,尤其通过本次程序设计,对人工智能的学习兴趣更加浓厚了!
15数码问题是人工智能的一个经典的问题。本文中通过设计一个基于A*算法的状态空间搜索程序,对于给定的初始状态,采用f(n)=h(n)+g(n)表示以当前节点与其目标节点相应位置不相同元素的个数与搜索深度之和作为启发函数的度量,并用可视化编程语言C#来实现该问题。
在程序的设计与实现过程中,遇到了很多的问题。首先由于初学人工智能,理解上有一定的困难,对A*算法的深入学习是一个曲折的过程。其次,在程序真正的设计及实现过程中,的确需要花费大量的精力来思考,反复试验。所设计的程序能够运行,但缺陷还是非常之大的,如其中重排OPEN表时,没有进行真正意义上的重新排列,只是选出代价最小的放在最先的位置,这实际上对程序的运行效率有很大的影响。
同时通过输入大量的初始状态和目标状态发现,在一般情况下都可以找到最优的动作序列。但对某些稍微复杂的初始状态虽能得到正确解却不能完全得到最短的搜索路径,对于某些极其复杂的状态,甚至得不到解。这是有待进一步学习并改进的地方。
但本程序还是有些值得肯定之处。界面设计比较友好,容易操作。而且在程序开始时,就判断目标状态是否可达,这样可节约大量的时间。虽然很多地方设计的不尽如意,但这是针对十五数码这个具体问题的一点优化。
附录:
//Program using 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()); } } }
//Form1
using 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个数码为0~15 ///
private void initialize() {
//定义初始态和目标态矩阵
int[][] Start_Matrix = new int[4][]; int[][] End_Matrix = new int[4][]; for (int i = 0; i < 4; i++) {
Start_Matrix[i] = new int[4]; End_Matrix[i] = new int[4]; }
//目标状态矩阵的赋值 for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
End_Matrix[i][j] = 4 * i + j + 1; } }
End_Matrix[3][3] = 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(\ int k2 = 16 + 4 * i + j + 1;
((TextBox)this.findTextbox(\tbox.Text.ToString ();
if (tbox.Text == \ {
MessageBox.Show(\请将所有空格填写完整!\ return; }
Start_Matrix[i][j] = 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)result[currentIndex]; 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(\ tbox2.Text = currentNode.matrix[i][j].ToString(); } }
currentIndex = (++currentIndex) % result.Count; if (currentIndex == 0)
this.button1.Enabled = false; }
//初始化
private void button2_Click(object sender, EventArgs e) {
initialize(); } } }
//Form1.Designer namespace _15Digital {
partial class Form1 {
///
/// 必需的设计器变量。 ///
private System.ComponentModel.IContainer components = null;
///
/// 清理所有正在使用的资源。 ///
///
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();