c++实现简单的产生式推理系统

c++实现简单的产生式推理系统

事实库

编号描述编号描述编号描述
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
有羽毛325
会飞、生蛋4∧525
有爪、有犬齿、目盯前方、哺乳动物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},               //1
    {{2},24},
    {{3},25},
    {{4,5},25},
    {{6,7,8,24},26},        //5
    {{9,24},26},
    {{10,24},27},
    {{11,27},28},
    {{12,13,24},29},
    {{12,14,24},30},        //10
    {{12,15,16,17,27},31},
    {{13,18,27},32},
    {{15,16,19,25},33},
    {{19,20,21,25},34},
    {{22,23,25},35}         //15
};
//主程序
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 ++;
            //按顺序判断映射表key是否包含于fact
            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){
                //包含,则取fact和key的差集,并重新赋值给fact
                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;
}

程序运行结果

在这里插入图片描述

  • 2
    点赞
  • 50
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值