专家系统实验

动物识别系统by prolog

一、实验描述:

本实验仿照书中例题,用prolog语言实现了一个简单的动物识别专家系统。该系统可以识别老虎、金钱豹、斑马、长颈鹿、鸵鸟、企鹅、信天翁这7种动物及根据一些特征识别部分物种。

二、实验原理与内容:

知识库
在本系统当中,知识库中的知识用产生式规则来表示,共有如下15条规则。
R1 IF该动物有奶THEN该动物是哺乳动物
R2 IF该动物有毛发THEN该动物是哺乳动物
R3 IF该动物有羽毛THEN该动物是鸟
R4 IF该动物会飞AND会下蛋THEN该动物是鸟
R5 IF该动物有爪AND有犬齿AND眼盯前方THEN该动物是食肉动物
R6 IF该动物吃肉THEN该动物是肉食动物
R7 IF该动物是哺乳动物AND有蹄THEN该动物是有蹄类动物
R8 IF该动物是哺乳动物AND嚼反刍THEN该动物是有蹄类动物
R9 IF 该动物是哺乳动物 AND 该动物是食肉动物 AND 是黄褐色 AND身上有
黑色条纹 THEN 该动物是虎
R10 IF该动物是哺乳动物 AND 该动物是食肉动物AND是黄褐色AND身上有暗斑点THEN该动物是金钱豹
R11 IF该动物是有蹄类动物AND有长脖子AND有长腿AND身上有暗斑点THEN该动物是长颈鹿
R12 IF该动物是有蹄类动物AND身上有黑色条纹THEN该动物是斑马
R13 IF该动物是鸟AND有不会飞AND有长腿AND长脖子AND是黑白二色THEN该动物是鸵鸟
R14 IF该动物是鸟AND不会飞AND会游泳AND是黑白二色THEN该动物是企鹅
R15 IF该动物是鸟AND善飞THEN该动物是信天翁

三、实验代码与截图:

/* animal.pro
  animal identification game.  

    start with ?- go.     */

go :- hypothesize(Animal),
      write('I guess that the animal is: '),
      write(Animal),
      nl,
      undo.

/* hypotheses to be tested */
hypothesize(cheetah)   :- cheetah, !.    // 猎豹
hypothesize(tiger)     :- tiger, !.
hypothesize(giraffe)   :- giraffe, !.
hypothesize(zebra)     :- zebra, !.
hypothesize(ostrich)   :- ostrich, !.  //鸵鸟
hypothesize(penguin)   :- penguin, !.
hypothesize(albatross) :- albatross, !.  //信天翁
hypothesize(unknown).             /* no diagnosis */

/* animal identification rules */
cheetah :- mammal, 
           carnivore, 
           verify(has_tawny_color),
           verify(has_dark_spots).
tiger :- mammal,  
         carnivore,               // carnivore--肉食动物
         verify(has_tawny_color), 
         verify(has_black_stripes).
giraffe :- ungulate,        //giraffe--长颈鹿
           verify(has_long_neck), 
           verify(has_long_legs).   // zebra-- 斑马
zebra :- ungulate,         // ungulate--有蹄类
         verify(has_black_stripes).
ostrich :- bird,  
           verify(does_not_fly), 
           verify(has_long_neck).
penguin :- bird, 
           verify(does_not_fly), 
           verify(swims),
           verify(is_black_and_white).
albatross :- bird,
             verify(appears_in_story_Ancient_Mariner),
             verify(flys_well).

/* classification rules */
mammal    :- verify(has_hair), !.
mammal    :- verify(gives_milk).
bird      :- verify(has_feathers), !.
bird      :- verify(flys), 
             verify(lays_eggs).
carnivore :- verify(eats_meat), !.
carnivore :- verify(has_pointed_teeth), 
             verify(has_claws),
             verify(has_forward_eyes).
ungulate :- mammal, 
            verify(has_hooves), !.
ungulate :- mammal, 
            verify(chews_cud).

/* how to ask questions */
ask(Question) :-
    write('Does the animal have the following attribute: '),
    write(Question),
    write('? '),
    read(Response),
    nl,
    ( (Response == yes ; Response == y)
      ->assert(yes(Question)) ;
        assert(no(Question)), fail).
:- dynamic yes/1,no/1.

/* How to verify something */
verify(S) :-
   (yes(S) ->true ;
    (no(S)->fail ;
     ask(S))).

/* undo all yes/no assertions */
undo :- retract(yes(_)),fail. 
undo :- retract(no(_)),fail.
undo.

这里写图片描述

有6个error,调试成功后,运行截图如下,运行成功。

这里写图片描述


附录[0]本篇报告下载链接

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章