AI实验-七种动物识别系统

时间:2024-01-28 20:07:55

AI-动物识别

一、实验目的

1. 理解产生式系统的结构原理与实际应用。 
2. 掌握产生式规则表示及规则库组建的实现方法。 
3. 熟悉和掌握产生式系统的运行机制,掌握基于规则推理的基本方法。

二、实验原理

   产生式系统用来描述若干个不同的以一个基本概念为基础的系统,这个基本概念就是产生式规则或产生式条件和操作对。在产生式系统中,论域的知识分为两部分:用事实表示静态知识;用产生式规则表示推理过程和行为

1.实验要求

运用所学知识,设计并编程一个小型的动物识别系统,识别对象:虎、金钱豹、斑马、长颈鹿、鸵鸟、企鹅、信天翁 七种动物

2.识别规则库

R1:  有毛(1)  --> 哺乳动物(12)  
R2:  有奶(2)  --> 哺乳动物(12)
R3:  有羽毛(3)   --> 鸟类(13)
R4:  会飞(4) & 会下蛋  --> 鸟类(13)
R5:  吃肉(6)  --> 食肉动物(14)
R6:  犬齿(7) & 有爪(8) & 盯前方(9)  --> 食肉动物(14)
R7:  哺乳动物(12) & 有蹄(10)  -->  有蹄类动物(15)
R8:  哺乳动物(12) & 反刍(11)  -->  有蹄类动物(15)
R9:  哺乳动物(12) & 食肉动物(14) & 黄褐色(16) & 暗斑点(17)  --> 金钱豹(a)
R10: 哺乳动物(12) & 食肉动物(14) & 黄褐色(16) & 黑色条纹(18)  --> 虎(b)
R11: 有蹄类动物(15) & 长脖(19) & 长腿(20) & 暗斑色(17)  --> 长颈鹿(c)
R12: 有蹄类动物(15) & 黑色条纹(18) &  -->  斑马(d)
R13: 鸟类(13) & 长脖(19) & 长腿(20) & 不会飞(21) & 黑白两色(22)  -->  鸵鸟(e)
R14: 鸟类(13) & 会游泳(23) & 不会飞(21) & 黑白二色(22)  -->  企鹅(f)
R15: 鸟类(13) & 善飞(24)  -->  信天翁(g)

以上为:动物识别规则的15条规则,已编码

3.思路分析

  1. 第一次编写时:采用的是,条件对应原则,每个条件对应的动物做一个集合,多个条件集合取交集,得到对应动物。方法-错误×,未使用该15条规则,推理而得。

    如:当选择 条件不会飞 和 会飞时,集合方式无法推理出结果,
       - 会飞 && 会下蛋  -->  鸟类
       - 鸟类 && 不会飞 && 会游泳 && 黑白二色  --> 企鹅
       + 输入条件:
       - 会飞 && 会下蛋 && 不会飞 && 会游泳 && 黑白二色  -×-> 企鹅
    
  2. 改版,版本2.0,重新设计对应方式:

    通过两个数组: 
      - two1 = []    // 存放单击选择的条件
      - three = []   // 初始值为 13个 0 组成的数组
    1. 遍历 选择的条件数组: two1[]  
    
    2. 将拿到的数组结果,依次 if 判断,
       如:
       - 有羽毛(3) 则 将 three 的three[1] = 13 
       - 依次类推
    3. 最后拿到结果:three[] 数组,满足对应条件,而重新赋值的数组,进行判断:
      如:信天翁判断
      -  if ( this.three[1] == 13 && this.three[12] == 24 &&
              this.three[0] == 0 && this.three[2] == 0 && this.three[3] == 0 &&
              this.three[4] == 0 && this.three[5] == 0 && this.three[6] == 0 &&
              this.three[7] == 0 && this.three[8] == 0 && this.three[9] == 0 &&
              this.three[10] == 0 && this.three[11] == 0 )
      - 则为 :信天翁          
    

三、实验内容

1. 实验要求

  1. 实现可以输入任何的事实,并基于原有的规则和输入的事实进行推理

2.遍历-数组规则

  1. 由点击传入 two1 数组 的值,为 three数组 赋值

            /*
            遍历所有的规则,得到最好结果
            */
    
            for (let i = 0;i<cond.length;i++){
              // 有毛 || 有奶 ---> 哺乳animal
              if (cond[i] == 1) this.three[0]=12
              if (cond[i] == 2) this.three[0]=12
              // 有羽毛 || (会飞&会下蛋) --->  鸟
              if (cond[i] == 3) this.three[1]=13
    
              if (cond[i] == 4) {
                this.three[1] = 1
                for (let n=0;n<cond.length;n++){
                  if (cond[n] == 5){
                    this.three[1]=13
                  }
                }
              }
              if (cond[i] == 5) {
                this.three[1] = 1
                for (let n=0;n<cond.length;n++){
                  if (cond[n] == 4){
                    this.three[1]=13
                  }
                }
              }
    
              // 吃肉 || (犬齿 & 有爪 & 盯前方) ----> 食肉animal
              if (cond[i] == 6) this.three[2]=14
    
              if (cond[i] == 7) {
                this.three[2] = 4
                for (let n = 0; n < cond.length; n++) {
                  if (cond[n] == 8) {
                    this.three[2] = 4
                    for (let m = 0; m < cond.length; m++) {
                      if (cond[m] == 9) {
                        this.three[2] = 14
                        console.log("循环次数:")
                        break
                      }
                    }
                    break
                  }else if (cond[n] == 9){
                    this.three[2] = 4
                    for (let m = 0; m < cond.length; m++) {
                      if (cond[m] == 8) {
                        this.three[2] = 14
                        break
                      }
                    }
                    break
                  }
                }
              }
              if (cond[i] == 8) {
                this.three[2] = 4
                for (let n = 0; n < cond.length; n++) {
                  if (cond[n] == 7) {
                    this.three[2] = 4
                    for (let m = 0; m < cond.length; m++) {
                      if (cond[m] == 9) {
                        this.three[2] = 14
                        break
                      }
                    }
                    break
                  } else if (cond[n] == 9) {
                    this.three[2] = 4
                    for (let m = 0; m < cond.length; m++) {
                      if (cond[m] == 7) {
                        this.three[2] = 14
                        break
                      }
                    }
                    break
                  }
    
                }
              }
    
              if (cond[i] == 9) {
                this.three[2] = 4
                for (let n = 0; n < cond.length; n++) {
                  if (cond[n] == 7) {
                    this.three[2] = 4
                    for (let m = 0; m < cond.length; m++) {
                      if (cond[m] == 8) {
                        this.three[2] = 14
                        break
                      }
                    }
                    break
                  } else if (cond[n] == 8) {
                    this.three[2] = 4
                    for (let m = 0; m < cond.length; m++) {
                      if (cond[m] == 7) {
                        this.three[2] = 14
                        break
                      }
                    }
                    break
                  }
    
                }
              }
    
              // (哺乳 & 有蹄)|| (哺乳 & 反刍)  ---> 有蹄类动物
              if (this.three[0] == 12 && cond[i] == 10) this.three[3]=15
              if (this.three[0] == 12 && cond[i] == 11) this.three[3]=15
    
              // 以下是,判断是什么动物
              // 黄褐色
              if (cond[i] == 16) this.three[4]=16
              // 暗斑色
              if (cond[i] == 17) this.three[5]=17
              // 黑色条纹
              if (cond[i] == 18) this.three[6]=18
              // 长脖
              if (cond[i] == 19) this.three[7]=19
              // 长腿
              if (cond[i] == 20) this.three[8]=20
              // 不会飞
              if (cond[i] == 21) this.three[9]=21
              // 黑白两色
              if (cond[i] == 22) this.three[10]=22
              // 会游泳
              if (cond[i] == 23) this.three[11]=23
              // 善飞
              if (cond[i] == 24) this.three[12]=24
            }
    
    

四、实验过程

1.源代码

<template>
  <div>
    <h2>以下是:版本2.0</h2>
    <!-- 按钮区域 -->
    <div class="change">
      <button @click="onclick1(1)">有毛</button>
      <button @click="onclick1(2)">有奶</button>
      <button @click="onclick1(3)">有羽毛</button>
      <button @click="onclick1(4)">会飞</button>
      <button @click="onclick1(5)">会下蛋</button>
      <button @click="onclick1(6)">吃肉</button>
      <br/>
      <button @click="onclick1(7)">犬齿</button>
      <button @click="onclick1(8)">有爪</button>
      <button @click="onclick1(9)">盯前方</button>
      <button @click="onclick1(10)">有蹄</button>
      <button @click="onclick1(11)">反刍</button>
      <button @click="onclick1(16)">黄褐色</button>
      <button @click="onclick1(17)">暗斑色</button>
      <br/>
      <button @click="onclick1(18)">黑色条纹</button>
      <button @click="onclick1(19)">长脖子</button>
      <button @click="onclick1(20)">长腿</button>
      <button @click="onclick1(21)">不会飞</button>
      <button @click="onclick1(22)">黑白</button>
      <button @click="onclick1(23)">会游泳</button>
      <button @click="onclick1(24)">善飞</button>
    </div>
    <br>
    <br>
    <div>
      <!--确定按钮-->
      <button @click="ruleAnimal()">确定</button>
      <button @click="clean()">清空</button>
    </div>
    <br>
    <br>
    <!-- 2.0结果输出区 -->
    <div>
      <h4>{{this.overChoose}}</h4>
      <h3>{{this.animalResult1}}</h3>
      <h5>{{this.three}}</h5>
    </div>
  </div>

</template>

<script>

  import \'@/assets/less/TableExpand.less\'
  import PictModal from \'./modules/PictModal\'
  export default {
    name: "PictList",
    components: {
      PictModal
    },
    data () {
      return {
        /*版本2.0*/
        two1: [],
        three: [],
        animalResult1:\'\',
        overChoose:\'\'
      }
    },
    created() {
      for (let i = 0;i<13;i++){
        this.three.push(0)
      }
    },
    methods: {       
      /*
      * 版本2.0
      * */
      onclick1(e){
        /* 将点击按钮后,将对应按钮的数字,push 到 two【】数组里*/
        this.two1.push(e)
      },
      /*清空three数组*/
      clean(){
        this.three = []
        this.two1 = []
        for (let i = 0;i<13;i++){
          this.three.push(0)
        }
      },
      ruleAnimal(){
        console.log("输出已经选择的条件编码:"+this.two1)
        for (let i = 0;i<this.two1.length;i++){
          if (this.two1[i] == 1){
            console.log("有毛")
          }else if (this.two1[i] == 2){
            console.log("有奶")
          }else if (this.two1[i] == 3){
            console.log("有羽毛")
          }else if (this.two1[i] == 4){
            console.log("会飞")
          }else if (this.two1[i] == 5){
            console.log("会下蛋")
          }else if (this.two1[i] == 6){
            console.log("吃肉")
          }else if (this.two1[i] == 7){
            console.log("犬齿")
          }else if (this.two1[i] == 8){
            console.log("有爪")
          }else if (this.two1[i] == 9){
            console.log("盯前方")
          }else if (this.two1[i] == 10){
            console.log("有蹄")
          }else if (this.two1[i] == 11){
            console.log("反刍")
          }else if (this.two1[i] == 16){
            console.log("黄褐色")
          }else if (this.two1[i] == 17){
            console.log("暗斑色")
          }else if (this.two1[i] == 18){
            console.log("黑色条纹")
          }else if (this.two1[i] == 19){
            console.log("长脖子")
          }else if (this.two1[i] == 20){
            console.log("长腿")
          }else if (this.two1[i] == 21){
            console.log("不会飞")
          }else if (this.two1[i] == 22){
            console.log("黑白")
          }else if (this.two1[i] == 23){
            console.log("会游泳")
          }else if (this.two1[i] == 24){
            console.log("善飞")
          }

        }
        this.overChoose = this.two1
        //定义一个数组变量condition:存储 this.two1
        let cond = this.two1

        /*
        遍历所有的规则,
        得到最好结果
        */

        for (let i = 0;i<cond.length;i++){
          // 有毛 || 有奶 ---> 哺乳animal
          if (cond[i] == 1) this.three[0]=12
          if (cond[i] == 2) this.three[0]=12
          // 有羽毛 || (会飞&会下蛋) --->  鸟
          if (cond[i] == 3) this.three[1]=13

          if (cond[i] == 4) {
            this.three[1] = 1
            for (let n=0;n<cond.length;n++){
              if (cond[n] == 5){
                this.three[1]=13
              }
            }
          }
          if (cond[i] == 5) {
            this.three[1] = 1
            for (let n=0;n<cond.length;n++){
              if (cond[n] == 4){
                this.three[1]=13
              }
            }
          }

          // 吃肉 || (犬齿 & 有爪 & 盯前方) ----> 食肉animal
          if (cond[i] == 6) this.three[2]=14

          if (cond[i] == 7) {
            this.three[2] = 4
            for (let n = 0; n < cond.length; n++) {
              if (cond[n] == 8) {
                this.three[2] = 4
                for (let m = 0; m < cond.length; m++) {
                  if (cond[m] == 9) {
                    this.three[2] = 14
                    console.log("循环次数:")
                    break
                  }
                }
                break
              }else if (cond[n] == 9){
                this.three[2] = 4
                for (let m = 0; m < cond.length; m++) {
                  if (cond[m] == 8) {
                    this.three[2] = 14
                    break
                  }
                }
                break
              }
            }
          }
          if (cond[i] == 8) {
            this.three[2] = 4
            for (let n = 0; n < cond.length; n++) {
              if (cond[n] == 7) {
                this.three[2] = 4
                for (let m = 0; m < cond.length; m++) {
                  if (cond[m] == 9) {
                    this.three[2] = 14
                    break
                  }
                }
                break
              } else if (cond[n] == 9) {
                this.three[2] = 4
                for (let m = 0; m < cond.length; m++) {
                  if (cond[m] == 7) {
                    this.three[2] = 14
                    break
                  }
                }
                break
              }

            }
          }

          if (cond[i] == 9) {
            this.three[2] = 4
            for (let n = 0; n < cond.length; n++) {
              if (cond[n] == 7) {
                this.three[2] = 4
                for (let m = 0; m < cond.length; m++) {
                  if (cond[m] == 8) {
                    this.three[2] = 14
                    break
                  }
                }
                break
              } else if (cond[n] == 8) {
                this.three[2] = 4
                for (let m = 0; m < cond.length; m++) {
                  if (cond[m] == 7) {
                    this.three[2] = 14
                    break
                  }
                }
                break
              }

            }
          }

          // (哺乳 & 有蹄)|| (哺乳 & 反刍)  ---> 有蹄类动物
          if (this.three[0] == 12 && cond[i] == 10) this.three[3]=15
          if (this.three[0] == 12 && cond[i] == 11) this.three[3]=15

          // 以下是,判断是什么动物
          // 黄褐色
          if (cond[i] == 16) this.three[4]=16
          // 暗斑色
          if (cond[i] == 17) this.three[5]=17
          // 黑色条纹
          if (cond[i] == 18) this.three[6]=18
          // 长脖
          if (cond[i] == 19) this.three[7]=19
          // 长腿
          if (cond[i] == 20) this.three[8]=20
          // 不会飞
          if (cond[i] == 21) this.three[9]=21
          // 黑白两色
          if (cond[i] == 22) this.three[10]=22
          // 会游泳
          if (cond[i] == 23) this.three[11]=23
          // 善飞
          if (cond[i] == 24) this.three[12]=24
        }


        /* 进行动物规则 判断*/
        // 哺乳 & 食肉 & 黄褐色 & 暗斑色 --> 金钱豹

        if (this.three[0] == 12 && this.three[2] == 14 && this.three[4] == 16 && this.three[5] == 17 &&
          this.three[1] == 0 && this.three[3] == 0 && this.three[7] == 0 &&
          this.three[8] == 0 && this.three[6] == 0 && this.three[9] == 0 &&
          this.three[11] == 0 && this.three[12] == 0 && this.three[10] == 0){
          this.animalResult1 = "金钱豹"
          console.log("这里是金钱豹:")
        }else if (this.three[0] == 12 && this.three[2] == 14 && this.three[4] == 16 && this.three[6] == 18 &&
          this.three[1] == 0 && this.three[3] == 0 && this.three[7] == 0 &&
          this.three[8] == 0 && this.three[5] == 0 && this.three[9] == 0 &&
          this.three[11] == 0 && this.three[12] == 0 && this.three[10] == 0){
          this.animalResult1 = "虎"
          console.log("这里是虎:")
        }else if (this.three[3] == 15 && this.three[7] == 19 && this.three[8] == 20 && this.three[5] == 17 && this.three[0] == 12 &&
          this.three[2] == 0 && this.three[1] == 0 && this.three[4] == 0 &&
          this.three[9] == 0 && this.three[6] == 0 && this.three[11] == 0 &&
          this.three[12] == 0 && this.three[10] == 0){
          this.animalResult1 = "长颈鹿"
          console.log("这里是长颈鹿:")
        }else if (this.three[3] == 15 && this.three[6] == 18 && this.three[0] == 12 &&
          this.three[2] == 0 && this.three[1] == 0 &&
          this.three[4] == 0 && this.three[5] == 0 && this.three[12] == 0 &&
          this.three[7] == 0 && this.three[8] == 0 && this.three[9] == 0 &&
          this.three[10] == 0 && this.three[11] == 0 ){
          this.animalResult1 = "斑马"
          console.log("这里是斑马:")
        }else if (this.three[1] == 13 && this.three[7] == 19 && this.three[8] == 20 && this.three[9] == 21 && this.three[10] == 22 &&
          this.three[2] == 0 && this.three[3] == 0 && this.three[4] == 0 &&
          this.three[5] == 0 && this.three[6] == 0 && this.three[0] == 0 &&
          this.three[11] == 0 && this.three[12] == 0
        ){
          this.animalResult1 = "鸵鸟"
          console.log("这里是鸵鸟:")
        }else if (this.three[1] == 13 && this.three[11] == 23 && this.three[9] == 21 && this.three[10] ==22 &&
          this.three[2] == 0 && this.three[3] == 0 && this.three[4] == 0 &&
          this.three[5] == 0 && this.three[6] == 0 && this.three[7] == 0 &&
          this.three[8] == 0 && this.three[12] == 0 &&
          this.three[0] == 0 ){
          this.animalResult1 = "企鹅"
          console.log("这里是企鹅:")
        }else if (this.three[1] == 13 && this.three[12] == 24 &&
          this.three[0] == 0 && this.three[2] == 0 && this.three[3] == 0 &&
          this.three[4] == 0 && this.three[5] == 0 && this.three[6] == 0 &&
          this.three[7] == 0 && this.three[8] == 0 && this.three[9] == 0 &&
          this.three[10] == 0 && this.three[11] == 0 ){
          this.animalResult1 = "信天翁"
          console.log("这里是信天翁:")
        }else if (this.three[1] == 0 && this.three[11] == 0 && this.three[9] == 0 && this.three[10] ==0 &&
          this.three[2] == 0 && this.three[3] == 0 && this.three[4] == 0 &&
          this.three[5] == 0 && this.three[6] == 0 && this.three[7] == 0 &&
          this.three[8] == 0 && this.three[12] == 0 &&
          this.three[0] == 12 ){
          this.animalResult1 = "哺乳动物"
          console.log("这里是哺乳动物:")
        }else if (this.three[1] == 13 && this.three[11] == 0 && this.three[9] == 0 && this.three[10] ==0 &&
          this.three[2] == 0 && this.three[3] == 0 && this.three[4] == 0 &&
          this.three[5] == 0 && this.three[6] == 0 && this.three[7] == 0 &&
          this.three[8] == 0 && this.three[12] == 0 &&
          this.three[0] == 0 ){
          this.animalResult1 = "鸟类动物"
          console.log("这里是鸟类动物:")
        }else if (this.three[1] == 0 && this.three[11] == 0 && this.three[9] == 0 && this.three[10] ==0 &&
          this.three[2] == 14 && this.three[3] == 0 && this.three[4] == 0 &&
          this.three[5] == 0 && this.three[6] == 0 && this.three[7] == 0 &&
          this.three[8] == 0 && this.three[12] == 0 &&
          this.three[0] == 0 ){
          this.animalResult1 = "食肉动物"
          console.log("这里是食肉动物:")
        }else if (this.three[1] == 0 && this.three[11] == 0 && this.three[9] == 0 && this.three[10] ==0 &&
          this.three[2] == 0 && this.three[3] == 15 && this.three[4] == 0 &&
          this.three[5] == 0 && this.three[6] == 0 && this.three[7] == 0 &&
          this.three[8] == 0 && this.three[12] == 0 &&
          this.three[0] == 12 ){
          this.animalResult1 = "有蹄动物"
          console.log("这里是有蹄动物:")
        }else {
          this.animalResult1 = "无法推理出何种动物"
        }
      },
    }
  }
</script>
<style scoped>
  @import \'~@assets/less/common.less\';
</style>

五、实验结果

1.规则R1:

有毛(1)  --> 哺乳动物(12)
image-20210320124136563

2.规则R2:

 有奶(2)  --> 哺乳动物(12)
image-20210320124422238

3. 规则R3:

R3:  有羽毛(3)   --> 鸟类(13)
image-20210320124532999

4.规则R4:

R4:  会飞(4) & 会下蛋  --> 鸟类(13)
image-20210320124639418

5.规则R5:

R5:  吃肉(6)  --> 食肉动物(14)
image-20210320124736110

6.规则R6:

R6:  犬齿(7) & 有爪(8) & 盯前方(9)  --> 食肉动物(14)
image-20210320124833112

7.规则R7:

R7:  哺乳动物(12) & 有蹄(10)  -->  有蹄类动物(15)
image-20210320124945294

8.规则R8:

R8:  哺乳动物(12) & 反刍(11)  -->  有蹄类动物(15)
image-20210320125058322

9.规则R9:

R9:  哺乳动物(12) & 食肉动物(14) & 黄褐色(16) & 暗斑点(17)  --> 金钱豹(a)
image-20210320125314302

10.规则R10:

R10: 哺乳动物(12) & 食肉动物(14) & 黄褐色(16) & 黑色条纹(18)  --> 虎(b)
image-20210320125440349

11.规则R11:

R11: 有蹄类动物(15) & 长脖(19) & 长腿(20) & 暗斑色(17)  --> 长颈鹿(c)
image-20210320125545182

12.规则R12:

R12: 有蹄类动物(15) & 黑色条纹(18) &  -->  斑马(d)
image-20210320125643916

13.规则R13:

R13: 鸟类(13) & 长脖(19) & 长腿(20) & 不会飞(21) & 黑白两色(22)  -->  鸵鸟(e)
image-20210320125744453

14.规则R14:

R14: 鸟类(13) & 会游泳(23) & 不会飞(21) & 黑白二色(22)  -->  企鹅(f)
image-20210320125844886

15.规则R15:

R15: 鸟类(13) & 善飞(24)  -->  信天翁(g)
image-20210320125936691