人工智慧規則正向演繹系統簡單程式示範(c++)

來源:互聯網
上載者:User

#include "iostream.h"
#include "stdio.h"
#include "conio.h"
#include "string.h"

typedef struct Rule//第一規則庫
{
 char Condition[6][20];
 char Result[20];
 int  Cnum;
 bool used;
}rule;

typedef struct Fact//定義事實庫
{
 char item[20][20];
 int  Fnum;
}fact;

rule r[100];
fact f;

void Create_Rules();//初始化規則庫
void Create_Facts();//初始化事實庫
int  Get_RulesNum();
void Ratiocinate(int n);

void main()
{
 int Rnum=0;
 Create_Rules();
 Create_Facts();
 Rnum=Get_RulesNum();
 //cout<<Rnum<<endl;
 Ratiocinate(Rnum);

}

void Create_Rules()
{
 strcpy(r[0].Condition[0],"產奶");
 strcpy(r[0].Result,"哺乳動物");
 r[0].Cnum=1;
 r[0].used=false;
 
 strcpy(r[1].Condition[0],"有毛髮");
 strcpy(r[1].Result,"哺乳動物");
 r[1].Cnum=1;
 r[1].used=false;

 strcpy(r[2].Condition[0],"有羽毛");
 strcpy(r[2].Result,"鳥");
 r[2].Cnum=1;
 r[2].used=false;

 strcpy(r[3].Condition[0],"會飛");
 strcpy(r[3].Condition[1],"生蛋");
 strcpy(r[3].Result,"鳥");
    r[3].Cnum=2;
 r[3].used=false;

 strcpy(r[4].Condition[0],"哺乳動物");
 strcpy(r[4].Condition[1],"有抓");
 strcpy(r[4].Condition[2],"有齒");
 strcpy(r[4].Condition[3],"眼睛前視");
  strcpy(r[4].Result,"食肉動物");
 r[4].Cnum=4;
 r[4].used=false;

 strcpy(r[5].Condition[0],"哺乳動物");
 strcpy(r[5].Condition[1],"吃肉");
 strcpy(r[5].Result,"食肉動物");
 r[5].Cnum=2;
 r[5].used=false;

 strcpy(r[6].Condition[0],"哺乳動物");
 strcpy(r[6].Condition[1],"有蹄");
 strcpy(r[6].Result,"有蹄動物");
 r[6].Cnum=2;
 r[6].used=false;

 strcpy(r[7].Condition[0],"有蹄動物");
 strcpy(r[7].Condition[1],"反芻");
 strcpy(r[7].Result,"偶蹄動物");
 r[7].Cnum=2;
 r[7].used=false;

 strcpy(r[8].Condition[0],"食肉動物");
 strcpy(r[8].Condition[1],"黃褐色");
 strcpy(r[8].Condition[2],"有黑色條紋");
 strcpy(r[8].Result,"老虎");
 r[8].Cnum=3;
 r[8].used=false;
 
 strcpy(r[9].Condition[0],"食肉動物");
 strcpy(r[9].Condition[1],"黃褐色");
 strcpy(r[9].Condition[2],"有黑色斑點");
 strcpy(r[9].Result,"金錢豹");
 r[9].Cnum=3;
 r[9].used=false;

 strcpy(r[10].Condition[0],"有蹄動物");
 strcpy(r[10].Condition[1],"長腿");
 strcpy(r[10].Condition[2],"長頸");
 strcpy(r[10].Condition[3],"有暗斑點");
 strcpy(r[10].Condition[4],"黃褐色");
 strcpy(r[10].Result,"長頸鹿");
 r[10].Cnum=5;
 r[10].used=false;

 strcpy(r[11].Condition[0],"有蹄動物");
 strcpy(r[11].Condition[1],"白色");
 strcpy(r[11].Condition[2],"有黑色條紋");
 strcpy(r[11].Result,"斑馬");
 r[11].Cnum=3;
 r[11].used=false;

 strcpy(r[12].Condition[0],"鳥");
 strcpy(r[12].Condition[1],"不會飛");
 strcpy(r[12].Condition[2],"長腿");
 strcpy(r[12].Condition[3],"長頸");
 strcpy(r[12].Condition[4],"黑白色");
 strcpy(r[12].Result,"鴕鳥");
 r[12].Cnum=5;
 r[12].used=false;

 strcpy(r[13].Condition[0],"鳥");
 strcpy(r[13].Condition[1],"不會飛");
 strcpy(r[13].Condition[2],"會遊泳");
 strcpy(r[13].Condition[3],"黑白色");
 strcpy(r[13].Result,"企鵝");
 r[13].Cnum=4;
 r[13].used=false;

 strcpy(r[14].Condition[0],"鳥");
 strcpy(r[14].Condition[1],"會飛");
 strcpy(r[14].Condition[2],"不怕風浪");
 strcpy(r[14].Result,"海燕");
 r[14].Cnum=3;
 r[14].used=false;
}

void Create_Facts()
{
   strcpy(f.item[0],"有毛髮");
   strcpy(f.item[1],"有蹄");
   strcpy(f.item[2],"長腿");
   strcpy(f.item[3],"長頸");
   strcpy(f.item[4],"有暗斑點");
   strcpy(f.item[5],"黃褐色");
   f.Fnum=6;
}

int Get_RulesNum()
{
 int num=0;
 int i=0;
    while(1)
 {
  if(r[i].Condition[0][0]=='\0')
  {
           break;
  }
        i++;
  num++;
 }
    return num;
}

void Ratiocinate(int n)
{
 bool FindFact(char * str);
 void InsertIntoFact(char * str);

 int i=0,j;/// i用來控制尋找的是第幾條規則,j用來控制尋找的規則中的第即條
 while(1)
 {
  if(r[i].used==false)
  {
        for(j=0;j<r[i].Cnum;j++)
     {
     if(FindFact(r[i].Condition[j]))
     {
      continue;
     }
     else
     {
      cout<<"規則"<<i+1<<"匹配失敗"<<endl;
      break;
     }
     }
  }
  if(j==r[i].Cnum) ///如果所有的前件都滿足
  {
         r[i].used=true; 
   cout<<"規則"<<i+1<<"匹配成功,結論插入到事實庫"<<endl;
   if(!FindFact(r[i].Result))
   {
     InsertIntoFact(r[i].Result);///插入事實
     cout<<"新增加的事實為:"<<f.item[f.Fnum-1]<<endl;
   }
         i=0;
   //continue;
  }
        else i++;//查看下條規則
   if(i==n)
  {
   cout<<"沒有你要找符合要求的動物,請增加新的規則"<<endl;
   break;
  }
  else if(strcmp(f.item[f.Fnum-1],"老虎")==0||strcmp(f.item[f.Fnum-1],"金錢豹")==0||strcmp(f.item[f.Fnum-1],"長頸鹿")==0||strcmp(f.item[f.Fnum-1],"斑馬")==0||strcmp(f.item[f.Fnum-1],"鴕鳥")==0||strcmp(f.item[f.Fnum-1],"企鵝")==0||strcmp(f.item[f.Fnum-1],"海燕")==0)
  {
   cout<<"與你給的事實匹配的動物:"<<f.item[f.Fnum-1]<<endl;
   break;
  }
 }

}

//查看事實庫中的事實否否等於前件,或者是存在這條事實,那麼新的結論就不用加到事實庫中
bool FindFact(char * str)
{
 int i;
 for(i=0;i<f.Fnum;i++)
 {
  if(strcmp(f.item[i],str)) continue;
  else return true;
 }
 return false;

}

//插入新的事實
void InsertIntoFact(char * str)
{
 strcpy(f.item[f.Fnum],str);
 f.Fnum++;
}

非常簡單的功能,主要是要體現出人工智慧規則正向演繹系統簡單示範和演算法!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.