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.思路分析
-
第一次編寫時:采用的是,條件對應原則,每個條件對應的動物做一個集合,多個條件集合取交集,得到對應動物。方法-錯誤×,未使用該15條規則,推理而得。
如:當選擇 條件不會飛 和 會飛時,集合方式無法推理出結果, - 會飛 && 會下蛋 --> 鳥類 - 鳥類 && 不會飛 && 會游泳 && 黑白二色 --> 企鵝 + 輸入條件: - 會飛 && 會下蛋 && 不會飛 && 會游泳 && 黑白二色 -×-> 企鵝
-
改版,版本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. 實驗要求
- 實現可以輸入任何的事實,並基於原有的規則和輸入的事實進行推理
2.遍歷-數組規則
-
由點擊傳入 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)

2.規則R2:
有奶(2) --> 哺乳動物(12)

3. 規則R3:
R3: 有羽毛(3) --> 鳥類(13)

4.規則R4:
R4: 會飛(4) & 會下蛋 --> 鳥類(13)

5.規則R5:
R5: 吃肉(6) --> 食肉動物(14)

6.規則R6:
R6: 犬齒(7) & 有爪(8) & 盯前方(9) --> 食肉動物(14)

7.規則R7:
R7: 哺乳動物(12) & 有蹄(10) --> 有蹄類動物(15)

8.規則R8:
R8: 哺乳動物(12) & 反芻(11) --> 有蹄類動物(15)

9.規則R9:
R9: 哺乳動物(12) & 食肉動物(14) & 黃褐色(16) & 暗斑點(17) --> 金錢豹(a)

10.規則R10:
R10: 哺乳動物(12) & 食肉動物(14) & 黃褐色(16) & 黑色條紋(18) --> 虎(b)

11.規則R11:
R11: 有蹄類動物(15) & 長脖(19) & 長腿(20) & 暗斑色(17) --> 長頸鹿(c)

12.規則R12:
R12: 有蹄類動物(15) & 黑色條紋(18) & --> 斑馬(d)

13.規則R13:
R13: 鳥類(13) & 長脖(19) & 長腿(20) & 不會飛(21) & 黑白兩色(22) --> 鴕鳥(e)

14.規則R14:
R14: 鳥類(13) & 會游泳(23) & 不會飛(21) & 黑白二色(22) --> 企鵝(f)

15.規則R15:
R15: 鳥類(13) & 善飛(24) --> 信天翁(g)
