Production system

Production system

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

1. Build their own production system (including rule base and fact base), and then conduct reasoning, that is, they can input any facts themselves and conduct reasoning based on the original rules and input facts.

2. Build an animal recognition system, which can judge which animal it is or give corresponding answers according to the input animal characteristics.

3. Algorithm design

① First, establish a fact base

The fact base is directly input at the beginning of the program, and the user selects according to needs, that is, the user is required to input the number of features first, and then input the characteristics of animals for recognition. If it is not recognized, you can reselect or exit.

The characteristics of animals are as follows:

1 milk, 2 hair, 3 feathers, 4 flying, 5 raw eggs, 6 claws, 7 canine teeth

8 eyes staring at the front 9 eating meat 10 hoofed 11 ruminant food 12 yellowish brown 13 black stripes

14 black spots 15 long legs 16 long neck 17 dark spots 18 white 19 can't fly

20 black and white 21 can swim 22 good at flying 23 not afraid of wind and waves

24 mammals 25 birds 26 carnivores 27 hoofed animals 28 cloven hoofed animals

29 petrel 30 tiger 31 leopard 32 giraffe 33 zebra 34 ostrich 35 Penguin

② Establish static rule base

That is to establish production rules. This algorithm adopts the method of generating intermediate facts to facilitate the establishment and use of rules. In order to facilitate the design, we limit the animals to be identified to 7 species, which requires less production rules. There are 15 rules in this algorithm, as follows:

R1: if an animal has milk, it is a mammal

R2: if an animal has hair, it is a mammal

R3: if an animal has feathers, it is a bird

R4: if an animal can fly and lay eggs, it is a bird

R5: meat eating mammals are carnivores

R6: the mammal with claws and canine teeth in front of the wooden nail is a carnivore

R7: hoofed mammals are hoofed animals

R8: hoofed animals that ruminate food are cloven hoofed animals

R9: tawny carnivores with black stripes are tigers

R10: the yellow brown predator with black spots is the leopard

R11: hoofed animals with long legs and long neck and dark yellowish brown spots are giraffes

R12: hoofed animals with black and white stripes are zebras

R13: a bird that can't fly with long legs and long neck is an ostrich

R14: black and white birds that can't fly and swim are penguins

R15: the bird that is good at flying and not afraid of wind and waves is the petrel

The details are as follows:

R1:  1->24

R2:  2->24

R3:  3->25

R4:  4*5->25

R5:  6*7*8*24->26

R6:  9*24->26

R7:  10*24->27

R8:  11*27->28

R9:  12*13*24->30

R10: 12*14*24->31

R11: 12*15*16*17*27->32

R12: 13*18*27->33

R13: 15*16*19*25->34

R14: 19*20*21*25->35

R15: 22*23*25->29

③ Forward reasoning process

(1) Put the initial fact data into the dynamic database.

(2) Match the target conditions with the facts in the dynamic database. If the target conditions are met, the reasoning is successful and ends.

(3) Use the premise of each rule in the rule base to match the facts in the dynamic database, remove the successfully matched rule premise from the data dynamic base, and remove the successfully matched rule

(4) If the rule set to be used is empty, the operation fails and exits.

(5) Move out the rules in the rule set to be used, and add the conclusion to the dynamic database, or execute its action and turn to step (2)

If there are multiple matching rules, one of them needs to be selected as the use rule. This algorithm selects them in turn according to the order of the rules, and there are no multiple matching rules corresponding to the same set of facts in the rules.

④ Flow chart

⑤ Test results and analysis

The rule base of this system is static and cannot add new rules dynamically. This makes it impossible to change in time when the rules change, but the system can basically meet the needs, give corresponding answers to the input facts and judge what kind of animal it is.

code:

#include<stdio.h>
#define N 23
int main(void)
{
	int i,j,k,a,b,c;
 	int num;
 	int fact[N],temp[N];
 	int flag=1;
 	while(flag==1)
 	{
 		printf(" The characteristics of animals are as follows :\n");
 		printf("1 Have milk 2 have hair 3 have feathers 4 can fly 5 lay eggs \n6 There are claws, 7 canine teeth, 8 eyes staring at the front, 9 eating meat and 10 hooves \n11 Ruminant food 12 yellowish brown 13 black stripes 14 black spots 15 long legs \n16 Long neck 17 dark spots 18 white 19 can't fly 20 black and white \n21 Can swim 22 good at flying 23 not afraid of wind and waves \n");
 		printf(" Please enter the number of characters describing the animal :");
 		scanf("%d",&num);
 		printf(" Please enter the serial number of the characteristic description of this animal ( By serial number, from small to large ):\n");
 		for(i=0;i<num;i++)
 		{
   			scanf("%d",&a);
   			fact[i]=a;
 		}
 //******************************** 
  		for(i=0;i<num;i++)
 		{
   			if(fact[i]==1)
   			{
     			fact[num]=24;
    			num++;
     			printf(" Use rule 1, The newly added fact is :  mammal \n");
     			break;
   			}
 		}
 //********************************  
 		for(i=0;i<num;i++)
 		{
   			if(fact[i]==2)
   			{
     			fact[num]=24;
     			num++;
     			printf(" Use rule 2, The newly added fact is :  mammal \n");
     			break;
   			}
 		}
 //********************************  
 		for(i=0;i<num;i++)
 		{
   			if(fact[i]==3)
   			{
     			fact[num]=25;
     			num++;
     			printf(" Use rule 3, The newly added fact is : bird \n");
     			break;
   			}
 		}
 //********************************  
 		k=0;
 		for(i=0;i<num;i++)
 		{
   			if(fact[i]==4)
   			{
     			temp[k]=fact[i];
     			k++;
     			continue;
   			}
   			if(fact[i]==5)
   			{
     			temp[k]=fact[i];
     			break;
   			}
 		}
 		if(temp[0]==4&&temp[1]==5)
 		{
   			fact[num]=25;
   			num++;
   			printf(" Use rule 4, The newly added fact is : bird \n");
 		}
 //******************************** 
 		k=0;
 		for(i=0;i<num;i++)
 		{
   			if(fact[i]==6)
   			{
     			temp[k]=fact[i];
     			k++;
     			continue;
   			}
   			if(fact[i]==7)
   			{
     			temp[k]=fact[i];
     			k++;
     			continue;
   			}
   			if(fact[i]==8)
   			{
     			temp[k]=fact[i];
     			k++;
     			continue;
   			}
   			if(fact[i]==24)
  	 		{
     			temp[k]=fact[i];
     			break;
   			}
 		}
 		if(temp[0]==6&&temp[1]==7&&temp[2]==8&&temp[3]==24)
 		{
   			fact[num]=26;
   			num++;
   			printf(" Use Rule 5, The newly added fact is : Carnivore \n");
 		}
 //********************************  
 		k=0;
 		for(i=0;i<num;i++)
 		{
   			if(fact[i]==9)
   			{
     			temp[k]=fact[i];
     			k++;
     			continue;
   			}
   			if(fact[i]==24)
   			{
     			temp[k]=fact[i];
     			break;
   			}
 		}
 		if(temp[0]==9&&temp[1]==24)
 		{
   			fact[num]=26;
   			num++;
   			printf(" Use rule 6, The newly added fact is : Carnivore \n");
 		}
 //********************************  
 		k=0;
 		for(i=0;i<num;i++)
 		{
   			if(fact[i]==10)
   			{
     			temp[k]=fact[i];
     			k++;
     			continue;
   			}
   			if(fact[i]==24)
   			{
     			temp[k]=fact[i];
     			break;
   			}
 		}
 		if(temp[0]==10&&temp[1]==24)
 		{
   			fact[num]=27;
   			num++;
   			printf(" Use rule 7, The newly added fact is : Hoofed animal \n");
 		}
 //********************************  
 		k=0;
 		for(i=0;i<num;i++)
 		{
   			if(fact[i]==11)
   			{
     			temp[k]=fact[i];
     			k++;
     			continue;
   			}	
   			if(fact[i]==27)
   			{
     			temp[k]=fact[i];
     			break;
   			}
 		}
 		if(temp[0]==11&&temp[1]==27)
 		{
   			fact[num]=28;
   			num++;
   			printf(" Use rule 8, The newly added fact is : Cloven hoofed animal \n");
 		}
 //********************************  
 		k=0;
 		for(i=0;i<num;i++)
 		{
   			if(fact[i]==12)
   			{
     			temp[k]=fact[i];
     			k++;
     			continue;
   			}
   			if(fact[i]==13)
   			{
     			temp[k]=fact[i];
     			k++;
     			continue;
   			}
   			if(fact[i]==24)
   			{
     			temp[k]=fact[i];
     			break;
   			}
 		}
 		if(temp[0]==12&&temp[1]==13&&temp[2]==24)
 		{
   			fact[num]=30;
   			//
			num++;
   			printf(" Use rule 9, The newly added fact is : tiger \n The animal is a tiger \n");
 		}
 //********************************  
 		k=0;
 		for(i=0;i<num;i++)
 		{
   			if(fact[i]==12)
   			{
     			temp[k]=fact[i];
     			k++;
     			continue;
   			}
   			if(fact[i]==14)
   			{
     			temp[k]=fact[i];
     			k++;
     			continue;
   			}
   			if(fact[i]==24)
   			{
     			temp[k]=fact[i];
     			break;
   			}
 		}
 		if(temp[0]==12&&temp[1]==14&&temp[2]==24)
 		{
   			fact[num]=31;
   			//
			num++;
   			printf(" Use rule 10, The newly added fact is : leopard \n The animal is a leopard \n");
 		}
 //********************************  
 		k=0;
 		for(i=0;i<num;i++)
 		{
   			if(fact[i]==12)
   			{
     			temp[k]=fact[i];
     			k++;
     			continue;
   			}
   			if(fact[i]==15)
   			{
     			temp[k]=fact[i];
     			k++;
     			continue;
   			}
   			if(fact[i]==16)
   			{
     			temp[k]=fact[i];
     			k++;
     			continue;
   			}
   			if(fact[i]==17)
   			{
     			temp[k]=fact[i];
     			k++;
     			continue;
   			}
   			if(fact[i]==27)
   			{
     			temp[k]=fact[i];
     			break;
   			}
 		}
 		if(temp[0]==12&&temp[1]==15&&temp[2]==16&&temp[3]==17&&temp[4]==27)
 		{
   			fact[num]=32;
   			//
			num++;
   			printf(" Use rule 11, The newly added fact is : giraffe \n The animal is a giraffe \n");
 		}
 //********************************  
 		k=0;
 		for(i=0;i<num;i++)
 		{
   			if(fact[i]==13)
   			{
     			temp[k]=fact[i];
     			k++;
     			continue;
   			}
   			if(fact[i]==18)
   			{
     			temp[k]=fact[i];
     			k++;
     			continue;
   			}
   			if(fact[i]==27)
   			{
     			temp[k]=fact[i];
     			break;
   			}
 		}
 		if(temp[0]==13&&temp[1]==18&&temp[2]==27)
 		{
   			fact[num]=33;
   			//
			num++;
   			printf(" Usage rule 12, The newly added fact is : zebra \n The animal is a zebra \n");
 		}
 //********************************  
 		k=0;
 		for(i=0;i<num;i++)
 		{
   			if(fact[i]==15)
   			{
     			temp[k]=fact[i];
     			k++;
     			continue;
   			}
   			if(fact[i]==16)
   			{
     			temp[k]=fact[i];
     			k++;
     			continue;
   			}
   			if(fact[i]==19)
   			{
     			temp[k]=fact[i];
     			k++;
     			continue;
   			}
   			if(fact[i]==25)
   			{
     			temp[k]=fact[i];
     			break;
   			}
 		}
 		if(temp[0]==15&&temp[1]==16&&temp[2]==19&&temp[3]==25)
 		{
   			fact[num]=34;
   			//
			num++;
  	 		printf(" Use Rule 13, The newly added fact is : ostrich \n The animal is an ostrich \n");
 		}
 //********************************  
 		k=0;
 		for(i=0;i<num;i++)
 		{
   			if(fact[i]==19)
   			{
     			temp[k]=fact[i];
     			k++;
     			continue;
   			}
   			if(fact[i]==20)
   			{
     			temp[k]=fact[i];
     			k++;
     			continue;
   			}
   			if(fact[i]==21)
   			{
     			temp[k]=fact[i];
     			k++;
     			continue;
   			}
   			if(fact[i]==25)
   			{
     			temp[k]=fact[i];
     			break;
   			}
 		}
 		if(temp[0]==19&&temp[1]==20&&temp[2]==21&&temp[3]==25)
 		{
   			fact[num]=35;
   			//
			num++;
   			printf(" Use rule 14, The newly added fact is : penguin \n The animal is penguin \n");
 		}
 //********************************  
 		k=0;
 		for(i=0;i<num;i++)
 		{
   			if(fact[i]==22)
   			{
     			temp[k]=fact[i];
     			k++;
     			continue;
   			}
   			if(fact[i]==23)
   			{
     			temp[k]=fact[i];
     			k++;
     			continue;
   			}
   			if(fact[i]==25)
   			{
     			temp[k]=fact[i];
     			break;
   			}
 		}
 		if(temp[0]==22&&temp[1]==23&&temp[2]==25)
 		{
   			fact[num]=29;
   			//
			num++;
  	 		printf(" Use rule 15, The newly added fact is : Petrel \n The animal is a petrel \n");
 		}
 //********************************  
 		if(fact[num]<29)
   			printf(" The results cannot be inferred from the existing facts !\n");
 		printf("\n");
 		printf(" To continue, press 1, Exit press other numeric keys :");
 		scanf("%d",&c);
 		if(c==1) 
   			flag=c;
 		else 
		 	break;
 	}
 	return 0;
}

 

 

 

Keywords: Algorithm AI

Added by songwind on Thu, 03 Feb 2022 12:27:59 +0200