[AI] animal recognition system based on production rules

1, Experimental purpose

[experiment content]
Develop a production system that can identify seven kinds of animals, such as tigers, leopards, zebras, giraffes, penguins, ostriches and albatrosses.
[rule base]
IF hairy THEN mammal
IF milk THEN mammals
IF feathered THEN bird
IF can fly AND can lay eggs THEN bird
IF eating meat THEN carnivores
IF has canine teeth AND claws AND eyes staring at the front THEN predator
IF mammals AND ungulates
IF mammals AND ruminants THEN ungulates
IF mammal AND predator AND tawny AND dark spotted THEN Leopard
IF mammal AND predator AND tawny AND black striped THEN tiger
IF ungulates AND long neck AND long leg AND dark spot THEN Giraffe
IF ungulates AND black striped THEN zebra
IF bird AND long neck AND long legs AND can't fly AND black AND white THEN ostrich
IF birds AND can swim AND can't fly AND black AND white THEN penguins
IF bird AND good at flying THEN albatross

2, Experimental principle

Production system is used to describe several different systems based on a basic concept. This basic concept is the concept of production rules or production conditions and operation objects. In production system, knowledge is divided into two parts: using facts to represent static knowledge; Production rules are used to represent reasoning process and behavior.

3, Algorithm design

1. Establish knowledge base. Each precondition, intermediate conclusion and final conclusion in the rules can be converted into a corresponding unique number, such as: "1 hairy, 2 milk... 23 carnivorous... 31 albatross", and stored in a dictionary. Where the key is 1, the value is hairy, and so on.
2. Establish rule base. The production rules can be converted according to the corresponding numerical sequence number of the rule combination conditions or conclusions given in the book, such as 1 → 21, 2 → 21... 20, 22 → 31, etc., and written into the list.
3. Establish a fact base. The fact base in this experiment can be input directly at the beginning of the program. The user can select according to needs and ask the user to input the digital serial number of animal characteristics for recognition. If it is not recognized, you can reselect or exit.
4. Run reasoning.

4, System code design

'''system designed by MoBrook_Susie'''

# Fact base
# fact = [12, 14, 15, 2, 10]
fact = list(map(int, input("Please enter the animal characteristic number:").strip().split(" ")))

# knowledge base
knowledge = {1:"Hairy", 2:"Milk", 3:"Feathered", 4:"Can fly", 5:"Can lay eggs",6:"eat meat",7:"Canine teeth",8:"Clawed", 9:"Keep your eyes on the front", 10:"Hoofed", 11:"Yellowish brown" 12:"Dark spot", 13:"Black stripe", 14:"Long neck", 15:"Long legged", 16:"Can't fly", 17:"Black and white dichroism", 18:"Can swim", 19:"Good at flying",
             20:"mammal", 21:"bird", 22:"Carnivore", 23:"Ungulates", 24:"Ruminant",
             25:"leopard", 26:"tiger", 27:"giraffe", 28:"zebra", 29:"ostrich", 30:"penguin", 31:"Albatross", }

# Rule base
def regulation(fact):
    if 1 in fact and 20 not in fact:  #Make sure that 20 is not in the fact base, otherwise 20 will be added all the time
        fact.append(20)
    if 2 in fact and 20 not in fact:
        fact.append(20)
    if 3 in fact and 21 not in fact:
        fact.append(21)
    if 4 in fact and 5 in fact and 21 not in fact:
        fact.append(21)
    if 6 in fact and 22 not in fact:
        fact.append(22)
    if 7 in fact and 8 in fact and 9 in fact and 22 not in fact:
        fact.append(22)
    if 20 in fact and 10 in fact and 23 not in fact:
        fact.append(23)
    if 20 in fact and 24 in fact and 23 not in fact:
        fact.append(23)
    if 20 in fact and 22 in fact and 11 in fact and 12 in fact and 25 not in fact:
        fact.append(25)
    if 20 in fact and 22 in fact and 11 in fact and 13 in fact and 26 not in fact:
        fact.append(26)
    if 10 in fact and 14 in fact and 15 in fact and 12 in fact and 27 not in fact:
        fact.append(27)
    if 23 in fact and 13 in fact and 28 not in fact:
        fact.append(28)
    if 21 in fact and 14  in fact and 15 in fact and 16 in fact and 17 in fact and 29 not in fact:
        fact.append(29)
    if 21 in fact and 18 in fact and 16 in fact and 17 in fact and 30 not in fact:
        fact.append(30)
    if 21 in fact and 19 in fact and 31 not in fact:
        fact.append(31)

if __name__ == '__main__':
    print("The characteristics of the animal are:"+",".join(map(lambda x:knowledge[x],fact)))
    while(True):
        star_fact = len(fact)
        regulation(fact)
        end_fact = len(fact)
        if star_fact == end_fact or fact[-1]>24:  # If there is no change or specific animals have been obtained (25 ~ 31), stop
            print("This animal is"+knowledge[fact[-1]])
            break

Keywords: Python Algorithm AI

Added by cshinteractive on Wed, 27 Oct 2021 03:52:12 +0300