事实库
编号 |
描述 |
编号 |
描述 |
编号 |
描述 |
1 |
有奶(grease) |
13 |
黑色条纹(black strips) |
25 |
鸟(bird) |
2 |
有毛发(crinte) |
14 |
黑色斑点(black scatters) |
26 |
肉食动物(flesh-eater) |
3 |
有羽毛(feather) |
15 |
长腿(long legs) |
27 |
有蹄动物(ungulate) |
4 |
会飞(can fly) |
16 |
长脖子(long neck) |
28 |
偶蹄动物(artiadactyl) |
5 |
生蛋(oviparous) |
17 |
暗斑点(dark scatters) |
29 |
老虎(tiger) |
6 |
有爪(crawls) |
18 |
白色(white) |
30 |
金钱豹(leopard) |
7 |
有犬齿(canine) |
19 |
不会飞(cant fly) |
31 |
长颈鹿(giraffe) |
8 |
目盯前方(stare ahead) |
20 |
黑白色(black and white) |
32 |
斑马(zebra) |
9 |
吃肉(eating meat) |
21 |
会游泳(can swim) |
33 |
鸵鸟(ostrich) |
10 |
有蹄(hooves) |
22 |
善飞(be ggod at flying) |
34 |
企鹅(penguin) |
11 |
反刍食物(ruminant) |
23 |
不怕风浪(good coping with wind and waves) |
35 |
海燕(salangane) |
12 |
黄褐色(filemot) |
24 |
哺乳动物(mammal) |
|
|
推理规则
(∧合取符号)
依据 |
编号 |
结论 |
编号 |
有奶 |
1 |
哺乳动物 |
24 |
有毛发 |
2 |
哺乳动物 |
24 |
有羽毛 |
3 |
鸟 |
25 |
会飞、生蛋 |
4∧5 |
鸟 |
25 |
有爪、有犬齿、目盯前方、哺乳动物 |
6∧7∧8∧24 |
肉食动物 |
26 |
吃肉、哺乳动物 |
9∧24 |
肉食动物 |
26 |
有蹄、哺乳动物 |
10∧24 |
有蹄动物 |
27 |
反刍食物、有蹄动物 |
11∧27 |
偶蹄动物 |
28 |
黄褐色、黑条纹、肉食动物 |
12∧13∧24 |
老虎 |
29 |
黄褐色、黑斑点、肉食动物 |
12∧14∧24 |
金钱豹 |
30 |
黄褐色、长腿、长脖子、暗斑点、有蹄动物 |
12∧15∧16∧17 |
长颈鹿 |
31 |
黑色条纹、白色、有蹄动物 |
13∧18∧27 |
斑马 |
32 |
长腿、长脖子、不会飞、鸟 |
15∧16∧19∧25 |
鸵鸟 |
33 |
不会飞、黑白色、会游泳、鸟 |
19∧20∧21∧25 |
企鹅 |
34 |
善飞、不怕风浪、鸟 |
22∧23∧25 |
海燕 |
35 |
编程思路
对 事实库 建立映射表,实现编码到特征之间的映射,数据类型采用 整型(int)和字符串(string)
对 推理规则 建立映射表,实现特征集合与结论之间的映射,数据类型采用 向量(vector)和整形(int)
运算采用集合运算,按照映射表中推理规则的顺序,输入特征集合与推理以据的特征集合取交集判断,是否按照该推理规则归并特征为新的事实
代码
#include<iostream>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
map<int,string> tables = {
{1,"grease"},
{2,"crinte"},
{3,"feather"},
{4,"can fly"},
{5,"oviparous"},
{6,"crawls"},
{7,"canine"},
{8,"stare ahead"},
{9,"eating-meat"},
{10,"hooves"},
{11,"ruminant"},
{12,"filemot"},
{13,"black stripes"},
{14,"black scatters"},
{15,"long legs"},
{16,"long neck"},
{17,"dark scatters"},
{18,"white"},
{19,"cant fly"},
{20,"black and white"},
{21,"can swim"},
{22,"be good at flying"},
{23,"good coping with wind and waves"},
{24,"mammal"},
{25,"bird"},
{26,"flesh-eater"},
{27,"ungulate"},
{28,"artiodactyl"},
{29,"tiger"},
{30,"leopard"},
{31,"giraffe"},
{32,"zebra"},
{33,"ostrich"},
{34,"penguin"},
{35,"salangane"}
};
map<vector<int>,int> classific = {
{{1},24},
{{2},24},
{{3},25},
{{4,5},25},
{{6,7,8,24},26},
{{9,24},26},
{{10,24},27},
{{11,27},28},
{{12,13,24},29},
{{12,14,24},30},
{{12,15,16,17,27},31},
{{13,18,27},32},
{{15,16,19,25},33},
{{19,20,21,25},34},
{{22,23,25},35}
};
int main(){
int i,j,a,b,c;
int num;
vector<int> fact,temp;
int flag=1;
while(flag==1){
cout<<"input the features ad following:"<<endl;
map<int,string>::iterator iter;
iter = tables.begin();
while(iter->first < 24){
cout<< iter->first << "."<< iter->second<<"\t";
if(iter->first%5==0)
cout<<endl;
iter ++;
}
cout<<endl;
cout<<"number of features:"<<endl;
cin>>num;
cout<<"input the features in order:"<<endl;
for(i=0;i<num;i++){
cin>>a;
fact.push_back(a);
}
sort(fact.begin(),fact.end());
map<vector<int>,int>::iterator vec_iter;
vector<int> insec,tmp_sec;
vec_iter = classific.begin();
i=0;
while(vec_iter != classific.end()){
i ++;
set_intersection(vec_iter->first.begin(),vec_iter->first.end(),fact.begin(),fact.end(),back_inserter(insec));
set_difference(vec_iter->first.begin(),vec_iter->first.end(),insec.begin(),insec.end(),inserter(tmp_sec,tmp_sec.begin()));
if (tmp_sec.size()==0){
insec.clear();
set_difference(fact.begin(),fact.end(),vec_iter->first.begin(),vec_iter->first.end(),inserter(insec,insec.begin()));
fact.assign(insec.begin(),insec.end());
fact.push_back(vec_iter->second);
cout<<"rule "<<i<<" ,add "<<tables[vec_iter->second]<<" to fact"<<endl;
}
tmp_sec.clear();
insec.clear();
vec_iter ++;
}
int res = fact.back();
if(res<29)
cout<<"cant infer any result according to information you give"<<endl;
else
cout<<"the animal may be "<<tables[res]<<endl;
cout<<"input number 1 if you want continue, or any other key to quit:"<<endl;
cin>>flag;
}
return 1;
}
程序运行结果