《人工智能》实验报告之产生式系统.docx
《《人工智能》实验报告之产生式系统.docx》由会员分享,可在线阅读,更多相关《《人工智能》实验报告之产生式系统.docx(13页珍藏版)》请在冰豆网上搜索。
《人工智能》实验报告之产生式系统
《人工智能》实验报告
实验序号:
09260021-01实验项目名称:
产生式系统的设计与实现
学 号
姓 名
专业、班
实验地点
机房
指导教师
时间
2013年3月
一、实验目的及要求
1.理解产生式系统的结构原理与实际应用。
2.掌握产生式规则表示及规则库组建的实现方法。
3.理解推理机的算法原理并掌握其编程实现方法。
二、实验设备(环境)及方式
1.实验环境:
PC机,WindowsXP,VC(可自选);
2.实验方式:
两人一组,开发某个产生式系统分别实现正向推理和反向推理,系统主题可自选。
三、实验内容与要求
运用所学知识,设计并编程实现一个简单的产生式系统(如三角形判定系统、动物识别系统、感冒诊断系统等)。
1.简单产生式系统的名称与推理方式
①名称:
动物识别系统(正向)
②推理方式:
正向使用规则,即问题的初始状态作为初始数据库,仅当数据库中的事实满足某条规则的前提时,该规则才能被使用。
2.简单产生式系统的总体设计(系统组成模块及功能说明,包括人机交互模块、规则库存储及管理模块、综合数据库管理模块以及推理机制模块等)。
①人机交互模块:
通过勾选系统给出的判别条件来输入判别信息。
如图1,2所示:
3.简单产生式系统的详细设计(包括数据结构设计、参数设计、函数说明以及产生式规则集)
①数据结构设计:
classAnimal//定义实体存放规则
{
inta_id;
publicintA_id
{
get{returna_id;}
set{a_id=value;}
}
privateStringa_condition;
publicStringA_condition
{
get{returna_condition;}
set{a_condition=value;}
}
privateStringa_result;
publicStringA_result
{
get{returna_result;}
set{a_result=value;}
}
}
②参数设计:
int[]temp=newint[50];//存储当前所选择的的事实及推导出来的事实编号
string[]factInput=newstring[30];
//存储当前所选择的的事实及推导出来的事实内容
publicstaticstring[]factSelected=newstring[20];
//存储用户选择的事实,方便两个界面传递数据
publicstaticintfactLength;//存储事实库中的实际数据个数
string[]resultList=newstring[length];//存储规则库中所有后件
string[][]conditionList=newstring[length][];//存储规则库中所有前件
③函数说明:
publicListgetAll();//取出规则库中的所有规则
publicAnimalgetResultByID(inta_id);//根据ID取出规则的后件
privatevoidCompare(string[]factInput,string[][]conditionList,intj);
//匹配规则和已知事实库
privatevoidInsertRules(Animala);//将匹配成功的规则显示在界面上
4.系统流程
5.源程序清单
①用户选择界面:
Form1
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Windows.Forms;
usinganimal_IS.data;
namespaceanimal_IS
{
publicpartialclassForm1:
Form
{
int[]temp=newint[50];//存?
储洹?
当獭?
前°所ù选?
择?
的?
实害?
事?
以?
及°推?
导?
出?
来ぁ?
的?
事?
实害?
publicForm1()
{
InitializeComponent();
}
privatevoidbutton1_Click(objectsender,EventArgse)
{
this.checkBox1.Checked=false;
this.checkBox2.Checked=false;
this.checkBox3.Checked=false;
this.checkBox4.Checked=false;
this.checkBox5.Checked=false;
this.checkBox6.Checked=false;
this.checkBox7.Checked=false;
this.checkBox8.Checked=false;
this.checkBox9.Checked=false;
this.checkBox10.Checked=false;
this.checkBox11.Checked=false;
this.checkBox12.Checked=false;
this.checkBox13.Checked=false;
this.checkBox14.Checked=false;
this.checkBox15.Checked=false;
this.checkBox16.Checked=false;
this.checkBox17.Checked=false;
this.checkBox18.Checked=false;
this.checkBox19.Checked=false;
this.checkBox20.Checked=false;
this.checkBox21.Checked=false;
this.checkBox22.Checked=false;
for(inti=0;i{
Program.factSelected[i]=null;
}
}//重?
置?
所ù有瓺选?
项?
privatevoidbutton2_Click(objectsender,EventArgse)
{
int[]factNum=newint[20];
intk=0;
for(inti=1;i<=22;i++)
{
CheckBoxche=(CheckBox)this.groupBox1.Controls.Find("checkBox"+i,true)[0];
if(che.Checked)
{
factNum[k]=i;
k++;//记?
录?
用?
户§输?
入?
事?
实害?
的?
编括?
号?
}
}
Program.factLength=k;
for(intj=0;j{
DBdata=newDB();
stringsql="selectfactContentfrom[fact]wherefactID="+factNum[j];//根ù据Y编括?
号?
查é找ò出?
规?
则ò的?
前°键ü的?
内ú容╕,?
显?
示?
在útextbox中D
DataTabledt=data.query(sql);
if(dt.Rows.Count>0)
{
Program.factSelected[j]=dt.Rows[0][0].ToString();
}
}
Form2fr=newForm2();
fr.ShowDialog();
}
}
}
②结果显示界面:
Form2
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Windows.Forms;
usinganimal_IS.data;
usinganimal_IS.business;
usinganimal_IS.entity;
namespaceanimal_IS
{
publicpartialclassForm2:
Form
{
publicForm2()
{
InitializeComponent();
}
privatevoidForm2_Load(objectsender,EventArgse)
{
/**********************将?
数簓据Y库a中D的?
数簓据Y取?
出?
放?
在ú二t维?
数簓组哩?
里?
*******************************/
Listls=newList();
AnimalOptionaop=newAnimalOption();
ls=aop.getAll();
intlength=ls.Count;
string[]resultList=newstring[length];
string[][]conditionList=newstring[length][];
for(inti=0;i{
resultList[i]=ls[i].A_result;
conditionList[i]=ls[i].A_condition.Split(',?
');
}
/************************************************************************************/
/****************读á取?
出?
用?
户§选?
择?
的?
内ú容╕,?
显?
示?
出?
来ぁ?
,?
并¢放?
入?
数簓组哩actInput*****************/
stringfact=null;
string[]factInput=newstring[30];//定¨义?
数簓组哩?
,?
用?
来ぁ?
存?
储洹?
用?
户§输?
入?
的?
已?
知a事?
实害?
库a
for(inti=0;i{
fact+=""+Program.factSelected[i];
factInput[i]=Program.factSelected[i];
}
this.textBox3.Text=fact;
/**********************************************************************************/
stringstart="/***********************推?
理え?
开a始?
**********************/\r\n\r\n";
textBox1.Text=start;
for(intj=0;j{
if(conditionList[j]!
=null)
{
Compare(factInput,conditionList,j);
}
}
if(this.textBox1.Text.Equals(start))
{
this.textBox1.Text+="糟?
糕a了?
,?
啥?
也?
没?
推?
出?
。
£o(╯□?
╰)oTryAgain?
";
}
if(factInput[Program.factLength-1].Equals("老?
虎¢"))
{
this.textBox2.Text="O(∩é_∩é)O~~~老?
虎¢";
return;
}
if(factInput[Program.factLength-1].Equals("豹繿子哩?
))
{
this.textBox2.Text="O(∩é_∩é)O~~~豹繿子哩?
;
return;
}
if(factInput[Program.factLength-1].Equals("斑?
马í"))
{
this.textBox2.Text="O(∩é_∩é)O~~~斑?
马í";
return;
}
if(factInput[Program.factLength-1].Equals("长¤颈±鹿1"))
{
this.textBox2.Text="O(∩é_∩é)O~~~长¤颈±鹿1";
return;
}
if(factInput[Program.factLength-1].Equals("海£鸟?
"))
{
this.textBox2.Text="O(∩é_∩é)O~~~海£鸟?
";
return;
}
if(factInput[Program.factLength-1].Equals("企ó鹅ì"))
{
this.textBox2.Text="O(∩é_∩é)O~~~企ó鹅ì";
return;
}
if(factInput[Program.factLength-1].Equals("鸵?
鸟?
"))
{
this.textBox2.Text="O(∩é_∩é)O~~~鸵?
鸟?
";
return;
}
else
{
this.textBox2.Text="o_O?
?
?
小?
的?
才?
疏酣?
学§浅3没?
见?
过y这a动ˉ物?
,?
你?
丫?
上?
火e星?
找ò去ǎ?
吧悒?
!
?
";
}
}
privatevoidCompare(string[]factInput,string[][]conditionList,intj)
{
intflag=0;//将?
匹¥配?
标括?
志?
个?
数簓
intwidth=conditionList[j].Length;
for(intk=0;k{
for(intm=0;m{
if((conditionList[j][k].Equals(factInput[m])))
{
flag++;//一?
旦?
有瓺规?
则ò前°键ü不?
与?
已?
知a事?
实害?
库a匹¥配?
的?
,?
匹¥配?
标括?
志?
个?
数簓加ó1
}
}
}
if(flag==width)//当獭?
一?
条?
规?
则ò中D的?
所ù有瓺前°键ü都?
匹¥配?
时骸?
{
Animala=newAnimal();
AnimalOptionaop=newAnimalOption();
a=aop.getResultByID(j+1);//获?
取?
该?
条?
规?
则ò
stringresult=a.A_result;//获?
得?
完?
全?
匹¥配?
的?
后ó键ü
Program.factLength++;//已?
知a事?
实害?
库afactInput的?
非?
空?
位?
数簓加ó1
factInput[Program.factLength-1]=result;//将?
匹¥配?
规?
则ò的?
后ó键ü放?
入?
已?
知a实害?
时骸?
库a中D
InsertRules(a);//讲2该?
条?
规?
则ò显?
示?
出?
来ぁ?
}
else
{
return;
}
}
privatevoidInsertRules(Animala)
{
stringcondition=a.A_condition;
stringresult=a.A_result;
intid=a.A_id;
stringrun=null;
run="使?
用?
第台?
+id+"条?
规?
则ò:
阰"+condition+"╮(╯▽?
╰)╭>>"+result+"\r\n\r\n";
this.textBox1.Text+=run;
}
}
}
四、分析讨论
此次试验在建立数据库的过程中斟酌了很久,最终选定前件和后件各为一个属性的办法,最后使用的时候取出放入二维数组,这样既方便取出,又减少了数据冗余。
六、教师评语
签名:
日期:
2013.4
成绩
说明:
实验检查方式(7-8周)
1.系统演示
a)界面显示要求
(1)有若干选择初始事实的选择列表;
(2)显示规则的调用次序及综合数据库的变化过程;(正向推理中显示每使用一次规则后,产生的新事实。
反向推理中显示新的目标事实有哪些需要证明及哪些已经得到证实);
(3)显示最后的推理结果。
b)代码基本功能的实现要求
初始事实的输入;规则的存储与载入;规则匹配、冲突消解、规则执行;结果输出。
2.提交实验报告