人工智能动物识别专家系统
一.题目:动物识别专家系统
二.实验目的
1.理解并掌握基于规则系统的表示与推理
2.学会编写小型的生产式系统,理解正向推理和反向推理的过程以及两者的区
别
3.学会设计简单的人机交互界面
三.实验内容
动物识别专家系统是流行的专家系统实验模型,它用产生式规则来表示知识,共15条规则、可以识别七种动物,这些规则既少又简单,可以改造他们,也可以加进新的规则,还可以用来识别其他东西的新规则来取代这些规则。
四.实验要求
1、确定推理方法(正向还是反向),并根据问题设计实现一个简单的不通用推理机(匹配、冲突消解)
2、规则库要求至少包含15条规则
3、初始事实可以任意给定,输入初始事实后能够得到推理结果
4、设计人机界面,解释模块提供查询规则的功能
5、可以不考虑知识库管理模块
6、提交实验报告,
7、报告中要有推理树
五.实验原理
动物识别专家系统是流行的专家系统实验模型,它用产生式规则来表示知识,共15条规则、可以识别七种动物,这些规则既少又简单,可以改造他们,也可以加进新的规则,还可以用来识别其他东西的新规则来取代这些规则。动物识别15条规则:
规则1:
如果:动物有毛发
则:该动物是哺乳动物
规则2:
如果:动物能产奶
则:该单位是哺乳动物
规则3:
如果:该动物有羽毛
则:该动物是鸟
规则4:
如果:动物会飞,且会下蛋
则:该动物是鸟
规则5:
如果:动物吃肉
则:该动物是肉食动物
规则6:
如果:动物有犬齿,且有爪,且眼盯前方
则:该动物是食肉动物
规则7:
如果:动物是哺乳动物,且有蹄
则:该动物是有蹄动物
规则8:
如果:动物是哺乳动物,且是反刍动物
则:该动物是有蹄动物
规则9:
如果:动物是哺乳动物,且是食肉动物,且是黄褐色的,且有暗斑点
则:该动物是豹
规则10:
如果:如果:动物是黄褐色的,且是哺乳动物,且是食肉,且有黑条纹
则:该动物是虎
规则11:
如果:动物有暗斑点,且有长腿,且有长脖子,且是有蹄类
则:该动物是长颈鹿
规则12:
如果:动物有黑条纹,且是有蹄类动物
则:该动物是斑马
规则13:
如果:动物有长腿,且有长脖子,且是黑色的,且是鸟,且不会飞
则:该动物是鸵鸟
规则14:
如果:动物是鸟,且不会飞,且会游泳,且是黑色的
则:该动物是企鹅
规则15:
如果:动物是鸟,且善飞
则:该动物是信天翁
六.推理树
七.代码
#include <>
#include <>
#include <>
#include <>
#define True 1
#define False 0
#define DontKnow -1
char *str[]={"chew_cud反刍动物","hooves蹄类动物","mammal哺乳动物","forward_eyes眼盯前方",
"claws有爪","pointed_teeth有犬齿","eat_meat吃肉","lay_eggs会下蛋","fly会飞",
"feathers有羽毛","ungulate有蹄","carnivore食肉动物","bird鸟","give_milk能产奶",
"has_hair有毛发","fly_well善飞","black&white_color黑白色","can_swim 会游泳",
"long_legs长腿","long_neck长脖子","black_stripes黑条纹","dark_spots 黑斑点",
"tawny_color黄褐色","albatross信天翁","penguin企鹅","ostrich驼鸟","zebra斑马",
"giraffe长颈鹿","tiger老虎","cheetah猎豹",0};
int rulep[][6]={{22,23,12,3,0,0},{21,23,12,3,0,0},{22,19,20,11,0,0}, {21,11,0,0,0,0},{17,19,20,13,-9,0},{17,18,13,-9,0,0},{16,13,0,0,0,0}, {15,0,0,0,0,0},{14,0,0,0,0,0},{10,0,0,0,0,0},{8,7,0,0,0,0},
{7,0,0,0,0,0},{4,5,6,0,0,0},{2,3,0,0,0,0},{1,3,0,0,0,0}};
int rulec[]={30,29,28,27,26,25,24,3,3,13,13,12,12,11,11,0};
class fact
{private:
int Number;
char Name[21];
int Active;
int Succ;
public:
fact *Next;
fact(int Num,char *L)
{
strcpy(Name,L);
Number=Num;
Active=False;
Succ=DontKnow;
Next=NULL;
}
char *GetName()
{
char *L;
L=new char[21];
strcpy(L,Name);
return L;
}
int GetNumber()
{
return Number;
}
int GetAct()
{
return Active;
}
int GetSucc()
{
return Succ;
}
void PutAct(const int Act0,int Suc0) {
Active=Act0;
Succ=Suc0;
}
};
fact *Fact;
class list
{private:
int Number;
public:
list *Next;
list(int Num)
{
Number=Num;
Next=NULL;
}
int GetNumber()
{
return Number;
}
};
class rule
{
char *Name;
list *Pre;
int Conc;
public:
rule *Next;
rule(char *N,int P[],int C);
~rule();
int Query();
void GetName()
{
cout< } }; rule::~rule() { list *L; while(Pre) { L=Pre->Next; delete Pre; Pre=L; } delete Name; } rule::rule(char *N,int P[],int C) { int i; list *L; Pre=NULL; Next=NULL; Name=new char[strlen(N)+1]; strcpy(Name,N); i=0; while(P[i]!=0) { L=new list(P[i++]); L->Next=Pre; Pre=L; } Conc=C; } int rule::Query() { char c; int Tag=0; list *L; fact *F; F=Fact; L=Pre; if(L==NULL) cout<<"\nError"; while(L!=NULL) { F=Fact; for(;;) { if(abs(L->GetNumber())==F->GetNumber()) break; F=F->Next; } if(L->GetNumber()>0) { if((F->GetSucc())==true) {L=L->Next;continue;} if((F->GetSucc())==false) return false; } else { if((F->GetSucc())==True) return False; if((F->GetSucc())==False) { L=L->Next; continue; } } cout< c=getchar(); flushall(); if((c=='Y')||(c=='y')) { if(L->GetNumber()>0) F->PutAct(1,True); if(L->GetNumber()<0) { F->PutAct(1,True); Tag=-1; return False; } } else { if(L->GetNumber()<0) F->PutAct(-1,False); else { F->PutAct(-1,False); Tag=-1; return False; } } L=L->Next; } F=Fact; for(;;) { if(Conc==F->GetNumber()) break; F=F->Next; } if(Conc<24) { F->PutAct(1,True); return False; } if(Tag!=-1) { F=Fact; for(;;) { if(Conc==F->GetNumber()) break; F=F->Next; } if(Conc<24) { F->PutAct(1,True); return False; } cout<<"\nThis aniamal is "< } return False; } int main() { fact *F,*T; rule *Rule,*R; char ch[8]; int i=1; Fact=NULL; while(str[i-1]) { F=new fact(i,str[i-1]); F->Next=Fact; Fact=F; i++; } F=Fact; Fact=NULL; while(F) { T=F; F=F->Next; T->Next=Fact; Fact=T; } i=0; ch[0]='R'; ch[1]='U'; ch[2]='L'; ch[3]='E'; ch[4]='_'; ch[5]='a'; ch[6]='\0'; Rule=NULL; for(i=0;i<15;i++) { R=new rule(ch,rulep[i],rulec[i]); R->Next=Rule; Rule=R; ch[5]++; } R=Rule; for(;;) { i=R->Query(); if((i==1)||(i==-1)) break; R=R->Next; if(!R) break; } if(!R) cout<<"没有这种动物."< cout<<"press any key to exit."< return True; } 八.截图 九.感想 通过这次课程设计我对专业课的学习有了更加深刻的认识,让我知道了学无止境的道理。我们每一个人永远不能满足于现有的成就,人生就像在爬山,一座峰的后面还有更高的山峰在等着你。挫折是一份财富,经历是一份拥有。这次课程设计必将成为我人生旅途上一段美好的回忆。以后我要更加努力学好每门专业课,让自己拥有更多的知识,才能解决更多问题。