生活随笔
收集整理的這篇文章主要介紹了
人工智能—产生式系统(专家系统)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
產生式系統
概念
產生式系統(production system)是指認知心理學程序表征系統的一種。為解決某一問題或完成某一作業而按一定層次聯結組成的認知規則系統。
原理
產生式系統由規則庫、推理機、綜合數據庫,控制程序四部分組成。其中,規則庫里面存儲大量的知識,綜合數據庫則是儲存事實,綜合數據庫通過推理機根據規則庫里面的知識,由控制程序的控制下完成推理,若是推出中間結果,則把中間結果放到綜合數據庫中,繼續重新推理,直到推理出最終結果或推理失敗,程序結束。
例子
下面我們就以交通工具識別系統為例,用java編寫。
規則庫:
R1:it_is(車),postive(體型小),postive(有輪子);
R2:it_is(飛機),postive(體型大),postive(流線型);
R3:it_is(飛機),postive(天上飛的);
R4:it_is(船),postive(水上游的);
R5:it_is(車),postive(地上跑的);
R6:trto_is(輪船),it_is(船),postive(體型大),postive(冒黑煙);
R7:trto_is(航天飛機),it_is(飛機),postive(有機翼);
R8:trto_is(直升機),it_is(飛機),postive(有螺旋槳);
R9:trto_is(汽車),it_is(車),postive(四個輪);
R10:trto_is(兩輪車),it_is(車),postive(兩個輪);
代碼:
MainGui.java
該類為主界面類,調用GuiFrame類(自定義的類,代碼在后面)顯示主窗口。
package chanshengsi01
;import java
.awt
.Container
;
import java
.awt
.FlowLayout
;
import java
.awt
.event
.ActionEvent
;
import java
.awt
.event
.ActionListener
;
import java
.util
.ArrayList
;import javax
.swing
.JButton
;
import javax
.swing
.JFrame
;
import javax
.swing
.JLabel
;
import javax
.swing
.JTextField
;public class MainGUI {public static void main(String
[] args
) {GuiFrame g
=new GuiFrame("交通工具識別");}}
GuiFrame.java
該類主要實現主界面的布局,以及兩個按鈕的監聽器。點擊匹配按鈕會調用Brain(自定義的類,代碼在后面)類的identify函數,來根據輸入到綜合數據庫的事實去匹配結果。點擊查看規則按鈕會調用GuiRules(自定義的類,代碼在后面)顯示出規則庫里面的知識。
package chanshengsi01
;import java
.awt
.Container
;
import java
.awt
.FlowLayout
;
import java
.awt
.GridLayout
;
import java
.awt
.event
.ActionEvent
;
import java
.awt
.event
.ActionListener
;
import java
.util
.ArrayList
;import javax
.swing
.JButton
;
import javax
.swing
.JFrame
;
import javax
.swing
.JLabel
;
import javax
.swing
.JTextField
;public class GuiFrame extends JFrame {JButton button
;JButton button1
;JLabel label
;JLabel label_result
;JTextField textField1
;JTextField textField2
;JTextField textField3
;JTextField textField4
;Container contentPane
;public GuiFrame(String title
) {super(title
);this.setSize(330, 350);this.setLocation(350, 150);this.setDefaultLookAndFeelDecorated(true);this.setDefaultCloseOperation(JFrame
.EXIT_ON_CLOSE
);button
= new JButton("匹配");button
.setBounds(60, 168, 75, 30);button1
= new JButton("查看規則");button1
.setBounds(150, 168, 100, 30);label
= new JLabel("設置事實:");label
.setBounds(120, 2, 90, 50);label_result
= new JLabel("最終結果是:");label_result
.setBounds(60, 210, 200, 20);textField1
= new JTextField(16);textField1
.setBounds(55, 40, 200, 23);textField2
= new JTextField(16);textField2
.setBounds(55, 72, 200, 23);textField3
= new JTextField(16);textField3
.setBounds(55, 104, 200, 23);textField4
= new JTextField(16);textField4
.setBounds(55, 136, 200, 23);contentPane
= this.getContentPane();contentPane
.setLayout(null
);contentPane
.add(label
);contentPane
.add(textField1
);contentPane
.add(textField2
);contentPane
.add(textField3
);contentPane
.add(textField4
);contentPane
.add(button
);contentPane
.add(button1
);contentPane
.add(label_result
);button
.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e
) {ArrayList al
= new ArrayList();String str1
= textField1
.getText();if (!str1
.equals("")) {al
.add(str1
);}String str2
= textField2
.getText();if (!str2
.equals("")) {al
.add(str2
);}String str3
= textField3
.getText();if (!str3
.equals("")) {al
.add(str3
);}String str4
= textField4
.getText();if (!str4
.equals("")) {al
.add(str4
);}Brain brain
= new Brain();String result
= brain
.identify(al
);label_result
.setText("最終結果是:" + result
);setVisible(true);}});button1
.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e
) {GuiRules gt
=new GuiRules();}});this.setVisible(true);}
}
GuiRules.java
主要用于展示規則庫的知識。
package chanshengsi01
;import java
.awt
.Container
;
import java
.util
.ArrayList
;import javax
.swing
.JFrame
;
import javax
.swing
.JLabel
;public class GuiRules extends JFrame {JLabel l
;String s
;Rules rule
;Rules
[] r
;Container contentPane
;GuiRules() {super("查看規則");this.setSize(400, 400);this.setLocation(800, 150);this.setDefaultLookAndFeelDecorated(true);contentPane
= this.getContentPane();contentPane
.setLayout(null
);rule
= new Rules();r
= new Rules[10];r
= rule
.RuleInit();int x
=0;for (int i
= 0; i
< 10; i
++) {ArrayList a
= r
[i
].getCondition();s
= "R"+i
+": "+(String
) a
.get(0);for (int j
= 1; j
< a
.size(); j
++) {s
= s
+ "+" + a
.get(j
);}s
=s
+"--->"+r
[i
].result
;l
=new JLabel(s
);l
.setBounds(20, x
, 300, 100);x
=x
+20;contentPane
.add(l
);}this.setVisible(true);}
}
Rules.java
規則庫
package chanshengsi01
;
import java
.util
.ArrayList
;public class Rules {ArrayList condition
;String result
;int is_temp
;int is_use
= 0;Rules() {condition
= new ArrayList();}public int getIs_use() {return is_use
;}public void setIs_use(int is_use
) {this.is_use
= is_use
;}public int getIs_temp() {return is_temp
;}public void setIs_temp(int is_temp
) {this.is_temp
= is_temp
;}public ArrayList
getCondition() {return condition
;}public void setCondition(ArrayList condition
) {this.condition
= (ArrayList
) condition
.clone();}public String
getResult() {return result
;}public void setResult(String result
) {this.result
= result
;}public static Rules
[] RuleInit() {Rules
[] r
= new Rules[10];Rules rule0
= new Rules();r
[0] = rule0
;ArrayList c0
= new ArrayList();c0
.add("體型小");c0
.add("有輪子");r
[0].setCondition(c0
);r
[0].setResult("車");r
[0].setIs_temp(1);Rules rule1
= new Rules();r
[1] = rule1
;ArrayList c1
= new ArrayList();c1
.add("體型大");c1
.add("流線型");r
[1].setCondition(c1
);r
[1].setResult("飛機");r
[1].setIs_temp(1);Rules rule2
= new Rules();r
[2] = rule2
;ArrayList c2
= new ArrayList();c2
.add("天上飛的");r
[2].setCondition(c2
);r
[2].setResult("飛機");r
[2].setIs_temp(1);Rules rule3
= new Rules();r
[3] = rule3
;ArrayList c3
= new ArrayList();c3
.add("水上游的");r
[3].setCondition(c3
);r
[3].setResult("船");r
[3].setIs_temp(1);Rules rule4
= new Rules();r
[4] = rule4
;ArrayList c4
= new ArrayList();c4
.add("地上跑的");r
[4].setCondition(c4
);r
[4].setResult("車");r
[4].setIs_temp(1);Rules rule5
= new Rules();r
[5] = rule5
;ArrayList c5
= new ArrayList();c5
.add("體型大");c5
.add("冒黑煙");c5
.add("船");r
[5].setCondition(c5
);r
[5].setResult("輪船");r
[5].setIs_temp(0);Rules rule6
= new Rules();r
[6] = rule6
;ArrayList c6
= new ArrayList();c6
.add("飛機");c6
.add("有機翼");r
[6].setCondition(c6
);r
[6].setResult("航天飛機");r
[6].setIs_temp(0);Rules rule7
= new Rules();r
[7] = rule7
;ArrayList c7
= new ArrayList();c7
.add("飛機");c7
.add("有螺旋槳");r
[7].setCondition(c7
);r
[7].setResult("直升機");r
[7].setIs_temp(0);Rules rule8
= new Rules();r
[8] = rule8
;ArrayList c8
= new ArrayList();c8
.add("車");c8
.add("四個輪");r
[8].setCondition(c8
);r
[8].setResult("汽車");r
[8].setIs_temp(0);Rules rule9
= new Rules();r
[9] = rule9
;ArrayList c9
= new ArrayList();c9
.add("車");c9
.add("兩個輪");r
[9].setCondition(c9
);r
[9].setResult("兩輪車");r
[9].setIs_temp(0);return r
;}
}
Postive.java
綜合數據庫,用于接收用戶輸入的事實或者推理機產生的中間結果。
package chanshengsi01
;import java
.util
.ArrayList
;public class Postive {public ArrayList s1
;Postive() {s1
= new ArrayList();}public ArrayList
getS1() {return s1
;}public void setS1(ArrayList s1
) {this.s1
= (ArrayList
) s1
.clone();}public void delete() {this.s1
.clear();}
}
Brain.java
控制程序,控制推理機進行推理。
package chanshengsi01
;
import java
.util
.ArrayList
;public class Brain {GuiTemp gt
;public String
identify(ArrayList al
) {String result
= null
;Postive p
= new Postive();p
.setS1(al
);Check ch
= new Check();Rules rule
= new Rules();Rules
[] r
= new Rules[10];r
= rule
.RuleInit();int i
;for (i
= 0; i
< r
.length
; i
++) {if (r
[i
].getIs_use() == 0 && ch
.checkRule(r
[i
].getCondition(), p
.getS1()) == 1) {if (r
[i
].getIs_temp() == 0) {result
= r
[i
].getResult();break;} else {r
[i
].setIs_use(1);gt
= new GuiTemp(r
[i
]);p
.s1
.add(r
[i
].getResult());i
= 0;}}}if (i
>= r
.length
) {result
= "不認識";}return result
;}
}
Check.java
用于推理匹配,檢查規則庫中是否有符合的知識。
package chanshengsi01
;import java
.util
.ArrayList
;public class Check {public int checkRule(ArrayList a1
, ArrayList a2
) {int length1
= a1
.size();int length2
= a2
.size();for (int i
= 0; i
< length1
; i
++) {String s1
= (String
) a1
.get(i
);int j
;for (j
= 0; j
< length2
; j
++) {String s2
= (String
) a2
.get(j
);if (s1
.equals(s2
)) {break;}}if (j
>= length2
) {return 0;}}return 1;}
}
GuiTemp.java
用于顯示中間結果,如果推理出中間結果,則顯示出來。
package chanshengsi01
;import java
.awt
.Container
;
import java
.util
.ArrayList
;import javax
.swing
.JFrame
;
import javax
.swing
.JLabel
;public class GuiTemp extends JFrame {JLabel l1
;Container contentPane
;String s1
="";public GuiTemp(Rules r
) {super("推理出中間結果");this.setSize(200, 200);this.setLocation(800, 150);this.setDefaultLookAndFeelDecorated(true);contentPane
= this.getContentPane();contentPane
.setLayout(null
);ArrayList a
=r
.getCondition();s1
=(String
) a
.get(0);for(int i
=1;i
<a
.size();i
++) {s1
=s1
+"+"+a
.get(i
);}s1
=s1
+"--->"+r
.result
;l1
=new JLabel(s1
);l1
.setBounds(20, 20, 200, 100);contentPane
.add(l1
);this.setVisible(true);}
}
運行結果
主界面的截圖
查看規則后截圖:
匹配后截圖
總結
以上是生活随笔為你收集整理的人工智能—产生式系统(专家系统)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。