【人工智能I】知识表示实验

知识表示实验

  • 猴子摘香蕉问题
    • 问题描述
    • 实验代码
  • 简单动物识别系统的产生式表示
    • 问题描述
    • 实验代码

猴子摘香蕉问题

问题描述

在这里插入图片描述

实验代码

方法1:

#include
#include
using namespace std;
int banana(int a,int c,int b){int i;if(a!=b){if(a<b){cout<<"猴子在箱子左边,它向右走"<<b-a<<"步,已到达箱子旁"<<endl;}else if(a>b){cout<<"猴子在箱子右边,它向左走"<<a-b<<"步,已到达箱子旁"<<endl;}a=b; }else if(a==b){cout<<"猴子就在箱子旁"<<endl; }if(b!=c){if(b<c){cout<<"猴子和箱子在香蕉的左下方,它推着箱子向右走"<<c-b<<"步,已到达香蕉下方"<<endl;}if(c<b){cout<<"猴子和箱子在香蕉的右下方,它推着箱子向左走"<<b-c<<"步,已到达香蕉下方"<<endl;}b=c;a=c; } else if(b==c){cout<<"猴子和箱子就在香蕉正下方"<<endl; }if(a==c&&b==c){cout<<"猴子爬上箱子,成功摘到香蕉!"<<endl; }}
int main(){int a,c,b;//a=猴子目前位置,c=香蕉对应地面位置,b=箱子目前位置cout<<"请输入猴子当前在地面的位置:";cin>>a; cout<<"请输入香蕉对应地面的位置:";cin>>c; cout<<"请输入箱子位置:";cin>>b;banana(a,c,b);return 0;
} 

方法2:

#include 
#include 
using namespace std; struct State{int monkey;//-1,Monkey at A;0,Monkey at C;1,Monkey at Bint box;//-1,box at A;0,box at C;1,box at Bint banana;//Banana at C,Banana=0int monbox;//-1,monkey on the box;1:monkey not on the bo 
};
struct State States[150];
string routesave[150];
//monkeygoto,it makes the monkey goto the other place
void monkeygoto(int b,int i){int a=b;if(a == -1){routesave[i]="Monkey go to A";States[i+1]=States[i];States[i+1].monkey= -1;}else if(a == 0){routesave[i]="Monkey go to C";States[i+1]=States[i];States[i+1].monkey = 0;}else if(a == 1){routesave[i]="Monkey go to B";States[i+1]=States[i];States[i+1].monkey = 1;}else printf("parameter is wrong");
} 
//the monkey move the box to the other place
void movebox(int a,int i){int B = a;if(B == -1){routesave[i] = "Monkey moves the box to A";States[i+1] = States[i];States[i+1].monkey = -1;States[i+1].box = -1;}else if(B == 0){routesave[i] = "Monkey moves the box to C";States[i+1] = States[i];States[i+1].monkey = 0;States[i+1].box = 0;}else if(B == 1){routesave[i] = "Monkey moves the box to B";States[i+1] = States[i];States[i+1].monkey = 1;States[i+1].box = 1;}else{printf("parameter is wrong");}
} 
//the monkey climb onto the box
void climbonto(int i)
{routesave[i] = "Monkey climb onto the box";States[i + 1] = States[i];States[i + 1].monbox = 1;
}
//monkey climb down from the box
void climbdown(int i)
{	routesave[i] = "Monkey climb down from the box";States[i + 1] = States[i];States[i + 1].monbox = -1;
}
//if the monkey,box,and banana are at the same place,the monkey reach banana
void reach(int i)
{routesave[i] = "Monkey reach the banana";
}
//output the solution to the problem
void showSolution(int i)
{int c;cout<<"Result to problem:"<<endl;for (c = 0; c < i + 1; c++){cout<<"Step"<< c + 1<<":" <<routesave[c]<<endl;}cout<<endl;
}
//perform next step
void nextStep(int i)
{int c,j;if (i >= 150){cout << "steplength reached 150,have problem " << endl;return;}for (c = 0; c < i; c++) {//if the current state is same to previous,retrospectif (States[c].monkey == States[i].monkey&&States[c].box == States[i].box&&States[c].banana == States[i].banana&&States[c].monbox == States[i].monbox){return;}}//if the box,the monkey,the banana at the same place,in the meantime,the monkey on the box .the monkey can reach the bananaif (States[i].monbox == 1 && States[i].monkey == States[0].banana && States[i].banana == States[0].banana && States[i].box == States[0].banana){showSolution(i);cout << "Press any key to continue " << endl;return;}j = i + 1;//if the box,the monkey,the banana at the same place,but the monkey is not on the box,then climb itif (States[i].box == States[i].monkey&&States[i].box == States[i].banana){if (States[i].monbox == -1){climbonto(i);reach(i + 1);nextStep(j);}else{reach(i + 1);nextStep(j);}}//if the three things are not in the same place else if (States[i].box == States[i].monkey&&States[i].box != States[i].banana){if (States[i].monbox == -1){movebox(States[i].banana, i);nextStep(j);}else{climbdown(i);nextStep(j);}}else if (States[i].box != States[i].monkey&&States[i].box == States[i].banana){monkeygoto(States[i].box, i);nextStep(j);}else if (States[i].box != States[i].monkey&&States[i].box != States[i].banana){monkeygoto(States[i].box, i);nextStep(j);}
}
int main()
{cout << "初始位置:" << endl;cout << "monkey(-1 or 0 or 1):";cin>>States[0].monkey;cout << "box(-1 or 0 or 1):";cin >> States[0].box;cout << "banana(-1 or 0 or 1):";cin>>States[0].banana;cout << "monbox(-1 or 1):";cin>>States[0].monbox;nextStep(0);
}

简单动物识别系统的产生式表示

问题描述

在这里插入图片描述

实验代码

#include 
#include 
using namespace std;
struct RULES					
{int count;char pre[255];char back[255];int mark;
};
void check();
RULES r[100] = {{ 1,"有毛发","哺乳动物",0 },					//所有规则静态数据库{ 1,"有奶","哺乳动物",0 },{ 1,"有羽毛","鸟",0 },{ 2,"会飞&下蛋&","鸟",0 },{ 1,"吃肉","食肉动物",0 },{ 3,"有锋利的牙齿&有爪&眼睛盯着前方&","食肉动物",0 },{ 2,"哺乳动物&有蹄&","有蹄类哺乳动物",0 },{ 2,"哺乳动物&反刍&","有偶蹄类哺乳动物",0 },{ 4,"哺乳动物&食肉动物&黄褐色&有暗斑&","金钱豹",0 },{ 4,"哺乳动物&食肉动物&黄褐色&黑色条纹&","老虎",0 },{ 4,"有蹄类哺乳动物&有长脖子&有长腿&有暗斑&","长颈鹿",0 },{ 2,"有蹄类哺乳动物&黑条纹&","斑马",0 },{ 5,"鸟&不会飞&有长脖子&有长腿&黑白色&","鸵鸟",0 },{ 4,"鸟&不会飞&会游泳&黑白色&","企鹅",0 },{ 2,"鸟&会飞&","信天翁",0 },
};int number;
int m;
int cat = 15;
int a;
int length;					//输入的事实长度
string f[255];				//输入的事实数组
void result()
{int i = 1, m = 0;while (i != length + 1){if (f[i] == "金钱豹"){cout << "该动物是金钱豹" << endl;m = 1;break;}elseif (f[i] == "老虎"){cout << "该动物是老虎" << endl;m = 1;break;}elseif (f[i] == "长颈鹿"){cout << "该动物是长颈鹿" << endl;m = 1;break;}elseif (f[i] == "斑马"){cout << "该动物是斑马" << endl;m = 1;break;}elseif (f[i] == "鸵鸟"){cout << "该动物是鸵鸟" << endl;m = 1;break;}elseif (f[i] == "企鹅"){cout << "该动物是企鹅" << endl;m = 1;break;}elseif (f[i] == "信天翁"){cout << "信天翁" << endl;m = 1;break;}elsei++;}if (m == 0)cout << "没有符合的动物,请确认特征,重新输入" << endl;}
int find_rule(int s)			//查找规则库中是否还有可使用的规则
{for (int i = 0; i <= 15; i++)s = s*r[i].mark;//cout<<"find_rule结果"<return s;
}
int compare1(RULES r)   //当前提条件为1时
{int j = 0, i = 1;string str, str2;str = r.pre;while (i <= length){if (f[i] == str){str2 = r.back;f[length + 1] = str2;			//加入事实库length++;					//事实库的长度加1r.mark = 1;					//标记规则已使用过break;}elsei++;}return r.mark;
}
int compare2(RULES r)				//前提条件不为1
{string b[10];string str, str2;int i, j = 1, num = 0;int a = 0;str = r.pre;for (i = 0; i != 10; ++i)          //转换数组{b[i] = "";}for (i = 0; i != str.length(); ++i){if (str.at(i) != '&'){b[j] += str.at(i);}else{j++;}}i = 1;while (i <= r.count){for (j = 1; j != length + 1; j++){if (f[j] == b[i]){a += 1;}}i++;}if (a == r.count){str2 = r.back;f[length + 1] = str2;			//加入事实库length++;					//事实库的长度加1r.mark = 1;					//标记规则已使用过}return r.mark;
}void idetify()
{int i = 0, u = 0;if (find_rule(u) == 0)           //如果规则库中还有未使用的规则{//cout<<"还有未使用的规则"<int num = length;while (i<16)					//从第一条规则开始遍历{if (r[i].mark == 0)				//如果该条规则未使用{if (r[i].count == 1)				//该条规则前提数为1{u = compare1(r[i]);if (u == 1)r[i].mark = 1;if (r[i].mark == 1){cout << "使用规则" << i + 1;cout << "且加入的新事实为" << r[i].back << endl;}}else{u = compare2(r[i]);if (u == 1)r[i].mark = 1;if (r[i].mark == 1){cout << "使用规则" << i + 1;cout << "且加入的新事实为" << r[i].back << endl;}}}if (i == 15){if (num != length){i = 0;num = length;}elsei = 16;}else{i++;}}}else{cout << "所有的规则都已使用" << endl;}result();
}/*主函数*/
int main()
{	cout << "进行动物识别" << endl;int u = 0;cout << "请输入动物的特征数" << endl;cin >> length;cout << "请输入动物的特征" << endl;for (int i = 1; i <= length; i++)cin >> f[i];idetify();system("pause");return 0;
}


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部